blob: a75219e0a2fec68d9921b82189198d2c09a0b9dd [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 Moolenaar9fffef92021-12-10 16:55:58 +00003765 // When the outer function is compiled for profiling or debugging, the
3766 // lambda may be called without profiling or debugging. Compile it here in
3767 // the right context.
3768 if (cctx->ctx_compile_type == CT_DEBUG
Bram Moolenaar648594e2021-07-11 17:55:01 +02003769#ifdef FEAT_PROFILE
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003770 || cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar648594e2021-07-11 17:55:01 +02003771#endif
Bram Moolenaar9fffef92021-12-10 16:55:58 +00003772 )
3773 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaard9162552021-07-11 15:26:13 +02003774
Bram Moolenaar844fb642021-10-23 13:32:30 +01003775 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3776 // "*arg" may point into it. Point into the original line to avoid a
3777 // dangling pointer.
3778 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003779 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003780 garray_T *gap = &evalarg.eval_tofree_ga;
3781 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003782
3783 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3784 + off;
3785 }
3786
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003787 clear_evalarg(&evalarg, NULL);
3788
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003789 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003790 {
3791 // The return type will now be known.
3792 set_function_type(ufunc);
3793
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003794 // The function reference count will be 1. When the ISN_FUNCREF
3795 // instruction is deleted the reference count is decremented and the
3796 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003797 return generate_FUNCREF(cctx, ufunc);
3798 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003799
3800 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 return FAIL;
3802}
3803
3804/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003805 * Get a lambda and compile it. Uses Vim9 syntax.
3806 */
3807 int
3808get_lambda_tv_and_compile(
3809 char_u **arg,
3810 typval_T *rettv,
3811 int types_optional,
3812 evalarg_T *evalarg)
3813{
3814 int r;
3815 ufunc_T *ufunc;
3816 int save_sc_version = current_sctx.sc_version;
3817
3818 // Get the funcref in "rettv".
3819 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3820 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3821 current_sctx.sc_version = save_sc_version;
3822 if (r != OK)
3823 return r;
3824
3825 // "rettv" will now be a partial referencing the function.
3826 ufunc = rettv->vval.v_partial->pt_func;
3827
3828 // Compile it here to get the return type. The return type is optional,
3829 // when it's missing use t_unknown. This is recognized in
3830 // compile_return().
3831 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3832 ufunc->uf_ret_type = &t_unknown;
3833 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3834
3835 if (ufunc->uf_def_status == UF_COMPILED)
3836 {
3837 // The return type will now be known.
3838 set_function_type(ufunc);
3839 return OK;
3840 }
3841 clear_tv(rettv);
3842 return FAIL;
3843}
3844
3845/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003846 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003848 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 */
3850 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003851compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852{
3853 garray_T *instr = &cctx->ctx_instr;
3854 int count = 0;
3855 dict_T *d = dict_alloc();
3856 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003857 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003858 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003859 int is_const;
3860 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861
3862 if (d == NULL)
3863 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003864 if (generate_ppconst(cctx, ppconst) == FAIL)
3865 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003866 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003868 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869
Bram Moolenaar23c55272020-06-21 16:58:13 +02003870 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003871 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003872 *arg = NULL;
3873 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003874 }
3875
3876 if (**arg == '}')
3877 break;
3878
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003879 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003881 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003883 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003884 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003885 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003888 if (isn->isn_type == ISN_PUSHNR)
3889 {
3890 char buf[NUMBUFLEN];
3891
3892 // Convert to string at compile time.
3893 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3894 isn->isn_type = ISN_PUSHS;
3895 isn->isn_arg.string = vim_strsave((char_u *)buf);
3896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 if (isn->isn_type == ISN_PUSHS)
3898 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003899 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003900 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003901 *arg = skipwhite(*arg);
3902 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003903 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003904 emsg(_(e_missing_matching_bracket_after_dict_key));
3905 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003906 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003907 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003909 else
3910 {
3911 // {"name": value},
3912 // {'name': value},
3913 // {name: value} use "name" as a literal key
3914 key = get_literal_key(arg);
3915 if (key == NULL)
3916 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003917 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003918 return FAIL;
3919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920
3921 // Check for duplicate keys, if using string keys.
3922 if (key != NULL)
3923 {
3924 item = dict_find(d, key, -1);
3925 if (item != NULL)
3926 {
3927 semsg(_(e_duplicate_key), key);
3928 goto failret;
3929 }
3930 item = dictitem_alloc(key);
3931 if (item != NULL)
3932 {
3933 item->di_tv.v_type = VAR_UNKNOWN;
3934 item->di_tv.v_lock = 0;
3935 if (dict_add(d, item) == FAIL)
3936 dictitem_free(item);
3937 }
3938 }
3939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 if (**arg != ':')
3941 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003942 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003943 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003944 else
3945 semsg(_(e_missing_dict_colon), *arg);
3946 return FAIL;
3947 }
3948 whitep = *arg + 1;
3949 if (!IS_WHITE_OR_NUL(*whitep))
3950 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003951 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 return FAIL;
3953 }
3954
Bram Moolenaar23c55272020-06-21 16:58:13 +02003955 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003956 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003957 *arg = NULL;
3958 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003959 }
3960
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003961 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003963 if (!is_const)
3964 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 ++count;
3966
Bram Moolenaar2c330432020-04-13 14:41:35 +02003967 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003968 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003969 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003970 *arg = NULL;
3971 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 if (**arg == '}')
3974 break;
3975 if (**arg != ',')
3976 {
3977 semsg(_(e_missing_dict_comma), *arg);
3978 goto failret;
3979 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003980 if (IS_WHITE_OR_NUL(*whitep))
3981 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003982 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003983 return FAIL;
3984 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003985 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003986 if (!IS_WHITE_OR_NUL(*whitep))
3987 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003988 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003989 return FAIL;
3990 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003991 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 }
3993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 *arg = *arg + 1;
3995
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003996 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003997 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003998 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003999 *arg += STRLEN(*arg);
4000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004002 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 return generate_NEWDICT(cctx, count);
4004
4005failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004006 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004007 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004008 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004009 *arg = (char_u *)"";
4010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 dict_unref(d);
4012 return FAIL;
4013}
4014
4015/*
4016 * Compile "&option".
4017 */
4018 static int
4019compile_get_option(char_u **arg, cctx_T *cctx)
4020{
4021 typval_T rettv;
4022 char_u *start = *arg;
4023 int ret;
4024
4025 // parse the option and get the current value to get the type.
4026 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004027 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 if (ret == OK)
4029 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004030 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004031 char_u *name = vim_strnsave(start, *arg - start);
4032 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4033 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
4035 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4036 vim_free(name);
4037 }
4038 clear_tv(&rettv);
4039
4040 return ret;
4041}
4042
4043/*
4044 * Compile "$VAR".
4045 */
4046 static int
4047compile_get_env(char_u **arg, cctx_T *cctx)
4048{
4049 char_u *start = *arg;
4050 int len;
4051 int ret;
4052 char_u *name;
4053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 ++*arg;
4055 len = get_env_len(arg);
4056 if (len == 0)
4057 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004058 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 return FAIL;
4060 }
4061
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004062 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 name = vim_strnsave(start, len + 1);
4064 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4065 vim_free(name);
4066 return ret;
4067}
4068
4069/*
4070 * Compile "@r".
4071 */
4072 static int
4073compile_get_register(char_u **arg, cctx_T *cctx)
4074{
4075 int ret;
4076
4077 ++*arg;
4078 if (**arg == NUL)
4079 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004080 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 return FAIL;
4082 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004083 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
4085 emsg_invreg(**arg);
4086 return FAIL;
4087 }
4088 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4089 ++*arg;
4090 return ret;
4091}
4092
4093/*
4094 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004095 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 */
4097 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004098apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004100 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
4102 // this works from end to start
4103 while (p > start)
4104 {
4105 --p;
4106 if (*p == '-' || *p == '+')
4107 {
4108 // only '-' has an effect, for '+' we only check the type
4109#ifdef FEAT_FLOAT
4110 if (rettv->v_type == VAR_FLOAT)
4111 {
4112 if (*p == '-')
4113 rettv->vval.v_float = -rettv->vval.v_float;
4114 }
4115 else
4116#endif
4117 {
4118 varnumber_T val;
4119 int error = FALSE;
4120
4121 // tv_get_number_chk() accepts a string, but we don't want that
4122 // here
4123 if (check_not_string(rettv) == FAIL)
4124 return FAIL;
4125 val = tv_get_number_chk(rettv, &error);
4126 clear_tv(rettv);
4127 if (error)
4128 return FAIL;
4129 if (*p == '-')
4130 val = -val;
4131 rettv->v_type = VAR_NUMBER;
4132 rettv->vval.v_number = val;
4133 }
4134 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004135 else if (numeric_only)
4136 {
4137 ++p;
4138 break;
4139 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004140 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 {
4142 int v = tv2bool(rettv);
4143
4144 // '!' is permissive in the type.
4145 clear_tv(rettv);
4146 rettv->v_type = VAR_BOOL;
4147 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4148 }
4149 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004150 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 return OK;
4152}
4153
4154/*
4155 * Recognize v: variables that are constants and set "rettv".
4156 */
4157 static void
4158get_vim_constant(char_u **arg, typval_T *rettv)
4159{
4160 if (STRNCMP(*arg, "v:true", 6) == 0)
4161 {
4162 rettv->v_type = VAR_BOOL;
4163 rettv->vval.v_number = VVAL_TRUE;
4164 *arg += 6;
4165 }
4166 else if (STRNCMP(*arg, "v:false", 7) == 0)
4167 {
4168 rettv->v_type = VAR_BOOL;
4169 rettv->vval.v_number = VVAL_FALSE;
4170 *arg += 7;
4171 }
4172 else if (STRNCMP(*arg, "v:null", 6) == 0)
4173 {
4174 rettv->v_type = VAR_SPECIAL;
4175 rettv->vval.v_number = VVAL_NULL;
4176 *arg += 6;
4177 }
4178 else if (STRNCMP(*arg, "v:none", 6) == 0)
4179 {
4180 rettv->v_type = VAR_SPECIAL;
4181 rettv->vval.v_number = VVAL_NONE;
4182 *arg += 6;
4183 }
4184}
4185
Bram Moolenaar657137c2021-01-09 15:45:23 +01004186 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004187get_compare_type(char_u *p, int *len, int *type_is)
4188{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004189 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004190 int i;
4191
4192 switch (p[0])
4193 {
4194 case '=': if (p[1] == '=')
4195 type = EXPR_EQUAL;
4196 else if (p[1] == '~')
4197 type = EXPR_MATCH;
4198 break;
4199 case '!': if (p[1] == '=')
4200 type = EXPR_NEQUAL;
4201 else if (p[1] == '~')
4202 type = EXPR_NOMATCH;
4203 break;
4204 case '>': if (p[1] != '=')
4205 {
4206 type = EXPR_GREATER;
4207 *len = 1;
4208 }
4209 else
4210 type = EXPR_GEQUAL;
4211 break;
4212 case '<': if (p[1] != '=')
4213 {
4214 type = EXPR_SMALLER;
4215 *len = 1;
4216 }
4217 else
4218 type = EXPR_SEQUAL;
4219 break;
4220 case 'i': if (p[1] == 's')
4221 {
4222 // "is" and "isnot"; but not a prefix of a name
4223 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4224 *len = 5;
4225 i = p[*len];
4226 if (!isalnum(i) && i != '_')
4227 {
4228 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4229 *type_is = TRUE;
4230 }
4231 }
4232 break;
4233 }
4234 return type;
4235}
4236
4237/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004238 * Skip over an expression, ignoring most errors.
4239 */
4240 static void
4241skip_expr_cctx(char_u **arg, cctx_T *cctx)
4242{
4243 evalarg_T evalarg;
4244
Bram Moolenaar844fb642021-10-23 13:32:30 +01004245 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004246 evalarg.eval_cctx = cctx;
4247 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004248 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004249}
4250
4251/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004253 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 */
4255 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004256compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004258 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259
4260 // this works from end to start
4261 while (p > start)
4262 {
4263 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004264 while (VIM_ISWHITE(*p))
4265 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 if (*p == '-' || *p == '+')
4267 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004268 int negate = *p == '-';
4269 isn_T *isn;
4270 garray_T *stack = &cctx->ctx_type_stack;
4271 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004273 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4274 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4275 return FAIL;
4276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4278 {
4279 --p;
4280 if (*p == '-')
4281 negate = !negate;
4282 }
4283 // only '-' has an effect, for '+' we only check the type
4284 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004285 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004287 if (isn == NULL)
4288 return FAIL;
4289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004291 else if (numeric_only)
4292 {
4293 ++p;
4294 break;
4295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 else
4297 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004298 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004300 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004302 if (p[-1] == '!')
4303 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004306 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 return FAIL;
4308 }
4309 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004310 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 return OK;
4312}
4313
4314/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004315 * Compile "(expression)": recursive!
4316 * Return FAIL/OK.
4317 */
4318 static int
4319compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4320{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004321 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004322 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004323
Bram Moolenaar24156692021-01-14 20:35:49 +01004324 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4325 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004326 if (ppconst->pp_used <= PPSIZE - 10)
4327 {
4328 ret = compile_expr1(arg, cctx, ppconst);
4329 }
4330 else
4331 {
4332 // Not enough space in ppconst, flush constants.
4333 if (generate_ppconst(cctx, ppconst) == FAIL)
4334 return FAIL;
4335 ret = compile_expr0(arg, cctx);
4336 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004337 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4338 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004339 if (**arg == ')')
4340 ++*arg;
4341 else if (ret == OK)
4342 {
4343 emsg(_(e_missing_close));
4344 ret = FAIL;
4345 }
4346 return ret;
4347}
4348
4349/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004351 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 */
4353 static int
4354compile_subscript(
4355 char_u **arg,
4356 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004357 char_u *start_leader,
4358 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004359 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004361 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004362 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 for (;;)
4365 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004366 char_u *p = skipwhite(*arg);
4367
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004368 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004369 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004370 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004371
4372 // If a following line starts with "->{" or "->X" advance to that
4373 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004374 // Also if a following line starts with ".x".
4375 if (next != NULL &&
4376 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004377 && (next[2] == '{'
4378 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004379 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004380 {
4381 next = next_line_from_context(cctx, TRUE);
4382 if (next == NULL)
4383 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004384 *arg = next;
4385 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004386 }
4387 }
4388
Bram Moolenaarcd268012021-07-22 19:11:08 +02004389 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4390 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004391 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004393 garray_T *stack = &cctx->ctx_type_stack;
4394 type_T *type;
4395 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004397 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004398 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004399 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004402 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4403
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004404 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004405 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004407 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004409 if (keeping_dict)
4410 {
4411 keeping_dict = FALSE;
4412 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4413 return FAIL;
4414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004416 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004418 char_u *pstart = p;
4419
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004420 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004421 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004422 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 // something->method()
4425 // Apply the '!', '-' and '+' first:
4426 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004427 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004430 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004431 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004432 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004433 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004434 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004435 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004436 garray_T *stack = &cctx->ctx_type_stack;
4437 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004438 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004439 int expr_isn_start = cctx->ctx_instr.ga_len;
4440 int expr_isn_end;
4441 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004442
4443 // Funcref call: list->(Refs[2])(arg)
4444 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004445 //
4446 // Fist compile the function expression.
4447 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004448 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004449
4450 // Remember the next instruction index, where the instructions
4451 // for arguments are being written.
4452 expr_isn_end = cctx->ctx_instr.ga_len;
4453
4454 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004455 if (**arg != '(')
4456 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004457 if (*skipwhite(*arg) == '(')
4458 emsg(_(e_nowhitespace));
4459 else
4460 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004461 return FAIL;
4462 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004463 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004464 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004465 return FAIL;
4466
Bram Moolenaar2927c072021-04-05 19:41:21 +02004467 // Move the instructions for the arguments to before the
4468 // instructions of the expression and move the type of the
4469 // expression after the argument types. This is what ISN_PCALL
4470 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004471 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004472 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4473 if (arg_isn_count > 0)
4474 {
4475 int expr_isn_count = expr_isn_end - expr_isn_start;
4476 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4477
4478 if (isn == NULL)
4479 return FAIL;
4480 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4481 + expr_isn_start,
4482 sizeof(isn_T) * expr_isn_count);
4483 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4484 + expr_isn_start,
4485 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4486 sizeof(isn_T) * arg_isn_count);
4487 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4488 + expr_isn_start + arg_isn_count,
4489 isn, sizeof(isn_T) * expr_isn_count);
4490 vim_free(isn);
4491
4492 type = ((type_T **)stack->ga_data)[type_idx_start];
4493 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4494 ((type_T **)stack->ga_data) + type_idx_start + 1,
4495 sizeof(type_T *)
4496 * (stack->ga_len - type_idx_start - 1));
4497 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4498 }
4499
Bram Moolenaar7e368202020-12-25 21:56:57 +01004500 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004501 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 return FAIL;
4503 }
4504 else
4505 {
4506 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004507 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004508 if (!eval_isnamec1(*p))
4509 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004510 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004511 return FAIL;
4512 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004513 if (ASCII_ISALPHA(*p) && p[1] == ':')
4514 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004515 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 ;
4517 if (*p != '(')
4518 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004519 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 return FAIL;
4521 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 return FAIL;
4524 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004525 if (keeping_dict)
4526 {
4527 keeping_dict = FALSE;
4528 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4529 return FAIL;
4530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004532 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004534 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004535
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004536 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004537 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004538 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004539 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004540 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004541 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004542 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004543
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004544 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004545 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004546 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004547 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004548 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004549 // missing first index is equal to zero
4550 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004551 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004552 else
4553 {
4554 if (compile_expr0(arg, cctx) == FAIL)
4555 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004556 if (**arg == ':')
4557 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004558 semsg(_(e_white_space_required_before_and_after_str_at_str),
4559 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004560 return FAIL;
4561 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004562 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004563 return FAIL;
4564 *arg = skipwhite(*arg);
4565 }
4566 if (**arg == ':')
4567 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004568 is_slice = TRUE;
4569 ++*arg;
4570 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4571 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004572 semsg(_(e_white_space_required_before_and_after_str_at_str),
4573 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004574 return FAIL;
4575 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004576 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004577 return FAIL;
4578 if (**arg == ']')
4579 // missing second index is equal to end of string
4580 generate_PUSHNR(cctx, -1);
4581 else
4582 {
4583 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004584 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004585 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004586 return FAIL;
4587 *arg = skipwhite(*arg);
4588 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590
4591 if (**arg != ']')
4592 {
4593 emsg(_(e_missbrac));
4594 return FAIL;
4595 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004596 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004598 if (keeping_dict)
4599 {
4600 keeping_dict = FALSE;
4601 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4602 return FAIL;
4603 }
4604 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004605 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004607 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004609 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004610 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004611 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004612 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004613
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004614 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004615 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004616 {
4617 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004618 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004619 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004620 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004621 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 while (eval_isnamec(*p))
4623 MB_PTR_ADV(p);
4624 if (p == *arg)
4625 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004626 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 return FAIL;
4628 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004629 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4630 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004631 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004633 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 *arg = p;
4635 }
4636 else
4637 break;
4638 }
4639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004641 // This needs to be done at runtime to be able to check the type.
4642 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4643 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
4645 return OK;
4646}
4647
4648/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004649 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4650 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004652 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004653 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004654 *
4655 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 */
4657
4658/*
4659 * number number constant
4660 * 0zFFFFFFFF Blob constant
4661 * "string" string constant
4662 * 'string' literal string constant
4663 * &option-name option value
4664 * @r register contents
4665 * identifier variable value
4666 * function() function call
4667 * $VAR environment variable
4668 * (expression) nested expression
4669 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004670 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 *
4672 * Also handle:
4673 * ! in front logical NOT
4674 * - in front unary minus
4675 * + in front unary plus (ignored)
4676 * trailing (arg) funcref/partial call
4677 * trailing [] subscript in String or List
4678 * trailing .name entry in Dictionary
4679 * trailing ->name() method call
4680 */
4681 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004682compile_expr7(
4683 char_u **arg,
4684 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004685 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 char_u *start_leader, *end_leader;
4688 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004689 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004690 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004692 ppconst->pp_is_const = FALSE;
4693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 /*
4695 * Skip '!', '-' and '+' characters. They are handled later.
4696 */
4697 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004698 if (eval_leader(arg, TRUE) == FAIL)
4699 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 end_leader = *arg;
4701
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004702 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 switch (**arg)
4704 {
4705 /*
4706 * Number constant.
4707 */
4708 case '0': // also for blob starting with 0z
4709 case '1':
4710 case '2':
4711 case '3':
4712 case '4':
4713 case '5':
4714 case '6':
4715 case '7':
4716 case '8':
4717 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004718 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004720 // Apply "-" and "+" just before the number now, right to
4721 // left. Matters especially when "->" follows. Stops at
4722 // '!'.
4723 if (apply_leader(rettv, TRUE,
4724 start_leader, &end_leader) == FAIL)
4725 {
4726 clear_tv(rettv);
4727 return FAIL;
4728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 break;
4730
4731 /*
4732 * String constant: "string".
4733 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004734 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 return FAIL;
4736 break;
4737
4738 /*
4739 * Literal string constant: 'str''ing'.
4740 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004741 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 return FAIL;
4743 break;
4744
4745 /*
4746 * Constant Vim variable.
4747 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004748 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 ret = NOTDONE;
4750 break;
4751
4752 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004753 * "true" constant
4754 */
4755 case 't': if (STRNCMP(*arg, "true", 4) == 0
4756 && !eval_isnamec((*arg)[4]))
4757 {
4758 *arg += 4;
4759 rettv->v_type = VAR_BOOL;
4760 rettv->vval.v_number = VVAL_TRUE;
4761 }
4762 else
4763 ret = NOTDONE;
4764 break;
4765
4766 /*
4767 * "false" constant
4768 */
4769 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4770 && !eval_isnamec((*arg)[5]))
4771 {
4772 *arg += 5;
4773 rettv->v_type = VAR_BOOL;
4774 rettv->vval.v_number = VVAL_FALSE;
4775 }
4776 else
4777 ret = NOTDONE;
4778 break;
4779
4780 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004781 * "null" constant
4782 */
4783 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004784 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004785 {
4786 *arg += 4;
4787 rettv->v_type = VAR_SPECIAL;
4788 rettv->vval.v_number = VVAL_NULL;
4789 }
4790 else
4791 ret = NOTDONE;
4792 break;
4793
4794 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 * List: [expr, expr]
4796 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004797 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4798 return FAIL;
4799 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 break;
4801
4802 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 * Dictionary: {'key': val, 'key': val}
4804 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004805 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4806 return FAIL;
4807 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 break;
4809
4810 /*
4811 * Option value: &name
4812 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004813 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4814 return FAIL;
4815 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 break;
4817
4818 /*
4819 * Environment variable: $VAR.
4820 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004821 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4822 return FAIL;
4823 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 break;
4825
4826 /*
4827 * Register contents: @r.
4828 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004829 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4830 return FAIL;
4831 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 break;
4833 /*
4834 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004835 * lambda: (arg, arg) => expr
4836 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004838 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4839 ret = compile_lambda(arg, cctx);
4840 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004841 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 break;
4843
4844 default: ret = NOTDONE;
4845 break;
4846 }
4847 if (ret == FAIL)
4848 return FAIL;
4849
Bram Moolenaar1c747212020-05-09 18:28:34 +02004850 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004852 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853 clear_tv(rettv);
4854 else
4855 // A constant expression can possibly be handled compile time,
4856 // return the value instead of generating code.
4857 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 }
4859 else if (ret == NOTDONE)
4860 {
4861 char_u *p;
4862 int r;
4863
4864 if (!eval_isnamec1(**arg))
4865 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004866 if (!vim9_bad_comment(*arg))
4867 {
4868 if (ends_excmd(*skipwhite(*arg)))
4869 semsg(_(e_empty_expression_str), *arg);
4870 else
4871 semsg(_(e_name_expected_str), *arg);
4872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 return FAIL;
4874 }
4875
4876 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004877 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004878 if (p - *arg == (size_t)1 && **arg == '_')
4879 {
4880 emsg(_(e_cannot_use_underscore_here));
4881 return FAIL;
4882 }
4883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004885 {
4886 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004889 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004890 if (cctx->ctx_skip != SKIP_YES
4891 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004892 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004893 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 if (r == FAIL)
4896 return FAIL;
4897 }
4898
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004899 // Handle following "[]", ".member", etc.
4900 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004901 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004902 ppconst) == FAIL)
4903 return FAIL;
4904 if (ppconst->pp_used > 0)
4905 {
4906 // apply the '!', '-' and '+' before the constant
4907 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004908 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004909 return FAIL;
4910 return OK;
4911 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004912 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004914 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915}
4916
4917/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004918 * Give the "white on both sides" error, taking the operator from "p[len]".
4919 */
4920 void
4921error_white_both(char_u *op, int len)
4922{
4923 char_u buf[10];
4924
4925 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004926 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004927}
4928
4929/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004930 * <type>expr7: runtime type check / conversion
4931 */
4932 static int
4933compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4934{
4935 type_T *want_type = NULL;
4936
4937 // Recognize <type>
4938 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4939 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004940 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004941 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4942 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004943 return FAIL;
4944
4945 if (**arg != '>')
4946 {
4947 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004948 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004949 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004950 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004951 return FAIL;
4952 }
4953 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004954 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004955 return FAIL;
4956 }
4957
4958 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4959 return FAIL;
4960
4961 if (want_type != NULL)
4962 {
4963 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004964 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004965 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004966
Bram Moolenaard1103582020-08-14 22:44:25 +02004967 generate_ppconst(cctx, ppconst);
4968 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004969 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004970 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004971 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004972 return FAIL;
4973 }
4974 }
4975
4976 return OK;
4977}
4978
4979/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 * * number multiplication
4981 * / number division
4982 * % number modulo
4983 */
4984 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004985compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986{
4987 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004988 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004989 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004991 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004992 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 return FAIL;
4994
4995 /*
4996 * Repeat computing, until no "*", "/" or "%" is following.
4997 */
4998 for (;;)
4999 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005000 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 if (*op != '*' && *op != '/' && *op != '%')
5002 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005003 if (next != NULL)
5004 {
5005 *arg = next_line_from_context(cctx, TRUE);
5006 op = skipwhite(*arg);
5007 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005008
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005009 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005011 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005012 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005014 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005015 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005017 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005018 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005020
5021 if (ppconst->pp_used == ppconst_used + 2
5022 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5023 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005024 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005025 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5026 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5027 varnumber_T res = 0;
5028 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005030 // both are numbers: compute the result
5031 switch (*op)
5032 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005033 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005034 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005035 case '/': res = num_divide(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;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005038 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005039 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005040 break;
5041 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005042 if (failed)
5043 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005044 tv1->vval.v_number = res;
5045 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005046 }
5047 else
5048 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005049 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005050 generate_two_op(cctx, op);
5051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 }
5053
5054 return OK;
5055}
5056
5057/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005058 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 * - number subtraction
5060 * .. string concatenation
5061 */
5062 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005063compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064{
5065 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005066 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069
5070 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005071 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 return FAIL;
5073
5074 /*
5075 * Repeat computing, until no "+", "-" or ".." is following.
5076 */
5077 for (;;)
5078 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005079 op = may_peek_next_line(cctx, *arg, &next);
5080 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005082 if (op[0] == op[1] && *op != '.' && next)
5083 // Finding "++" or "--" on the next line is a separate command.
5084 // But ".." is concatenation.
5085 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005087 if (next != NULL)
5088 {
5089 *arg = next_line_from_context(cctx, TRUE);
5090 op = skipwhite(*arg);
5091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005093 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005095 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 }
5098
Bram Moolenaare0de1712020-12-02 17:36:54 +01005099 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005100 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005102 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005103 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 return FAIL;
5105
Bram Moolenaara5565e42020-05-09 15:44:01 +02005106 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005107 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005108 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5109 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5110 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5111 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005113 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5114 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005115
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005116 // concat/subtract/add constant numbers
5117 if (*op == '+')
5118 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5119 else if (*op == '-')
5120 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5121 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005122 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005123 // concatenate constant strings
5124 char_u *s1 = tv1->vval.v_string;
5125 char_u *s2 = tv2->vval.v_string;
5126 size_t len1 = STRLEN(s1);
5127
5128 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5129 if (tv1->vval.v_string == NULL)
5130 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005132 return FAIL;
5133 }
5134 mch_memmove(tv1->vval.v_string, s1, len1);
5135 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005136 vim_free(s1);
5137 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005138 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005139 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 }
5141 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005142 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005144 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005145 if (*op == '.')
5146 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005147 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5148 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005149 return FAIL;
5150 generate_instr_drop(cctx, ISN_CONCAT, 1);
5151 }
5152 else
5153 generate_two_op(cctx, op);
5154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 }
5156
5157 return OK;
5158}
5159
5160/*
5161 * expr5a == expr5b
5162 * expr5a =~ expr5b
5163 * expr5a != expr5b
5164 * expr5a !~ expr5b
5165 * expr5a > expr5b
5166 * expr5a >= expr5b
5167 * expr5a < expr5b
5168 * expr5a <= expr5b
5169 * expr5a is expr5b
5170 * expr5a isnot expr5b
5171 *
5172 * Produces instructions:
5173 * EVAL expr5a Push result of "expr5a"
5174 * EVAL expr5b Push result of "expr5b"
5175 * COMPARE one of the compare instructions
5176 */
5177 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005178compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005180 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005182 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005185 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186
5187 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005188 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 return FAIL;
5190
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005191 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005192 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193
5194 /*
5195 * If there is a comparative operator, use it.
5196 */
5197 if (type != EXPR_UNKNOWN)
5198 {
5199 int ic = FALSE; // Default: do not ignore case
5200
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005201 if (next != NULL)
5202 {
5203 *arg = next_line_from_context(cctx, TRUE);
5204 p = skipwhite(*arg);
5205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 if (type_is && (p[len] == '?' || p[len] == '#'))
5207 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005208 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 return FAIL;
5210 }
5211 // extra question mark appended: ignore case
5212 if (p[len] == '?')
5213 {
5214 ic = TRUE;
5215 ++len;
5216 }
5217 // extra '#' appended: match case (ignored)
5218 else if (p[len] == '#')
5219 ++len;
5220 // nothing appended: match case
5221
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005222 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005224 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005225 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226 }
5227
5228 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005229 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005230 return FAIL;
5231
Bram Moolenaara5565e42020-05-09 15:44:01 +02005232 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 return FAIL;
5234
Bram Moolenaara5565e42020-05-09 15:44:01 +02005235 if (ppconst->pp_used == ppconst_used + 2)
5236 {
5237 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5238 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5239 int ret;
5240
5241 // Both sides are a constant, compute the result now.
5242 // First check for a valid combination of types, this is more
5243 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005244 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005245 ret = FAIL;
5246 else
5247 {
5248 ret = typval_compare(tv1, tv2, type, ic);
5249 tv1->v_type = VAR_BOOL;
5250 tv1->vval.v_number = tv1->vval.v_number
5251 ? VVAL_TRUE : VVAL_FALSE;
5252 clear_tv(tv2);
5253 --ppconst->pp_used;
5254 }
5255 return ret;
5256 }
5257
5258 generate_ppconst(cctx, ppconst);
5259 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 }
5261
5262 return OK;
5263}
5264
Bram Moolenaar7f141552020-05-09 17:35:53 +02005265static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267/*
5268 * Compile || or &&.
5269 */
5270 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005271compile_and_or(
5272 char_u **arg,
5273 cctx_T *cctx,
5274 char *op,
5275 ppconst_T *ppconst,
5276 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005278 char_u *next;
5279 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005280 int opchar = *op;
5281
5282 if (p[0] == opchar && p[1] == opchar)
5283 {
5284 garray_T *instr = &cctx->ctx_instr;
5285 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005286 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287
5288 /*
5289 * Repeat until there is no following "||" or "&&"
5290 */
5291 ga_init2(&end_ga, sizeof(int), 10);
5292 while (p[0] == opchar && p[1] == opchar)
5293 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005294 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005295 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005296 int start_ctx_lnum = cctx->ctx_lnum;
5297 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005298 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005299 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005300 jumpwhen_T jump_when = opchar == '|'
5301 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005302
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005303 if (next != NULL)
5304 {
5305 *arg = next_line_from_context(cctx, TRUE);
5306 p = skipwhite(*arg);
5307 }
5308
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005309 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5310 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005311 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005312 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005313 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005314 return FAIL;
5315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005317 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005318 SOURCING_LNUM = start_lnum;
5319 save_lnum = cctx->ctx_lnum;
5320 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005321
5322 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005323 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005324 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005325 // Use the last ppconst if possible.
5326 if (ppconst->pp_used > 0)
5327 {
5328 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5329 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005330
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005331 if ((is_true && opchar == '|')
5332 || (!is_true && opchar == '&'))
5333 {
5334 // For "false && expr" and "true || expr" the "expr"
5335 // does not need to be evaluated.
5336 cctx->ctx_skip = SKIP_YES;
5337 clear_tv(tv);
5338 tv->v_type = VAR_BOOL;
5339 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5340 }
5341 else
5342 {
5343 // For "true && expr" and "false || expr" only "expr"
5344 // needs to be evaluated.
5345 --ppconst->pp_used;
5346 jump_when = JUMP_NEVER;
5347 }
5348 }
5349 else
5350 {
5351 // Every part must evaluate to a bool.
5352 status = bool_on_stack(cctx);
5353 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005354 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005355 if (status != FAIL)
5356 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005357 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005358 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359 {
5360 ga_clear(&end_ga);
5361 return FAIL;
5362 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005363
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005364 if (jump_when != JUMP_NEVER)
5365 {
5366 if (cctx->ctx_skip != SKIP_YES)
5367 {
5368 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5369 ++end_ga.ga_len;
5370 }
5371 generate_JUMP(cctx, jump_when, 0);
5372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373
5374 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005375 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005376 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005377 {
5378 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005379 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005380 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005381
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005382 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005383 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5384 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385 {
5386 ga_clear(&end_ga);
5387 return FAIL;
5388 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005389
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005390 // "0 || 1" results in true, "1 && 0" results in false.
5391 if (ppconst->pp_used == const_used + 1)
5392 {
5393 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5394
5395 if (tv->v_type == VAR_NUMBER
5396 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5397 {
5398 tv->vval.v_number = tv->vval.v_number == 1
5399 ? VVAL_TRUE : VVAL_FALSE;
5400 tv->v_type = VAR_BOOL;
5401 }
5402 }
5403
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005404 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005406
5407 if (check_ppconst_bool(ppconst) == FAIL)
5408 {
5409 ga_clear(&end_ga);
5410 return FAIL;
5411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005413 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5414 // Every part must evaluate to a bool.
5415 if (bool_on_stack(cctx) == FAIL)
5416 {
5417 ga_clear(&end_ga);
5418 return FAIL;
5419 }
5420
5421 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005422 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005423 // Fill in the end label in all jumps.
5424 generate_ppconst(cctx, ppconst);
5425 while (end_ga.ga_len > 0)
5426 {
5427 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005428
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005429 --end_ga.ga_len;
5430 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005432 isn->isn_arg.jump.jump_where = instr->ga_len;
5433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005435 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005436
5437 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 }
5439
5440 return OK;
5441}
5442
5443/*
5444 * expr4a && expr4a && expr4a logical AND
5445 *
5446 * Produces instructions:
5447 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005448 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005449 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005451 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 * EVAL expr4c Push result of "expr4c"
5453 * end:
5454 */
5455 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005456compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005458 int ppconst_used = ppconst->pp_used;
5459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005461 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 return FAIL;
5463
5464 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005465 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466}
5467
5468/*
5469 * expr3a || expr3b || expr3c logical OR
5470 *
5471 * Produces instructions:
5472 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005473 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005474 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005476 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477 * EVAL expr3c Push result of "expr3c"
5478 * end:
5479 */
5480 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005483 int ppconst_used = ppconst->pp_used;
5484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005486 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 return FAIL;
5488
5489 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005490 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491}
5492
5493/*
5494 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005496 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 * JUMP_IF_FALSE alt jump if false
5498 * EVAL expr1a
5499 * JUMP_ALWAYS end
5500 * alt: EVAL expr1b
5501 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005502 *
5503 * Toplevel expression: expr2 ?? expr1
5504 * Produces instructions:
5505 * EVAL expr2 Push result of "expr2"
5506 * JUMP_AND_KEEP_IF_TRUE end jump if true
5507 * EVAL expr1
5508 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509 */
5510 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005511compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512{
5513 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005514 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005515 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516
Bram Moolenaar3988f642020-08-27 22:43:03 +02005517 // Ignore all kinds of errors when not producing code.
5518 if (cctx->ctx_skip == SKIP_YES)
5519 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005520 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005521 return OK;
5522 }
5523
Bram Moolenaar61a89812020-05-07 16:58:17 +02005524 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005525 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005526 return FAIL;
5527
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005528 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529 if (*p == '?')
5530 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005531 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532 garray_T *instr = &cctx->ctx_instr;
5533 garray_T *stack = &cctx->ctx_type_stack;
5534 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005535 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005537 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005538 int has_const_expr = FALSE;
5539 int const_value = FALSE;
5540 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005542 if (next != NULL)
5543 {
5544 *arg = next_line_from_context(cctx, TRUE);
5545 p = skipwhite(*arg);
5546 }
5547
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005548 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005549 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005550 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005551 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005552 return FAIL;
5553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554
Bram Moolenaara5565e42020-05-09 15:44:01 +02005555 if (ppconst->pp_used == ppconst_used + 1)
5556 {
5557 // the condition is a constant, we know whether the ? or the :
5558 // expression is to be evaluated.
5559 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005560 if (op_falsy)
5561 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5562 else
5563 {
5564 int error = FALSE;
5565
5566 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5567 &error);
5568 if (error)
5569 return FAIL;
5570 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005571 cctx->ctx_skip = save_skip == SKIP_YES ||
5572 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5573
5574 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5575 // "left ?? right" and "left" is truthy: produce "left"
5576 generate_ppconst(cctx, ppconst);
5577 else
5578 {
5579 clear_tv(&ppconst->pp_tv[ppconst_used]);
5580 --ppconst->pp_used;
5581 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005582 }
5583 else
5584 {
5585 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005586 if (op_falsy)
5587 end_idx = instr->ga_len;
5588 generate_JUMP(cctx, op_falsy
5589 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5590 if (op_falsy)
5591 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593
5594 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005595 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005596 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005597 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005598 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599
Bram Moolenaara5565e42020-05-09 15:44:01 +02005600 if (!has_const_expr)
5601 {
5602 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005604 if (!op_falsy)
5605 {
5606 // remember the type and drop it
5607 --stack->ga_len;
5608 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005610 end_idx = instr->ga_len;
5611 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005612
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005613 // jump here from JUMP_IF_FALSE
5614 isn = ((isn_T *)instr->ga_data) + alt_idx;
5615 isn->isn_arg.jump.jump_where = instr->ga_len;
5616 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005619 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005621 // Check for the ":".
5622 p = may_peek_next_line(cctx, *arg, &next);
5623 if (*p != ':')
5624 {
5625 emsg(_(e_missing_colon));
5626 return FAIL;
5627 }
5628 if (next != NULL)
5629 {
5630 *arg = next_line_from_context(cctx, TRUE);
5631 p = skipwhite(*arg);
5632 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005633
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005634 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5635 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005636 semsg(_(e_white_space_required_before_and_after_str_at_str),
5637 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005638 return FAIL;
5639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005641 // evaluate the third expression
5642 if (has_const_expr)
5643 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005644 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005645 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005646 return FAIL;
5647 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5648 return FAIL;
5649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650
Bram Moolenaara5565e42020-05-09 15:44:01 +02005651 if (!has_const_expr)
5652 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005653 type_T **typep;
5654
Bram Moolenaara5565e42020-05-09 15:44:01 +02005655 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656
Bram Moolenaara5565e42020-05-09 15:44:01 +02005657 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005658 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5659 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005660
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005661 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005662 isn = ((isn_T *)instr->ga_data) + end_idx;
5663 isn->isn_arg.jump.jump_where = instr->ga_len;
5664 }
5665
5666 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 }
5668 return OK;
5669}
5670
5671/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005672 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005673 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5674 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005675 */
5676 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005677compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005678{
5679 ppconst_T ppconst;
5680
5681 CLEAR_FIELD(ppconst);
5682 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5683 {
5684 clear_ppconst(&ppconst);
5685 return FAIL;
5686 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005687 if (is_const != NULL)
5688 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005689 if (generate_ppconst(cctx, &ppconst) == FAIL)
5690 return FAIL;
5691 return OK;
5692}
5693
5694/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005695 * Toplevel expression.
5696 */
5697 static int
5698compile_expr0(char_u **arg, cctx_T *cctx)
5699{
5700 return compile_expr0_ext(arg, cctx, NULL);
5701}
5702
5703/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005704 * Compile "return [expr]".
5705 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706 */
5707 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005708compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709{
5710 char_u *p = arg;
5711 garray_T *stack = &cctx->ctx_type_stack;
5712 type_T *stack_type;
5713
5714 if (*p != NUL && *p != '|' && *p != '\n')
5715 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005716 if (legacy)
5717 {
5718 int save_flags = cmdmod.cmod_flags;
5719
5720 generate_LEGACY_EVAL(cctx, p);
5721 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5722 0, cctx, FALSE, FALSE) == FAIL)
5723 return NULL;
5724 cmdmod.cmod_flags |= CMOD_LEGACY;
5725 (void)skip_expr(&p, NULL);
5726 cmdmod.cmod_flags = save_flags;
5727 }
5728 else
5729 {
5730 // compile return argument into instructions
5731 if (compile_expr0(&p, cctx) == FAIL)
5732 return NULL;
5733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005735 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005736 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005737 // "check_return_type" with uf_ret_type set to &t_unknown is used
5738 // for an inline function without a specified return type. Set the
5739 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005740 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005741 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005742 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5743 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005744 || (!check_return_type
5745 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005746 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005747 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005748 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005749 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005750 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005751 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5752 && stack_type->tt_type != VAR_VOID
5753 && stack_type->tt_type != VAR_UNKNOWN)
5754 {
5755 emsg(_(e_returning_value_in_function_without_return_type));
5756 return NULL;
5757 }
5758 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005759 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005760 return NULL;
5761 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 }
5764 else
5765 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005766 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005767 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005768 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5769 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005771 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772 return NULL;
5773 }
5774
5775 // No argument, return zero.
5776 generate_PUSHNR(cctx, 0);
5777 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005778
5779 // Undo any command modifiers.
5780 generate_undo_cmdmods(cctx);
5781
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005782 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783 return NULL;
5784
5785 // "return val | endif" is possible
5786 return skipwhite(p);
5787}
5788
5789/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005790 * Get a line from the compilation context, compatible with exarg_T getline().
5791 * Return a pointer to the line in allocated memory.
5792 * Return NULL for end-of-file or some error.
5793 */
5794 static char_u *
5795exarg_getline(
5796 int c UNUSED,
5797 void *cookie,
5798 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005799 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005800{
5801 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005802 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005803
Bram Moolenaar66250c92020-08-20 15:02:42 +02005804 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005805 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005806 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005807 return NULL;
5808 ++cctx->ctx_lnum;
5809 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5810 // Comment lines result in NULL pointers, skip them.
5811 if (p != NULL)
5812 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005813 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005814}
5815
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005816 void
5817fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5818{
5819 eap->getline = exarg_getline;
5820 eap->cookie = cctx;
5821}
5822
Bram Moolenaar04b12692020-05-04 23:24:44 +02005823/*
5824 * Compile a nested :def command.
5825 */
5826 static char_u *
5827compile_nested_function(exarg_T *eap, cctx_T *cctx)
5828{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005829 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005830 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005831 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005832 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005833 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005834 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005835 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005836
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005837 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005838 {
5839 emsg(_(e_cannot_use_bang_with_nested_def));
5840 return NULL;
5841 }
5842
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005843 if (*name_start == '/')
5844 {
5845 name_end = skip_regexp(name_start + 1, '/', TRUE);
5846 if (*name_end == '/')
5847 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005848 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005849 }
5850 if (name_end == name_start || *skipwhite(name_end) != '(')
5851 {
5852 if (!ends_excmd2(name_start, name_end))
5853 {
5854 semsg(_(e_invalid_command_str), eap->cmd);
5855 return NULL;
5856 }
5857
5858 // "def" or "def Name": list functions
5859 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5860 return NULL;
5861 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5862 }
5863
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005864 // Only g:Func() can use a namespace.
5865 if (name_start[1] == ':' && !is_global)
5866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005867 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005868 return NULL;
5869 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005870 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005871 return NULL;
5872
Bram Moolenaar04b12692020-05-04 23:24:44 +02005873 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005874 fill_exarg_from_cctx(eap, cctx);
5875
Bram Moolenaar04b12692020-05-04 23:24:44 +02005876 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005877 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005878 lambda_name = vim_strsave(get_lambda_name());
5879 if (lambda_name == NULL)
5880 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005881 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005882
Bram Moolenaar822ba242020-05-24 23:00:18 +02005883 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005884 {
5885 r = eap->skip ? OK : FAIL;
5886 goto theend;
5887 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005888
5889 // copy over the block scope IDs before compiling
5890 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5891 {
5892 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5893
5894 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5895 if (ufunc->uf_block_ids != NULL)
5896 {
5897 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5898 sizeof(int) * block_depth);
5899 ufunc->uf_block_depth = block_depth;
5900 }
5901 }
5902
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005903 compile_type = COMPILE_TYPE(ufunc);
5904#ifdef FEAT_PROFILE
5905 // If the outer function is profiled, also compile the nested function for
5906 // profiling.
5907 if (cctx->ctx_compile_type == CT_PROFILE)
5908 compile_type = CT_PROFILE;
5909#endif
5910 if (func_needs_compiling(ufunc, compile_type)
5911 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005912 {
5913 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005914 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005915 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005916
Bram Moolenaar648594e2021-07-11 17:55:01 +02005917#ifdef FEAT_PROFILE
5918 // When the outer function is compiled for profiling, the nested function
5919 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005920 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005921 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5922#endif
5923
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005924 if (is_global)
5925 {
5926 char_u *func_name = vim_strnsave(name_start + 2,
5927 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005928
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005929 if (func_name == NULL)
5930 r = FAIL;
5931 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005932 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005933 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005934 lambda_name = NULL;
5935 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005936 }
5937 else
5938 {
5939 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005940 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005941 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005942
Bram Moolenaareef21022020-08-01 22:16:43 +02005943 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005944 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005945 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005946 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005947 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5948 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005949
5950theend:
5951 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005952 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005953}
5954
5955/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 * Return the length of an assignment operator, or zero if there isn't one.
5957 */
5958 int
5959assignment_len(char_u *p, int *heredoc)
5960{
5961 if (*p == '=')
5962 {
5963 if (p[1] == '<' && p[2] == '<')
5964 {
5965 *heredoc = TRUE;
5966 return 3;
5967 }
5968 return 1;
5969 }
5970 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5971 return 2;
5972 if (STRNCMP(p, "..=", 3) == 0)
5973 return 3;
5974 return 0;
5975}
5976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005978 * Generate the load instruction for "name".
5979 */
5980 static void
5981generate_loadvar(
5982 cctx_T *cctx,
5983 assign_dest_T dest,
5984 char_u *name,
5985 lvar_T *lvar,
5986 type_T *type)
5987{
5988 switch (dest)
5989 {
5990 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005991 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005992 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5993 break;
5994 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005995 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5996 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5997 else
5998 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005999 break;
6000 case dest_buffer:
6001 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
6002 break;
6003 case dest_window:
6004 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
6005 break;
6006 case dest_tab:
6007 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
6008 break;
6009 case dest_script:
6010 compile_load_scriptvar(cctx,
6011 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6012 break;
6013 case dest_env:
6014 // Include $ in the name here
6015 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6016 break;
6017 case dest_reg:
6018 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6019 break;
6020 case dest_vimvar:
6021 generate_LOADV(cctx, name + 2, TRUE);
6022 break;
6023 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006024 if (lvar->lv_from_outer > 0)
6025 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6026 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006027 else
6028 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6029 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006030 case dest_expr:
6031 // list or dict value should already be on the stack.
6032 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006033 }
6034}
6035
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006036/*
6037 * Skip over "[expr]" or ".member".
6038 * Does not check for any errors.
6039 */
6040 static char_u *
6041skip_index(char_u *start)
6042{
6043 char_u *p = start;
6044
6045 if (*p == '[')
6046 {
6047 p = skipwhite(p + 1);
6048 (void)skip_expr(&p, NULL);
6049 p = skipwhite(p);
6050 if (*p == ']')
6051 return p + 1;
6052 return p;
6053 }
6054 // if (*p == '.')
6055 return to_name_end(p + 1, TRUE);
6056}
6057
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006058 void
6059vim9_declare_error(char_u *name)
6060{
6061 char *scope = "";
6062
6063 switch (*name)
6064 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006065 case 'g': scope = _("global"); break;
6066 case 'b': scope = _("buffer"); break;
6067 case 'w': scope = _("window"); break;
6068 case 't': scope = _("tab"); break;
6069 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006070 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006071 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006072 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006073 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006074 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006075 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006076 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006077 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006078 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006079}
6080
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006081/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006082 * For one assignment figure out the type of destination. Return it in "dest".
6083 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006084 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006085 * For a v:var "vimvaridx" is set.
6086 * "type" is set to the destination type if known, unchanted otherwise.
6087 * Return FAIL if an error message was given.
6088 */
6089 static int
6090get_var_dest(
6091 char_u *name,
6092 assign_dest_T *dest,
6093 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006094 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006095 int *vimvaridx,
6096 type_T **type,
6097 cctx_T *cctx)
6098{
6099 char_u *p;
6100
6101 if (*name == '&')
6102 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006103 int cc;
6104 long numval;
6105 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006106 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006107
6108 *dest = dest_option;
6109 if (cmdidx == CMD_final || cmdidx == CMD_const)
6110 {
6111 emsg(_(e_const_option));
6112 return FAIL;
6113 }
6114 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006115 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006116 if (p == NULL)
6117 {
6118 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006119 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006120 return FAIL;
6121 }
6122 cc = *p;
6123 *p = NUL;
6124 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006125 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006126 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006127 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006128 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006129 case gov_unknown:
6130 semsg(_(e_unknown_option), name);
6131 return FAIL;
6132 case gov_string:
6133 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006134 if (opt_p_flags & P_FUNC)
6135 {
6136 // might be a Funcref, check the type later
6137 *type = &t_any;
6138 *dest = dest_func_option;
6139 }
6140 else
6141 {
6142 *type = &t_string;
6143 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006144 break;
6145 case gov_bool:
6146 case gov_hidden_bool:
6147 *type = &t_bool;
6148 break;
6149 case gov_number:
6150 case gov_hidden_number:
6151 *type = &t_number;
6152 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006153 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006154 }
6155 else if (*name == '$')
6156 {
6157 *dest = dest_env;
6158 *type = &t_string;
6159 }
6160 else if (*name == '@')
6161 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006162 if (name[1] != '@'
6163 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006164 {
6165 emsg_invreg(name[1]);
6166 return FAIL;
6167 }
6168 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006169 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006170 }
6171 else if (STRNCMP(name, "g:", 2) == 0)
6172 {
6173 *dest = dest_global;
6174 }
6175 else if (STRNCMP(name, "b:", 2) == 0)
6176 {
6177 *dest = dest_buffer;
6178 }
6179 else if (STRNCMP(name, "w:", 2) == 0)
6180 {
6181 *dest = dest_window;
6182 }
6183 else if (STRNCMP(name, "t:", 2) == 0)
6184 {
6185 *dest = dest_tab;
6186 }
6187 else if (STRNCMP(name, "v:", 2) == 0)
6188 {
6189 typval_T *vtv;
6190 int di_flags;
6191
6192 *vimvaridx = find_vim_var(name + 2, &di_flags);
6193 if (*vimvaridx < 0)
6194 {
6195 semsg(_(e_variable_not_found_str), name);
6196 return FAIL;
6197 }
6198 // We use the current value of "sandbox" here, is that OK?
6199 if (var_check_ro(di_flags, name, FALSE))
6200 return FAIL;
6201 *dest = dest_vimvar;
6202 vtv = get_vim_var_tv(*vimvaridx);
6203 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6204 }
6205 return OK;
6206}
6207
6208/*
6209 * Generate a STORE instruction for "dest", not being "dest_local".
6210 * Return FAIL when out of memory.
6211 */
6212 static int
6213generate_store_var(
6214 cctx_T *cctx,
6215 assign_dest_T dest,
6216 int opt_flags,
6217 int vimvaridx,
6218 int scriptvar_idx,
6219 int scriptvar_sid,
6220 type_T *type,
6221 char_u *name)
6222{
6223 switch (dest)
6224 {
6225 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006226 return generate_STOREOPT(cctx, ISN_STOREOPT,
6227 skip_option_env_lead(name), opt_flags);
6228 case dest_func_option:
6229 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
6230 skip_option_env_lead(name), opt_flags);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006231 case dest_global:
6232 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006233 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6234 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006235 case dest_buffer:
6236 // include b: with the name, easier to execute that way
6237 return generate_STORE(cctx, ISN_STOREB, 0, name);
6238 case dest_window:
6239 // include w: with the name, easier to execute that way
6240 return generate_STORE(cctx, ISN_STOREW, 0, name);
6241 case dest_tab:
6242 // include t: with the name, easier to execute that way
6243 return generate_STORE(cctx, ISN_STORET, 0, name);
6244 case dest_env:
6245 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6246 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006247 return generate_STORE(cctx, ISN_STOREREG,
6248 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006249 case dest_vimvar:
6250 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6251 case dest_script:
6252 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006253 // "s:" may be included in the name.
6254 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006255 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006256 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6257 scriptvar_sid, scriptvar_idx, type);
6258 case dest_local:
6259 case dest_expr:
6260 // cannot happen
6261 break;
6262 }
6263 return FAIL;
6264}
6265
Bram Moolenaar752fc692021-01-04 21:57:11 +01006266 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006267generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6268{
6269 if (lhs->lhs_dest != dest_local)
6270 return generate_store_var(cctx, lhs->lhs_dest,
6271 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6272 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6273 lhs->lhs_type, lhs->lhs_name);
6274
6275 if (lhs->lhs_lvar != NULL)
6276 {
6277 garray_T *instr = &cctx->ctx_instr;
6278 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6279
6280 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6281 // ISN_STORENR
6282 if (lhs->lhs_lvar->lv_from_outer == 0
6283 && instr->ga_len == instr_count + 1
6284 && isn->isn_type == ISN_PUSHNR)
6285 {
6286 varnumber_T val = isn->isn_arg.number;
6287 garray_T *stack = &cctx->ctx_type_stack;
6288
6289 isn->isn_type = ISN_STORENR;
6290 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6291 isn->isn_arg.storenr.stnr_val = val;
6292 if (stack->ga_len > 0)
6293 --stack->ga_len;
6294 }
6295 else if (lhs->lhs_lvar->lv_from_outer > 0)
6296 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6297 lhs->lhs_lvar->lv_from_outer);
6298 else
6299 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6300 }
6301 return OK;
6302}
6303
6304 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006305is_decl_command(int cmdidx)
6306{
6307 return cmdidx == CMD_let || cmdidx == CMD_var
6308 || cmdidx == CMD_final || cmdidx == CMD_const;
6309}
6310
6311/*
6312 * Figure out the LHS type and other properties for an assignment or one item
6313 * of ":unlet" with an index.
6314 * Returns OK or FAIL.
6315 */
6316 static int
6317compile_lhs(
6318 char_u *var_start,
6319 lhs_T *lhs,
6320 int cmdidx,
6321 int heredoc,
6322 int oplen,
6323 cctx_T *cctx)
6324{
6325 char_u *var_end;
6326 int is_decl = is_decl_command(cmdidx);
6327
6328 CLEAR_POINTER(lhs);
6329 lhs->lhs_dest = dest_local;
6330 lhs->lhs_vimvaridx = -1;
6331 lhs->lhs_scriptvar_idx = -1;
6332
6333 // "dest_end" is the end of the destination, including "[expr]" or
6334 // ".name".
6335 // "var_end" is the end of the variable/option/etc. name.
6336 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6337 if (*var_start == '@')
6338 var_end = var_start + 2;
6339 else
6340 {
6341 // skip over the leading "&", "&l:", "&g:" and "$"
6342 var_end = skip_option_env_lead(var_start);
6343 var_end = to_name_end(var_end, TRUE);
6344 }
6345
6346 // "a: type" is declaring variable "a" with a type, not dict "a:".
6347 if (is_decl && lhs->lhs_dest_end == var_start + 2
6348 && lhs->lhs_dest_end[-1] == ':')
6349 --lhs->lhs_dest_end;
6350 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6351 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006352 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006353
6354 // compute the length of the destination without "[expr]" or ".name"
6355 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006356 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006357 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6358 if (lhs->lhs_name == NULL)
6359 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006360
6361 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6362 // Something follows after the variable: "var[idx]" or "var.key".
6363 lhs->lhs_has_index = TRUE;
6364
Bram Moolenaar752fc692021-01-04 21:57:11 +01006365 if (heredoc)
6366 lhs->lhs_type = &t_list_string;
6367 else
6368 lhs->lhs_type = &t_any;
6369
6370 if (cctx->ctx_skip != SKIP_YES)
6371 {
6372 int declare_error = FALSE;
6373
6374 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6375 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6376 &lhs->lhs_type, cctx) == FAIL)
6377 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006378 if (lhs->lhs_dest != dest_local
6379 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006380 {
6381 // Specific kind of variable recognized.
6382 declare_error = is_decl;
6383 }
6384 else
6385 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006386 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006387 if (check_reserved_name(lhs->lhs_name) == FAIL)
6388 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006389
6390 if (lookup_local(var_start, lhs->lhs_varlen,
6391 &lhs->lhs_local_lvar, cctx) == OK)
6392 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6393 else
6394 {
6395 CLEAR_FIELD(lhs->lhs_arg_lvar);
6396 if (arg_exists(var_start, lhs->lhs_varlen,
6397 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6398 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6399 {
6400 if (is_decl)
6401 {
6402 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6403 return FAIL;
6404 }
6405 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6406 }
6407 }
6408 if (lhs->lhs_lvar != NULL)
6409 {
6410 if (is_decl)
6411 {
6412 semsg(_(e_variable_already_declared), lhs->lhs_name);
6413 return FAIL;
6414 }
6415 }
6416 else
6417 {
6418 int script_namespace = lhs->lhs_varlen > 1
6419 && STRNCMP(var_start, "s:", 2) == 0;
6420 int script_var = (script_namespace
6421 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006422 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006423 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006424 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006425 imported_T *import =
6426 find_imported(var_start, lhs->lhs_varlen, cctx);
6427
6428 if (script_namespace || script_var || import != NULL)
6429 {
6430 char_u *rawname = lhs->lhs_name
6431 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6432
6433 if (is_decl)
6434 {
6435 if (script_namespace)
6436 semsg(_(e_cannot_declare_script_variable_in_function),
6437 lhs->lhs_name);
6438 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006439 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006440 lhs->lhs_name);
6441 return FAIL;
6442 }
6443 else if (cctx->ctx_ufunc->uf_script_ctx_version
6444 == SCRIPT_VERSION_VIM9
6445 && script_namespace
6446 && !script_var && import == NULL)
6447 {
6448 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6449 return FAIL;
6450 }
6451
6452 lhs->lhs_dest = dest_script;
6453
6454 // existing script-local variables should have a type
6455 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6456 if (import != NULL)
6457 lhs->lhs_scriptvar_sid = import->imp_sid;
6458 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6459 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006460 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006461 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006462 lhs->lhs_scriptvar_sid, rawname,
6463 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6464 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006465 if (lhs->lhs_scriptvar_idx >= 0)
6466 {
6467 scriptitem_T *si = SCRIPT_ITEM(
6468 lhs->lhs_scriptvar_sid);
6469 svar_T *sv =
6470 ((svar_T *)si->sn_var_vals.ga_data)
6471 + lhs->lhs_scriptvar_idx;
6472 lhs->lhs_type = sv->sv_type;
6473 }
6474 }
6475 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006476 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477 == FAIL)
6478 return FAIL;
6479 }
6480 }
6481
6482 if (declare_error)
6483 {
6484 vim9_declare_error(lhs->lhs_name);
6485 return FAIL;
6486 }
6487 }
6488
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006489 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006490 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6491 var_end = lhs->lhs_dest_end;
6492
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006493 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006494 {
6495 if (is_decl && *var_end == ':')
6496 {
6497 char_u *p;
6498
6499 // parse optional type: "let var: type = expr"
6500 if (!VIM_ISWHITE(var_end[1]))
6501 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006502 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006503 return FAIL;
6504 }
6505 p = skipwhite(var_end + 1);
6506 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6507 if (lhs->lhs_type == NULL)
6508 return FAIL;
6509 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006510 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006511 }
6512 else if (lhs->lhs_lvar != NULL)
6513 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6514 }
6515
Bram Moolenaare42939a2021-04-05 17:11:17 +02006516 if (oplen == 3 && !heredoc
6517 && lhs->lhs_dest != dest_global
6518 && !lhs->lhs_has_index
6519 && lhs->lhs_type->tt_type != VAR_STRING
6520 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006521 {
6522 emsg(_(e_can_only_concatenate_to_string));
6523 return FAIL;
6524 }
6525
6526 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6527 && cctx->ctx_skip != SKIP_YES)
6528 {
6529 if (oplen > 1 && !heredoc)
6530 {
6531 // +=, /=, etc. require an existing variable
6532 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6533 return FAIL;
6534 }
6535 if (!is_decl)
6536 {
6537 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6538 return FAIL;
6539 }
6540
Bram Moolenaar3f327882021-03-17 20:56:38 +01006541 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006542 if ((lhs->lhs_type->tt_type == VAR_FUNC
6543 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006544 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006545 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006546
6547 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006548 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6549 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6550 if (lhs->lhs_lvar == NULL)
6551 return FAIL;
6552 lhs->lhs_new_local = TRUE;
6553 }
6554
6555 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006556 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006557 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006558 char_u *after = var_start + lhs->lhs_varlen;
6559 char_u *p;
6560
Bram Moolenaar752fc692021-01-04 21:57:11 +01006561 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006562 if (is_decl)
6563 {
6564 emsg(_(e_cannot_use_index_when_declaring_variable));
6565 return FAIL;
6566 }
6567
Bram Moolenaarc750d912021-11-29 22:02:12 +00006568 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6569 // Only the last index is used below, if there are others
6570 // before it generate code for the expression. Thus for
6571 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6572 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006574 p = skip_index(after);
6575 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006577 lhs->lhs_varlen_total = p - var_start;
6578 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006579 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006580 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006581 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006582 if (after > var_start + lhs->lhs_varlen)
6583 {
6584 lhs->lhs_varlen = after - var_start;
6585 lhs->lhs_dest = dest_expr;
6586 // We don't know the type before evaluating the expression,
6587 // use "any" until then.
6588 lhs->lhs_type = &t_any;
6589 }
6590
6591 if (lhs->lhs_type->tt_member == NULL)
6592 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006593 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006594 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 }
6596 return OK;
6597}
6598
6599/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006600 * Figure out the LHS and check a few errors.
6601 */
6602 static int
6603compile_assign_lhs(
6604 char_u *var_start,
6605 lhs_T *lhs,
6606 int cmdidx,
6607 int is_decl,
6608 int heredoc,
6609 int oplen,
6610 cctx_T *cctx)
6611{
6612 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6613 return FAIL;
6614
6615 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6616 {
6617 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6618 return FAIL;
6619 }
6620 if (!is_decl && lhs->lhs_lvar != NULL
6621 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6622 {
6623 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6624 return FAIL;
6625 }
6626 return OK;
6627}
6628
6629/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006630 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6631 */
6632 static int
6633has_list_index(char_u *idx_start, cctx_T *cctx)
6634{
6635 char_u *p = idx_start;
6636 int save_skip;
6637
6638 if (*p != '[')
6639 return FALSE;
6640
6641 p = skipwhite(p + 1);
6642 if (*p == ':')
6643 return TRUE;
6644
6645 save_skip = cctx->ctx_skip;
6646 cctx->ctx_skip = SKIP_YES;
6647 (void)compile_expr0(&p, cctx);
6648 cctx->ctx_skip = save_skip;
6649 return *skipwhite(p) == ':';
6650}
6651
6652/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006653 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6654 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006655 */
6656 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006657compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006658 char_u *var_start,
6659 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006660 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006661 cctx_T *cctx)
6662{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006663 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006664 char_u *p;
6665 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006666 int need_white_before = TRUE;
6667 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006668
Bram Moolenaar752fc692021-01-04 21:57:11 +01006669 p = var_start + varlen;
6670 if (*p == '[')
6671 {
6672 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006673 if (*p == ':')
6674 {
6675 // empty first index, push zero
6676 r = generate_PUSHNR(cctx, 0);
6677 need_white_before = FALSE;
6678 }
6679 else
6680 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006681
6682 if (r == OK && *skipwhite(p) == ':')
6683 {
6684 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006685 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006686 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006687 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006688 empty_second = *skipwhite(p + 1) == ']';
6689 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6690 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006691 {
6692 semsg(_(e_white_space_required_before_and_after_str_at_str),
6693 ":", p);
6694 return FAIL;
6695 }
6696 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006697 if (*p == ']')
6698 // empty second index, push "none"
6699 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6700 else
6701 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006702 }
6703
Bram Moolenaar752fc692021-01-04 21:57:11 +01006704 if (r == OK && *skipwhite(p) != ']')
6705 {
6706 // this should not happen
6707 emsg(_(e_missbrac));
6708 r = FAIL;
6709 }
6710 }
6711 else // if (*p == '.')
6712 {
6713 char_u *key_end = to_name_end(p + 1, TRUE);
6714 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6715
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006716 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006717 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006718 return r;
6719}
6720
6721/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006722 * For a LHS with an index, load the variable to be indexed.
6723 */
6724 static int
6725compile_load_lhs(
6726 lhs_T *lhs,
6727 char_u *var_start,
6728 type_T *rhs_type,
6729 cctx_T *cctx)
6730{
6731 if (lhs->lhs_dest == dest_expr)
6732 {
6733 size_t varlen = lhs->lhs_varlen;
6734 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006735 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006736 char_u *p = var_start;
6737 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006738 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006739
Bram Moolenaare97976b2021-08-01 13:17:17 +02006740 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6741 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006742 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006743 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6744 res = compile_expr0(&p, cctx);
6745 var_start[varlen] = c;
6746 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6747 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006748 {
6749 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006750 if (res != FAIL)
6751 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006752 return FAIL;
6753 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006754
6755 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6756 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6757 // now we can properly check the type
6758 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6759 && rhs_type != &t_void
6760 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6761 FALSE, FALSE) == FAIL)
6762 return FAIL;
6763 }
6764 else
6765 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6766 lhs->lhs_lvar, lhs->lhs_type);
6767 return OK;
6768}
6769
6770/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006771 * Produce code for loading "lhs" and also take care of an index.
6772 * Return OK/FAIL.
6773 */
6774 static int
6775compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6776{
6777 compile_load_lhs(lhs, var_start, NULL, cctx);
6778
6779 if (lhs->lhs_has_index)
6780 {
6781 int range = FALSE;
6782
6783 // Get member from list or dict. First compile the
6784 // index value.
6785 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6786 return FAIL;
6787 if (range)
6788 {
6789 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6790 var_start);
6791 return FAIL;
6792 }
6793
6794 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006795 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006796 return FAIL;
6797 }
6798 return OK;
6799}
6800
6801/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006802 * Assignment to a list or dict member, or ":unlet" for the item, using the
6803 * information in "lhs".
6804 * Returns OK or FAIL.
6805 */
6806 static int
6807compile_assign_unlet(
6808 char_u *var_start,
6809 lhs_T *lhs,
6810 int is_assign,
6811 type_T *rhs_type,
6812 cctx_T *cctx)
6813{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006814 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006815 garray_T *stack = &cctx->ctx_type_stack;
6816 int range = FALSE;
6817
Bram Moolenaar68452172021-04-12 21:21:02 +02006818 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006819 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006820 if (is_assign && range
6821 && lhs->lhs_type->tt_type != VAR_LIST
6822 && lhs->lhs_type != &t_blob
6823 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006824 {
6825 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6826 return FAIL;
6827 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006828
6829 if (lhs->lhs_type == &t_any)
6830 {
6831 // Index on variable of unknown type: check at runtime.
6832 dest_type = VAR_ANY;
6833 }
6834 else
6835 {
6836 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006837 if (dest_type == VAR_DICT && range)
6838 {
6839 emsg(e_cannot_use_range_with_dictionary);
6840 return FAIL;
6841 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006842 if (dest_type == VAR_DICT
6843 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006844 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006845 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006846 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006847 type_T *type;
6848
6849 if (range)
6850 {
6851 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6852 if (need_type(type, &t_number,
6853 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006854 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006855 }
6856 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006857 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006858 && need_type(type, &t_number,
6859 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006860 return FAIL;
6861 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006862 }
6863
6864 // Load the dict or list. On the stack we then have:
6865 // - value (for assignment, not for :unlet)
6866 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006867 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006868 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006869 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6870 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006871
Bram Moolenaar68452172021-04-12 21:21:02 +02006872 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6873 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006874 {
6875 if (is_assign)
6876 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006877 if (range)
6878 {
6879 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6880 return FAIL;
6881 }
6882 else
6883 {
6884 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006885
Bram Moolenaar68452172021-04-12 21:21:02 +02006886 if (isn == NULL)
6887 return FAIL;
6888 isn->isn_arg.vartype = dest_type;
6889 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006890 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006891 else if (range)
6892 {
6893 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6894 return FAIL;
6895 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006896 else
6897 {
6898 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6899 return FAIL;
6900 }
6901 }
6902 else
6903 {
6904 emsg(_(e_indexable_type_required));
6905 return FAIL;
6906 }
6907
6908 return OK;
6909}
6910
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006911/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006912 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006913 * "let name"
6914 * "var name = expr"
6915 * "final name = expr"
6916 * "const name = expr"
6917 * "name = expr"
6918 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006919 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006920 * Return NULL for an error.
6921 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 */
6923 static char_u *
6924compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6925{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006926 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006928 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 char_u *ret = NULL;
6930 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006931 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006933 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006935 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 int oplen = 0;
6938 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006939 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006940 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006942 int is_decl = is_decl_command(cmdidx);
6943 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006944 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006946 // Skip over the "var" or "[var, var]" to get to any "=".
6947 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6948 if (p == NULL)
6949 return *arg == '[' ? arg : NULL;
6950
Bram Moolenaar752fc692021-01-04 21:57:11 +01006951 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 sp = p;
6954 p = skipwhite(p);
6955 op = p;
6956 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006957
6958 if (var_count > 0 && oplen == 0)
6959 // can be something like "[1, 2]->func()"
6960 return arg;
6961
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006962 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006964 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006965 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006966 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006967 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6968 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006969 if (VIM_ISWHITE(eap->cmd[2]))
6970 {
6971 semsg(_(e_no_white_space_allowed_after_str_str),
6972 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6973 return NULL;
6974 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006975 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6976 oplen = 2;
6977 incdec = TRUE;
6978 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 if (heredoc)
6981 {
6982 list_T *l;
6983 listitem_T *li;
6984
6985 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006986 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006988 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006989 if (l == NULL)
6990 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991
Bram Moolenaar078269b2020-09-21 20:35:55 +02006992 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006994 // Push each line and the create the list.
6995 FOR_ALL_LIST_ITEMS(l, li)
6996 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006997 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006998 li->li_tv.vval.v_string = NULL;
6999 }
7000 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 list_free(l);
7003 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007004 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007006 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007008 char_u *wp;
7009
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007010 // for "[var, var] = expr" evaluate the expression here, loop over the
7011 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007012 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02007013
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007014 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01007015 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01007016 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007017 if (compile_expr0(&p, cctx) == FAIL)
7018 return NULL;
7019 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007021 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007023 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007024 int needed_list_len;
7025 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007026
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007027 stacktype = stack->ga_len == 0 ? &t_void
7028 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007029 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007031 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007032 goto theend;
7033 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007034 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007035 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007036 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007037 // If a constant list was used we can check the length right here.
7038 needed_list_len = semicolon ? var_count - 1 : var_count;
7039 if (instr->ga_len > 0)
7040 {
7041 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7042
7043 if (isn->isn_type == ISN_NEWLIST)
7044 {
7045 did_check = TRUE;
7046 if (semicolon ? isn->isn_arg.number < needed_list_len
7047 : isn->isn_arg.number != needed_list_len)
7048 {
7049 semsg(_(e_expected_nr_items_but_got_nr),
7050 needed_list_len, isn->isn_arg.number);
7051 goto theend;
7052 }
7053 }
7054 }
7055 if (!did_check)
7056 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007057 if (stacktype->tt_member != NULL)
7058 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007059 }
7060 }
7061
7062 /*
7063 * Loop over variables in "[var, var] = expr".
7064 * For "var = expr" and "let var: type" this is done only once.
7065 */
7066 if (var_count > 0)
7067 var_start = skipwhite(arg + 1); // skip over the "["
7068 else
7069 var_start = arg;
7070 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7071 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007072 int instr_count = -1;
7073 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007074
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007075 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7076 {
7077 // Ignore underscore in "[a, _, b] = list".
7078 if (var_count > 0)
7079 {
7080 var_start = skipwhite(var_start + 2);
7081 continue;
7082 }
7083 emsg(_(e_cannot_use_underscore_here));
7084 goto theend;
7085 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007086 vim_free(lhs.lhs_name);
7087
7088 /*
7089 * Figure out the LHS type and other properties.
7090 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007091 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7092 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007093 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007094 if (heredoc)
7095 {
7096 SOURCING_LNUM = start_lnum;
7097 if (lhs.lhs_has_type
7098 && need_type(&t_list_string, lhs.lhs_type,
7099 -1, 0, cctx, FALSE, FALSE) == FAIL)
7100 goto theend;
7101 }
7102 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007103 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007104 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007105 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007106 if (oplen > 0 && var_count == 0)
7107 {
7108 // skip over the "=" and the expression
7109 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007110 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007111 }
7112 }
7113 else if (oplen > 0)
7114 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007115 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007116 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007117
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007118 // for "+=", "*=", "..=" etc. first load the current value
7119 if (*op != '='
7120 && compile_load_lhs_with_index(&lhs, var_start,
7121 cctx) == FAIL)
7122 goto theend;
7123
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007124 // For "var = expr" evaluate the expression.
7125 if (var_count == 0)
7126 {
7127 int r;
7128
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007129 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007130 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007131 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007132 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007133 r = generate_PUSHNR(cctx, 1);
7134 }
7135 else
7136 {
7137 // Temporarily hide the new local variable here, it is
7138 // not available to this expression.
7139 if (lhs.lhs_new_local)
7140 --cctx->ctx_locals.ga_len;
7141 wp = op + oplen;
7142 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7143 {
7144 if (lhs.lhs_new_local)
7145 ++cctx->ctx_locals.ga_len;
7146 goto theend;
7147 }
7148 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007149 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007150 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007151 if (r == FAIL)
7152 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007153 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007154 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007155 else if (semicolon && var_idx == var_count - 1)
7156 {
7157 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007158 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007159 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7160 goto theend;
7161 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007162 else
7163 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007164 // For "[var, var] = expr" get the "var_idx" item from the
7165 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007166 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007167 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007168 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007169
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007170 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007171 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007172 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007173 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007174 if ((rhs_type->tt_type == VAR_FUNC
7175 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007176 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007177 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007178 goto theend;
7179
Bram Moolenaar752fc692021-01-04 21:57:11 +01007180 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007181 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007182 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007183 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007184 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007185 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007186 }
7187 else
7188 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007189 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007190 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007191 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007192 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007193 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007194 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007195 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007196 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007197 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007198 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007199 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007200 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007201 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007202 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007203 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007204 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007205
Bram Moolenaar77709b12021-04-03 21:01:01 +02007206 // Without operator check type here, otherwise below.
7207 // Use the line number of the assignment.
7208 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007209 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7210 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007211 // If assigning to a list or dict member, use the
7212 // member type. Not for "list[:] =".
7213 if (lhs.lhs_has_index
7214 && !has_list_index(var_start + lhs.lhs_varlen,
7215 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007216 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007217 if (need_type_where(rhs_type, use_type, -1, where,
7218 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007219 goto theend;
7220 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007221 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007222 else
7223 {
7224 type_T *lhs_type = lhs.lhs_member_type;
7225
7226 // Special case: assigning to @# can use a number or a
7227 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007228 // Also: can assign a number to a float.
7229 if ((lhs_type == &t_number_or_string
7230 || lhs_type == &t_float)
7231 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007232 lhs_type = &t_number;
7233 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007234 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007235 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007238 else if (cmdidx == CMD_final)
7239 {
7240 emsg(_(e_final_requires_a_value));
7241 goto theend;
7242 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007243 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007245 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007246 goto theend;
7247 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007248 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
7249 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007250 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007251 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007252 goto theend;
7253 }
7254 else
7255 {
7256 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007257 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007258 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007259 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007260 {
7261 case VAR_BOOL:
7262 generate_PUSHBOOL(cctx, VVAL_FALSE);
7263 break;
7264 case VAR_FLOAT:
7265#ifdef FEAT_FLOAT
7266 generate_PUSHF(cctx, 0.0);
7267#endif
7268 break;
7269 case VAR_STRING:
7270 generate_PUSHS(cctx, NULL);
7271 break;
7272 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007273 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007274 break;
7275 case VAR_FUNC:
7276 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7277 break;
7278 case VAR_LIST:
7279 generate_NEWLIST(cctx, 0);
7280 break;
7281 case VAR_DICT:
7282 generate_NEWDICT(cctx, 0);
7283 break;
7284 case VAR_JOB:
7285 generate_PUSHJOB(cctx, NULL);
7286 break;
7287 case VAR_CHANNEL:
7288 generate_PUSHCHANNEL(cctx, NULL);
7289 break;
7290 case VAR_NUMBER:
7291 case VAR_UNKNOWN:
7292 case VAR_ANY:
7293 case VAR_PARTIAL:
7294 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007295 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007296 case VAR_SPECIAL: // cannot happen
7297 generate_PUSHNR(cctx, 0);
7298 break;
7299 }
7300 }
7301 if (var_count == 0)
7302 end = p;
7303 }
7304
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007305 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007306 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007307 break;
7308
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007309 if (oplen > 0 && *op != '=')
7310 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007311 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007312 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007313
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007314 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007315 {
7316 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7317 goto theend;
7318 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007319 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007320 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007321 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007322 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7323 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007324#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007325 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007326 !(expected == &t_float && (stacktype == &t_number
7327 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007328#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007329 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007330 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007331 goto theend;
7332 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007333
7334 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007335 {
7336 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7337 goto theend;
7338 }
7339 else if (*op == '+')
7340 {
7341 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007342 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007343 lhs.lhs_member_type, stacktype,
7344 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007345 goto theend;
7346 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007347 else if (generate_two_op(cctx, op) == FAIL)
7348 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007351 // Use the line number of the assignment for store instruction.
7352 save_lnum = cctx->ctx_lnum;
7353 cctx->ctx_lnum = start_lnum - 1;
7354
Bram Moolenaar752fc692021-01-04 21:57:11 +01007355 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007356 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007357 // Use the info in "lhs" to store the value at the index in the
7358 // list or dict.
7359 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7360 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007361 {
7362 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007363 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007364 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007365 }
7366 else
7367 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007368 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007369 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007370 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007371 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007372 generate_LOCKCONST(cctx);
7373
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007374 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007375 && (lhs.lhs_type->tt_type == VAR_DICT
7376 || lhs.lhs_type->tt_type == VAR_LIST)
7377 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007378 && !(lhs.lhs_type->tt_member == &t_any
7379 && oplen > 0
7380 && rhs_type != NULL
7381 && rhs_type->tt_type == lhs.lhs_type->tt_type
7382 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007383 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007384 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007385 // also in legacy script. Not for "list<any> = val", then the
7386 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007387 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007388
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007389 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007390 {
7391 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007392 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007393 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007394 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007395 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007396
7397 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007398 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007400
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007401 // For "[var, var] = expr" drop the "expr" value.
7402 // Also for "[var, var; _] = expr".
7403 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007404 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007405 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007406 goto theend;
7407 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007408
Bram Moolenaarb2097502020-07-19 17:17:02 +02007409 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410
7411theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007412 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 return ret;
7414}
7415
7416/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007417 * Check for an assignment at "eap->cmd", compile it if found.
7418 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7419 */
7420 static int
7421may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7422{
7423 char_u *pskip;
7424 char_u *p;
7425
7426 // Assuming the command starts with a variable or function name,
7427 // find what follows.
7428 // Skip over "var.member", "var[idx]" and the like.
7429 // Also "&opt = val", "$ENV = val" and "@r = val".
7430 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7431 ? eap->cmd + 1 : eap->cmd;
7432 p = to_name_end(pskip, TRUE);
7433 if (p > eap->cmd && *p != NUL)
7434 {
7435 char_u *var_end;
7436 int oplen;
7437 int heredoc;
7438
7439 if (eap->cmd[0] == '@')
7440 var_end = eap->cmd + 2;
7441 else
7442 var_end = find_name_end(pskip, NULL, NULL,
7443 FNE_CHECK_START | FNE_INCL_BR);
7444 oplen = assignment_len(skipwhite(var_end), &heredoc);
7445 if (oplen > 0)
7446 {
7447 size_t len = p - eap->cmd;
7448
7449 // Recognize an assignment if we recognize the variable
7450 // name:
7451 // "g:var = expr"
7452 // "local = expr" where "local" is a local var.
7453 // "script = expr" where "script" is a script-local var.
7454 // "import = expr" where "import" is an imported var
7455 // "&opt = expr"
7456 // "$ENV = expr"
7457 // "@r = expr"
7458 if (*eap->cmd == '&'
7459 || *eap->cmd == '$'
7460 || *eap->cmd == '@'
7461 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007462 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007463 {
7464 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7465 if (*line == NULL || *line == eap->cmd)
7466 return FAIL;
7467 return OK;
7468 }
7469 }
7470 }
7471
7472 if (*eap->cmd == '[')
7473 {
7474 // [var, var] = expr
7475 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7476 if (*line == NULL)
7477 return FAIL;
7478 if (*line != eap->cmd)
7479 return OK;
7480 }
7481 return NOTDONE;
7482}
7483
7484/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007485 * Check if "name" can be "unlet".
7486 */
7487 int
7488check_vim9_unlet(char_u *name)
7489{
7490 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7491 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007492 // "unlet s:var" is allowed in legacy script.
7493 if (*name == 's' && !script_is_vim9())
7494 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007495 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007496 return FAIL;
7497 }
7498 return OK;
7499}
7500
7501/*
7502 * Callback passed to ex_unletlock().
7503 */
7504 static int
7505compile_unlet(
7506 lval_T *lvp,
7507 char_u *name_end,
7508 exarg_T *eap,
7509 int deep UNUSED,
7510 void *coookie)
7511{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007512 cctx_T *cctx = coookie;
7513 char_u *p = lvp->ll_name;
7514 int cc = *name_end;
7515 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007516
Bram Moolenaar752fc692021-01-04 21:57:11 +01007517 if (cctx->ctx_skip == SKIP_YES)
7518 return OK;
7519
7520 *name_end = NUL;
7521 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007522 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007523 // :unlet $ENV_VAR
7524 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7525 }
7526 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7527 {
7528 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007529
Bram Moolenaar752fc692021-01-04 21:57:11 +01007530 // This is similar to assigning: lookup the list/dict, compile the
7531 // idx/key. Then instead of storing the value unlet the item.
7532 // unlet {list}[idx]
7533 // unlet {dict}[key] dict.key
7534 //
7535 // Figure out the LHS type and other properties.
7536 //
7537 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7538
7539 // : unlet an indexed item
7540 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007541 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007542 iemsg("called compile_lhs() without an index");
7543 ret = FAIL;
7544 }
7545 else
7546 {
7547 // Use the info in "lhs" to unlet the item at the index in the
7548 // list or dict.
7549 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007550 }
7551
Bram Moolenaar752fc692021-01-04 21:57:11 +01007552 vim_free(lhs.lhs_name);
7553 }
7554 else if (check_vim9_unlet(p) == FAIL)
7555 {
7556 ret = FAIL;
7557 }
7558 else
7559 {
7560 // Normal name. Only supports g:, w:, t: and b: namespaces.
7561 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007562 }
7563
Bram Moolenaar752fc692021-01-04 21:57:11 +01007564 *name_end = cc;
7565 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007566}
7567
7568/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007569 * Callback passed to ex_unletlock().
7570 */
7571 static int
7572compile_lock_unlock(
7573 lval_T *lvp,
7574 char_u *name_end,
7575 exarg_T *eap,
7576 int deep UNUSED,
7577 void *coookie)
7578{
7579 cctx_T *cctx = coookie;
7580 int cc = *name_end;
7581 char_u *p = lvp->ll_name;
7582 int ret = OK;
7583 size_t len;
7584 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007585 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007586
7587 if (cctx->ctx_skip == SKIP_YES)
7588 return OK;
7589
7590 // Cannot use :lockvar and :unlockvar on local variables.
7591 if (p[1] != ':')
7592 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007593 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007594
7595 if (lookup_local(p, end - p, NULL, cctx) == OK)
7596 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007597 char_u *s = p;
7598
7599 if (*end != '.' && *end != '[')
7600 {
7601 emsg(_(e_cannot_lock_unlock_local_variable));
7602 return FAIL;
7603 }
7604
7605 // For "d.member" put the local variable on the stack, it will be
7606 // passed to ex_lockvar() indirectly.
7607 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7608 return FAIL;
7609 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007610 }
7611 }
7612
7613 // Checking is done at runtime.
7614 *name_end = NUL;
7615 len = name_end - p + 20;
7616 buf = alloc(len);
7617 if (buf == NULL)
7618 ret = FAIL;
7619 else
7620 {
7621 vim_snprintf((char *)buf, len, "%s %s",
7622 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7623 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007624 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007625
7626 vim_free(buf);
7627 *name_end = cc;
7628 }
7629 return ret;
7630}
7631
7632/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007633 * compile "unlet var", "lock var" and "unlock var"
7634 * "arg" points to "var".
7635 */
7636 static char_u *
7637compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7638{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007639 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7640 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7641 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007642 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7643}
7644
7645/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007646 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7647 */
7648 static int
7649compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7650{
7651 garray_T *instr = &cctx->ctx_instr;
7652 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7653
7654 if (endlabel == NULL)
7655 return FAIL;
7656 endlabel->el_next = *el;
7657 *el = endlabel;
7658 endlabel->el_end_label = instr->ga_len;
7659
7660 generate_JUMP(cctx, when, 0);
7661 return OK;
7662}
7663
7664 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007665compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666{
7667 garray_T *instr = &cctx->ctx_instr;
7668
7669 while (*el != NULL)
7670 {
7671 endlabel_T *cur = (*el);
7672 isn_T *isn;
7673
7674 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007675 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 *el = cur->el_next;
7677 vim_free(cur);
7678 }
7679}
7680
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007681 static void
7682compile_free_jump_to_end(endlabel_T **el)
7683{
7684 while (*el != NULL)
7685 {
7686 endlabel_T *cur = (*el);
7687
7688 *el = cur->el_next;
7689 vim_free(cur);
7690 }
7691}
7692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693/*
7694 * Create a new scope and set up the generic items.
7695 */
7696 static scope_T *
7697new_scope(cctx_T *cctx, scopetype_T type)
7698{
7699 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7700
7701 if (scope == NULL)
7702 return NULL;
7703 scope->se_outer = cctx->ctx_scope;
7704 cctx->ctx_scope = scope;
7705 scope->se_type = type;
7706 scope->se_local_count = cctx->ctx_locals.ga_len;
7707 return scope;
7708}
7709
7710/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007711 * Free the current scope and go back to the outer scope.
7712 */
7713 static void
7714drop_scope(cctx_T *cctx)
7715{
7716 scope_T *scope = cctx->ctx_scope;
7717
7718 if (scope == NULL)
7719 {
7720 iemsg("calling drop_scope() without a scope");
7721 return;
7722 }
7723 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007724 switch (scope->se_type)
7725 {
7726 case IF_SCOPE:
7727 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7728 case FOR_SCOPE:
7729 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7730 case WHILE_SCOPE:
7731 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7732 case TRY_SCOPE:
7733 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7734 case NO_SCOPE:
7735 case BLOCK_SCOPE:
7736 break;
7737 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007738 vim_free(scope);
7739}
7740
7741/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 * compile "if expr"
7743 *
7744 * "if expr" Produces instructions:
7745 * EVAL expr Push result of "expr"
7746 * JUMP_IF_FALSE end
7747 * ... body ...
7748 * end:
7749 *
7750 * "if expr | else" Produces instructions:
7751 * EVAL expr Push result of "expr"
7752 * JUMP_IF_FALSE else
7753 * ... body ...
7754 * JUMP_ALWAYS end
7755 * else:
7756 * ... body ...
7757 * end:
7758 *
7759 * "if expr1 | elseif expr2 | else" Produces instructions:
7760 * EVAL expr Push result of "expr"
7761 * JUMP_IF_FALSE elseif
7762 * ... body ...
7763 * JUMP_ALWAYS end
7764 * elseif:
7765 * EVAL expr Push result of "expr"
7766 * JUMP_IF_FALSE else
7767 * ... body ...
7768 * JUMP_ALWAYS end
7769 * else:
7770 * ... body ...
7771 * end:
7772 */
7773 static char_u *
7774compile_if(char_u *arg, cctx_T *cctx)
7775{
7776 char_u *p = arg;
7777 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007778 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007780 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007781 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007782
Bram Moolenaara5565e42020-05-09 15:44:01 +02007783 CLEAR_FIELD(ppconst);
7784 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007785 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007786 clear_ppconst(&ppconst);
7787 return NULL;
7788 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007789 if (!ends_excmd2(arg, skipwhite(p)))
7790 {
7791 semsg(_(e_trailing_arg), p);
7792 return NULL;
7793 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007794 if (cctx->ctx_skip == SKIP_YES)
7795 clear_ppconst(&ppconst);
7796 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007797 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007798 int error = FALSE;
7799 int v;
7800
Bram Moolenaara5565e42020-05-09 15:44:01 +02007801 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007802 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007803 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007804 if (error)
7805 return NULL;
7806 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007807 }
7808 else
7809 {
7810 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007811 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007812 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007813 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007814 if (bool_on_stack(cctx) == FAIL)
7815 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817
Bram Moolenaara91a7132021-03-25 21:12:15 +01007818 // CMDMOD_REV must come before the jump
7819 generate_undo_cmdmods(cctx);
7820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821 scope = new_scope(cctx, IF_SCOPE);
7822 if (scope == NULL)
7823 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007824 scope->se_skip_save = skip_save;
7825 // "is_had_return" will be reset if any block does not end in :return
7826 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007828 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007829 {
7830 // "where" is set when ":elseif", "else" or ":endif" is found
7831 scope->se_u.se_if.is_if_label = instr->ga_len;
7832 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7833 }
7834 else
7835 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836
Bram Moolenaarced68a02021-01-24 17:53:47 +01007837#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007838 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007839 && skip_save != SKIP_YES)
7840 {
7841 // generated a profile start, need to generate a profile end, since it
7842 // won't be done after returning
7843 cctx->ctx_skip = SKIP_NOT;
7844 generate_instr(cctx, ISN_PROF_END);
7845 cctx->ctx_skip = SKIP_YES;
7846 }
7847#endif
7848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 return p;
7850}
7851
7852 static char_u *
7853compile_elseif(char_u *arg, cctx_T *cctx)
7854{
7855 char_u *p = arg;
7856 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007857 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858 isn_T *isn;
7859 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007860 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007861 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862
7863 if (scope == NULL || scope->se_type != IF_SCOPE)
7864 {
7865 emsg(_(e_elseif_without_if));
7866 return NULL;
7867 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007868 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007869 if (!cctx->ctx_had_return)
7870 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871
Bram Moolenaarced68a02021-01-24 17:53:47 +01007872 if (cctx->ctx_skip == SKIP_NOT)
7873 {
7874 // previous block was executed, this one and following will not
7875 cctx->ctx_skip = SKIP_YES;
7876 scope->se_u.se_if.is_seen_skip_not = TRUE;
7877 }
7878 if (scope->se_u.se_if.is_seen_skip_not)
7879 {
7880 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007881 // Do not count the "elseif" for profiling and cmdmod
7882 instr->ga_len = current_instr_idx(cctx);
7883
Bram Moolenaarced68a02021-01-24 17:53:47 +01007884 skip_expr_cctx(&p, cctx);
7885 return p;
7886 }
7887
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007888 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007889 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007890 int moved_cmdmod = FALSE;
7891 int saved_debug = FALSE;
7892 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007893
7894 // Move any CMDMOD instruction to after the jump
7895 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7896 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007897 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007898 return NULL;
7899 ((isn_T *)instr->ga_data)[instr->ga_len] =
7900 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7901 --instr->ga_len;
7902 moved_cmdmod = TRUE;
7903 }
7904
Bram Moolenaar093165c2021-08-22 13:35:31 +02007905 // Remove the already generated ISN_DEBUG, it is written below the
7906 // ISN_FOR instruction.
7907 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7908 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7909 .isn_type == ISN_DEBUG)
7910 {
7911 --instr->ga_len;
7912 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7913 saved_debug = TRUE;
7914 }
7915
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007916 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007917 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007918 return NULL;
7919 // previous "if" or "elseif" jumps here
7920 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7921 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007922
Bram Moolenaara91a7132021-03-25 21:12:15 +01007923 if (moved_cmdmod)
7924 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007925
7926 if (saved_debug)
7927 {
7928 // move the debug instruction here
7929 if (GA_GROW_FAILS(instr, 1))
7930 return NULL;
7931 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7932 ++instr->ga_len;
7933 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007936 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007937 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007938 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007939 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007940 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007941#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007942 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007943 // the previous block was skipped, need to profile this line
7944 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007945#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007946 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007947 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007948 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007949 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007950
7951 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007952 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007953 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007954 clear_ppconst(&ppconst);
7955 return NULL;
7956 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007957 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007958 if (!ends_excmd2(arg, skipwhite(p)))
7959 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007960 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007961 semsg(_(e_trailing_arg), p);
7962 return NULL;
7963 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007964 if (scope->se_skip_save == SKIP_YES)
7965 clear_ppconst(&ppconst);
7966 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007967 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007968 int error = FALSE;
7969 int v;
7970
Bram Moolenaarfad27422021-11-30 21:58:19 +00007971 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007972 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7973 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007974 {
7975 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007976 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007977 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007978 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007979 clear_ppconst(&ppconst);
7980 scope->se_u.se_if.is_if_label = -1;
7981 }
7982 else
7983 {
7984 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007985 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007986 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007987 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007988 if (bool_on_stack(cctx) == FAIL)
7989 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007990
Bram Moolenaara91a7132021-03-25 21:12:15 +01007991 // CMDMOD_REV must come before the jump
7992 generate_undo_cmdmods(cctx);
7993
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007994 // "where" is set when ":elseif", "else" or ":endif" is found
7995 scope->se_u.se_if.is_if_label = instr->ga_len;
7996 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998
7999 return p;
8000}
8001
8002 static char_u *
8003compile_else(char_u *arg, cctx_T *cctx)
8004{
8005 char_u *p = arg;
8006 garray_T *instr = &cctx->ctx_instr;
8007 isn_T *isn;
8008 scope_T *scope = cctx->ctx_scope;
8009
8010 if (scope == NULL || scope->se_type != IF_SCOPE)
8011 {
8012 emsg(_(e_else_without_if));
8013 return NULL;
8014 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01008015 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008016 if (!cctx->ctx_had_return)
8017 scope->se_u.se_if.is_had_return = FALSE;
8018 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019
Bram Moolenaarced68a02021-01-24 17:53:47 +01008020#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008021 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01008022 {
8023 if (cctx->ctx_skip == SKIP_NOT
8024 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8025 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008026 // the previous block was executed, do not count "else" for
8027 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008028 --instr->ga_len;
8029 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8030 {
8031 // the previous block was not executed, this one will, do count the
8032 // "else" for profiling
8033 cctx->ctx_skip = SKIP_NOT;
8034 generate_instr(cctx, ISN_PROF_END);
8035 generate_instr(cctx, ISN_PROF_START);
8036 cctx->ctx_skip = SKIP_YES;
8037 }
8038 }
8039#endif
8040
8041 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008042 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008043 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008044 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008045 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008046 if (!cctx->ctx_had_return
8047 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8048 JUMP_ALWAYS, cctx) == FAIL)
8049 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008050 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008051
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008052 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008053 {
8054 if (scope->se_u.se_if.is_if_label >= 0)
8055 {
8056 // previous "if" or "elseif" jumps here
8057 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8058 isn->isn_arg.jump.jump_where = instr->ga_len;
8059 scope->se_u.se_if.is_if_label = -1;
8060 }
8061 }
8062
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008063 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008064 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066
8067 return p;
8068}
8069
8070 static char_u *
8071compile_endif(char_u *arg, cctx_T *cctx)
8072{
8073 scope_T *scope = cctx->ctx_scope;
8074 ifscope_T *ifscope;
8075 garray_T *instr = &cctx->ctx_instr;
8076 isn_T *isn;
8077
Bram Moolenaarfa984412021-03-25 22:15:28 +01008078 if (misplaced_cmdmod(cctx))
8079 return NULL;
8080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008081 if (scope == NULL || scope->se_type != IF_SCOPE)
8082 {
8083 emsg(_(e_endif_without_if));
8084 return NULL;
8085 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008086 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008087 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008088 if (!cctx->ctx_had_return)
8089 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008091 if (scope->se_u.se_if.is_if_label >= 0)
8092 {
8093 // previous "if" or "elseif" jumps here
8094 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8095 isn->isn_arg.jump.jump_where = instr->ga_len;
8096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008098 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008099
8100#ifdef FEAT_PROFILE
8101 // even when skipping we count the endif as executed, unless the block it's
8102 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008103 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008104 && scope->se_skip_save != SKIP_YES)
8105 {
8106 cctx->ctx_skip = SKIP_NOT;
8107 generate_instr(cctx, ISN_PROF_START);
8108 }
8109#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008110 cctx->ctx_skip = scope->se_skip_save;
8111
8112 // If all the blocks end in :return and there is an :else then the
8113 // had_return flag is set.
8114 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008116 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 return arg;
8118}
8119
8120/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008121 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 *
8123 * Produces instructions:
8124 * PUSHNR -1
8125 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008126 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8128 * - if beyond end, jump to "end"
8129 * - otherwise get item from list and push it
8130 * STORE var Store item in "var"
8131 * ... body ...
8132 * JUMP top Jump back to repeat
8133 * end: DROP Drop the result of "expr"
8134 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008135 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8136 * UNPACK 2 Split item in 2
8137 * STORE var1 Store item in "var1"
8138 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 */
8140 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008141compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008143 char_u *arg;
8144 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008145 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008147 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008148 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008149 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008150 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008153 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008155 lvar_T *loop_lvar; // loop iteration variable
8156 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008158 type_T *item_type = &t_any;
8159 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008160 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008161
Bram Moolenaar792f7862020-11-23 08:31:18 +01008162 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008163 if (p == NULL)
8164 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008165 if (var_count == 0)
8166 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008167 else
8168 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169
8170 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008171 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008172 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8173 return NULL;
8174 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008175 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008176 if (*p == ':' && wp != p)
8177 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8178 else
8179 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 return NULL;
8181 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008182 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008183 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8184 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008186 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8187 // instruction.
8188 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8189 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8190 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008191 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008192 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008193 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8194 .isn_arg.debug.dbg_break_lnum;
8195 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008197 scope = new_scope(cctx, FOR_SCOPE);
8198 if (scope == NULL)
8199 return NULL;
8200
Bram Moolenaar792f7862020-11-23 08:31:18 +01008201 // Reserve a variable to store the loop iteration counter and initialize it
8202 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008203 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8204 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008205 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008206 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008207 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008208 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008209 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008210 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008211
8212 // compile "expr", it remains on the stack until "endfor"
8213 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008214 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008215 {
8216 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008218 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008219 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008220
rbtnnbebf0692021-08-21 17:26:50 +02008221 if (cctx->ctx_skip != SKIP_YES)
8222 {
8223 // If we know the type of "var" and it is a not a supported type we can
8224 // give an error now.
8225 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8226 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008227 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008228 {
rbtnnbebf0692021-08-21 17:26:50 +02008229 semsg(_(e_for_loop_on_str_not_supported),
8230 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008231 drop_scope(cctx);
8232 return NULL;
8233 }
rbtnnbebf0692021-08-21 17:26:50 +02008234
8235 if (vartype->tt_type == VAR_STRING)
8236 item_type = &t_string;
8237 else if (vartype->tt_type == VAR_BLOB)
8238 item_type = &t_number;
8239 else if (vartype->tt_type == VAR_LIST
8240 && vartype->tt_member->tt_type != VAR_ANY)
8241 {
8242 if (!var_list)
8243 item_type = vartype->tt_member;
8244 else if (vartype->tt_member->tt_type == VAR_LIST
8245 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008246 item_type = vartype->tt_member->tt_member;
8247 }
8248
8249 // CMDMOD_REV must come before the FOR instruction.
8250 generate_undo_cmdmods(cctx);
8251
8252 // "for_end" is set when ":endfor" is found
8253 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8254
8255 generate_FOR(cctx, loop_lvar->lv_idx);
8256
8257 arg = arg_start;
8258 if (var_list)
8259 {
8260 generate_UNPACK(cctx, var_count, semicolon);
8261 arg = skipwhite(arg + 1); // skip white after '['
8262
8263 // the list item is replaced by a number of items
8264 if (GA_GROW_FAILS(stack, var_count - 1))
8265 {
8266 drop_scope(cctx);
8267 return NULL;
8268 }
8269 --stack->ga_len;
8270 for (idx = 0; idx < var_count; ++idx)
8271 {
8272 ((type_T **)stack->ga_data)[stack->ga_len] =
8273 (semicolon && idx == 0) ? vartype : item_type;
8274 ++stack->ga_len;
8275 }
8276 }
8277
Bram Moolenaar792f7862020-11-23 08:31:18 +01008278 for (idx = 0; idx < var_count; ++idx)
8279 {
rbtnnbebf0692021-08-21 17:26:50 +02008280 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008281 int opt_flags = 0;
8282 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008283 type_T *type = &t_any;
8284 type_T *lhs_type = &t_any;
8285 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008286
rbtnnbebf0692021-08-21 17:26:50 +02008287 p = skip_var_one(arg, FALSE);
8288 varlen = p - arg;
8289 name = vim_strnsave(arg, varlen);
8290 if (name == NULL)
8291 goto failed;
8292 if (*p == ':')
8293 {
8294 p = skipwhite(p + 1);
8295 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8296 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008297
Bram Moolenaarfad27422021-11-30 21:58:19 +00008298 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008299 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008300 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008301 goto failed;
8302 if (dest != dest_local)
8303 {
8304 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008305 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008306 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008307 }
rbtnnbebf0692021-08-21 17:26:50 +02008308 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008309 {
rbtnnbebf0692021-08-21 17:26:50 +02008310 // Assigning to "_": drop the value.
8311 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8312 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008313 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008314 else
rbtnnbebf0692021-08-21 17:26:50 +02008315 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008316 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008317 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008318 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8319 {
8320 semsg(_(e_variable_already_declared), arg);
8321 goto failed;
8322 }
8323
8324 if (STRNCMP(name, "s:", 2) == 0)
8325 {
8326 semsg(_(e_cannot_declare_script_variable_in_function), name);
8327 goto failed;
8328 }
8329
8330 // Reserve a variable to store "var".
8331 where.wt_index = var_list ? idx + 1 : 0;
8332 where.wt_variable = TRUE;
8333 if (lhs_type == &t_any)
8334 lhs_type = item_type;
8335 else if (item_type != &t_unknown
8336 && (item_type == &t_any
8337 ? need_type(item_type, lhs_type,
8338 -1, 0, cctx, FALSE, FALSE)
8339 : check_type(lhs_type, item_type, TRUE, where))
8340 == FAIL)
8341 goto failed;
8342 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8343 if (var_lvar == NULL)
8344 // out of memory or used as an argument
8345 goto failed;
8346
8347 if (semicolon && idx == var_count - 1)
8348 var_lvar->lv_type = vartype;
8349 else
8350 var_lvar->lv_type = item_type;
8351 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8352 }
8353
8354 if (*p == ',' || *p == ';')
8355 ++p;
8356 arg = skipwhite(p);
8357 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008358 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008359
rbtnnbebf0692021-08-21 17:26:50 +02008360 if (cctx->ctx_compile_type == CT_DEBUG)
8361 {
8362 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008363
rbtnnbebf0692021-08-21 17:26:50 +02008364 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8365 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8366 cctx->ctx_prev_lnum = prev_lnum;
8367 generate_instr_debug(cctx);
8368 cctx->ctx_prev_lnum = save_prev_lnum;
8369 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008370 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008371
Bram Moolenaar792f7862020-11-23 08:31:18 +01008372 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008373
8374failed:
8375 vim_free(name);
8376 drop_scope(cctx);
8377 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008378}
8379
8380/*
8381 * compile "endfor"
8382 */
8383 static char_u *
8384compile_endfor(char_u *arg, cctx_T *cctx)
8385{
8386 garray_T *instr = &cctx->ctx_instr;
8387 scope_T *scope = cctx->ctx_scope;
8388 forscope_T *forscope;
8389 isn_T *isn;
8390
Bram Moolenaarfa984412021-03-25 22:15:28 +01008391 if (misplaced_cmdmod(cctx))
8392 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 if (scope == NULL || scope->se_type != FOR_SCOPE)
8395 {
8396 emsg(_(e_for));
8397 return NULL;
8398 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008399 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008401 if (cctx->ctx_skip != SKIP_YES)
8402 {
8403 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008404
rbtnnbebf0692021-08-21 17:26:50 +02008405 // At end of ":for" scope jump back to the FOR instruction.
8406 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407
rbtnnbebf0692021-08-21 17:26:50 +02008408 // Fill in the "end" label in the FOR statement so it can jump here.
8409 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8410 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008411
rbtnnbebf0692021-08-21 17:26:50 +02008412 // Fill in the "end" label any BREAK statements
8413 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008414
rbtnnbebf0692021-08-21 17:26:50 +02008415 // Below the ":for" scope drop the "expr" list from the stack.
8416 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8417 return NULL;
8418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419
8420 vim_free(scope);
8421
8422 return arg;
8423}
8424
8425/*
8426 * compile "while expr"
8427 *
8428 * Produces instructions:
8429 * top: EVAL expr Push result of "expr"
8430 * JUMP_IF_FALSE end jump if false
8431 * ... body ...
8432 * JUMP top Jump back to repeat
8433 * end:
8434 *
8435 */
8436 static char_u *
8437compile_while(char_u *arg, cctx_T *cctx)
8438{
8439 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 scope_T *scope;
8441
8442 scope = new_scope(cctx, WHILE_SCOPE);
8443 if (scope == NULL)
8444 return NULL;
8445
Bram Moolenaara91a7132021-03-25 21:12:15 +01008446 // "endwhile" jumps back here, one before when profiling or using cmdmods
8447 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448
8449 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008450 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008451 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008452
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008453 if (!ends_excmd2(arg, skipwhite(p)))
8454 {
8455 semsg(_(e_trailing_arg), p);
8456 return NULL;
8457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008458
rbtnnd895b1d2021-08-20 20:54:25 +02008459 if (cctx->ctx_skip != SKIP_YES)
8460 {
8461 if (bool_on_stack(cctx) == FAIL)
8462 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008463
rbtnnd895b1d2021-08-20 20:54:25 +02008464 // CMDMOD_REV must come before the jump
8465 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008466
rbtnnd895b1d2021-08-20 20:54:25 +02008467 // "while_end" is set when ":endwhile" is found
8468 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008469 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008470 return FAIL;
8471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472
8473 return p;
8474}
8475
8476/*
8477 * compile "endwhile"
8478 */
8479 static char_u *
8480compile_endwhile(char_u *arg, cctx_T *cctx)
8481{
8482 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008483 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008484
Bram Moolenaarfa984412021-03-25 22:15:28 +01008485 if (misplaced_cmdmod(cctx))
8486 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8488 {
8489 emsg(_(e_while));
8490 return NULL;
8491 }
8492 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008493 if (cctx->ctx_skip != SKIP_YES)
8494 {
8495 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008496
Bram Moolenaarf002a412021-01-24 13:34:18 +01008497#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008498 // count the endwhile before jumping
8499 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008500#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008501
rbtnnd895b1d2021-08-20 20:54:25 +02008502 // At end of ":for" scope jump back to the FOR instruction.
8503 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504
rbtnnd895b1d2021-08-20 20:54:25 +02008505 // Fill in the "end" label in the WHILE statement so it can jump here.
8506 // And in any jumps for ":break"
8507 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008508 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008510
8511 vim_free(scope);
8512
8513 return arg;
8514}
8515
8516/*
8517 * compile "continue"
8518 */
8519 static char_u *
8520compile_continue(char_u *arg, cctx_T *cctx)
8521{
8522 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008523 int try_scopes = 0;
8524 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525
8526 for (;;)
8527 {
8528 if (scope == NULL)
8529 {
8530 emsg(_(e_continue));
8531 return NULL;
8532 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008533 if (scope->se_type == FOR_SCOPE)
8534 {
8535 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008537 }
8538 if (scope->se_type == WHILE_SCOPE)
8539 {
8540 loop_label = scope->se_u.se_while.ws_top_label;
8541 break;
8542 }
8543 if (scope->se_type == TRY_SCOPE)
8544 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008545 scope = scope->se_outer;
8546 }
8547
Bram Moolenaarc150c092021-02-13 15:02:46 +01008548 if (try_scopes > 0)
8549 // Inside one or more try/catch blocks we first need to jump to the
8550 // "finally" or "endtry" to cleanup.
8551 generate_TRYCONT(cctx, try_scopes, loop_label);
8552 else
8553 // Jump back to the FOR or WHILE instruction.
8554 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556 return arg;
8557}
8558
8559/*
8560 * compile "break"
8561 */
8562 static char_u *
8563compile_break(char_u *arg, cctx_T *cctx)
8564{
8565 scope_T *scope = cctx->ctx_scope;
8566 endlabel_T **el;
8567
8568 for (;;)
8569 {
8570 if (scope == NULL)
8571 {
8572 emsg(_(e_break));
8573 return NULL;
8574 }
8575 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8576 break;
8577 scope = scope->se_outer;
8578 }
8579
8580 // Jump to the end of the FOR or WHILE loop.
8581 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008582 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008584 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008585 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8586 return FAIL;
8587
8588 return arg;
8589}
8590
8591/*
8592 * compile "{" start of block
8593 */
8594 static char_u *
8595compile_block(char_u *arg, cctx_T *cctx)
8596{
8597 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8598 return NULL;
8599 return skipwhite(arg + 1);
8600}
8601
8602/*
8603 * compile end of block: drop one scope
8604 */
8605 static void
8606compile_endblock(cctx_T *cctx)
8607{
8608 scope_T *scope = cctx->ctx_scope;
8609
8610 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008611 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612 vim_free(scope);
8613}
8614
8615/*
8616 * compile "try"
8617 * Creates a new scope for the try-endtry, pointing to the first catch and
8618 * finally.
8619 * Creates another scope for the "try" block itself.
8620 * TRY instruction sets up exception handling at runtime.
8621 *
8622 * "try"
8623 * TRY -> catch1, -> finally push trystack entry
8624 * ... try block
8625 * "throw {exception}"
8626 * EVAL {exception}
8627 * THROW create exception
8628 * ... try block
8629 * " catch {expr}"
8630 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008631 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008632 * EVAL {expr}
8633 * MATCH
8634 * JUMP nomatch -> catch2
8635 * CATCH remove exception
8636 * ... catch block
8637 * " catch"
8638 * JUMP -> finally
8639 * catch2: CATCH remove exception
8640 * ... catch block
8641 * " finally"
8642 * finally:
8643 * ... finally block
8644 * " endtry"
8645 * ENDTRY pop trystack entry, may rethrow
8646 */
8647 static char_u *
8648compile_try(char_u *arg, cctx_T *cctx)
8649{
8650 garray_T *instr = &cctx->ctx_instr;
8651 scope_T *try_scope;
8652 scope_T *scope;
8653
Bram Moolenaarfa984412021-03-25 22:15:28 +01008654 if (misplaced_cmdmod(cctx))
8655 return NULL;
8656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657 // scope that holds the jumps that go to catch/finally/endtry
8658 try_scope = new_scope(cctx, TRY_SCOPE);
8659 if (try_scope == NULL)
8660 return NULL;
8661
Bram Moolenaar69f70502021-01-01 16:10:46 +01008662 if (cctx->ctx_skip != SKIP_YES)
8663 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008664 isn_T *isn;
8665
8666 // "try_catch" is set when the first ":catch" is found or when no catch
8667 // is found and ":finally" is found.
8668 // "try_finally" is set when ":finally" is found
8669 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008670 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008671 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8672 return NULL;
8673 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8674 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008675 return NULL;
8676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008677
8678 // scope for the try block itself
8679 scope = new_scope(cctx, BLOCK_SCOPE);
8680 if (scope == NULL)
8681 return NULL;
8682
8683 return arg;
8684}
8685
8686/*
8687 * compile "catch {expr}"
8688 */
8689 static char_u *
8690compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8691{
8692 scope_T *scope = cctx->ctx_scope;
8693 garray_T *instr = &cctx->ctx_instr;
8694 char_u *p;
8695 isn_T *isn;
8696
Bram Moolenaarfa984412021-03-25 22:15:28 +01008697 if (misplaced_cmdmod(cctx))
8698 return NULL;
8699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008700 // end block scope from :try or :catch
8701 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8702 compile_endblock(cctx);
8703 scope = cctx->ctx_scope;
8704
8705 // Error if not in a :try scope
8706 if (scope == NULL || scope->se_type != TRY_SCOPE)
8707 {
8708 emsg(_(e_catch));
8709 return NULL;
8710 }
8711
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008712 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008713 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008714 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715 return NULL;
8716 }
8717
Bram Moolenaar69f70502021-01-01 16:10:46 +01008718 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008719 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008720#ifdef FEAT_PROFILE
8721 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008722 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008723 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008724 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008725 .isn_type == ISN_PROF_START)
8726 --instr->ga_len;
8727#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008728 // Jump from end of previous block to :finally or :endtry
8729 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8730 JUMP_ALWAYS, cctx) == FAIL)
8731 return NULL;
8732
8733 // End :try or :catch scope: set value in ISN_TRY instruction
8734 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008735 if (isn->isn_arg.try.try_ref->try_catch == 0)
8736 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008737 if (scope->se_u.se_try.ts_catch_label != 0)
8738 {
8739 // Previous catch without match jumps here
8740 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8741 isn->isn_arg.jump.jump_where = instr->ga_len;
8742 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008743#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008744 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008745 {
8746 // a "throw" that jumps here needs to be counted
8747 generate_instr(cctx, ISN_PROF_END);
8748 // the "catch" is also counted
8749 generate_instr(cctx, ISN_PROF_START);
8750 }
8751#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008752 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008753 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008754 }
8755
8756 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008757 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008758 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008759 scope->se_u.se_try.ts_caught_all = TRUE;
8760 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008761 }
8762 else
8763 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008764 char_u *end;
8765 char_u *pat;
8766 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008767 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008768 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008770 // Push v:exception, push {expr} and MATCH
8771 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8772
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008773 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008774 if (*end != *p)
8775 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008776 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008777 vim_free(tofree);
8778 return FAIL;
8779 }
8780 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008781 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008782 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008783 len = (int)(end - tofree);
8784 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008785 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008786 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008787 if (pat == NULL)
8788 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008789 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008790 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008792 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8793 return NULL;
8794
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008795 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008796 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8797 return NULL;
8798 }
8799
Bram Moolenaar69f70502021-01-01 16:10:46 +01008800 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008801 return NULL;
8802
8803 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8804 return NULL;
8805 return p;
8806}
8807
8808 static char_u *
8809compile_finally(char_u *arg, cctx_T *cctx)
8810{
8811 scope_T *scope = cctx->ctx_scope;
8812 garray_T *instr = &cctx->ctx_instr;
8813 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008814 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815
Bram Moolenaarfa984412021-03-25 22:15:28 +01008816 if (misplaced_cmdmod(cctx))
8817 return NULL;
8818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008819 // end block scope from :try or :catch
8820 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8821 compile_endblock(cctx);
8822 scope = cctx->ctx_scope;
8823
8824 // Error if not in a :try scope
8825 if (scope == NULL || scope->se_type != TRY_SCOPE)
8826 {
8827 emsg(_(e_finally));
8828 return NULL;
8829 }
8830
rbtnn84934992021-08-07 13:26:53 +02008831 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008832 {
rbtnn84934992021-08-07 13:26:53 +02008833 // End :catch or :finally scope: set value in ISN_TRY instruction
8834 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8835 if (isn->isn_arg.try.try_ref->try_finally != 0)
8836 {
8837 emsg(_(e_finally_dup));
8838 return NULL;
8839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008840
rbtnn84934992021-08-07 13:26:53 +02008841 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008842#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008843 if (cctx->ctx_compile_type == CT_PROFILE
8844 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008845 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008846 {
8847 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008848 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008849
8850 // jump to the profile end above it
8851 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008852 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008853 --this_instr;
8854 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008855#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008856
rbtnn84934992021-08-07 13:26:53 +02008857 // Fill in the "end" label in jumps at the end of the blocks.
8858 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008859 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008860
rbtnn84934992021-08-07 13:26:53 +02008861 // If there is no :catch then an exception jumps to :finally.
8862 if (isn->isn_arg.try.try_ref->try_catch == 0)
8863 isn->isn_arg.try.try_ref->try_catch = this_instr;
8864 isn->isn_arg.try.try_ref->try_finally = this_instr;
8865 if (scope->se_u.se_try.ts_catch_label != 0)
8866 {
8867 // Previous catch without match jumps here
8868 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8869 isn->isn_arg.jump.jump_where = this_instr;
8870 scope->se_u.se_try.ts_catch_label = 0;
8871 }
8872 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8873 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875
8876 return arg;
8877}
8878
8879 static char_u *
8880compile_endtry(char_u *arg, cctx_T *cctx)
8881{
8882 scope_T *scope = cctx->ctx_scope;
8883 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008884 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008885
Bram Moolenaarfa984412021-03-25 22:15:28 +01008886 if (misplaced_cmdmod(cctx))
8887 return NULL;
8888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008889 // end block scope from :catch or :finally
8890 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8891 compile_endblock(cctx);
8892 scope = cctx->ctx_scope;
8893
8894 // Error if not in a :try scope
8895 if (scope == NULL || scope->se_type != TRY_SCOPE)
8896 {
8897 if (scope == NULL)
8898 emsg(_(e_no_endtry));
8899 else if (scope->se_type == WHILE_SCOPE)
8900 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008901 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008902 emsg(_(e_endfor));
8903 else
8904 emsg(_(e_endif));
8905 return NULL;
8906 }
8907
Bram Moolenaarc150c092021-02-13 15:02:46 +01008908 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008909 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008910 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008911 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8912 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008913 {
8914 emsg(_(e_missing_catch_or_finally));
8915 return NULL;
8916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008917
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008918#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008919 if (cctx->ctx_compile_type == CT_PROFILE
8920 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008921 .isn_type == ISN_PROF_START)
8922 // move the profile start after "endtry" so that it's not counted when
8923 // the exception is rethrown.
8924 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008925#endif
8926
Bram Moolenaar69f70502021-01-01 16:10:46 +01008927 // Fill in the "end" label in jumps at the end of the blocks, if not
8928 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008929 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8930 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008931
Bram Moolenaar69f70502021-01-01 16:10:46 +01008932 if (scope->se_u.se_try.ts_catch_label != 0)
8933 {
8934 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008935 isn_T *isn = ((isn_T *)instr->ga_data)
8936 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008937 isn->isn_arg.jump.jump_where = instr->ga_len;
8938 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008939 }
8940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008941 compile_endblock(cctx);
8942
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008943 if (cctx->ctx_skip != SKIP_YES)
8944 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008945 // End :catch or :finally scope: set instruction index in ISN_TRY
8946 // instruction
8947 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008948 if (cctx->ctx_skip != SKIP_YES
8949 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8950 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008951#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008952 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008953 generate_instr(cctx, ISN_PROF_START);
8954#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956 return arg;
8957}
8958
8959/*
8960 * compile "throw {expr}"
8961 */
8962 static char_u *
8963compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8964{
8965 char_u *p = skipwhite(arg);
8966
Bram Moolenaara5565e42020-05-09 15:44:01 +02008967 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008968 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008969 if (cctx->ctx_skip == SKIP_YES)
8970 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008971 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972 return NULL;
8973 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8974 return NULL;
8975
8976 return p;
8977}
8978
Bram Moolenaarc3235272021-07-10 19:42:03 +02008979 static char_u *
8980compile_eval(char_u *arg, cctx_T *cctx)
8981{
8982 char_u *p = arg;
8983 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008984 long lnum = SOURCING_LNUM;
8985
8986 // find_ex_command() will consider a variable name an expression, assuming
8987 // that something follows on the next line. Check that something actually
8988 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008989 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008990
Bram Moolenaarc3235272021-07-10 19:42:03 +02008991 if (compile_expr0(&p, cctx) == FAIL)
8992 return NULL;
8993
8994 if (name_only && lnum == SOURCING_LNUM)
8995 {
8996 semsg(_(e_expression_without_effect_str), arg);
8997 return NULL;
8998 }
8999
9000 // drop the result
9001 generate_instr_drop(cctx, ISN_DROP, 1);
9002
9003 return skipwhite(p);
9004}
9005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009006/*
9007 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009008 * compile "echomsg expr"
9009 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02009010 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01009011 * compile "execute expr"
9012 */
9013 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009014compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009015{
9016 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01009017 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009018 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009019 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009020 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009021 garray_T *stack = &cctx->ctx_type_stack;
9022 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009023
9024 for (;;)
9025 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009026 if (ends_excmd2(prev, p))
9027 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009028 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009029 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009030 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009031
9032 if (cctx->ctx_skip != SKIP_YES)
9033 {
9034 // check for non-void type
9035 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9036 if (type->tt_type == VAR_VOID)
9037 {
9038 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9039 return NULL;
9040 }
9041 }
9042
Bram Moolenaarad39c092020-02-26 18:23:43 +01009043 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009044 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009045 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009046 }
9047
Bram Moolenaare4984292020-12-13 14:19:25 +01009048 if (count > 0)
9049 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009050 long save_lnum = cctx->ctx_lnum;
9051
9052 // Use the line number where the command started.
9053 cctx->ctx_lnum = start_ctx_lnum;
9054
Bram Moolenaare4984292020-12-13 14:19:25 +01009055 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9056 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9057 else if (cmdidx == CMD_execute)
9058 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9059 else if (cmdidx == CMD_echomsg)
9060 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009061 else if (cmdidx == CMD_echoconsole)
9062 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009063 else
9064 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009065
9066 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009068 return p;
9069}
9070
9071/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009072 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009073 * instruction to compute it and return OK.
9074 * Otherwise return FAIL, the caller must deal with any range.
9075 */
9076 static int
9077compile_variable_range(exarg_T *eap, cctx_T *cctx)
9078{
9079 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9080 char_u *p = skipdigits(eap->cmd);
9081
9082 if (p == range_end)
9083 return FAIL;
9084 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9085}
9086
9087/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009088 * :put r
9089 * :put ={expr}
9090 */
9091 static char_u *
9092compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9093{
9094 char_u *line = arg;
9095 linenr_T lnum;
9096 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009097 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009098
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009099 eap->regname = *line;
9100
9101 if (eap->regname == '=')
9102 {
9103 char_u *p = line + 1;
9104
9105 if (compile_expr0(&p, cctx) == FAIL)
9106 return NULL;
9107 line = p;
9108 }
9109 else if (eap->regname != NUL)
9110 ++line;
9111
Bram Moolenaar08597872020-12-10 19:43:40 +01009112 if (compile_variable_range(eap, cctx) == OK)
9113 {
9114 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9115 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009116 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009117 {
9118 // Either no range or a number.
9119 // "errormsg" will not be set because the range is ADDR_LINES.
9120 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009121 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009122 return NULL;
9123 if (eap->addr_count == 0)
9124 lnum = -1;
9125 else
9126 lnum = eap->line2;
9127 if (above)
9128 --lnum;
9129 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009130
9131 generate_PUT(cctx, eap->regname, lnum);
9132 return line;
9133}
9134
9135/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009136 * A command that is not compiled, execute with legacy code.
9137 */
9138 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009139compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009140{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009141 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009142 char_u *p;
9143 int has_expr = FALSE;
9144 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009145 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009146 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009147
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009148 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009149 goto theend;
9150
Bram Moolenaare729ce22021-06-06 21:38:09 +02009151 // If there was a prececing command modifier, drop it and include it in the
9152 // EXEC command.
9153 if (cctx->ctx_has_cmdmod)
9154 {
9155 garray_T *instr = &cctx->ctx_instr;
9156 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9157
9158 if (isn->isn_type == ISN_CMDMOD)
9159 {
9160 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9161 ->cmod_filter_regmatch.regprog);
9162 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9163 --instr->ga_len;
9164 cctx->ctx_has_cmdmod = FALSE;
9165 }
9166 }
9167
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009168 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009169 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009170 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009171 int usefilter = FALSE;
9172
9173 has_expr = argt & (EX_XFILE | EX_EXPAND);
9174
9175 // If the command can be followed by a bar, find the bar and truncate
9176 // it, so that the following command can be compiled.
9177 // The '|' is overwritten with a NUL, it is put back below.
9178 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9179 && *eap->arg == '!')
9180 // :w !filter or :r !filter or :r! filter
9181 usefilter = TRUE;
9182 if ((argt & EX_TRLBAR) && !usefilter)
9183 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009184 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009185 separate_nextcmd(eap);
9186 if (eap->nextcmd != NULL)
9187 nextcmd = eap->nextcmd;
9188 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009189 else if (eap->cmdidx == CMD_wincmd)
9190 {
9191 p = eap->arg;
9192 if (*p != NUL)
9193 ++p;
9194 if (*p == 'g' || *p == Ctrl_G)
9195 ++p;
9196 p = skipwhite(p);
9197 if (*p == '|')
9198 {
9199 *p = NUL;
9200 nextcmd = p + 1;
9201 }
9202 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009203 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9204 {
9205 // If there is a trailing '{' read lines until the '}'
9206 p = eap->arg + STRLEN(eap->arg) - 1;
9207 while (p > eap->arg && VIM_ISWHITE(*p))
9208 --p;
9209 if (*p == '{')
9210 {
9211 exarg_T ea;
9212 int flags; // unused
9213 int start_lnum = SOURCING_LNUM;
9214
9215 CLEAR_FIELD(ea);
9216 ea.arg = eap->arg;
9217 fill_exarg_from_cctx(&ea, cctx);
9218 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9219 if (tofree != NULL)
9220 {
9221 *p = NUL;
9222 line = concat_str(line, tofree);
9223 if (line == NULL)
9224 goto theend;
9225 vim_free(tofree);
9226 tofree = line;
9227 SOURCING_LNUM = start_lnum;
9228 }
9229 }
9230 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009231 }
9232
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009233 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9234 {
9235 // expand filename in "syntax include [@group] filename"
9236 has_expr = TRUE;
9237 eap->arg = skipwhite(eap->arg + 7);
9238 if (*eap->arg == '@')
9239 eap->arg = skiptowhite(eap->arg);
9240 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009241
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009242 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9243 && STRLEN(eap->arg) > 4)
9244 {
9245 int delim = *eap->arg;
9246
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009247 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009248 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009249 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009250 }
9251
Bram Moolenaarecac5912021-01-05 19:23:28 +01009252 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009253 cmd_arg = eap->arg;
9254
9255 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009256 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009257 exarg_T nea;
9258
9259 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009260 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009261 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009262 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009263 {
9264 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9265 if (has_expr)
9266 eap->arg = skiptowhite(eap->arg);
9267 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009268 }
9269
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009270 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009271 {
9272 int count = 0;
9273 char_u *start = skipwhite(line);
9274
9275 // :cmd xxx`=expr1`yyy`=expr2`zzz
9276 // PUSHS ":cmd xxx"
9277 // eval expr1
9278 // PUSHS "yyy"
9279 // eval expr2
9280 // PUSHS "zzz"
9281 // EXECCONCAT 5
9282 for (;;)
9283 {
9284 if (p > start)
9285 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009286 char_u *val = vim_strnsave(start, p - start);
9287
9288 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009289 ++count;
9290 }
9291 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009292 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009293 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009294 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009295 ++count;
9296 p = skipwhite(p);
9297 if (*p != '`')
9298 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009299 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009300 return NULL;
9301 }
9302 start = p + 1;
9303
9304 p = (char_u *)strstr((char *)start, "`=");
9305 if (p == NULL)
9306 {
9307 if (*skipwhite(start) != NUL)
9308 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009309 char_u *val = vim_strsave(start);
9310
9311 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009312 ++count;
9313 }
9314 break;
9315 }
9316 }
9317 generate_EXECCONCAT(cctx, count);
9318 }
9319 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009320 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009321
9322theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009323 if (*nextcmd != NUL)
9324 {
9325 // the parser expects a pointer to the bar, put it back
9326 --nextcmd;
9327 *nextcmd = '|';
9328 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009329 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009330
9331 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009332}
9333
Bram Moolenaar20677332021-06-06 17:02:53 +02009334/*
9335 * A script command with heredoc, e.g.
9336 * ruby << EOF
9337 * command
9338 * EOF
9339 * Has been turned into one long line with NL characters by
9340 * get_function_body():
9341 * ruby << EOF<NL> command<NL>EOF
9342 */
9343 static char_u *
9344compile_script(char_u *line, cctx_T *cctx)
9345{
9346 if (cctx->ctx_skip != SKIP_YES)
9347 {
9348 isn_T *isn;
9349
9350 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9351 return NULL;
9352 isn->isn_arg.string = vim_strsave(line);
9353 }
9354 return (char_u *)"";
9355}
9356
Bram Moolenaar8238f082021-04-20 21:10:48 +02009357
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009358/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009359 * :s/pat/repl/
9360 */
9361 static char_u *
9362compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9363{
9364 char_u *cmd = eap->arg;
9365 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9366
9367 if (expr != NULL)
9368 {
9369 int delimiter = *cmd++;
9370
9371 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009372 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009373 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9374 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009375 garray_T save_ga = cctx->ctx_instr;
9376 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009377 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009378 int trailing_error;
9379 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009380 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009381 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009382
9383 cmd += 3;
9384 end = skip_substitute(cmd, delimiter);
9385
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009386 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009387 // labels are correct.
9388 cctx->ctx_instr.ga_len = 0;
9389 cctx->ctx_instr.ga_maxlen = 0;
9390 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009391 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009392 if (end[-1] == NUL)
9393 end[-1] = delimiter;
9394 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009395 trailing_error = *cmd != delimiter && *cmd != NUL;
9396
Bram Moolenaar169502f2021-04-21 12:19:35 +02009397 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009398 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009399 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009400 if (trailing_error)
9401 semsg(_(e_trailing_arg), cmd);
9402 clear_instr_ga(&cctx->ctx_instr);
9403 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009404 return NULL;
9405 }
9406
Bram Moolenaar8238f082021-04-20 21:10:48 +02009407 // Move the generated instructions into the ISN_SUBSTITUTE
9408 // instructions, then restore the list of instructions before
9409 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009410 instr_count = cctx->ctx_instr.ga_len;
9411 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009412 instr[instr_count].isn_type = ISN_FINISH;
9413
9414 cctx->ctx_instr = save_ga;
9415 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9416 {
9417 int idx;
9418
9419 for (idx = 0; idx < instr_count; ++idx)
9420 delete_instr(instr + idx);
9421 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009422 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009423 }
9424 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9425 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009426
9427 // skip over flags
9428 if (*end == '&')
9429 ++end;
9430 while (ASCII_ISALPHA(*end) || *end == '#')
9431 ++end;
9432 return end;
9433 }
9434 }
9435
9436 return compile_exec(arg, eap, cctx);
9437}
9438
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009439 static char_u *
9440compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9441{
9442 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009443 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009444
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009445 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009446 {
9447 if (STRNCMP(arg, "END", 3) == 0)
9448 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009449 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009450 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009451 // First load the current variable value.
9452 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9453 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009454 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009455 }
9456
9457 // Gets the redirected text and put it on the stack, then store it
9458 // in the variable.
9459 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9460
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009461 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009462 generate_instr_drop(cctx, ISN_CONCAT, 1);
9463
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009464 if (lhs->lhs_has_index)
9465 {
9466 // Use the info in "lhs" to store the value at the index in the
9467 // list or dict.
9468 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9469 &t_string, cctx) == FAIL)
9470 return NULL;
9471 }
9472 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009473 return NULL;
9474
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009475 VIM_CLEAR(lhs->lhs_name);
9476 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009477 return arg + 3;
9478 }
9479 emsg(_(e_cannot_nest_redir));
9480 return NULL;
9481 }
9482
9483 if (arg[0] == '=' && arg[1] == '>')
9484 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009485 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009486
9487 // redirect to a variable is compiled
9488 arg += 2;
9489 if (*arg == '>')
9490 {
9491 ++arg;
9492 append = TRUE;
9493 }
9494 arg = skipwhite(arg);
9495
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009496 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009497 FALSE, FALSE, 1, cctx) == FAIL)
9498 return NULL;
9499 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009500 lhs->lhs_append = append;
9501 if (lhs->lhs_has_index)
9502 {
9503 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9504 if (lhs->lhs_whole == NULL)
9505 return NULL;
9506 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009507
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009508 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009509 }
9510
9511 // other redirects are handled like at script level
9512 return compile_exec(line, eap, cctx);
9513}
9514
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009515#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009516 static char_u *
9517compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9518{
9519 isn_T *isn;
9520 char_u *p;
9521
9522 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9523 if (isn == NULL)
9524 return NULL;
9525 isn->isn_arg.number = eap->cmdidx;
9526
9527 p = eap->arg;
9528 if (compile_expr0(&p, cctx) == FAIL)
9529 return NULL;
9530
9531 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9532 if (isn == NULL)
9533 return NULL;
9534 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9535 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9536 return NULL;
9537 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9538 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9539 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9540
9541 return p;
9542}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009543#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009544
Bram Moolenaar4c137212021-04-19 16:48:48 +02009545/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009546 * Check if the separator for a :global or :substitute command is OK.
9547 */
9548 int
9549check_global_and_subst(char_u *cmd, char_u *arg)
9550{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009551 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009552 {
9553 semsg(_(e_separator_not_supported_str), arg);
9554 return FAIL;
9555 }
9556 if (VIM_ISWHITE(cmd[1]))
9557 {
9558 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9559 return FAIL;
9560 }
9561 return OK;
9562}
9563
9564
9565/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009566 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009567 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009568 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009569 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009570add_def_function(ufunc_T *ufunc)
9571{
9572 dfunc_T *dfunc;
9573
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009574 if (def_functions.ga_len == 0)
9575 {
9576 // The first position is not used, so that a zero uf_dfunc_idx means it
9577 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009578 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009579 return FAIL;
9580 ++def_functions.ga_len;
9581 }
9582
Bram Moolenaar09689a02020-05-09 22:50:08 +02009583 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009584 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009585 return FAIL;
9586 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9587 CLEAR_POINTER(dfunc);
9588 dfunc->df_idx = def_functions.ga_len;
9589 ufunc->uf_dfunc_idx = dfunc->df_idx;
9590 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009591 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009592 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009593 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009594 ++def_functions.ga_len;
9595 return OK;
9596}
9597
9598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009599 * After ex_function() has collected all the function lines: parse and compile
9600 * the lines into instructions.
9601 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009602 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9603 * the return statement (used for lambda). When uf_ret_type is already set
9604 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009605 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009606 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009607 * This can be used recursively through compile_lambda(), which may reallocate
9608 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009609 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009610 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009611 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009612compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009613 ufunc_T *ufunc,
9614 int check_return_type,
9615 compiletype_T compile_type,
9616 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009617{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009618 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009619 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009620 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009621 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009622 cctx_T cctx;
9623 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009624 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009625 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009626 int ret = FAIL;
9627 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009628 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009629 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009630 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009631 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009632#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009633 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009634#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009635 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009636
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009637 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009638 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009639 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009640 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009641 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9642 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009643 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009644
9645 switch (compile_type)
9646 {
9647 case CT_PROFILE:
9648#ifdef FEAT_PROFILE
9649 instr_dest = dfunc->df_instr_prof; break;
9650#endif
9651 case CT_NONE: instr_dest = dfunc->df_instr; break;
9652 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9653 }
9654 if (instr_dest != NULL)
9655 // Was compiled in this mode before: Free old instructions.
9656 delete_def_function_contents(dfunc, FALSE);
9657 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009658 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009659 else
9660 {
9661 if (add_def_function(ufunc) == FAIL)
9662 return FAIL;
9663 new_def_function = TRUE;
9664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009665
Bram Moolenaar985116a2020-07-12 17:31:09 +02009666 ufunc->uf_def_status = UF_COMPILING;
9667
Bram Moolenaara80faa82020-04-12 19:37:17 +02009668 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009669
Bram Moolenaare99d4222021-06-13 14:01:26 +02009670 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009671 cctx.ctx_ufunc = ufunc;
9672 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009673 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009674 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9675 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9676 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9677 cctx.ctx_type_list = &ufunc->uf_type_list;
9678 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9679 instr = &cctx.ctx_instr;
9680
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009681 // Set the context to the function, it may be compiled when called from
9682 // another script. Set the script version to the most modern one.
9683 // The line number will be set in next_line_from_context().
9684 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009685 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9686
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009687 // Don't use the flag from ":legacy" here.
9688 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9689
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009690 // Make sure error messages are OK.
9691 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9692 if (do_estack_push)
9693 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009694 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009695
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009696 if (ufunc->uf_def_args.ga_len > 0)
9697 {
9698 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009699 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009700 int i;
9701 char_u *arg;
9702 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009703 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009704
9705 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009706 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009707 for (i = 0; i < count; ++i)
9708 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009709 garray_T *stack = &cctx.ctx_type_stack;
9710 type_T *val_type;
9711 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009712 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009713 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009714 int jump_instr_idx = instr->ga_len;
9715 isn_T *isn;
9716
9717 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9718 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9719 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009720
9721 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009722 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009723
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009724 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009725 r = compile_expr0(&arg, &cctx);
9726
Bram Moolenaar12bce952021-03-11 20:04:04 +01009727 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009728 goto erret;
9729
9730 // If no type specified use the type of the default value.
9731 // Otherwise check that the default value type matches the
9732 // specified type.
9733 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009734 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009735 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009736 {
9737 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009738 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009739 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009740 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009741 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009742 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009743
9744 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009745 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009746
9747 // set instruction index in JUMP_IF_ARG_SET to here
9748 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9749 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009750 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009751
9752 if (did_set_arg_type)
9753 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009754 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009755 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009756
9757 /*
9758 * Loop over all the lines of the function and generate instructions.
9759 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009760 for (;;)
9761 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009762 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009763 int starts_with_colon = FALSE;
9764 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009765 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009766
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009767 // Bail out on the first error to avoid a flood of errors and report
9768 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009769 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009770 goto erret;
9771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009772 if (line != NULL && *line == '|')
9773 // the line continues after a '|'
9774 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009775 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009776 && !(*line == '#' && (line == cctx.ctx_line_start
9777 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009778 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009779 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009780 goto erret;
9781 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009782 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9783 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009784 else
9785 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009786 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009787 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009788 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009789 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009790#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009791 if (cctx.ctx_skip != SKIP_YES)
9792 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009793#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009794 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009795 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009796 // Make a copy, splitting off nextcmd and removing trailing spaces
9797 // may change it.
9798 if (line != NULL)
9799 {
9800 line = vim_strsave(line);
9801 vim_free(line_to_free);
9802 line_to_free = line;
9803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009804 }
9805
Bram Moolenaara80faa82020-04-12 19:37:17 +02009806 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009807 ea.cmdlinep = &line;
9808 ea.cmd = skipwhite(line);
9809
Bram Moolenaarb2049902021-01-24 12:53:53 +01009810 if (*ea.cmd == '#')
9811 {
9812 // "#" starts a comment
9813 line = (char_u *)"";
9814 continue;
9815 }
9816
Bram Moolenaarf002a412021-01-24 13:34:18 +01009817#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009818 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9819 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009820 {
9821 may_generate_prof_end(&cctx, prof_lnum);
9822
9823 prof_lnum = cctx.ctx_lnum;
9824 generate_instr(&cctx, ISN_PROF_START);
9825 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009826#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009827 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9828 && cctx.ctx_skip != SKIP_YES)
9829 {
9830 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009831 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009832 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009833 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009834
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009835 // Some things can be recognized by the first character.
9836 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009837 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009838 case '}':
9839 {
9840 // "}" ends a block scope
9841 scopetype_T stype = cctx.ctx_scope == NULL
9842 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009843
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009844 if (stype == BLOCK_SCOPE)
9845 {
9846 compile_endblock(&cctx);
9847 line = ea.cmd;
9848 }
9849 else
9850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009851 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009852 goto erret;
9853 }
9854 if (line != NULL)
9855 line = skipwhite(ea.cmd + 1);
9856 continue;
9857 }
9858
9859 case '{':
9860 // "{" starts a block scope
9861 // "{'a': 1}->func() is something else
9862 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9863 {
9864 line = compile_block(ea.cmd, &cctx);
9865 continue;
9866 }
9867 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009868 }
9869
9870 /*
9871 * COMMAND MODIFIERS
9872 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009873 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009874 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9875 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009876 {
9877 if (errormsg != NULL)
9878 goto erret;
9879 // empty line or comment
9880 line = (char_u *)"";
9881 continue;
9882 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009883 generate_cmdmods(&cctx, &local_cmdmod);
9884 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009885
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009886 // Check if there was a colon after the last command modifier or before
9887 // the current position.
9888 for (p = ea.cmd; p >= line; --p)
9889 {
9890 if (*p == ':')
9891 starts_with_colon = TRUE;
9892 if (p < ea.cmd && !VIM_ISWHITE(*p))
9893 break;
9894 }
9895
Bram Moolenaarce024c32021-06-26 13:00:49 +02009896 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009897 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009898 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009899 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009900 if (checkforcmd(&ea.cmd, "call", 3))
9901 {
9902 if (*ea.cmd == '(')
9903 // not for "call()"
9904 ea.cmd = p;
9905 else
9906 ea.cmd = skipwhite(ea.cmd);
9907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009908
Bram Moolenaarce024c32021-06-26 13:00:49 +02009909 if (!starts_with_colon)
9910 {
9911 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009912
Bram Moolenaarce024c32021-06-26 13:00:49 +02009913 // Check for assignment after command modifiers.
9914 assign = may_compile_assignment(&ea, &line, &cctx);
9915 if (assign == OK)
9916 goto nextline;
9917 if (assign == FAIL)
9918 goto erret;
9919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009920 }
9921
9922 /*
9923 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009924 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009925 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009926 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009927 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009928 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009929 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009930 && (starts_with_colon || !(*cmd == '\''
9931 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009932 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009933 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009934 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009935 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009936 if (!starts_with_colon
9937 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009938 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009939 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009940 goto erret;
9941 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009942 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009943 if (ends_excmd2(line, ea.cmd))
9944 {
9945 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009946 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009947 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009948 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009949 goto nextline;
9950 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009951 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009952 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009953 p = find_ex_command(&ea, NULL,
9954 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009955 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009956
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009957 if (p == NULL)
9958 {
9959 if (cctx.ctx_skip != SKIP_YES)
9960 emsg(_(e_ambiguous_use_of_user_defined_command));
9961 goto erret;
9962 }
9963
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009964 // When using ":legacy cmd" always use compile_exec().
9965 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009966 {
9967 char_u *start = ea.cmd;
9968
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009969 switch (ea.cmdidx)
9970 {
9971 case CMD_if:
9972 case CMD_elseif:
9973 case CMD_else:
9974 case CMD_endif:
9975 case CMD_for:
9976 case CMD_endfor:
9977 case CMD_continue:
9978 case CMD_break:
9979 case CMD_while:
9980 case CMD_endwhile:
9981 case CMD_try:
9982 case CMD_catch:
9983 case CMD_finally:
9984 case CMD_endtry:
9985 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9986 goto erret;
9987 default: break;
9988 }
9989
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009990 // ":legacy return expr" needs to be handled differently.
9991 if (checkforcmd(&start, "return", 4))
9992 ea.cmdidx = CMD_return;
9993 else
9994 ea.cmdidx = CMD_legacy;
9995 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009997 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9998 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009999 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010000 {
10001 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +010010002 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010003 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +020010004 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010005 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +010010006 // CMD_var cannot happen, compile_assignment() above would be
10007 // used. Most likely an assignment to a non-existing variable.
10008 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010009 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010011 }
10012
Bram Moolenaar3988f642020-08-27 22:43:03 +020010013 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +010010014 && ea.cmdidx != CMD_elseif
10015 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +020010016 && ea.cmdidx != CMD_endif
10017 && ea.cmdidx != CMD_endfor
10018 && ea.cmdidx != CMD_endwhile
10019 && ea.cmdidx != CMD_catch
10020 && ea.cmdidx != CMD_finally
10021 && ea.cmdidx != CMD_endtry)
10022 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010023 emsg(_(e_unreachable_code_after_return));
10024 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010025 }
10026
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010027 p = skipwhite(p);
10028 if (ea.cmdidx != CMD_SIZE
10029 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10030 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010031 if (ea.cmdidx >= 0)
10032 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010033 if ((ea.argt & EX_BANG) && *p == '!')
10034 {
10035 ea.forceit = TRUE;
10036 p = skipwhite(p + 1);
10037 }
10038 }
10039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010040 switch (ea.cmdidx)
10041 {
10042 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010043 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010044 ea.arg = p;
10045 line = compile_nested_function(&ea, &cctx);
10046 break;
10047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010048 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010049 line = compile_return(p, check_return_type,
10050 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010051 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010052 break;
10053
10054 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010055 emsg(_(e_cannot_use_let_in_vim9_script));
10056 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010057 case CMD_var:
10058 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010059 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010060 case CMD_increment:
10061 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010062 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010063 if (line == p)
10064 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010065 break;
10066
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010067 case CMD_unlet:
10068 case CMD_unlockvar:
10069 case CMD_lockvar:
10070 line = compile_unletlock(p, &ea, &cctx);
10071 break;
10072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010073 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010074 emsg(_(e_import_can_only_be_used_in_script));
10075 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076 break;
10077
10078 case CMD_if:
10079 line = compile_if(p, &cctx);
10080 break;
10081 case CMD_elseif:
10082 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010083 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010084 break;
10085 case CMD_else:
10086 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010087 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010088 break;
10089 case CMD_endif:
10090 line = compile_endif(p, &cctx);
10091 break;
10092
10093 case CMD_while:
10094 line = compile_while(p, &cctx);
10095 break;
10096 case CMD_endwhile:
10097 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010098 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010099 break;
10100
10101 case CMD_for:
10102 line = compile_for(p, &cctx);
10103 break;
10104 case CMD_endfor:
10105 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010106 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010107 break;
10108 case CMD_continue:
10109 line = compile_continue(p, &cctx);
10110 break;
10111 case CMD_break:
10112 line = compile_break(p, &cctx);
10113 break;
10114
10115 case CMD_try:
10116 line = compile_try(p, &cctx);
10117 break;
10118 case CMD_catch:
10119 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010120 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010121 break;
10122 case CMD_finally:
10123 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010124 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010125 break;
10126 case CMD_endtry:
10127 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010128 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010129 break;
10130 case CMD_throw:
10131 line = compile_throw(p, &cctx);
10132 break;
10133
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010134 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010135 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010136 break;
10137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010138 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010139 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010140 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010141 case CMD_echomsg:
10142 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010143 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010144 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010145 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010146
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010147 case CMD_put:
10148 ea.cmd = cmd;
10149 line = compile_put(p, &ea, &cctx);
10150 break;
10151
Bram Moolenaar4c137212021-04-19 16:48:48 +020010152 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010153 if (check_global_and_subst(ea.cmd, p) == FAIL)
10154 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010155 if (cctx.ctx_skip == SKIP_YES)
10156 line = (char_u *)"";
10157 else
10158 {
10159 ea.arg = p;
10160 line = compile_substitute(line, &ea, &cctx);
10161 }
10162 break;
10163
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010164 case CMD_redir:
10165 ea.arg = p;
10166 line = compile_redir(line, &ea, &cctx);
10167 break;
10168
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010169 case CMD_cexpr:
10170 case CMD_lexpr:
10171 case CMD_caddexpr:
10172 case CMD_laddexpr:
10173 case CMD_cgetexpr:
10174 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010175#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010176 ea.arg = p;
10177 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010178#else
10179 ex_ni(&ea);
10180 line = NULL;
10181#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010182 break;
10183
Bram Moolenaarae616492020-07-28 20:07:27 +020010184 case CMD_append:
10185 case CMD_change:
10186 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010187 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010188 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010189 case CMD_xit:
10190 not_in_vim9(&ea);
10191 goto erret;
10192
Bram Moolenaar002262f2020-07-08 17:47:57 +020010193 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010194 if (cctx.ctx_skip != SKIP_YES)
10195 {
10196 semsg(_(e_invalid_command_str), ea.cmd);
10197 goto erret;
10198 }
10199 // We don't check for a next command here.
10200 line = (char_u *)"";
10201 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010202
Bram Moolenaar20677332021-06-06 17:02:53 +020010203 case CMD_lua:
10204 case CMD_mzscheme:
10205 case CMD_perl:
10206 case CMD_py3:
10207 case CMD_python3:
10208 case CMD_python:
10209 case CMD_pythonx:
10210 case CMD_ruby:
10211 case CMD_tcl:
10212 ea.arg = p;
10213 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010214 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010215 else
10216 // heredoc lines have been concatenated with NL
10217 // characters in get_function_body()
10218 line = compile_script(line, &cctx);
10219 break;
10220
Bram Moolenaar7b829262021-10-13 15:04:34 +010010221 case CMD_global:
10222 if (check_global_and_subst(ea.cmd, p) == FAIL)
10223 goto erret;
10224 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010225 default:
10226 // Not recognized, execute with do_cmdline_cmd().
10227 ea.arg = p;
10228 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010229 break;
10230 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010231nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010232 if (line == NULL)
10233 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010234 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010235
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010236 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010237 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010239 if (cctx.ctx_type_stack.ga_len < 0)
10240 {
10241 iemsg("Type stack underflow");
10242 goto erret;
10243 }
10244 }
10245
10246 if (cctx.ctx_scope != NULL)
10247 {
10248 if (cctx.ctx_scope->se_type == IF_SCOPE)
10249 emsg(_(e_endif));
10250 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10251 emsg(_(e_endwhile));
10252 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10253 emsg(_(e_endfor));
10254 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010255 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010256 goto erret;
10257 }
10258
Bram Moolenaarefd88552020-06-18 20:50:10 +020010259 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010260 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010261 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10262 ufunc->uf_ret_type = &t_void;
10263 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010264 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010265 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010266 goto erret;
10267 }
10268
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010269 // Return void if there is no return at the end.
10270 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010271 }
10272
Bram Moolenaar599410c2021-04-10 14:03:43 +020010273 // When compiled with ":silent!" and there was an error don't consider the
10274 // function compiled.
10275 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010276 {
10277 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10278 + ufunc->uf_dfunc_idx;
10279 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010280 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010281#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010282 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010283 {
10284 dfunc->df_instr_prof = instr->ga_data;
10285 dfunc->df_instr_prof_count = instr->ga_len;
10286 }
10287 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010288#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010289 if (cctx.ctx_compile_type == CT_DEBUG)
10290 {
10291 dfunc->df_instr_debug = instr->ga_data;
10292 dfunc->df_instr_debug_count = instr->ga_len;
10293 }
10294 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010295 {
10296 dfunc->df_instr = instr->ga_data;
10297 dfunc->df_instr_count = instr->ga_len;
10298 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010299 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010300 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010301 if (cctx.ctx_outer_used)
10302 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010303 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010305
10306 ret = OK;
10307
10308erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010309 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010310 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010311 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10312 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010313
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010314 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010315 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010316 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010317 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010318
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010319 // If using the last entry in the table and it was added above, we
10320 // might as well remove it.
10321 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010322 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010323 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010324 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010325 ufunc->uf_dfunc_idx = 0;
10326 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010327 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010328
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010329 while (cctx.ctx_scope != NULL)
10330 drop_scope(&cctx);
10331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010332 if (errormsg != NULL)
10333 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010334 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010335 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010336 }
10337
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010338 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10339 {
10340 if (ret == OK)
10341 {
10342 emsg(_(e_missing_redir_end));
10343 ret = FAIL;
10344 }
10345 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010346 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010347 }
10348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010349 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010350 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010351 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010352 if (do_estack_push)
10353 estack_pop();
10354
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010355 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010356 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010357 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010358 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010359 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010360}
10361
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010362 void
10363set_function_type(ufunc_T *ufunc)
10364{
10365 int varargs = ufunc->uf_va_name != NULL;
10366 int argcount = ufunc->uf_args.ga_len;
10367
10368 // Create a type for the function, with the return type and any
10369 // argument types.
10370 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10371 // The type is included in "tt_args".
10372 if (argcount > 0 || varargs)
10373 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010374 if (ufunc->uf_type_list.ga_itemsize == 0)
10375 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010376 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10377 argcount, &ufunc->uf_type_list);
10378 // Add argument types to the function type.
10379 if (func_type_add_arg_types(ufunc->uf_func_type,
10380 argcount + varargs,
10381 &ufunc->uf_type_list) == FAIL)
10382 return;
10383 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10384 ufunc->uf_func_type->tt_min_argcount =
10385 argcount - ufunc->uf_def_args.ga_len;
10386 if (ufunc->uf_arg_types == NULL)
10387 {
10388 int i;
10389
10390 // lambda does not have argument types.
10391 for (i = 0; i < argcount; ++i)
10392 ufunc->uf_func_type->tt_args[i] = &t_any;
10393 }
10394 else
10395 mch_memmove(ufunc->uf_func_type->tt_args,
10396 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10397 if (varargs)
10398 {
10399 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010400 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010401 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10402 }
10403 }
10404 else
10405 // No arguments, can use a predefined type.
10406 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10407 argcount, &ufunc->uf_type_list);
10408}
10409
10410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010411/*
10412 * Delete an instruction, free what it contains.
10413 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010414 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010415delete_instr(isn_T *isn)
10416{
10417 switch (isn->isn_type)
10418 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010419 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010420 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010421 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010422 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010423 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010424 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010425 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010426 case ISN_LOADENV:
10427 case ISN_LOADG:
10428 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010429 case ISN_LOADT:
10430 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010431 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010432 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010433 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010434 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010435 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010436 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010437 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010438 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010439 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010440 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010441 case ISN_STOREW:
10442 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010443 vim_free(isn->isn_arg.string);
10444 break;
10445
Bram Moolenaar4c137212021-04-19 16:48:48 +020010446 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010447 {
10448 int idx;
10449 isn_T *list = isn->isn_arg.subs.subs_instr;
10450
10451 vim_free(isn->isn_arg.subs.subs_cmd);
10452 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10453 delete_instr(list + idx);
10454 vim_free(list);
10455 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010456 break;
10457
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010458 case ISN_INSTR:
10459 {
10460 int idx;
10461 isn_T *list = isn->isn_arg.instr;
10462
10463 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10464 delete_instr(list + idx);
10465 vim_free(list);
10466 }
10467 break;
10468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010469 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010470 case ISN_STORES:
10471 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010472 break;
10473
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010474 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010475 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010476 vim_free(isn->isn_arg.unlet.ul_name);
10477 break;
10478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010479 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +000010480 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010481 vim_free(isn->isn_arg.storeopt.so_name);
10482 break;
10483
10484 case ISN_PUSHBLOB: // push blob isn_arg.blob
10485 blob_unref(isn->isn_arg.blob);
10486 break;
10487
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010488 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010489#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010490 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010491#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010492 break;
10493
10494 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010496 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010497#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010498 break;
10499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010500 case ISN_UCALL:
10501 vim_free(isn->isn_arg.ufunc.cuf_name);
10502 break;
10503
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010504 case ISN_FUNCREF:
10505 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010506 if (isn->isn_arg.funcref.fr_func_name == NULL)
10507 {
10508 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10509 + isn->isn_arg.funcref.fr_dfunc_idx;
10510 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010511
Bram Moolenaar38453522021-11-28 22:00:12 +000010512 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10513 func_ptr_unref(ufunc);
10514 }
10515 else
10516 {
10517 char_u *name = isn->isn_arg.funcref.fr_func_name;
10518
10519 if (name != NULL)
10520 func_unref(name);
10521 vim_free(isn->isn_arg.funcref.fr_func_name);
10522 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010523 }
10524 break;
10525
10526 case ISN_DCALL:
10527 {
10528 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10529 + isn->isn_arg.dfunc.cdf_idx;
10530
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010531 if (dfunc->df_ufunc != NULL
10532 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010533 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010534 }
10535 break;
10536
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010537 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010538 {
10539 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10540 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10541
10542 if (ufunc != NULL)
10543 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010544 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010545 func_ptr_unref(ufunc);
10546 }
10547
10548 vim_free(lambda);
10549 vim_free(isn->isn_arg.newfunc.nf_global);
10550 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010551 break;
10552
Bram Moolenaar5e654232020-09-16 15:22:00 +020010553 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010554 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010555 free_type(isn->isn_arg.type.ct_type);
10556 break;
10557
Bram Moolenaar02194d22020-10-24 23:08:38 +020010558 case ISN_CMDMOD:
10559 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10560 ->cmod_filter_regmatch.regprog);
10561 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10562 break;
10563
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010564 case ISN_LOADSCRIPT:
10565 case ISN_STORESCRIPT:
10566 vim_free(isn->isn_arg.script.scriptref);
10567 break;
10568
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010569 case ISN_TRY:
10570 vim_free(isn->isn_arg.try.try_ref);
10571 break;
10572
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010573 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010574 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010575 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10576 break;
10577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010578 case ISN_2BOOL:
10579 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010580 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010581 case ISN_ADDBLOB:
10582 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010583 case ISN_ANYINDEX:
10584 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010585 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010586 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010587 case ISN_BLOBINDEX:
10588 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010589 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010590 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010591 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010592 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010593 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010594 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010595 case ISN_COMPAREANY:
10596 case ISN_COMPAREBLOB:
10597 case ISN_COMPAREBOOL:
10598 case ISN_COMPAREDICT:
10599 case ISN_COMPAREFLOAT:
10600 case ISN_COMPAREFUNC:
10601 case ISN_COMPARELIST:
10602 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010603 case ISN_COMPARESPECIAL:
10604 case ISN_COMPARESTRING:
10605 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010606 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010607 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010608 case ISN_DROP:
10609 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010610 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010611 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010612 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010613 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010614 case ISN_EXECCONCAT:
10615 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010616 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010617 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010618 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010619 case ISN_GETITEM:
10620 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010621 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010622 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010623 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010624 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010625 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010626 case ISN_LOADBDICT:
10627 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010628 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010629 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010630 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010631 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010632 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010633 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010634 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010635 case ISN_NEGATENR:
10636 case ISN_NEWDICT:
10637 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010638 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010639 case ISN_OPFLOAT:
10640 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010641 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010642 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010643 case ISN_PROF_END:
10644 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010645 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010646 case ISN_PUSHF:
10647 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010648 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010649 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010650 case ISN_REDIREND:
10651 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010652 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010653 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010654 case ISN_SHUFFLE:
10655 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010656 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010657 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010658 case ISN_STORENR:
10659 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010660 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010661 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010662 case ISN_STOREV:
10663 case ISN_STRINDEX:
10664 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010665 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010666 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010667 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010668 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010669 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010670 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010671 // nothing allocated
10672 break;
10673 }
10674}
10675
10676/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010677 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010678 */
10679 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010680delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010681{
10682 int idx;
10683
10684 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010685 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010686
10687 if (dfunc->df_instr != NULL)
10688 {
10689 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10690 delete_instr(dfunc->df_instr + idx);
10691 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010692 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010693 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010694 if (dfunc->df_instr_debug != NULL)
10695 {
10696 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10697 delete_instr(dfunc->df_instr_debug + idx);
10698 VIM_CLEAR(dfunc->df_instr_debug);
10699 dfunc->df_instr_debug = NULL;
10700 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010701#ifdef FEAT_PROFILE
10702 if (dfunc->df_instr_prof != NULL)
10703 {
10704 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10705 delete_instr(dfunc->df_instr_prof + idx);
10706 VIM_CLEAR(dfunc->df_instr_prof);
10707 dfunc->df_instr_prof = NULL;
10708 }
10709#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010710
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010711 if (mark_deleted)
10712 dfunc->df_deleted = TRUE;
10713 if (dfunc->df_ufunc != NULL)
10714 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010715}
10716
10717/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010718 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010719 * function, unless another user function still uses it.
10720 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010721 */
10722 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010723unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010724{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010725 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010726 {
10727 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10728 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010729
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010730 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010731 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010732 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010733 ufunc->uf_dfunc_idx = 0;
10734 if (dfunc->df_ufunc == ufunc)
10735 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010736 }
10737}
10738
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010739/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010740 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010741 */
10742 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010743link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010744{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010745 if (ufunc->uf_dfunc_idx > 0)
10746 {
10747 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10748 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010749
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010750 ++dfunc->df_refcount;
10751 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010752}
10753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010754#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010755/*
10756 * Free all functions defined with ":def".
10757 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010758 void
10759free_def_functions(void)
10760{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010761 int idx;
10762
10763 for (idx = 0; idx < def_functions.ga_len; ++idx)
10764 {
10765 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10766
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010767 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010768 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010769 }
10770
10771 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010772}
10773#endif
10774
10775
10776#endif // FEAT_EVAL