blob: 5b47746e358850bd154da528c12401614244ed8c [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,
121 dest_env,
122 dest_global,
123 dest_buffer,
124 dest_window,
125 dest_tab,
126 dest_vimvar,
127 dest_script,
128 dest_reg,
129 dest_expr,
130} assign_dest_T;
131
132// Used by compile_lhs() to store information about the LHS of an assignment
133// and one argument of ":unlet" with an index.
134typedef struct {
135 assign_dest_T lhs_dest; // type of destination
136
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200137 char_u *lhs_name; // allocated name excluding the last
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200138 // "[expr]" or ".name".
139 size_t lhs_varlen; // length of the variable without
140 // "[expr]" or ".name"
Bram Moolenaar753bcf82021-04-21 14:24:24 +0200141 char_u *lhs_whole; // allocated name including the last
142 // "[expr]" or ".name" for :redir
143 size_t lhs_varlen_total; // length of the variable including
144 // any "[expr]" or ".name"
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200145 char_u *lhs_dest_end; // end of the destination, including
146 // "[expr]" or ".name".
147
148 int lhs_has_index; // has "[expr]" or ".name"
149
150 int lhs_new_local; // create new local variable
151 int lhs_opt_flags; // for when destination is an option
152 int lhs_vimvaridx; // for when destination is a v:var
153
154 lvar_T lhs_local_lvar; // used for existing local destination
155 lvar_T lhs_arg_lvar; // used for argument destination
156 lvar_T *lhs_lvar; // points to destination lvar
157 int lhs_scriptvar_sid;
158 int lhs_scriptvar_idx;
159
160 int lhs_has_type; // type was specified
161 type_T *lhs_type;
162 type_T *lhs_member_type;
163
164 int lhs_append; // used by ISN_REDIREND
165} lhs_T;
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167/*
168 * Context for compiling lines of Vim script.
169 * Stores info about the local variables and condition stack.
170 */
171struct cctx_S {
172 ufunc_T *ctx_ufunc; // current function
173 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200174 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 garray_T ctx_instr; // generated instructions
176
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200177 int ctx_prev_lnum; // line number below previous command, for
178 // debugging
179
Bram Moolenaare99d4222021-06-13 14:01:26 +0200180 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200184 int ctx_has_closure; // set to one if a closures was created in
185 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 garray_T ctx_imports; // imported items
188
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200189 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200191 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 cctx_T *ctx_outer; // outer scope for lambda or nested
194 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200198 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200199
Bram Moolenaar02194d22020-10-24 23:08:38 +0200200 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200201
202 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
203 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204};
205
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100206static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207
208/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100209 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100210 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100211 * If "lvar" is NULL only check if the variable can be found.
212 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100214 static int
215lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100218 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100221 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200222
223 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100226 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 if (STRNCMP(name, lvp->lv_name, len) == 0
228 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200229 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100230 if (lvar != NULL)
231 {
232 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100233 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100234 }
235 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
239 // Find local in outer function scope.
240 if (cctx->ctx_outer != NULL)
241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100242 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100244 if (lvar != NULL)
245 {
246 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100247 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100248 }
249 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200250 }
251 }
252
Bram Moolenaar709664c2020-12-12 14:33:41 +0100253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254}
255
256/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 * Lookup an argument in the current function and an enclosing function.
258 * Returns the argument index in "idxp"
259 * Returns the argument type in "type"
260 * Sets "gen_load_outer" to TRUE if found in outer scope.
261 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 */
263 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 char_u *name,
266 size_t len,
267 int *idxp,
268 type_T **type,
269 int *gen_load_outer,
270 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271{
272 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200273 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100275 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200276 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200277 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
279 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200281 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 {
283 if (idxp != NULL)
284 {
285 // Arguments are located above the frame pointer. One further
286 // if there is a vararg argument
287 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 + STACK_FRAME_SIZE)
289 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290
291 if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 else
294 *type = &t_any;
295 }
296 return OK;
297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200300 va_name = cctx->ctx_ufunc->uf_va_name;
301 if (va_name != NULL
302 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303 {
304 if (idxp != NULL)
305 {
306 // varargs is always the last argument
307 *idxp = -STACK_FRAME_SIZE - 1;
308 *type = cctx->ctx_ufunc->uf_va_type;
309 }
310 return OK;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200313 if (cctx->ctx_outer != NULL)
314 {
315 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200316 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200317 == OK)
318 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100319 if (gen_load_outer != NULL)
320 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200321 return OK;
322 }
323 }
324
325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326}
327
328/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 * Lookup a script-local variable in the current script, possibly defined in a
330 * block that contains the function "cctx->ctx_ufunc".
331 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100332 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Return NULL when not found.
334 */
335 static sallvar_T *
336find_script_var(char_u *name, size_t len, cctx_T *cctx)
337{
338 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
339 hashitem_T *hi;
340 int cc;
341 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200342 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 ufunc_T *ufunc;
344
345 // Find the list of all script variables with the right name.
346 if (len > 0)
347 {
348 cc = name[len];
349 name[len] = NUL;
350 }
351 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
352 if (len > 0)
353 name[len] = cc;
354 if (HASHITEM_EMPTY(hi))
355 return NULL;
356
357 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200358 if (sav->sav_block_id == 0)
359 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200360 return sav;
361
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200362 if (cctx == NULL)
363 {
364 // Not in a function scope, find variable with block id equal to or
365 // smaller than the current block id.
366 while (sav != NULL)
367 {
368 if (sav->sav_block_id <= si->sn_current_block_id)
369 break;
370 sav = sav->sav_next;
371 }
372 return sav;
373 }
374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200375 // Go over the variables with this name and find one that was visible
376 // from the function.
377 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200378 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 while (sav != NULL)
380 {
381 int idx;
382
383 // Go over the blocks that this function was defined in. If the
384 // variable block ID matches it was visible to the function.
385 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
386 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
387 return sav;
388 sav = sav->sav_next;
389 }
390
Bram Moolenaar88421d62021-07-24 14:14:52 +0200391 // Not found, assume variable at script level was visible.
392 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200393}
394
395/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100396 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200397 */
398 static int
399script_is_vim9()
400{
401 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
402}
403
404/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200405 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200406 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 * Returns OK or FAIL.
408 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200409 static int
410script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200412 if (current_sctx.sc_sid <= 0)
413 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200414 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200415 {
416 // Check script variables that were visible where the function was
417 // defined.
418 if (find_script_var(name, len, cctx) != NULL)
419 return OK;
420 }
421 else
422 {
423 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
424 dictitem_T *di;
425 int cc;
426
427 // Check script variables that are currently visible
428 cc = name[len];
429 name[len] = NUL;
430 di = find_var_in_ht(ht, 0, name, TRUE);
431 name[len] = cc;
432 if (di != NULL)
433 return OK;
434 }
435
436 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437}
438
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100439/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100440 * Return TRUE if "name" is a local variable, argument, script variable or
441 * imported.
442 */
443 static int
444variable_exists(char_u *name, size_t len, cctx_T *cctx)
445{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100446 return (cctx != NULL
447 && (lookup_local(name, len, NULL, cctx) == OK
448 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200449 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100450 || find_imported(name, len, cctx) != NULL;
451}
452
453/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100454 * Return TRUE if "name" is a local variable, argument, script variable,
455 * imported or function.
456 */
457 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100458item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100459{
460 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100461 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100462
463 if (variable_exists(name, len, cctx))
464 return TRUE;
465
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100466 // This is similar to what is in lookup_scriptitem():
467 // Find a function, so that a following "->" works.
468 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
469 // a function call.
470 p = skipwhite(name + len);
471
472 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
473 {
474 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200475 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100476 // Skip "g:" before a function name.
477 is_global = (name[0] == 'g' && name[1] == ':');
478 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
479 }
480 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100481}
482
483/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100484 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200485 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200486 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100487 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100488 * Return FAIL and give an error if it defined.
489 */
490 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100491check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100492{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200493 int c = p[len];
494 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200495
Bram Moolenaarda479c72021-04-10 21:01:38 +0200496 // underscore argument is OK
497 if (len == 1 && *p == '_')
498 return OK;
499
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200500 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100501 {
502 if (is_arg)
503 semsg(_(e_argument_already_declared_in_script_str), p);
504 else
505 semsg(_(e_variable_already_declared_in_script_str), p);
506 return FAIL;
507 }
508
Bram Moolenaarad486a02020-08-01 23:22:18 +0200509 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100510 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100511 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200512 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200513 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200514 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100515 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200516 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200517 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
518 && (!func_is_global(ufunc)
519 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200520 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100521 if (is_arg)
522 semsg(_(e_argument_name_shadows_existing_variable_str), p);
523 else
524 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200525 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200526 return FAIL;
527 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100528 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200529 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100530 return OK;
531}
532
Bram Moolenaar65b95452020-07-19 14:03:09 +0200533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534/////////////////////////////////////////////////////////////////////
535// Following generate_ functions expect the caller to call ga_grow().
536
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200537#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
538#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540/*
541 * Generate an instruction without arguments.
542 * Returns a pointer to the new instruction, NULL if failed.
543 */
544 static isn_T *
545generate_instr(cctx_T *cctx, isntype_T isn_type)
546{
547 garray_T *instr = &cctx->ctx_instr;
548 isn_T *isn;
549
Bram Moolenaar080457c2020-03-03 21:53:32 +0100550 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200551 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 return NULL;
553 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
554 isn->isn_type = isn_type;
555 isn->isn_lnum = cctx->ctx_lnum + 1;
556 ++instr->ga_len;
557
558 return isn;
559}
560
561/*
562 * Generate an instruction without arguments.
563 * "drop" will be removed from the stack.
564 * Returns a pointer to the new instruction, NULL if failed.
565 */
566 static isn_T *
567generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
568{
569 garray_T *stack = &cctx->ctx_type_stack;
570
Bram Moolenaar080457c2020-03-03 21:53:32 +0100571 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 stack->ga_len -= drop;
573 return generate_instr(cctx, isn_type);
574}
575
576/*
577 * Generate instruction "isn_type" and put "type" on the type stack.
578 */
579 static isn_T *
580generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
585 if ((isn = generate_instr(cctx, isn_type)) == NULL)
586 return NULL;
587
Bram Moolenaar35578162021-08-02 19:10:38 +0200588 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200590 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 ++stack->ga_len;
592
593 return isn;
594}
595
596/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200597 * Generate an ISN_DEBUG instruction.
598 */
599 static isn_T *
600generate_instr_debug(cctx_T *cctx)
601{
602 isn_T *isn;
603 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
604 + cctx->ctx_ufunc->uf_dfunc_idx;
605
606 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
607 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200608 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
609 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200610 return isn;
611}
612
613/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200615 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200616 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 */
618 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200619may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620{
621 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200622 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100624 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100626 RETURN_OK_IF_SKIP(cctx);
627 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200628 switch ((*type)->tt_type)
629 {
630 // nothing to be done
631 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200633 // conversion possible
634 case VAR_SPECIAL:
635 case VAR_BOOL:
636 case VAR_NUMBER:
637 case VAR_FLOAT:
638 break;
639
640 // conversion possible (with runtime check)
641 case VAR_ANY:
642 case VAR_UNKNOWN:
643 isntype = ISN_2STRING_ANY;
644 break;
645
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200646 // conversion possible when tolerant
647 case VAR_LIST:
648 if (tolerant)
649 {
650 isntype = ISN_2STRING_ANY;
651 break;
652 }
653 // FALLTHROUGH
654
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200655 // conversion not possible
656 case VAR_VOID:
657 case VAR_BLOB:
658 case VAR_FUNC:
659 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200660 case VAR_DICT:
661 case VAR_JOB:
662 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200663 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200664 to_string_error((*type)->tt_type);
665 return FAIL;
666 }
667
668 *type = &t_string;
669 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200671 isn->isn_arg.tostring.offset = offset;
672 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 return OK;
675}
676
677 static int
678check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
679{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 {
684 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200685 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200687 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 return FAIL;
689 }
690 return OK;
691}
692
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200693 static int
694generate_add_instr(
695 cctx_T *cctx,
696 vartype_T vartype,
697 type_T *type1,
698 type_T *type2)
699{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100700 garray_T *stack = &cctx->ctx_type_stack;
701 isn_T *isn = generate_instr_drop(cctx,
702 vartype == VAR_NUMBER ? ISN_OPNR
703 : vartype == VAR_LIST ? ISN_ADDLIST
704 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200705#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100706 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200707#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100708 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200709
710 if (vartype != VAR_LIST && vartype != VAR_BLOB
711 && type1->tt_type != VAR_ANY
712 && type2->tt_type != VAR_ANY
713 && check_number_or_float(
714 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
715 return FAIL;
716
717 if (isn != NULL)
718 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100719
720 // When concatenating two lists with different member types the member type
721 // becomes "any".
722 if (vartype == VAR_LIST
723 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
724 && type1->tt_member != type2->tt_member)
725 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
726
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200727 return isn == NULL ? FAIL : OK;
728}
729
730/*
731 * Get the type to use for an instruction for an operation on "type1" and
732 * "type2". If they are matching use a type-specific instruction. Otherwise
733 * fall back to runtime type checking.
734 */
735 static vartype_T
736operator_type(type_T *type1, type_T *type2)
737{
738 if (type1->tt_type == type2->tt_type
739 && (type1->tt_type == VAR_NUMBER
740 || type1->tt_type == VAR_LIST
741#ifdef FEAT_FLOAT
742 || type1->tt_type == VAR_FLOAT
743#endif
744 || type1->tt_type == VAR_BLOB))
745 return type1->tt_type;
746 return VAR_ANY;
747}
748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749/*
750 * Generate an instruction with two arguments. The instruction depends on the
751 * type of the arguments.
752 */
753 static int
754generate_two_op(cctx_T *cctx, char_u *op)
755{
756 garray_T *stack = &cctx->ctx_type_stack;
757 type_T *type1;
758 type_T *type2;
759 vartype_T vartype;
760 isn_T *isn;
761
Bram Moolenaar080457c2020-03-03 21:53:32 +0100762 RETURN_OK_IF_SKIP(cctx);
763
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200764 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
766 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200767 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768
769 switch (*op)
770 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200771 case '+':
772 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 break;
775
776 case '-':
777 case '*':
778 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
779 op) == FAIL)
780 return FAIL;
781 if (vartype == VAR_NUMBER)
782 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
783#ifdef FEAT_FLOAT
784 else if (vartype == VAR_FLOAT)
785 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
786#endif
787 else
788 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
789 if (isn != NULL)
790 isn->isn_arg.op.op_type = *op == '*'
791 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
792 break;
793
Bram Moolenaar4c683752020-04-05 21:38:23 +0200794 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200796 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 && type2->tt_type != VAR_NUMBER))
798 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200799 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 return FAIL;
801 }
802 isn = generate_instr_drop(cctx,
803 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
804 if (isn != NULL)
805 isn->isn_arg.op.op_type = EXPR_REM;
806 break;
807 }
808
809 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200810 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 {
812 type_T *type = &t_any;
813
814#ifdef FEAT_FLOAT
815 // float+number and number+float results in float
816 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
817 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
818 type = &t_float;
819#endif
820 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
821 }
822
823 return OK;
824}
825
826/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200827 * Get the instruction to use for comparing "type1" with "type2"
828 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200830 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100831get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832{
833 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar4c683752020-04-05 21:38:23 +0200835 if (type1 == VAR_UNKNOWN)
836 type1 = VAR_ANY;
837 if (type2 == VAR_UNKNOWN)
838 type2 = VAR_ANY;
839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 if (type1 == type2)
841 {
842 switch (type1)
843 {
844 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
845 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
846 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
847 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
848 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
849 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
850 case VAR_LIST: isntype = ISN_COMPARELIST; break;
851 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
852 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 default: isntype = ISN_COMPAREANY; break;
854 }
855 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200856 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100858 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 isntype = ISN_COMPAREANY;
860
Bram Moolenaar657137c2021-01-09 15:45:23 +0100861 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 && (isntype == ISN_COMPAREBOOL
863 || isntype == ISN_COMPARESPECIAL
864 || isntype == ISN_COMPARENR
865 || isntype == ISN_COMPAREFLOAT))
866 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200867 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100868 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 }
871 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
874 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100875 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
876 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 && (type1 == VAR_BLOB || type2 == VAR_BLOB
878 || type1 == VAR_LIST || type2 == VAR_LIST))))
879 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200880 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200882 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200884 return isntype;
885}
886
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200887 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100888check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200889{
890 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
891 return FAIL;
892 return OK;
893}
894
Bram Moolenaara5565e42020-05-09 15:44:01 +0200895/*
896 * Generate an ISN_COMPARE* instruction with a boolean result.
897 */
898 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100899generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200900{
901 isntype_T isntype;
902 isn_T *isn;
903 garray_T *stack = &cctx->ctx_type_stack;
904 vartype_T type1;
905 vartype_T type2;
906
907 RETURN_OK_IF_SKIP(cctx);
908
909 // Get the known type of the two items on the stack. If they are matching
910 // use a type-specific instruction. Otherwise fall back to runtime type
911 // checking.
912 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
913 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100914 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200915 if (isntype == ISN_DROP)
916 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917
918 if ((isn = generate_instr(cctx, isntype)) == NULL)
919 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100920 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 isn->isn_arg.op.op_ic = ic;
922
923 // takes two arguments, puts one bool back
924 if (stack->ga_len >= 2)
925 {
926 --stack->ga_len;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
928 }
929
930 return OK;
931}
932
933/*
934 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200935 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 */
937 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200938generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939{
940 isn_T *isn;
941 garray_T *stack = &cctx->ctx_type_stack;
942
Bram Moolenaar080457c2020-03-03 21:53:32 +0100943 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
945 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200946 isn->isn_arg.tobool.invert = invert;
947 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948
949 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200950 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951
952 return OK;
953}
954
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200955/*
956 * Generate an ISN_COND2BOOL instruction.
957 */
958 static int
959generate_COND2BOOL(cctx_T *cctx)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
966 return FAIL;
967
968 // type becomes bool
969 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
970
971 return OK;
972}
973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200975generate_TYPECHECK(
976 cctx_T *cctx,
977 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100978 int offset,
979 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980{
981 isn_T *isn;
982 garray_T *stack = &cctx->ctx_type_stack;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
986 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200987 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100988 isn->isn_arg.type.ct_off = (int8_T)offset;
989 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990
Bram Moolenaar5e654232020-09-16 15:22:00 +0200991 // type becomes expected
992 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
994 return OK;
995}
996
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100997 static int
998generate_SETTYPE(
999 cctx_T *cctx,
1000 type_T *expected)
1001{
1002 isn_T *isn;
1003
1004 RETURN_OK_IF_SKIP(cctx);
1005 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.type.ct_type = alloc_type(expected);
1008 return OK;
1009}
1010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001012 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1013 * used. Return FALSE if the types will never match.
1014 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001015 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001016use_typecheck(type_T *actual, type_T *expected)
1017{
1018 if (actual->tt_type == VAR_ANY
1019 || actual->tt_type == VAR_UNKNOWN
1020 || (actual->tt_type == VAR_FUNC
1021 && (expected->tt_type == VAR_FUNC
1022 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001023 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1024 && ((actual->tt_member == &t_void)
1025 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001026 return TRUE;
1027 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1028 && actual->tt_type == expected->tt_type)
1029 // This takes care of a nested list or dict.
1030 return use_typecheck(actual->tt_member, expected->tt_member);
1031 return FALSE;
1032}
1033
1034/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001035 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001036 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001037 * - "actual" is a type that can be "expected" type: add a runtime check; or
1038 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001039 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1040 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001041 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001042 static int
1043need_type_where(
1044 type_T *actual,
1045 type_T *expected,
1046 int offset,
1047 where_T where,
1048 cctx_T *cctx,
1049 int silent,
1050 int actual_is_const)
1051{
1052 if (expected == &t_bool && actual != &t_bool
1053 && (actual->tt_flags & TTFLAG_BOOL_OK))
1054 {
1055 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1056 // boolean is OK but requires a conversion.
1057 generate_2BOOL(cctx, FALSE, offset);
1058 return OK;
1059 }
1060
1061 if (check_type(expected, actual, FALSE, where) == OK)
1062 return OK;
1063
1064 // If the actual type can be the expected type add a runtime check.
1065 // If it's a constant a runtime check makes no sense.
1066 if ((!actual_is_const || actual == &t_any)
1067 && use_typecheck(actual, expected))
1068 {
1069 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1070 return OK;
1071 }
1072
1073 if (!silent)
1074 type_mismatch_where(expected, actual, where);
1075 return FAIL;
1076}
1077
Bram Moolenaar351ead02021-01-16 16:07:01 +01001078 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001079need_type(
1080 type_T *actual,
1081 type_T *expected,
1082 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001083 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001084 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001085 int silent,
1086 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001087{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001088 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001089
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001090 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001091 return need_type_where(actual, expected, offset, where,
1092 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001093}
1094
1095/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001096 * Check that the top of the type stack has a type that can be used as a
1097 * condition. Give an error and return FAIL if not.
1098 */
1099 static int
1100bool_on_stack(cctx_T *cctx)
1101{
1102 garray_T *stack = &cctx->ctx_type_stack;
1103 type_T *type;
1104
1105 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1106 if (type == &t_bool)
1107 return OK;
1108
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001109 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001110 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1111 // This requires a runtime type check.
1112 return generate_COND2BOOL(cctx);
1113
Bram Moolenaar351ead02021-01-16 16:07:01 +01001114 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001115}
1116
1117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 * Generate an ISN_PUSHNR instruction.
1119 */
1120 static int
1121generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1122{
1123 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001124 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125
Bram Moolenaar080457c2020-03-03 21:53:32 +01001126 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1128 return FAIL;
1129 isn->isn_arg.number = number;
1130
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001131 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001132 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001133 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 return OK;
1135}
1136
1137/*
1138 * Generate an ISN_PUSHBOOL instruction.
1139 */
1140 static int
1141generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1142{
1143 isn_T *isn;
1144
Bram Moolenaar080457c2020-03-03 21:53:32 +01001145 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1147 return FAIL;
1148 isn->isn_arg.number = number;
1149
1150 return OK;
1151}
1152
1153/*
1154 * Generate an ISN_PUSHSPEC instruction.
1155 */
1156 static int
1157generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1163 return FAIL;
1164 isn->isn_arg.number = number;
1165
1166 return OK;
1167}
1168
1169#ifdef FEAT_FLOAT
1170/*
1171 * Generate an ISN_PUSHF instruction.
1172 */
1173 static int
1174generate_PUSHF(cctx_T *cctx, float_T fnumber)
1175{
1176 isn_T *isn;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1180 return FAIL;
1181 isn->isn_arg.fnumber = fnumber;
1182
1183 return OK;
1184}
1185#endif
1186
1187/*
1188 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001189 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 */
1191 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001192generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193{
1194 isn_T *isn;
1195
Bram Moolenaar508b5612021-01-02 18:17:26 +01001196 if (cctx->ctx_skip == SKIP_YES)
1197 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001198 if (str != NULL)
1199 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001200 return OK;
1201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001203 {
1204 if (str != NULL)
1205 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001207 }
1208 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209
1210 return OK;
1211}
1212
1213/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001214 * Generate an ISN_PUSHCHANNEL instruction.
1215 * Consumes "channel".
1216 */
1217 static int
1218generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1219{
1220 isn_T *isn;
1221
Bram Moolenaar080457c2020-03-03 21:53:32 +01001222 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001223 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1224 return FAIL;
1225 isn->isn_arg.channel = channel;
1226
1227 return OK;
1228}
1229
1230/*
1231 * Generate an ISN_PUSHJOB instruction.
1232 * Consumes "job".
1233 */
1234 static int
1235generate_PUSHJOB(cctx_T *cctx, job_T *job)
1236{
1237 isn_T *isn;
1238
Bram Moolenaar080457c2020-03-03 21:53:32 +01001239 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001240 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001241 return FAIL;
1242 isn->isn_arg.job = job;
1243
1244 return OK;
1245}
1246
1247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 * Generate an ISN_PUSHBLOB instruction.
1249 * Consumes "blob".
1250 */
1251 static int
1252generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1253{
1254 isn_T *isn;
1255
Bram Moolenaar080457c2020-03-03 21:53:32 +01001256 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1258 return FAIL;
1259 isn->isn_arg.blob = blob;
1260
1261 return OK;
1262}
1263
1264/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001265 * Generate an ISN_PUSHFUNC instruction with name "name".
1266 * Consumes "name".
1267 */
1268 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001269generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001270{
1271 isn_T *isn;
1272
Bram Moolenaar080457c2020-03-03 21:53:32 +01001273 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001274 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001275 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001276 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001277
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001282 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001283 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1284 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001285 */
1286 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001287generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001288{
1289 isn_T *isn;
1290 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001291 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1292 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001293 type_T *item_type = &t_any;
1294
1295 RETURN_OK_IF_SKIP(cctx);
1296
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001297 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001298 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001299 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001300 emsg(_(e_listreq));
1301 return FAIL;
1302 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001303 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001304 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1305 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001306 isn->isn_arg.getitem.gi_index = index;
1307 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001308
1309 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001310 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001311 return FAIL;
1312 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1313 ++stack->ga_len;
1314 return OK;
1315}
1316
1317/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001318 * Generate an ISN_SLICE instruction with "count".
1319 */
1320 static int
1321generate_SLICE(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324
1325 RETURN_OK_IF_SKIP(cctx);
1326 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1327 return FAIL;
1328 isn->isn_arg.number = count;
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_CHECKLEN instruction with "min_len".
1334 */
1335 static int
1336generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1337{
1338 isn_T *isn;
1339
1340 RETURN_OK_IF_SKIP(cctx);
1341
1342 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.checklen.cl_min_len = min_len;
1345 isn->isn_arg.checklen.cl_more_OK = more_OK;
1346
1347 return OK;
1348}
1349
1350/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 * Generate an ISN_STORE instruction.
1352 */
1353 static int
1354generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1355{
1356 isn_T *isn;
1357
Bram Moolenaar080457c2020-03-03 21:53:32 +01001358 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1360 return FAIL;
1361 if (name != NULL)
1362 isn->isn_arg.string = vim_strsave(name);
1363 else
1364 isn->isn_arg.number = idx;
1365
1366 return OK;
1367}
1368
1369/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001370 * Generate an ISN_STOREOUTER instruction.
1371 */
1372 static int
1373generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1374{
1375 isn_T *isn;
1376
1377 RETURN_OK_IF_SKIP(cctx);
1378 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1379 return FAIL;
1380 isn->isn_arg.outer.outer_idx = idx;
1381 isn->isn_arg.outer.outer_depth = level;
1382
1383 return OK;
1384}
1385
1386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1388 */
1389 static int
1390generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1391{
1392 isn_T *isn;
1393
Bram Moolenaar080457c2020-03-03 21:53:32 +01001394 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1396 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001397 isn->isn_arg.storenr.stnr_idx = idx;
1398 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399
1400 return OK;
1401}
1402
1403/*
1404 * Generate an ISN_STOREOPT instruction
1405 */
1406 static int
1407generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1408{
1409 isn_T *isn;
1410
Bram Moolenaar080457c2020-03-03 21:53:32 +01001411 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001412 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return FAIL;
1414 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1415 isn->isn_arg.storeopt.so_flags = opt_flags;
1416
1417 return OK;
1418}
1419
1420/*
1421 * Generate an ISN_LOAD or similar instruction.
1422 */
1423 static int
1424generate_LOAD(
1425 cctx_T *cctx,
1426 isntype_T isn_type,
1427 int idx,
1428 char_u *name,
1429 type_T *type)
1430{
1431 isn_T *isn;
1432
Bram Moolenaar080457c2020-03-03 21:53:32 +01001433 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1435 return FAIL;
1436 if (name != NULL)
1437 isn->isn_arg.string = vim_strsave(name);
1438 else
1439 isn->isn_arg.number = idx;
1440
1441 return OK;
1442}
1443
1444/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001445 * Generate an ISN_LOADOUTER instruction
1446 */
1447 static int
1448generate_LOADOUTER(
1449 cctx_T *cctx,
1450 int idx,
1451 int nesting,
1452 type_T *type)
1453{
1454 isn_T *isn;
1455
1456 RETURN_OK_IF_SKIP(cctx);
1457 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1458 return FAIL;
1459 isn->isn_arg.outer.outer_idx = idx;
1460 isn->isn_arg.outer.outer_depth = nesting;
1461
1462 return OK;
1463}
1464
1465/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001466 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001467 */
1468 static int
1469generate_LOADV(
1470 cctx_T *cctx,
1471 char_u *name,
1472 int error)
1473{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001474 int di_flags;
1475 int vidx = find_vim_var(name, &di_flags);
1476 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001477
Bram Moolenaar080457c2020-03-03 21:53:32 +01001478 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001479 if (vidx < 0)
1480 {
1481 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001482 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001483 return FAIL;
1484 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001485 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001486
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001487 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001488}
1489
1490/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001491 * Generate an ISN_UNLET instruction.
1492 */
1493 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001494generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001495{
1496 isn_T *isn;
1497
1498 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001499 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001500 return FAIL;
1501 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1502 isn->isn_arg.unlet.ul_forceit = forceit;
1503
1504 return OK;
1505}
1506
1507/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001508 * Generate an ISN_LOCKCONST instruction.
1509 */
1510 static int
1511generate_LOCKCONST(cctx_T *cctx)
1512{
1513 isn_T *isn;
1514
1515 RETURN_OK_IF_SKIP(cctx);
1516 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1517 return FAIL;
1518 return OK;
1519}
1520
1521/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 * Generate an ISN_LOADS instruction.
1523 */
1524 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001525generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001527 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001529 int sid,
1530 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531{
1532 isn_T *isn;
1533
Bram Moolenaar080457c2020-03-03 21:53:32 +01001534 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001535 if (isn_type == ISN_LOADS)
1536 isn = generate_instr_type(cctx, isn_type, type);
1537 else
1538 isn = generate_instr_drop(cctx, isn_type, 1);
1539 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001541 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1542 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543
1544 return OK;
1545}
1546
1547/*
1548 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1549 */
1550 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001551generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 cctx_T *cctx,
1553 isntype_T isn_type,
1554 int sid,
1555 int idx,
1556 type_T *type)
1557{
1558 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001559 scriptref_T *sref;
1560 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if (isn_type == ISN_LOADSCRIPT)
1564 isn = generate_instr_type(cctx, isn_type, type);
1565 else
1566 isn = generate_instr_drop(cctx, isn_type, 1);
1567 if (isn == NULL)
1568 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001569
1570 // This requires three arguments, which doesn't fit in an instruction, thus
1571 // we need to allocate a struct for this.
1572 sref = ALLOC_ONE(scriptref_T);
1573 if (sref == NULL)
1574 return FAIL;
1575 isn->isn_arg.script.scriptref = sref;
1576 sref->sref_sid = sid;
1577 sref->sref_idx = idx;
1578 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001579 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 return OK;
1581}
1582
1583/*
1584 * Generate an ISN_NEWLIST instruction.
1585 */
1586 static int
1587generate_NEWLIST(cctx_T *cctx, int count)
1588{
1589 isn_T *isn;
1590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 type_T *type;
1592 type_T *member;
1593
Bram Moolenaar080457c2020-03-03 21:53:32 +01001594 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1596 return FAIL;
1597 isn->isn_arg.number = count;
1598
Bram Moolenaar127542b2020-08-09 17:22:04 +02001599 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001600 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001601 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001602 else
1603 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001604 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1605 cctx->ctx_type_list);
1606 type = get_list_type(member, cctx->ctx_type_list);
1607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 // drop the value types
1609 stack->ga_len -= count;
1610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001612 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return FAIL;
1614 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1615 ++stack->ga_len;
1616
1617 return OK;
1618}
1619
1620/*
1621 * Generate an ISN_NEWDICT instruction.
1622 */
1623 static int
1624generate_NEWDICT(cctx_T *cctx, int count)
1625{
1626 isn_T *isn;
1627 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 type_T *type;
1629 type_T *member;
1630
Bram Moolenaar080457c2020-03-03 21:53:32 +01001631 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1633 return FAIL;
1634 isn->isn_arg.number = count;
1635
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001636 if (count == 0)
1637 member = &t_void;
1638 else
1639 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001640 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1641 cctx->ctx_type_list);
1642 type = get_dict_type(member, cctx->ctx_type_list);
1643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 // drop the key and value types
1645 stack->ga_len -= 2 * count;
1646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001648 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 return FAIL;
1650 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1651 ++stack->ga_len;
1652
1653 return OK;
1654}
1655
1656/*
1657 * Generate an ISN_FUNCREF instruction.
1658 */
1659 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001660generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661{
1662 isn_T *isn;
1663 garray_T *stack = &cctx->ctx_type_stack;
1664
Bram Moolenaar080457c2020-03-03 21:53:32 +01001665 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1667 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001668 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001669 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001671 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001672 // the nested context, including this one.
1673 if (ufunc->uf_flags & FC_CLOSURE)
1674 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1675
Bram Moolenaar35578162021-08-02 19:10:38 +02001676 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001678 ((type_T **)stack->ga_data)[stack->ga_len] =
1679 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 ++stack->ga_len;
1681
1682 return OK;
1683}
1684
1685/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001686 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001687 * "lambda_name" and "func_name" must be in allocated memory and will be
1688 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001689 */
1690 static int
1691generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1692{
1693 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001694
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001695 if (cctx->ctx_skip == SKIP_YES)
1696 {
1697 vim_free(lambda_name);
1698 vim_free(func_name);
1699 return OK;
1700 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001701 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001702 {
1703 vim_free(lambda_name);
1704 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001705 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001706 }
1707 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001708 isn->isn_arg.newfunc.nf_global = func_name;
1709
1710 return OK;
1711}
1712
1713/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001714 * Generate an ISN_DEF instruction: list functions
1715 */
1716 static int
1717generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1718{
1719 isn_T *isn;
1720
1721 RETURN_OK_IF_SKIP(cctx);
1722 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1723 return FAIL;
1724 if (len > 0)
1725 {
1726 isn->isn_arg.string = vim_strnsave(name, len);
1727 if (isn->isn_arg.string == NULL)
1728 return FAIL;
1729 }
1730 return OK;
1731}
1732
1733/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 * Generate an ISN_JUMP instruction.
1735 */
1736 static int
1737generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1738{
1739 isn_T *isn;
1740 garray_T *stack = &cctx->ctx_type_stack;
1741
Bram Moolenaar080457c2020-03-03 21:53:32 +01001742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1744 return FAIL;
1745 isn->isn_arg.jump.jump_when = when;
1746 isn->isn_arg.jump.jump_where = where;
1747
1748 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1749 --stack->ga_len;
1750
1751 return OK;
1752}
1753
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001754/*
1755 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1756 */
1757 static int
1758generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1759{
1760 isn_T *isn;
1761
1762 RETURN_OK_IF_SKIP(cctx);
1763 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1766 // jump_where is set later
1767 return OK;
1768}
1769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 static int
1771generate_FOR(cctx_T *cctx, int loop_idx)
1772{
1773 isn_T *isn;
1774 garray_T *stack = &cctx->ctx_type_stack;
1775
Bram Moolenaar080457c2020-03-03 21:53:32 +01001776 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1778 return FAIL;
1779 isn->isn_arg.forloop.for_idx = loop_idx;
1780
Bram Moolenaar35578162021-08-02 19:10:38 +02001781 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 return FAIL;
1783 // type doesn't matter, will be stored next
1784 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1785 ++stack->ga_len;
1786
1787 return OK;
1788}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001789/*
1790 * Generate an ISN_TRYCONT instruction.
1791 */
1792 static int
1793generate_TRYCONT(cctx_T *cctx, int levels, int where)
1794{
1795 isn_T *isn;
1796
1797 RETURN_OK_IF_SKIP(cctx);
1798 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1799 return FAIL;
1800 isn->isn_arg.trycont.tct_levels = levels;
1801 isn->isn_arg.trycont.tct_where = where;
1802
1803 return OK;
1804}
1805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806
1807/*
1808 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001809 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 * Return FAIL if the number of arguments is wrong.
1811 */
1812 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001813generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814{
1815 isn_T *isn;
1816 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001817 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001818 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001819 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001820 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821
Bram Moolenaar080457c2020-03-03 21:53:32 +01001822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001823 argoff = check_internal_func(func_idx, argcount);
1824 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 return FAIL;
1826
Bram Moolenaar389df252020-07-09 21:20:47 +02001827 if (method_call && argoff > 1)
1828 {
1829 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1830 return FAIL;
1831 isn->isn_arg.shuffle.shfl_item = argcount;
1832 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1833 }
1834
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001835 if (argcount > 0)
1836 {
1837 // Check the types of the arguments.
1838 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001839 if (method_call && argoff > 1)
1840 {
1841 int i;
1842
1843 for (i = 0; i < argcount; ++i)
1844 shuffled_argtypes[i] = (i < argoff - 1)
1845 ? argtypes[i + 1]
1846 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1847 argtypes = shuffled_argtypes;
1848 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001849 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1850 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001851 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001852 if (internal_func_is_map(func_idx))
1853 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001854 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1857 return FAIL;
1858 isn->isn_arg.bfunc.cbf_idx = func_idx;
1859 isn->isn_arg.bfunc.cbf_argcount = argcount;
1860
Bram Moolenaar94738d82020-10-21 14:25:07 +02001861 // Drop the argument types and push the return type.
1862 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001863 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 return FAIL;
1865 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001866 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001867 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001869 if (maptype != NULL && maptype->tt_member != NULL
1870 && maptype->tt_member != &t_any)
1871 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001872 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 return OK;
1875}
1876
1877/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001878 * Generate an ISN_LISTAPPEND instruction. Works like add().
1879 * Argument count is already checked.
1880 */
1881 static int
1882generate_LISTAPPEND(cctx_T *cctx)
1883{
1884 garray_T *stack = &cctx->ctx_type_stack;
1885 type_T *list_type;
1886 type_T *item_type;
1887 type_T *expected;
1888
1889 // Caller already checked that list_type is a list.
1890 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1891 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1892 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001893 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001894 return FAIL;
1895
1896 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1897 return FAIL;
1898
1899 --stack->ga_len; // drop the argument
1900 return OK;
1901}
1902
1903/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001904 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1905 * Argument count is already checked.
1906 */
1907 static int
1908generate_BLOBAPPEND(cctx_T *cctx)
1909{
1910 garray_T *stack = &cctx->ctx_type_stack;
1911 type_T *item_type;
1912
1913 // Caller already checked that blob_type is a blob.
1914 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001915 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001916 return FAIL;
1917
1918 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1919 return FAIL;
1920
1921 --stack->ga_len; // drop the argument
1922 return OK;
1923}
1924
1925/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001926 * Return TRUE if "ufunc" should be compiled, taking into account whether
1927 * "profile" indicates profiling is to be done.
1928 */
1929 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001930func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001931{
1932 switch (ufunc->uf_def_status)
1933 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001934 case UF_TO_BE_COMPILED:
1935 return TRUE;
1936
Bram Moolenaarb2049902021-01-24 12:53:53 +01001937 case UF_COMPILED:
1938 {
1939 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1940 + ufunc->uf_dfunc_idx;
1941
Bram Moolenaare99d4222021-06-13 14:01:26 +02001942 switch (compile_type)
1943 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001944 case CT_PROFILE:
1945#ifdef FEAT_PROFILE
1946 return dfunc->df_instr_prof == NULL;
1947#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001948 case CT_NONE:
1949 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001950 case CT_DEBUG:
1951 return dfunc->df_instr_debug == NULL;
1952 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001953 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001954
1955 case UF_NOT_COMPILED:
1956 case UF_COMPILE_ERROR:
1957 case UF_COMPILING:
1958 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001959 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001960 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001961}
1962
1963/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 * Generate an ISN_DCALL or ISN_UCALL instruction.
1965 * Return FAIL if the number of arguments is wrong.
1966 */
1967 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001968generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969{
1970 isn_T *isn;
1971 garray_T *stack = &cctx->ctx_type_stack;
1972 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001973 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974
Bram Moolenaar080457c2020-03-03 21:53:32 +01001975 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 if (argcount > regular_args && !has_varargs(ufunc))
1977 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001978 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 return FAIL;
1980 }
1981 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1982 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001983 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 return FAIL;
1985 }
1986
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001987 if (ufunc->uf_def_status != UF_NOT_COMPILED
1988 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001989 {
1990 int i;
1991
1992 for (i = 0; i < argcount; ++i)
1993 {
1994 type_T *expected;
1995 type_T *actual;
1996
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001997 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1998 if (actual == &t_special
1999 && i >= regular_args - ufunc->uf_def_args.ga_len)
2000 {
2001 // assume v:none used for default argument value
2002 continue;
2003 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002004 if (i < regular_args)
2005 {
2006 if (ufunc->uf_arg_types == NULL)
2007 continue;
2008 expected = ufunc->uf_arg_types[i];
2009 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002010 else if (ufunc->uf_va_type == NULL
2011 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002012 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002013 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002014 else
2015 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002016 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002017 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002018 {
2019 arg_type_mismatch(expected, actual, i + 1);
2020 return FAIL;
2021 }
2022 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002023 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002024 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002025 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002026 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002027 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002028 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2029 {
2030 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2031 ufunc->uf_name);
2032 return FAIL;
2033 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002036 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002037 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002039 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 {
2041 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2042 isn->isn_arg.dfunc.cdf_argcount = argcount;
2043 }
2044 else
2045 {
2046 // A user function may be deleted and redefined later, can't use the
2047 // ufunc pointer, need to look it up again at runtime.
2048 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2049 isn->isn_arg.ufunc.cuf_argcount = argcount;
2050 }
2051
2052 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002053 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 return FAIL;
2055 // add return value
2056 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2057 ++stack->ga_len;
2058
2059 return OK;
2060}
2061
2062/*
2063 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2064 */
2065 static int
2066generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2067{
2068 isn_T *isn;
2069 garray_T *stack = &cctx->ctx_type_stack;
2070
Bram Moolenaar080457c2020-03-03 21:53:32 +01002071 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2073 return FAIL;
2074 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2075 isn->isn_arg.ufunc.cuf_argcount = argcount;
2076
2077 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002078 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002079 return FAIL;
2080 // add return value
2081 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2082 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
2084 return OK;
2085}
2086
2087/*
2088 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002089 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 */
2091 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002092generate_PCALL(
2093 cctx_T *cctx,
2094 int argcount,
2095 char_u *name,
2096 type_T *type,
2097 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098{
2099 isn_T *isn;
2100 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002101 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102
Bram Moolenaar080457c2020-03-03 21:53:32 +01002103 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002104
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002105 if (type->tt_type == VAR_ANY)
2106 ret_type = &t_any;
2107 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002108 {
2109 if (type->tt_argcount != -1)
2110 {
2111 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2112
2113 if (argcount < type->tt_min_argcount - varargs)
2114 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002115 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002116 return FAIL;
2117 }
2118 if (!varargs && argcount > type->tt_argcount)
2119 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002120 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002121 return FAIL;
2122 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002123 if (type->tt_args != NULL)
2124 {
2125 int i;
2126
2127 for (i = 0; i < argcount; ++i)
2128 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002129 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002130 type_T *actual = ((type_T **)stack->ga_data)[
2131 stack->ga_len + offset];
2132 type_T *expected;
2133
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002134 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002135 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002136 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002137 else if (i >= type->tt_min_argcount
2138 && actual == &t_special)
2139 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002140 else
2141 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002142 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002143 cctx, TRUE, FALSE) == FAIL)
2144 {
2145 arg_type_mismatch(expected, actual, i + 1);
2146 return FAIL;
2147 }
2148 }
2149 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002150 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002151 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002152 if (ret_type == &t_unknown)
2153 // return type not known yet, use a runtime check
2154 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002155 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002156 else
2157 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002158 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002159 return FAIL;
2160 }
2161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2163 return FAIL;
2164 isn->isn_arg.pfunc.cpf_top = at_top;
2165 isn->isn_arg.pfunc.cpf_argcount = argcount;
2166
2167 stack->ga_len -= argcount; // drop the arguments
2168
2169 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002170 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002172 // If partial is above the arguments it must be cleared and replaced with
2173 // the return value.
2174 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2175 return FAIL;
2176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 return OK;
2178}
2179
2180/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002181 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 */
2183 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002184generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185{
2186 isn_T *isn;
2187 garray_T *stack = &cctx->ctx_type_stack;
2188 type_T *type;
2189
Bram Moolenaar080457c2020-03-03 21:53:32 +01002190 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002191 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002193 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002195 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002197 if (type->tt_type != VAR_DICT && type != &t_any)
2198 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002199 char *tofree;
2200
2201 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2202 name, type_name(type, &tofree));
2203 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002204 return FAIL;
2205 }
2206 // change dict type to dict member type
2207 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002208 {
2209 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2210 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212
2213 return OK;
2214}
2215
2216/*
2217 * Generate an ISN_ECHO instruction.
2218 */
2219 static int
2220generate_ECHO(cctx_T *cctx, int with_white, int count)
2221{
2222 isn_T *isn;
2223
Bram Moolenaar080457c2020-03-03 21:53:32 +01002224 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2226 return FAIL;
2227 isn->isn_arg.echo.echo_with_white = with_white;
2228 isn->isn_arg.echo.echo_count = count;
2229
2230 return OK;
2231}
2232
Bram Moolenaarad39c092020-02-26 18:23:43 +01002233/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002234 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002235 */
2236 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002237generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002238{
2239 isn_T *isn;
2240
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002241 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002242 return FAIL;
2243 isn->isn_arg.number = count;
2244
2245 return OK;
2246}
2247
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002248/*
2249 * Generate an ISN_PUT instruction.
2250 */
2251 static int
2252generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2253{
2254 isn_T *isn;
2255
2256 RETURN_OK_IF_SKIP(cctx);
2257 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2258 return FAIL;
2259 isn->isn_arg.put.put_regname = regname;
2260 isn->isn_arg.put.put_lnum = lnum;
2261 return OK;
2262}
2263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 static int
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002265generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266{
2267 isn_T *isn;
2268
Bram Moolenaar080457c2020-03-03 21:53:32 +01002269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002270 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 return FAIL;
2272 isn->isn_arg.string = vim_strsave(line);
2273 return OK;
2274}
2275
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002276 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002277generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2278{
2279 isn_T *isn;
2280 garray_T *stack = &cctx->ctx_type_stack;
2281
2282 RETURN_OK_IF_SKIP(cctx);
2283 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2284 return FAIL;
2285 isn->isn_arg.string = vim_strsave(line);
2286
Bram Moolenaar35578162021-08-02 19:10:38 +02002287 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002288 return FAIL;
2289 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2290 ++stack->ga_len;
2291
2292 return OK;
2293}
2294
2295 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002296generate_EXECCONCAT(cctx_T *cctx, int count)
2297{
2298 isn_T *isn;
2299
2300 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2301 return FAIL;
2302 isn->isn_arg.number = count;
2303 return OK;
2304}
2305
Bram Moolenaar08597872020-12-10 19:43:40 +01002306/*
2307 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2308 */
2309 static int
2310generate_RANGE(cctx_T *cctx, char_u *range)
2311{
2312 isn_T *isn;
2313 garray_T *stack = &cctx->ctx_type_stack;
2314
2315 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2316 return FAIL;
2317 isn->isn_arg.string = range;
2318
Bram Moolenaar35578162021-08-02 19:10:38 +02002319 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002320 return FAIL;
2321 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2322 ++stack->ga_len;
2323 return OK;
2324}
2325
Bram Moolenaar792f7862020-11-23 08:31:18 +01002326 static int
2327generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2328{
2329 isn_T *isn;
2330
2331 RETURN_OK_IF_SKIP(cctx);
2332 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2333 return FAIL;
2334 isn->isn_arg.unpack.unp_count = var_count;
2335 isn->isn_arg.unpack.unp_semicolon = semicolon;
2336 return OK;
2337}
2338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002340 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002341 */
2342 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002343generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002344{
2345 isn_T *isn;
2346
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002347 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002348 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002349 cctx->ctx_has_cmdmod = TRUE;
2350
2351 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002352 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002353 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2354 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2355 return FAIL;
2356 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002357 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002358 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002359 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002360
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002361 return OK;
2362}
2363
2364 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002365generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002366{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002367 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2368 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002369 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002370 return OK;
2371}
2372
Bram Moolenaarfa984412021-03-25 22:15:28 +01002373 static int
2374misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002375{
2376 garray_T *instr = &cctx->ctx_instr;
2377
Bram Moolenaara91a7132021-03-25 21:12:15 +01002378 if (cctx->ctx_has_cmdmod
2379 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2380 == ISN_CMDMOD)
2381 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002382 emsg(_(e_misplaced_command_modifier));
2383 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002384 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002385 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002386}
2387
2388/*
2389 * Get the index of the current instruction.
2390 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2391 */
2392 static int
2393current_instr_idx(cctx_T *cctx)
2394{
2395 garray_T *instr = &cctx->ctx_instr;
2396 int idx = instr->ga_len;
2397
Bram Moolenaare99d4222021-06-13 14:01:26 +02002398 while (idx > 0)
2399 {
2400 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002401 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002402 {
2403 --idx;
2404 continue;
2405 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002406#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002407 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2408 {
2409 --idx;
2410 continue;
2411 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002412#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002413 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2414 {
2415 --idx;
2416 continue;
2417 }
2418 break;
2419 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002420 return idx;
2421}
2422
Bram Moolenaarf002a412021-01-24 13:34:18 +01002423#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002424 static void
2425may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2426{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002427 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002428 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002429}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002430#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002431
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002432/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002434 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002436 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002437reserve_local(
2438 cctx_T *cctx,
2439 char_u *name,
2440 size_t len,
2441 int isConst,
2442 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002445 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002447 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002449 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002450 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 }
2452
Bram Moolenaar35578162021-08-02 19:10:38 +02002453 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002454 return NULL;
2455 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002456 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002458 // Every local variable uses the next entry on the stack. We could re-use
2459 // the last ones when leaving a scope, but then variables used in a closure
2460 // might get overwritten. To keep things simple do not re-use stack
2461 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002462 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2463 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002464
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002465 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 lvar->lv_const = isConst;
2467 lvar->lv_type = type;
2468
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002469 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002470 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002471 return NULL;
2472 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2473 vim_strsave(lvar->lv_name);
2474 ++dfunc->df_var_names.ga_len;
2475
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002476 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477}
2478
2479/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002480 * Remove local variables above "new_top".
2481 */
2482 static void
2483unwind_locals(cctx_T *cctx, int new_top)
2484{
2485 if (cctx->ctx_locals.ga_len > new_top)
2486 {
2487 int idx;
2488 lvar_T *lvar;
2489
2490 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2491 {
2492 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2493 vim_free(lvar->lv_name);
2494 }
2495 }
2496 cctx->ctx_locals.ga_len = new_top;
2497}
2498
2499/*
2500 * Free all local variables.
2501 */
2502 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002503free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002504{
2505 unwind_locals(cctx, 0);
2506 ga_clear(&cctx->ctx_locals);
2507}
2508
2509/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002510 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2511 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2512 * error if the variable was defined with :const.
2513 */
2514 static int
2515check_item_writable(svar_T *sv, int check_writable, char_u *name)
2516{
2517 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2518 || (check_writable == ASSIGN_FINAL
2519 && sv->sv_const == ASSIGN_CONST))
2520 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002521 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002522 return FAIL;
2523 }
2524 return OK;
2525}
2526
2527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002529 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 * Returns the index in "sn_var_vals" if found.
2531 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002532 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 */
2534 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002535get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536{
2537 hashtab_T *ht;
2538 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002539 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002540 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 int idx;
2542
Bram Moolenaare3d46852020-08-29 13:39:17 +02002543 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002545 if (sid == current_sctx.sc_sid)
2546 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002547 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002548
2549 if (sav == NULL)
2550 return -2;
2551 idx = sav->sav_var_vals_idx;
2552 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002553 if (check_item_writable(sv, check_writable, name) == FAIL)
2554 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002555 return idx;
2556 }
2557
2558 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 ht = &SCRIPT_VARS(sid);
2560 di = find_var_in_ht(ht, 0, name, TRUE);
2561 if (di == NULL)
2562 return -2;
2563
2564 // Now find the svar_T index in sn_var_vals.
2565 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2566 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002567 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (sv->sv_tv == &di->di_tv)
2569 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002570 if (check_item_writable(sv, check_writable, name) == FAIL)
2571 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 return idx;
2573 }
2574 }
2575 return -1;
2576}
2577
2578/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002579 * Find "name" in imported items of the current script or in "cctx" if not
2580 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 */
2582 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002583find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 int idx;
2586
Bram Moolenaare3d46852020-08-29 13:39:17 +02002587 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002588 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 if (cctx != NULL)
2590 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2591 {
2592 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2593 + idx;
2594
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002595 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2596 : STRLEN(import->imp_name) == len
2597 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 return import;
2599 }
2600
Bram Moolenaarefa94442020-08-08 22:16:00 +02002601 return find_imported_in_script(name, len, current_sctx.sc_sid);
2602}
2603
2604 imported_T *
2605find_imported_in_script(char_u *name, size_t len, int sid)
2606{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002607 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002608 int idx;
2609
Bram Moolenaare3d46852020-08-29 13:39:17 +02002610 if (!SCRIPT_ID_VALID(sid))
2611 return NULL;
2612 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2614 {
2615 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2616
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002617 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2618 : STRLEN(import->imp_name) == len
2619 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 return import;
2621 }
2622 return NULL;
2623}
2624
2625/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002626 * Free all imported variables.
2627 */
2628 static void
2629free_imported(cctx_T *cctx)
2630{
2631 int idx;
2632
2633 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2634 {
2635 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2636
2637 vim_free(import->imp_name);
2638 }
2639 ga_clear(&cctx->ctx_imports);
2640}
2641
2642/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643 * Return a pointer to the next line that isn't empty or only contains a
2644 * comment. Skips over white space.
2645 * Returns NULL if there is none.
2646 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002647 char_u *
2648peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002649{
2650 int lnum = cctx->ctx_lnum;
2651
2652 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2653 {
2654 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002655 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002656
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002657 // ignore NULLs inserted for continuation lines
2658 if (line != NULL)
2659 {
2660 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002661 if (vim9_bad_comment(p))
2662 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002663 if (*p != NUL && !vim9_comment_start(p))
2664 return p;
2665 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002666 }
2667 return NULL;
2668}
2669
2670/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002671 * Called when checking for a following operator at "arg". When the rest of
2672 * the line is empty or only a comment, peek the next line. If there is a next
2673 * line return a pointer to it and set "nextp".
2674 * Otherwise skip over white space.
2675 */
2676 static char_u *
2677may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2678{
2679 char_u *p = skipwhite(arg);
2680
2681 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002682 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002683 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002684 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002685 if (*nextp != NULL)
2686 return *nextp;
2687 }
2688 return p;
2689}
2690
2691/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002692 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002693 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002694 * Returns NULL when at the end.
2695 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002696 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002697next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002698{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002699 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002700
2701 do
2702 {
2703 ++cctx->ctx_lnum;
2704 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002705 {
2706 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002707 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002708 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002709 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002710 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002711 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002712 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002713 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002714 return line;
2715}
2716
2717/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002718 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002719 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002720 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002721 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2722 */
2723 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002724may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002725{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002726 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002727 if (vim9_bad_comment(*arg))
2728 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002729 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002730 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002731 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002732
2733 if (next == NULL)
2734 return FAIL;
2735 *arg = skipwhite(next);
2736 }
2737 return OK;
2738}
2739
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002740/*
2741 * Idem, and give an error when failed.
2742 */
2743 static int
2744may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2745{
2746 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2747 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002748 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002749 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002750 return FAIL;
2751 }
2752 return OK;
2753}
2754
2755
Bram Moolenaara5565e42020-05-09 15:44:01 +02002756// Structure passed between the compile_expr* functions to keep track of
2757// constants that have been parsed but for which no code was produced yet. If
2758// possible expressions on these constants are applied at compile time. If
2759// that is not possible, the code to push the constants needs to be generated
2760// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002761// Using 50 should be more than enough of 5 levels of ().
2762#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002763typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002764 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002765 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002766 int pp_is_const; // all generated code was constants, used for a
2767 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002768} ppconst_T;
2769
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002770static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002771static int compile_expr0(char_u **arg, cctx_T *cctx);
2772static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2773
Bram Moolenaara5565e42020-05-09 15:44:01 +02002774/*
2775 * Generate a PUSH instruction for "tv".
2776 * "tv" will be consumed or cleared.
2777 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2778 */
2779 static int
2780generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2781{
2782 if (tv != NULL)
2783 {
2784 switch (tv->v_type)
2785 {
2786 case VAR_UNKNOWN:
2787 break;
2788 case VAR_BOOL:
2789 generate_PUSHBOOL(cctx, tv->vval.v_number);
2790 break;
2791 case VAR_SPECIAL:
2792 generate_PUSHSPEC(cctx, tv->vval.v_number);
2793 break;
2794 case VAR_NUMBER:
2795 generate_PUSHNR(cctx, tv->vval.v_number);
2796 break;
2797#ifdef FEAT_FLOAT
2798 case VAR_FLOAT:
2799 generate_PUSHF(cctx, tv->vval.v_float);
2800 break;
2801#endif
2802 case VAR_BLOB:
2803 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2804 tv->vval.v_blob = NULL;
2805 break;
2806 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002807 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002808 tv->vval.v_string = NULL;
2809 break;
2810 default:
2811 iemsg("constant type not supported");
2812 clear_tv(tv);
2813 return FAIL;
2814 }
2815 tv->v_type = VAR_UNKNOWN;
2816 }
2817 return OK;
2818}
2819
2820/*
2821 * Generate code for any ppconst entries.
2822 */
2823 static int
2824generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2825{
2826 int i;
2827 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002828 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002829
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002830 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002831 for (i = 0; i < ppconst->pp_used; ++i)
2832 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2833 ret = FAIL;
2834 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002835 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002836 return ret;
2837}
2838
2839/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002840 * Check that the last item of "ppconst" is a bool.
2841 */
2842 static int
2843check_ppconst_bool(ppconst_T *ppconst)
2844{
2845 if (ppconst->pp_used > 0)
2846 {
2847 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002848 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002849
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002850 return check_typval_type(&t_bool, tv, where);
2851 }
2852 return OK;
2853}
2854
2855/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002856 * Clear ppconst constants. Used when failing.
2857 */
2858 static void
2859clear_ppconst(ppconst_T *ppconst)
2860{
2861 int i;
2862
2863 for (i = 0; i < ppconst->pp_used; ++i)
2864 clear_tv(&ppconst->pp_tv[i]);
2865 ppconst->pp_used = 0;
2866}
2867
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002868/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002869 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002870 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002871 */
2872 static int
2873compile_member(int is_slice, cctx_T *cctx)
2874{
2875 type_T **typep;
2876 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002877 vartype_T vartype;
2878 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879
Bram Moolenaar261417b2021-05-06 21:04:55 +02002880 // We can index a list, dict and blob. If we don't know the type
2881 // we can use the index value type. If we still don't know use an "ANY"
2882 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002883 typep = ((type_T **)stack->ga_data) + stack->ga_len
2884 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002885 vartype = (*typep)->tt_type;
2886 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002887 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002888 if (*typep == &t_any && idxtype == &t_string)
2889 vartype = VAR_DICT;
2890 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002891 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002892 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002893 return FAIL;
2894 if (is_slice)
2895 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002896 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2897 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002898 FALSE, FALSE) == FAIL)
2899 return FAIL;
2900 }
2901 }
2902
Bram Moolenaar261417b2021-05-06 21:04:55 +02002903 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002904 {
2905 if (is_slice)
2906 {
2907 emsg(_(e_cannot_slice_dictionary));
2908 return FAIL;
2909 }
2910 if ((*typep)->tt_type == VAR_DICT)
2911 {
2912 *typep = (*typep)->tt_member;
2913 if (*typep == &t_unknown)
2914 // empty dict was used
2915 *typep = &t_any;
2916 }
2917 else
2918 {
2919 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2920 FALSE, FALSE) == FAIL)
2921 return FAIL;
2922 *typep = &t_any;
2923 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002924 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002925 return FAIL;
2926 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2927 return FAIL;
2928 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002929 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002930 {
2931 *typep = &t_string;
2932 if ((is_slice
2933 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2934 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2935 return FAIL;
2936 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002937 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002938 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002939 if (is_slice)
2940 {
2941 *typep = &t_blob;
2942 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2943 return FAIL;
2944 }
2945 else
2946 {
2947 *typep = &t_number;
2948 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2949 return FAIL;
2950 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002951 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002952 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002953 {
2954 if (is_slice)
2955 {
2956 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002957 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002958 2) == FAIL)
2959 return FAIL;
2960 }
2961 else
2962 {
2963 if ((*typep)->tt_type == VAR_LIST)
2964 {
2965 *typep = (*typep)->tt_member;
2966 if (*typep == &t_unknown)
2967 // empty list was used
2968 *typep = &t_any;
2969 }
2970 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002971 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2972 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002973 return FAIL;
2974 }
2975 }
2976 else
2977 {
2978 emsg(_(e_string_list_dict_or_blob_required));
2979 return FAIL;
2980 }
2981 return OK;
2982}
2983
2984/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002985 * Generate an instruction to load script-local variable "name", without the
2986 * leading "s:".
2987 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 */
2989 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002990compile_load_scriptvar(
2991 cctx_T *cctx,
2992 char_u *name, // variable NUL terminated
2993 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002994 char_u **end, // end of variable
2995 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002997 scriptitem_T *si;
2998 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 imported_T *import;
3000
Bram Moolenaare3d46852020-08-29 13:39:17 +02003001 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3002 return FAIL;
3003 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003004 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003005 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003007 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003008 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3009 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 }
3011 if (idx >= 0)
3012 {
3013 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3014
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003015 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 current_sctx.sc_sid, idx, sv->sv_type);
3017 return OK;
3018 }
3019
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003020 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 if (import != NULL)
3022 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003023 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003024 {
3025 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003026 char_u *exp_name;
3027 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003028 ufunc_T *ufunc;
3029 type_T *type;
3030
3031 // Used "import * as Name", need to lookup the member.
3032 if (*p != '.')
3033 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003034 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003035 return FAIL;
3036 }
3037 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003038 if (VIM_ISWHITE(*p))
3039 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003040 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003041 return FAIL;
3042 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003043
Bram Moolenaar1c991142020-07-04 13:15:31 +02003044 // isolate one name
3045 exp_name = p;
3046 while (eval_isnamec(*p))
3047 ++p;
3048 cc = *p;
3049 *p = NUL;
3050
Bram Moolenaaredba7072021-03-13 21:14:18 +01003051 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3052 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003053 *p = cc;
3054 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003055 *end = p;
3056
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003057 if (idx < 0)
3058 {
3059 if (*p == '(' && ufunc != NULL)
3060 {
3061 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3062 return OK;
3063 }
3064 return FAIL;
3065 }
3066
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003067 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3068 import->imp_sid,
3069 idx,
3070 type);
3071 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003072 else if (import->imp_funcname != NULL)
3073 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003074 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003075 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3076 import->imp_sid,
3077 import->imp_var_vals_idx,
3078 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 return OK;
3080 }
3081
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003082 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003083 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 return FAIL;
3085}
3086
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003087 static int
3088generate_funcref(cctx_T *cctx, char_u *name)
3089{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003090 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003091
3092 if (ufunc == NULL)
3093 return FAIL;
3094
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003095 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003096 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3097 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003098 == FAIL)
3099 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003100 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003101}
3102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103/*
3104 * Compile a variable name into a load instruction.
3105 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003106 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 * When "error" is FALSE do not give an error when not found.
3108 */
3109 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003110compile_load(
3111 char_u **arg,
3112 char_u *end_arg,
3113 cctx_T *cctx,
3114 int is_expr,
3115 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116{
3117 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003118 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003119 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003121 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122
3123 if (*(*arg + 1) == ':')
3124 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003125 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003126 {
3127 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128
Bram Moolenaarfa596382021-04-07 21:58:16 +02003129 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003130 switch (**arg)
3131 {
3132 case 'g': isn_type = ISN_LOADGDICT; break;
3133 case 'w': isn_type = ISN_LOADWDICT; break;
3134 case 't': isn_type = ISN_LOADTDICT; break;
3135 case 'b': isn_type = ISN_LOADBDICT; break;
3136 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003137 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003138 goto theend;
3139 }
3140 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3141 goto theend;
3142 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 else
3145 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003146 isntype_T isn_type = ISN_DROP;
3147
Bram Moolenaarfa596382021-04-07 21:58:16 +02003148 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003149 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3150 if (name == NULL)
3151 return FAIL;
3152
3153 switch (**arg)
3154 {
3155 case 'v': res = generate_LOADV(cctx, name, error);
3156 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003157 case 's': if (is_expr && ASCII_ISUPPER(*name)
3158 && find_func(name, FALSE, cctx) != NULL)
3159 res = generate_funcref(cctx, name);
3160 else
3161 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003162 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003163 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003164 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003165 {
3166 if (is_expr && ASCII_ISUPPER(*name)
3167 && find_func(name, FALSE, cctx) != NULL)
3168 res = generate_funcref(cctx, name);
3169 else
3170 isn_type = ISN_LOADG;
3171 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003172 else
3173 {
3174 isn_type = ISN_LOADAUTO;
3175 vim_free(name);
3176 name = vim_strnsave(*arg, end - *arg);
3177 if (name == NULL)
3178 return FAIL;
3179 }
3180 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003181 case 'w': isn_type = ISN_LOADW; break;
3182 case 't': isn_type = ISN_LOADT; break;
3183 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003184 default: // cannot happen, just in case
3185 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003186 goto theend;
3187 }
3188 if (isn_type != ISN_DROP)
3189 {
3190 // Global, Buffer-local, Window-local and Tabpage-local
3191 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003192 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003193 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 }
3196 }
3197 else
3198 {
3199 size_t len = end - *arg;
3200 int idx;
3201 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003202 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203
3204 name = vim_strnsave(*arg, end - *arg);
3205 if (name == NULL)
3206 return FAIL;
3207
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003208 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3209 {
3210 script_autoload(name, FALSE);
3211 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3212 }
3213 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3214 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003216 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003217 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
3219 else
3220 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003221 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003222
Bram Moolenaar709664c2020-12-12 14:33:41 +01003223 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003225 type = lvar.lv_type;
3226 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003227 if (lvar.lv_from_outer != 0)
3228 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003229 else
3230 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232 else
3233 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003234 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003235 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003236 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003237 || find_imported(name, 0, cctx) != NULL)
3238 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003239
Bram Moolenaar0f769812020-09-12 18:32:34 +02003240 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003241 // uppercase letter it can be a user defined function.
3242 // generate_funcref() will fail if the function can't be found.
3243 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003244 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 }
3246 }
3247 if (gen_load)
3248 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003249 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003250 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003251 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003252 cctx->ctx_outer_used = TRUE;
3253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 }
3255
3256 *arg = end;
3257
3258theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003259 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003260 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 vim_free(name);
3262 return res;
3263}
3264
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003265 static void
3266clear_instr_ga(garray_T *gap)
3267{
3268 int idx;
3269
3270 for (idx = 0; idx < gap->ga_len; ++idx)
3271 delete_instr(((isn_T *)gap->ga_data) + idx);
3272 ga_clear(gap);
3273}
3274
3275/*
3276 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3277 * Returns FAIL if compilation fails.
3278 */
3279 static int
3280compile_string(isn_T *isn, cctx_T *cctx)
3281{
3282 char_u *s = isn->isn_arg.string;
3283 garray_T save_ga = cctx->ctx_instr;
3284 int expr_res;
3285 int trailing_error;
3286 int instr_count;
3287 isn_T *instr = NULL;
3288
Bram Moolenaarcd268012021-07-22 19:11:08 +02003289 // Remove the string type from the stack.
3290 --cctx->ctx_type_stack.ga_len;
3291
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003292 // Temporarily reset the list of instructions so that the jump labels are
3293 // correct.
3294 cctx->ctx_instr.ga_len = 0;
3295 cctx->ctx_instr.ga_maxlen = 0;
3296 cctx->ctx_instr.ga_data = NULL;
3297 expr_res = compile_expr0(&s, cctx);
3298 s = skipwhite(s);
3299 trailing_error = *s != NUL;
3300
Bram Moolenaarff652882021-05-16 15:24:49 +02003301 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003302 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003303 {
3304 if (trailing_error)
3305 semsg(_(e_trailing_arg), s);
3306 clear_instr_ga(&cctx->ctx_instr);
3307 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003308 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 return FAIL;
3310 }
3311
3312 // Move the generated instructions into the ISN_INSTR instruction, then
3313 // restore the list of instructions.
3314 instr_count = cctx->ctx_instr.ga_len;
3315 instr = cctx->ctx_instr.ga_data;
3316 instr[instr_count].isn_type = ISN_FINISH;
3317
3318 cctx->ctx_instr = save_ga;
3319 vim_free(isn->isn_arg.string);
3320 isn->isn_type = ISN_INSTR;
3321 isn->isn_arg.instr = instr;
3322 return OK;
3323}
3324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325/*
3326 * Compile the argument expressions.
3327 * "arg" points to just after the "(" and is advanced to after the ")"
3328 */
3329 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003330compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003332 char_u *p = *arg;
3333 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003334 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003335 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336
Bram Moolenaare6085c52020-04-12 20:19:16 +02003337 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003339 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3340 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003341 if (*p == ')')
3342 {
3343 *arg = p + 1;
3344 return OK;
3345 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003346 if (must_end)
3347 {
3348 semsg(_(e_missing_comma_before_argument_str), p);
3349 return FAIL;
3350 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003351
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003352 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003353 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 return FAIL;
3355 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003356
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003357 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003358 && cctx->ctx_instr.ga_len == instr_count + 1)
3359 {
3360 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3361
3362 // {skip} argument of searchpair() can be compiled if not empty
3363 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3364 compile_string(isn, cctx);
3365 }
3366
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003367 if (*p != ',' && *skipwhite(p) == ',')
3368 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003369 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003370 p = skipwhite(p);
3371 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003373 {
3374 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003375 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003376 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003377 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003378 else
3379 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003380 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003381 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003383failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003384 emsg(_(e_missing_close));
3385 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386}
3387
3388/*
3389 * Compile a function call: name(arg1, arg2)
3390 * "arg" points to "name", "arg + varlen" to the "(".
3391 * "argcount_init" is 1 for "value->method()"
3392 * Instructions:
3393 * EVAL arg1
3394 * EVAL arg2
3395 * BCALL / DCALL / UCALL
3396 */
3397 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003398compile_call(
3399 char_u **arg,
3400 size_t varlen,
3401 cctx_T *cctx,
3402 ppconst_T *ppconst,
3403 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404{
3405 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003406 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 int argcount = argcount_init;
3408 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003409 char_u fname_buf[FLEN_FIXED + 1];
3410 char_u *tofree = NULL;
3411 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003412 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003413 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003414 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003415 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003417 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003418 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003419 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003420 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003421 {
3422 char_u *s = skipwhite(*arg + varlen + 1);
3423 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003424 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003425
3426 argvars[0].v_type = VAR_UNKNOWN;
3427 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003428 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003429 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003430 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003431 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003432 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003433 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003434 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003435 {
3436 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3437
3438 *arg = s + 1;
3439 argvars[1].v_type = VAR_UNKNOWN;
3440 tv->v_type = VAR_NUMBER;
3441 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003442 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003443 f_has(argvars, tv);
3444 else
3445 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003446 clear_tv(&argvars[0]);
3447 ++ppconst->pp_used;
3448 return OK;
3449 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003450 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003451 if (!is_has)
3452 {
3453 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3454 return FAIL;
3455 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003456 }
3457
3458 if (generate_ppconst(cctx, ppconst) == FAIL)
3459 return FAIL;
3460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 if (varlen >= sizeof(namebuf))
3462 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003463 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 return FAIL;
3465 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003466 vim_strncpy(namebuf, *arg, varlen);
3467 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003469 // We handle the "skip" argument of searchpair() and searchpairpos()
3470 // differently.
3471 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3472 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3473 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3474 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003477 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003478 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
Bram Moolenaar03290b82020-12-19 16:30:44 +01003480 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003481 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 {
3483 int idx;
3484
3485 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003486 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003488 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003489 if (STRCMP(name, "flatten") == 0)
3490 {
3491 emsg(_(e_cannot_use_flatten_in_vim9_script));
3492 goto theend;
3493 }
3494
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003495 if (STRCMP(name, "add") == 0 && argcount == 2)
3496 {
3497 garray_T *stack = &cctx->ctx_type_stack;
3498 type_T *type = ((type_T **)stack->ga_data)[
3499 stack->ga_len - 2];
3500
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003501 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003502 if (type->tt_type == VAR_LIST)
3503 {
3504 // inline "add(list, item)" so that the type can be checked
3505 res = generate_LISTAPPEND(cctx);
3506 idx = -1;
3507 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003508 else if (type->tt_type == VAR_BLOB)
3509 {
3510 // inline "add(blob, nr)" so that the type can be checked
3511 res = generate_BLOBAPPEND(cctx);
3512 idx = -1;
3513 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003514 }
3515
3516 if (idx >= 0)
3517 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3518 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003519 else
3520 semsg(_(e_unknownfunc), namebuf);
3521 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 }
3523
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003524 // An argument or local variable can be a function reference, this
3525 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003526 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003527 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003528 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003529 // If we can find the function by name generate the right call.
3530 // Skip global functions here, a local funcref takes precedence.
3531 ufunc = find_func(name, FALSE, cctx);
3532 if (ufunc != NULL && !func_is_global(ufunc))
3533 {
3534 res = generate_CALL(cctx, ufunc, argcount);
3535 goto theend;
3536 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538
3539 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003540 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003541 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003543 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003544 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003545 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003546 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003547 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003548
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003549 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003550 goto theend;
3551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552
Bram Moolenaar0f769812020-09-12 18:32:34 +02003553 // If we can find a global function by name generate the right call.
3554 if (ufunc != NULL)
3555 {
3556 res = generate_CALL(cctx, ufunc, argcount);
3557 goto theend;
3558 }
3559
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003560 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003561 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003562 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003563 res = generate_UCALL(cctx, name, argcount);
3564 else
3565 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003566
3567theend:
3568 vim_free(tofree);
3569 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570}
3571
3572// like NAMESPACE_CHAR but with 'a' and 'l'.
3573#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3574
3575/*
3576 * Find the end of a variable or function name. Unlike find_name_end() this
3577 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003578 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 * Return a pointer to just after the name. Equal to "arg" if there is no
3580 * valid name.
3581 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02003582 static char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003583to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584{
3585 char_u *p;
3586
3587 // Quick check for valid starting character.
3588 if (!eval_isnamec1(*arg))
3589 return arg;
3590
3591 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3592 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3593 // and can be used in slice "[n:]".
3594 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003595 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3597 break;
3598 return p;
3599}
3600
3601/*
3602 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003603 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003604 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 */
3606 char_u *
3607to_name_const_end(char_u *arg)
3608{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003609 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 typval_T rettv;
3611
Bram Moolenaar678b2072021-07-26 21:10:11 +02003612 if (STRNCMP(p, "<SNR>", 5) == 0)
3613 p = skipdigits(p + 5);
3614 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 if (p == arg && *arg == '[')
3616 {
3617
3618 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003619 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 p = arg;
3621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 return p;
3623}
3624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625/*
3626 * parse a list: [expr, expr]
3627 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003628 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 */
3630 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003631compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632{
3633 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003634 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003636 int is_const;
3637 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003639 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003641 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003642 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003643 semsg(_(e_list_end), *arg);
3644 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003645 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003646 if (*p == ',')
3647 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003648 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003649 return FAIL;
3650 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003651 if (*p == ']')
3652 {
3653 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003654 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003655 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003656 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003657 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003658 if (!is_const)
3659 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 ++count;
3661 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003662 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003664 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3665 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003666 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003667 return FAIL;
3668 }
3669 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003670 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 p = skipwhite(p);
3672 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003673 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675 ppconst->pp_is_const = is_all_const;
3676 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677}
3678
3679/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003680 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003681 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003682 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 */
3684 static int
3685compile_lambda(char_u **arg, cctx_T *cctx)
3686{
Bram Moolenaare462f522020-12-27 14:43:30 +01003687 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 typval_T rettv;
3689 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003690 evalarg_T evalarg;
3691
3692 CLEAR_FIELD(evalarg);
3693 evalarg.eval_flags = EVAL_EVALUATE;
3694 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695
3696 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003697 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3698 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003699 {
3700 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003701 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003702 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003703
Bram Moolenaar65c44152020-12-24 15:14:01 +01003704 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003706 ++ufunc->uf_refcount;
3707 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
Bram Moolenaara9931532021-06-12 15:58:16 +02003709 // Compile it here to get the return type. The return type is optional,
3710 // when it's missing use t_unknown. This is recognized in
3711 // compile_return().
3712 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3713 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003714 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715
Bram Moolenaar648594e2021-07-11 17:55:01 +02003716#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003717 // When the outer function is compiled for profiling, the lambda may be
3718 // called without profiling. Compile it here in the right context.
3719 if (cctx->ctx_compile_type == CT_PROFILE)
3720 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003721#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003722
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003723 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3724 // points into it. Point to the original line to avoid a dangling pointer.
3725 if (evalarg.eval_tofree_cmdline != NULL)
3726 {
3727 size_t off = *arg - evalarg.eval_tofree_cmdline;
3728
3729 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3730 + off;
3731 }
3732
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003733 clear_evalarg(&evalarg, NULL);
3734
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003735 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003736 {
3737 // The return type will now be known.
3738 set_function_type(ufunc);
3739
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003740 // The function reference count will be 1. When the ISN_FUNCREF
3741 // instruction is deleted the reference count is decremented and the
3742 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003743 return generate_FUNCREF(cctx, ufunc);
3744 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003745
3746 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 return FAIL;
3748}
3749
3750/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003751 * Get a lambda and compile it. Uses Vim9 syntax.
3752 */
3753 int
3754get_lambda_tv_and_compile(
3755 char_u **arg,
3756 typval_T *rettv,
3757 int types_optional,
3758 evalarg_T *evalarg)
3759{
3760 int r;
3761 ufunc_T *ufunc;
3762 int save_sc_version = current_sctx.sc_version;
3763
3764 // Get the funcref in "rettv".
3765 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3766 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3767 current_sctx.sc_version = save_sc_version;
3768 if (r != OK)
3769 return r;
3770
3771 // "rettv" will now be a partial referencing the function.
3772 ufunc = rettv->vval.v_partial->pt_func;
3773
3774 // Compile it here to get the return type. The return type is optional,
3775 // when it's missing use t_unknown. This is recognized in
3776 // compile_return().
3777 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3778 ufunc->uf_ret_type = &t_unknown;
3779 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3780
3781 if (ufunc->uf_def_status == UF_COMPILED)
3782 {
3783 // The return type will now be known.
3784 set_function_type(ufunc);
3785 return OK;
3786 }
3787 clear_tv(rettv);
3788 return FAIL;
3789}
3790
3791/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003792 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003794 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 */
3796 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003797compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798{
3799 garray_T *instr = &cctx->ctx_instr;
3800 int count = 0;
3801 dict_T *d = dict_alloc();
3802 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003803 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003804 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003805 int is_const;
3806 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 if (d == NULL)
3809 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003810 if (generate_ppconst(cctx, ppconst) == FAIL)
3811 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003812 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003814 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815
Bram Moolenaar23c55272020-06-21 16:58:13 +02003816 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003817 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003818 *arg = NULL;
3819 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003820 }
3821
3822 if (**arg == '}')
3823 break;
3824
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003825 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003827 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003829 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003830 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003831 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003834 if (isn->isn_type == ISN_PUSHNR)
3835 {
3836 char buf[NUMBUFLEN];
3837
3838 // Convert to string at compile time.
3839 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3840 isn->isn_type = ISN_PUSHS;
3841 isn->isn_arg.string = vim_strsave((char_u *)buf);
3842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 if (isn->isn_type == ISN_PUSHS)
3844 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003845 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003846 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003847 *arg = skipwhite(*arg);
3848 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003849 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003850 emsg(_(e_missing_matching_bracket_after_dict_key));
3851 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003852 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003853 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003855 else
3856 {
3857 // {"name": value},
3858 // {'name': value},
3859 // {name: value} use "name" as a literal key
3860 key = get_literal_key(arg);
3861 if (key == NULL)
3862 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003863 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003864 return FAIL;
3865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866
3867 // Check for duplicate keys, if using string keys.
3868 if (key != NULL)
3869 {
3870 item = dict_find(d, key, -1);
3871 if (item != NULL)
3872 {
3873 semsg(_(e_duplicate_key), key);
3874 goto failret;
3875 }
3876 item = dictitem_alloc(key);
3877 if (item != NULL)
3878 {
3879 item->di_tv.v_type = VAR_UNKNOWN;
3880 item->di_tv.v_lock = 0;
3881 if (dict_add(d, item) == FAIL)
3882 dictitem_free(item);
3883 }
3884 }
3885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 if (**arg != ':')
3887 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003888 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003889 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003890 else
3891 semsg(_(e_missing_dict_colon), *arg);
3892 return FAIL;
3893 }
3894 whitep = *arg + 1;
3895 if (!IS_WHITE_OR_NUL(*whitep))
3896 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003897 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 return FAIL;
3899 }
3900
Bram Moolenaar23c55272020-06-21 16:58:13 +02003901 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003902 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003903 *arg = NULL;
3904 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003905 }
3906
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003907 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003909 if (!is_const)
3910 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 ++count;
3912
Bram Moolenaar2c330432020-04-13 14:41:35 +02003913 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003914 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003915 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003916 *arg = NULL;
3917 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 if (**arg == '}')
3920 break;
3921 if (**arg != ',')
3922 {
3923 semsg(_(e_missing_dict_comma), *arg);
3924 goto failret;
3925 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003926 if (IS_WHITE_OR_NUL(*whitep))
3927 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003928 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003929 return FAIL;
3930 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003931 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003932 if (!IS_WHITE_OR_NUL(*whitep))
3933 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003934 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003935 return FAIL;
3936 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003937 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 }
3939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 *arg = *arg + 1;
3941
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003942 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003943 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003944 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003945 *arg += STRLEN(*arg);
3946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003948 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return generate_NEWDICT(cctx, count);
3950
3951failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003952 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003953 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003954 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003955 *arg = (char_u *)"";
3956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 dict_unref(d);
3958 return FAIL;
3959}
3960
3961/*
3962 * Compile "&option".
3963 */
3964 static int
3965compile_get_option(char_u **arg, cctx_T *cctx)
3966{
3967 typval_T rettv;
3968 char_u *start = *arg;
3969 int ret;
3970
3971 // parse the option and get the current value to get the type.
3972 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003973 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 if (ret == OK)
3975 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003976 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003977 char_u *name = vim_strnsave(start, *arg - start);
3978 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3979 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980
3981 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3982 vim_free(name);
3983 }
3984 clear_tv(&rettv);
3985
3986 return ret;
3987}
3988
3989/*
3990 * Compile "$VAR".
3991 */
3992 static int
3993compile_get_env(char_u **arg, cctx_T *cctx)
3994{
3995 char_u *start = *arg;
3996 int len;
3997 int ret;
3998 char_u *name;
3999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 ++*arg;
4001 len = get_env_len(arg);
4002 if (len == 0)
4003 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004004 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 return FAIL;
4006 }
4007
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004008 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 name = vim_strnsave(start, len + 1);
4010 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4011 vim_free(name);
4012 return ret;
4013}
4014
4015/*
4016 * Compile "@r".
4017 */
4018 static int
4019compile_get_register(char_u **arg, cctx_T *cctx)
4020{
4021 int ret;
4022
4023 ++*arg;
4024 if (**arg == NUL)
4025 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004026 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 return FAIL;
4028 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004029 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 {
4031 emsg_invreg(**arg);
4032 return FAIL;
4033 }
4034 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4035 ++*arg;
4036 return ret;
4037}
4038
4039/*
4040 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004041 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 */
4043 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004044apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004046 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047
4048 // this works from end to start
4049 while (p > start)
4050 {
4051 --p;
4052 if (*p == '-' || *p == '+')
4053 {
4054 // only '-' has an effect, for '+' we only check the type
4055#ifdef FEAT_FLOAT
4056 if (rettv->v_type == VAR_FLOAT)
4057 {
4058 if (*p == '-')
4059 rettv->vval.v_float = -rettv->vval.v_float;
4060 }
4061 else
4062#endif
4063 {
4064 varnumber_T val;
4065 int error = FALSE;
4066
4067 // tv_get_number_chk() accepts a string, but we don't want that
4068 // here
4069 if (check_not_string(rettv) == FAIL)
4070 return FAIL;
4071 val = tv_get_number_chk(rettv, &error);
4072 clear_tv(rettv);
4073 if (error)
4074 return FAIL;
4075 if (*p == '-')
4076 val = -val;
4077 rettv->v_type = VAR_NUMBER;
4078 rettv->vval.v_number = val;
4079 }
4080 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004081 else if (numeric_only)
4082 {
4083 ++p;
4084 break;
4085 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004086 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 {
4088 int v = tv2bool(rettv);
4089
4090 // '!' is permissive in the type.
4091 clear_tv(rettv);
4092 rettv->v_type = VAR_BOOL;
4093 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4094 }
4095 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004096 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return OK;
4098}
4099
4100/*
4101 * Recognize v: variables that are constants and set "rettv".
4102 */
4103 static void
4104get_vim_constant(char_u **arg, typval_T *rettv)
4105{
4106 if (STRNCMP(*arg, "v:true", 6) == 0)
4107 {
4108 rettv->v_type = VAR_BOOL;
4109 rettv->vval.v_number = VVAL_TRUE;
4110 *arg += 6;
4111 }
4112 else if (STRNCMP(*arg, "v:false", 7) == 0)
4113 {
4114 rettv->v_type = VAR_BOOL;
4115 rettv->vval.v_number = VVAL_FALSE;
4116 *arg += 7;
4117 }
4118 else if (STRNCMP(*arg, "v:null", 6) == 0)
4119 {
4120 rettv->v_type = VAR_SPECIAL;
4121 rettv->vval.v_number = VVAL_NULL;
4122 *arg += 6;
4123 }
4124 else if (STRNCMP(*arg, "v:none", 6) == 0)
4125 {
4126 rettv->v_type = VAR_SPECIAL;
4127 rettv->vval.v_number = VVAL_NONE;
4128 *arg += 6;
4129 }
4130}
4131
Bram Moolenaar657137c2021-01-09 15:45:23 +01004132 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004133get_compare_type(char_u *p, int *len, int *type_is)
4134{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004135 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004136 int i;
4137
4138 switch (p[0])
4139 {
4140 case '=': if (p[1] == '=')
4141 type = EXPR_EQUAL;
4142 else if (p[1] == '~')
4143 type = EXPR_MATCH;
4144 break;
4145 case '!': if (p[1] == '=')
4146 type = EXPR_NEQUAL;
4147 else if (p[1] == '~')
4148 type = EXPR_NOMATCH;
4149 break;
4150 case '>': if (p[1] != '=')
4151 {
4152 type = EXPR_GREATER;
4153 *len = 1;
4154 }
4155 else
4156 type = EXPR_GEQUAL;
4157 break;
4158 case '<': if (p[1] != '=')
4159 {
4160 type = EXPR_SMALLER;
4161 *len = 1;
4162 }
4163 else
4164 type = EXPR_SEQUAL;
4165 break;
4166 case 'i': if (p[1] == 's')
4167 {
4168 // "is" and "isnot"; but not a prefix of a name
4169 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4170 *len = 5;
4171 i = p[*len];
4172 if (!isalnum(i) && i != '_')
4173 {
4174 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4175 *type_is = TRUE;
4176 }
4177 }
4178 break;
4179 }
4180 return type;
4181}
4182
4183/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004184 * Skip over an expression, ignoring most errors.
4185 */
4186 static void
4187skip_expr_cctx(char_u **arg, cctx_T *cctx)
4188{
4189 evalarg_T evalarg;
4190
4191 CLEAR_FIELD(evalarg);
4192 evalarg.eval_cctx = cctx;
4193 skip_expr(arg, &evalarg);
4194}
4195
4196/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004198 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 */
4200 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004201compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004203 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204
4205 // this works from end to start
4206 while (p > start)
4207 {
4208 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004209 while (VIM_ISWHITE(*p))
4210 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 if (*p == '-' || *p == '+')
4212 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004213 int negate = *p == '-';
4214 isn_T *isn;
4215 garray_T *stack = &cctx->ctx_type_stack;
4216 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004218 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4219 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4220 return FAIL;
4221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4223 {
4224 --p;
4225 if (*p == '-')
4226 negate = !negate;
4227 }
4228 // only '-' has an effect, for '+' we only check the type
4229 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004230 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004232 if (isn == NULL)
4233 return FAIL;
4234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004236 else if (numeric_only)
4237 {
4238 ++p;
4239 break;
4240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 else
4242 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004243 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004245 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004247 if (p[-1] == '!')
4248 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004251 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 return FAIL;
4253 }
4254 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004255 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 return OK;
4257}
4258
4259/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004260 * Compile "(expression)": recursive!
4261 * Return FAIL/OK.
4262 */
4263 static int
4264compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4265{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004266 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004267 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004268
Bram Moolenaar24156692021-01-14 20:35:49 +01004269 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4270 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004271 if (ppconst->pp_used <= PPSIZE - 10)
4272 {
4273 ret = compile_expr1(arg, cctx, ppconst);
4274 }
4275 else
4276 {
4277 // Not enough space in ppconst, flush constants.
4278 if (generate_ppconst(cctx, ppconst) == FAIL)
4279 return FAIL;
4280 ret = compile_expr0(arg, cctx);
4281 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004282 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4283 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004284 if (**arg == ')')
4285 ++*arg;
4286 else if (ret == OK)
4287 {
4288 emsg(_(e_missing_close));
4289 ret = FAIL;
4290 }
4291 return ret;
4292}
4293
4294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004296 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 */
4298 static int
4299compile_subscript(
4300 char_u **arg,
4301 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004302 char_u *start_leader,
4303 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004304 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004306 char_u *name_start = *end_leader;
4307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 for (;;)
4309 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004310 char_u *p = skipwhite(*arg);
4311
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004312 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004313 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004314 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004315
4316 // If a following line starts with "->{" or "->X" advance to that
4317 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004318 // Also if a following line starts with ".x".
4319 if (next != NULL &&
4320 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004321 && (next[2] == '{'
4322 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004323 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004324 {
4325 next = next_line_from_context(cctx, TRUE);
4326 if (next == NULL)
4327 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004328 *arg = next;
4329 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004330 }
4331 }
4332
Bram Moolenaarcd268012021-07-22 19:11:08 +02004333 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4334 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004335 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004337 garray_T *stack = &cctx->ctx_type_stack;
4338 type_T *type;
4339 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004341 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004342 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004343 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004346 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4347
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004348 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004349 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004351 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 return FAIL;
4353 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004354 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004356 char_u *pstart = p;
4357
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004358 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004359 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004360 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 // something->method()
4363 // Apply the '!', '-' and '+' first:
4364 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004365 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004368 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004369 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004370 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004371 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004372 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004373 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004374 garray_T *stack = &cctx->ctx_type_stack;
4375 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004376 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004377 int expr_isn_start = cctx->ctx_instr.ga_len;
4378 int expr_isn_end;
4379 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004380
4381 // Funcref call: list->(Refs[2])(arg)
4382 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004383 //
4384 // Fist compile the function expression.
4385 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004386 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004387
4388 // Remember the next instruction index, where the instructions
4389 // for arguments are being written.
4390 expr_isn_end = cctx->ctx_instr.ga_len;
4391
4392 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004393 if (**arg != '(')
4394 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004395 if (*skipwhite(*arg) == '(')
4396 emsg(_(e_nowhitespace));
4397 else
4398 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004399 return FAIL;
4400 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004401 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004402 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004403 return FAIL;
4404
Bram Moolenaar2927c072021-04-05 19:41:21 +02004405 // Move the instructions for the arguments to before the
4406 // instructions of the expression and move the type of the
4407 // expression after the argument types. This is what ISN_PCALL
4408 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004409 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004410 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4411 if (arg_isn_count > 0)
4412 {
4413 int expr_isn_count = expr_isn_end - expr_isn_start;
4414 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4415
4416 if (isn == NULL)
4417 return FAIL;
4418 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4419 + expr_isn_start,
4420 sizeof(isn_T) * expr_isn_count);
4421 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4422 + expr_isn_start,
4423 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4424 sizeof(isn_T) * arg_isn_count);
4425 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4426 + expr_isn_start + arg_isn_count,
4427 isn, sizeof(isn_T) * expr_isn_count);
4428 vim_free(isn);
4429
4430 type = ((type_T **)stack->ga_data)[type_idx_start];
4431 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4432 ((type_T **)stack->ga_data) + type_idx_start + 1,
4433 sizeof(type_T *)
4434 * (stack->ga_len - type_idx_start - 1));
4435 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4436 }
4437
Bram Moolenaar7e368202020-12-25 21:56:57 +01004438 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004439 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 return FAIL;
4441 }
4442 else
4443 {
4444 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004445 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004446 if (!eval_isnamec1(*p))
4447 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004448 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004449 return FAIL;
4450 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004451 if (ASCII_ISALPHA(*p) && p[1] == ':')
4452 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004453 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 ;
4455 if (*p != '(')
4456 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004457 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 return FAIL;
4459 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004460 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 return FAIL;
4462 }
4463 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004464 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004466 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004467
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004468 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004469 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004470 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004471 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004472 // TODO: more arguments
4473 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004474 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004475 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004476 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004477
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004478 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004479 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004480 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004481 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004482 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004483 // missing first index is equal to zero
4484 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004485 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004486 else
4487 {
4488 if (compile_expr0(arg, cctx) == FAIL)
4489 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004490 if (**arg == ':')
4491 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004492 semsg(_(e_white_space_required_before_and_after_str_at_str),
4493 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004494 return FAIL;
4495 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004496 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004497 return FAIL;
4498 *arg = skipwhite(*arg);
4499 }
4500 if (**arg == ':')
4501 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004502 is_slice = TRUE;
4503 ++*arg;
4504 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4505 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004506 semsg(_(e_white_space_required_before_and_after_str_at_str),
4507 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004508 return FAIL;
4509 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004510 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004511 return FAIL;
4512 if (**arg == ']')
4513 // missing second index is equal to end of string
4514 generate_PUSHNR(cctx, -1);
4515 else
4516 {
4517 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004518 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004519 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004520 return FAIL;
4521 *arg = skipwhite(*arg);
4522 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524
4525 if (**arg != ']')
4526 {
4527 emsg(_(e_missbrac));
4528 return FAIL;
4529 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004530 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531
Bram Moolenaare42939a2021-04-05 17:11:17 +02004532 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004533 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004535 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004537 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004538 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004539 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004540 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004541
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004542 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004543 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004544 {
4545 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004546 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004547 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004548 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004549 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 while (eval_isnamec(*p))
4551 MB_PTR_ADV(p);
4552 if (p == *arg)
4553 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004554 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 return FAIL;
4556 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004557 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 return FAIL;
4559 *arg = p;
4560 }
4561 else
4562 break;
4563 }
4564
4565 // TODO - see handle_subscript():
4566 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4567 // Don't do this when "Func" is already a partial that was bound
4568 // explicitly (pt_auto is FALSE).
4569
4570 return OK;
4571}
4572
4573/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004574 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4575 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004577 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004578 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004579 *
4580 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 */
4582
4583/*
4584 * number number constant
4585 * 0zFFFFFFFF Blob constant
4586 * "string" string constant
4587 * 'string' literal string constant
4588 * &option-name option value
4589 * @r register contents
4590 * identifier variable value
4591 * function() function call
4592 * $VAR environment variable
4593 * (expression) nested expression
4594 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004595 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 *
4597 * Also handle:
4598 * ! in front logical NOT
4599 * - in front unary minus
4600 * + in front unary plus (ignored)
4601 * trailing (arg) funcref/partial call
4602 * trailing [] subscript in String or List
4603 * trailing .name entry in Dictionary
4604 * trailing ->name() method call
4605 */
4606 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004607compile_expr7(
4608 char_u **arg,
4609 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004610 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 char_u *start_leader, *end_leader;
4613 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004614 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004615 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004617 ppconst->pp_is_const = FALSE;
4618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 /*
4620 * Skip '!', '-' and '+' characters. They are handled later.
4621 */
4622 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004623 if (eval_leader(arg, TRUE) == FAIL)
4624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 end_leader = *arg;
4626
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004627 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 switch (**arg)
4629 {
4630 /*
4631 * Number constant.
4632 */
4633 case '0': // also for blob starting with 0z
4634 case '1':
4635 case '2':
4636 case '3':
4637 case '4':
4638 case '5':
4639 case '6':
4640 case '7':
4641 case '8':
4642 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004643 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004645 // Apply "-" and "+" just before the number now, right to
4646 // left. Matters especially when "->" follows. Stops at
4647 // '!'.
4648 if (apply_leader(rettv, TRUE,
4649 start_leader, &end_leader) == FAIL)
4650 {
4651 clear_tv(rettv);
4652 return FAIL;
4653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 break;
4655
4656 /*
4657 * String constant: "string".
4658 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004659 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 return FAIL;
4661 break;
4662
4663 /*
4664 * Literal string constant: 'str''ing'.
4665 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004666 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 return FAIL;
4668 break;
4669
4670 /*
4671 * Constant Vim variable.
4672 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004673 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 ret = NOTDONE;
4675 break;
4676
4677 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004678 * "true" constant
4679 */
4680 case 't': if (STRNCMP(*arg, "true", 4) == 0
4681 && !eval_isnamec((*arg)[4]))
4682 {
4683 *arg += 4;
4684 rettv->v_type = VAR_BOOL;
4685 rettv->vval.v_number = VVAL_TRUE;
4686 }
4687 else
4688 ret = NOTDONE;
4689 break;
4690
4691 /*
4692 * "false" constant
4693 */
4694 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4695 && !eval_isnamec((*arg)[5]))
4696 {
4697 *arg += 5;
4698 rettv->v_type = VAR_BOOL;
4699 rettv->vval.v_number = VVAL_FALSE;
4700 }
4701 else
4702 ret = NOTDONE;
4703 break;
4704
4705 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004706 * "null" constant
4707 */
4708 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004709 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004710 {
4711 *arg += 4;
4712 rettv->v_type = VAR_SPECIAL;
4713 rettv->vval.v_number = VVAL_NULL;
4714 }
4715 else
4716 ret = NOTDONE;
4717 break;
4718
4719 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 * List: [expr, expr]
4721 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004722 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4723 return FAIL;
4724 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 break;
4726
4727 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 * Dictionary: {'key': val, 'key': val}
4729 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004730 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4731 return FAIL;
4732 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 break;
4734
4735 /*
4736 * Option value: &name
4737 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004738 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4739 return FAIL;
4740 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 break;
4742
4743 /*
4744 * Environment variable: $VAR.
4745 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004746 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4747 return FAIL;
4748 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 break;
4750
4751 /*
4752 * Register contents: @r.
4753 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004754 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4755 return FAIL;
4756 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 break;
4758 /*
4759 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004760 * lambda: (arg, arg) => expr
4761 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004763 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4764 ret = compile_lambda(arg, cctx);
4765 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004766 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 break;
4768
4769 default: ret = NOTDONE;
4770 break;
4771 }
4772 if (ret == FAIL)
4773 return FAIL;
4774
Bram Moolenaar1c747212020-05-09 18:28:34 +02004775 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004777 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 clear_tv(rettv);
4779 else
4780 // A constant expression can possibly be handled compile time,
4781 // return the value instead of generating code.
4782 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 }
4784 else if (ret == NOTDONE)
4785 {
4786 char_u *p;
4787 int r;
4788
4789 if (!eval_isnamec1(**arg))
4790 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004791 if (!vim9_bad_comment(*arg))
4792 {
4793 if (ends_excmd(*skipwhite(*arg)))
4794 semsg(_(e_empty_expression_str), *arg);
4795 else
4796 semsg(_(e_name_expected_str), *arg);
4797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 return FAIL;
4799 }
4800
4801 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004802 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004803 if (p - *arg == (size_t)1 && **arg == '_')
4804 {
4805 emsg(_(e_cannot_use_underscore_here));
4806 return FAIL;
4807 }
4808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004810 {
4811 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004814 {
4815 if (generate_ppconst(cctx, ppconst) == FAIL)
4816 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004817 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 if (r == FAIL)
4820 return FAIL;
4821 }
4822
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004823 // Handle following "[]", ".member", etc.
4824 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004825 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004826 ppconst) == FAIL)
4827 return FAIL;
4828 if (ppconst->pp_used > 0)
4829 {
4830 // apply the '!', '-' and '+' before the constant
4831 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004832 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004833 return FAIL;
4834 return OK;
4835 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004836 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004838 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839}
4840
4841/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004842 * Give the "white on both sides" error, taking the operator from "p[len]".
4843 */
4844 void
4845error_white_both(char_u *op, int len)
4846{
4847 char_u buf[10];
4848
4849 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004850 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004851}
4852
4853/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004854 * <type>expr7: runtime type check / conversion
4855 */
4856 static int
4857compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4858{
4859 type_T *want_type = NULL;
4860
4861 // Recognize <type>
4862 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4863 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004864 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004865 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4866 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004867 return FAIL;
4868
4869 if (**arg != '>')
4870 {
4871 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004872 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004873 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004874 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004875 return FAIL;
4876 }
4877 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004878 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004879 return FAIL;
4880 }
4881
4882 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4883 return FAIL;
4884
4885 if (want_type != NULL)
4886 {
4887 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004888 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004889 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004890
Bram Moolenaard1103582020-08-14 22:44:25 +02004891 generate_ppconst(cctx, ppconst);
4892 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004893 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004894 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004895 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004896 return FAIL;
4897 }
4898 }
4899
4900 return OK;
4901}
4902
4903/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 * * number multiplication
4905 * / number division
4906 * % number modulo
4907 */
4908 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004909compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910{
4911 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004912 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004913 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004915 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004916 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 return FAIL;
4918
4919 /*
4920 * Repeat computing, until no "*", "/" or "%" is following.
4921 */
4922 for (;;)
4923 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004924 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 if (*op != '*' && *op != '/' && *op != '%')
4926 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004927 if (next != NULL)
4928 {
4929 *arg = next_line_from_context(cctx, TRUE);
4930 op = skipwhite(*arg);
4931 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004932
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004933 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004935 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004936 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004938 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004939 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004941 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004942 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004944
4945 if (ppconst->pp_used == ppconst_used + 2
4946 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4947 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004948 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004949 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4950 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4951 varnumber_T res = 0;
4952 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004954 // both are numbers: compute the result
4955 switch (*op)
4956 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004957 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004958 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004959 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004960 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004961 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004962 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004963 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004964 break;
4965 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004966 if (failed)
4967 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004968 tv1->vval.v_number = res;
4969 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004970 }
4971 else
4972 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004973 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004974 generate_two_op(cctx, op);
4975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 }
4977
4978 return OK;
4979}
4980
4981/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004982 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 * - number subtraction
4984 * .. string concatenation
4985 */
4986 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004987compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988{
4989 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004990 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004992 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993
4994 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004995 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 return FAIL;
4997
4998 /*
4999 * Repeat computing, until no "+", "-" or ".." is following.
5000 */
5001 for (;;)
5002 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005003 op = may_peek_next_line(cctx, *arg, &next);
5004 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005006 if (op[0] == op[1] && *op != '.' && next)
5007 // Finding "++" or "--" on the next line is a separate command.
5008 // But ".." is concatenation.
5009 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005011 if (next != NULL)
5012 {
5013 *arg = next_line_from_context(cctx, TRUE);
5014 op = skipwhite(*arg);
5015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005017 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005019 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005020 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 }
5022
Bram Moolenaare0de1712020-12-02 17:36:54 +01005023 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005024 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005026 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 return FAIL;
5029
Bram Moolenaara5565e42020-05-09 15:44:01 +02005030 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005031 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005032 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5033 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5034 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5035 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005037 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5038 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005039
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005040 // concat/subtract/add constant numbers
5041 if (*op == '+')
5042 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5043 else if (*op == '-')
5044 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5045 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005046 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005047 // concatenate constant strings
5048 char_u *s1 = tv1->vval.v_string;
5049 char_u *s2 = tv2->vval.v_string;
5050 size_t len1 = STRLEN(s1);
5051
5052 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5053 if (tv1->vval.v_string == NULL)
5054 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005055 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005056 return FAIL;
5057 }
5058 mch_memmove(tv1->vval.v_string, s1, len1);
5059 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005060 vim_free(s1);
5061 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005062 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005063 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 }
5065 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005066 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005067 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005068 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005069 if (*op == '.')
5070 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005071 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5072 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005073 return FAIL;
5074 generate_instr_drop(cctx, ISN_CONCAT, 1);
5075 }
5076 else
5077 generate_two_op(cctx, op);
5078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 }
5080
5081 return OK;
5082}
5083
5084/*
5085 * expr5a == expr5b
5086 * expr5a =~ expr5b
5087 * expr5a != expr5b
5088 * expr5a !~ expr5b
5089 * expr5a > expr5b
5090 * expr5a >= expr5b
5091 * expr5a < expr5b
5092 * expr5a <= expr5b
5093 * expr5a is expr5b
5094 * expr5a isnot expr5b
5095 *
5096 * Produces instructions:
5097 * EVAL expr5a Push result of "expr5a"
5098 * EVAL expr5b Push result of "expr5b"
5099 * COMPARE one of the compare instructions
5100 */
5101 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005102compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005104 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005106 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005109 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110
5111 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005112 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 return FAIL;
5114
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005115 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005116 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117
5118 /*
5119 * If there is a comparative operator, use it.
5120 */
5121 if (type != EXPR_UNKNOWN)
5122 {
5123 int ic = FALSE; // Default: do not ignore case
5124
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005125 if (next != NULL)
5126 {
5127 *arg = next_line_from_context(cctx, TRUE);
5128 p = skipwhite(*arg);
5129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 if (type_is && (p[len] == '?' || p[len] == '#'))
5131 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005132 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 return FAIL;
5134 }
5135 // extra question mark appended: ignore case
5136 if (p[len] == '?')
5137 {
5138 ic = TRUE;
5139 ++len;
5140 }
5141 // extra '#' appended: match case (ignored)
5142 else if (p[len] == '#')
5143 ++len;
5144 // nothing appended: match case
5145
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005146 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005148 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005149 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150 }
5151
5152 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005153 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005154 return FAIL;
5155
Bram Moolenaara5565e42020-05-09 15:44:01 +02005156 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157 return FAIL;
5158
Bram Moolenaara5565e42020-05-09 15:44:01 +02005159 if (ppconst->pp_used == ppconst_used + 2)
5160 {
5161 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5162 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5163 int ret;
5164
5165 // Both sides are a constant, compute the result now.
5166 // First check for a valid combination of types, this is more
5167 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005168 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005169 ret = FAIL;
5170 else
5171 {
5172 ret = typval_compare(tv1, tv2, type, ic);
5173 tv1->v_type = VAR_BOOL;
5174 tv1->vval.v_number = tv1->vval.v_number
5175 ? VVAL_TRUE : VVAL_FALSE;
5176 clear_tv(tv2);
5177 --ppconst->pp_used;
5178 }
5179 return ret;
5180 }
5181
5182 generate_ppconst(cctx, ppconst);
5183 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 }
5185
5186 return OK;
5187}
5188
Bram Moolenaar7f141552020-05-09 17:35:53 +02005189static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191/*
5192 * Compile || or &&.
5193 */
5194 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005195compile_and_or(
5196 char_u **arg,
5197 cctx_T *cctx,
5198 char *op,
5199 ppconst_T *ppconst,
5200 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005202 char_u *next;
5203 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 int opchar = *op;
5205
5206 if (p[0] == opchar && p[1] == opchar)
5207 {
5208 garray_T *instr = &cctx->ctx_instr;
5209 garray_T end_ga;
5210
5211 /*
5212 * Repeat until there is no following "||" or "&&"
5213 */
5214 ga_init2(&end_ga, sizeof(int), 10);
5215 while (p[0] == opchar && p[1] == opchar)
5216 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005217 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005218 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005219 int start_ctx_lnum = cctx->ctx_lnum;
5220 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005221 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005222
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005223 if (next != NULL)
5224 {
5225 *arg = next_line_from_context(cctx, TRUE);
5226 p = skipwhite(*arg);
5227 }
5228
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005229 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5230 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005231 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005232 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005233 return FAIL;
5234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005236 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005237 SOURCING_LNUM = start_lnum;
5238 save_lnum = cctx->ctx_lnum;
5239 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005240
5241 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005242 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005243 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005244 // TODO: use ppconst if the value is a constant
5245 generate_ppconst(cctx, ppconst);
5246
5247 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005248 status = bool_on_stack(cctx);
5249 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005250 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005251 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005252 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005253 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 {
5255 ga_clear(&end_ga);
5256 return FAIL;
5257 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5260 ++end_ga.ga_len;
5261 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005262 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263
5264 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005265 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005266 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005267 {
5268 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005269 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005270 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005271
Bram Moolenaara5565e42020-05-09 15:44:01 +02005272 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5273 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 {
5275 ga_clear(&end_ga);
5276 return FAIL;
5277 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005278
5279 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005280 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005281
5282 if (check_ppconst_bool(ppconst) == FAIL)
5283 {
5284 ga_clear(&end_ga);
5285 return FAIL;
5286 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005287 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005289 // Every part must evaluate to a bool.
5290 if (bool_on_stack(cctx) == FAIL)
5291 {
5292 ga_clear(&end_ga);
5293 return FAIL;
5294 }
5295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 // Fill in the end label in all jumps.
5297 while (end_ga.ga_len > 0)
5298 {
5299 isn_T *isn;
5300
5301 --end_ga.ga_len;
5302 isn = ((isn_T *)instr->ga_data)
5303 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5304 isn->isn_arg.jump.jump_where = instr->ga_len;
5305 }
5306 ga_clear(&end_ga);
5307 }
5308
5309 return OK;
5310}
5311
5312/*
5313 * expr4a && expr4a && expr4a logical AND
5314 *
5315 * Produces instructions:
5316 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005317 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005318 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005320 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321 * EVAL expr4c Push result of "expr4c"
5322 * end:
5323 */
5324 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005325compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005327 int ppconst_used = ppconst->pp_used;
5328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005330 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 return FAIL;
5332
5333 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005334 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335}
5336
5337/*
5338 * expr3a || expr3b || expr3c logical OR
5339 *
5340 * Produces instructions:
5341 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005342 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005343 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005345 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 * EVAL expr3c Push result of "expr3c"
5347 * end:
5348 */
5349 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005350compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005352 int ppconst_used = ppconst->pp_used;
5353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005355 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356 return FAIL;
5357
5358 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005359 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360}
5361
5362/*
5363 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005365 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005366 * JUMP_IF_FALSE alt jump if false
5367 * EVAL expr1a
5368 * JUMP_ALWAYS end
5369 * alt: EVAL expr1b
5370 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005371 *
5372 * Toplevel expression: expr2 ?? expr1
5373 * Produces instructions:
5374 * EVAL expr2 Push result of "expr2"
5375 * JUMP_AND_KEEP_IF_TRUE end jump if true
5376 * EVAL expr1
5377 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 */
5379 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005380compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005381{
5382 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005383 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005384 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385
Bram Moolenaar3988f642020-08-27 22:43:03 +02005386 // Ignore all kinds of errors when not producing code.
5387 if (cctx->ctx_skip == SKIP_YES)
5388 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005389 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005390 return OK;
5391 }
5392
Bram Moolenaar61a89812020-05-07 16:58:17 +02005393 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005394 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 return FAIL;
5396
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005397 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398 if (*p == '?')
5399 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005400 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401 garray_T *instr = &cctx->ctx_instr;
5402 garray_T *stack = &cctx->ctx_type_stack;
5403 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005404 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005406 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005407 int has_const_expr = FALSE;
5408 int const_value = FALSE;
5409 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005411 if (next != NULL)
5412 {
5413 *arg = next_line_from_context(cctx, TRUE);
5414 p = skipwhite(*arg);
5415 }
5416
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005417 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005418 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005419 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005420 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005421 return FAIL;
5422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423
Bram Moolenaara5565e42020-05-09 15:44:01 +02005424 if (ppconst->pp_used == ppconst_used + 1)
5425 {
5426 // the condition is a constant, we know whether the ? or the :
5427 // expression is to be evaluated.
5428 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005429 if (op_falsy)
5430 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5431 else
5432 {
5433 int error = FALSE;
5434
5435 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5436 &error);
5437 if (error)
5438 return FAIL;
5439 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005440 cctx->ctx_skip = save_skip == SKIP_YES ||
5441 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5442
5443 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5444 // "left ?? right" and "left" is truthy: produce "left"
5445 generate_ppconst(cctx, ppconst);
5446 else
5447 {
5448 clear_tv(&ppconst->pp_tv[ppconst_used]);
5449 --ppconst->pp_used;
5450 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005451 }
5452 else
5453 {
5454 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005455 if (op_falsy)
5456 end_idx = instr->ga_len;
5457 generate_JUMP(cctx, op_falsy
5458 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5459 if (op_falsy)
5460 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462
5463 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005464 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005465 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005466 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005467 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468
Bram Moolenaara5565e42020-05-09 15:44:01 +02005469 if (!has_const_expr)
5470 {
5471 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005473 if (!op_falsy)
5474 {
5475 // remember the type and drop it
5476 --stack->ga_len;
5477 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005479 end_idx = instr->ga_len;
5480 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005482 // jump here from JUMP_IF_FALSE
5483 isn = ((isn_T *)instr->ga_data) + alt_idx;
5484 isn->isn_arg.jump.jump_where = instr->ga_len;
5485 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005486 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005488 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005490 // Check for the ":".
5491 p = may_peek_next_line(cctx, *arg, &next);
5492 if (*p != ':')
5493 {
5494 emsg(_(e_missing_colon));
5495 return FAIL;
5496 }
5497 if (next != NULL)
5498 {
5499 *arg = next_line_from_context(cctx, TRUE);
5500 p = skipwhite(*arg);
5501 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005502
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005503 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5504 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005505 semsg(_(e_white_space_required_before_and_after_str_at_str),
5506 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005507 return FAIL;
5508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005510 // evaluate the third expression
5511 if (has_const_expr)
5512 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005513 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005514 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005515 return FAIL;
5516 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5517 return FAIL;
5518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519
Bram Moolenaara5565e42020-05-09 15:44:01 +02005520 if (!has_const_expr)
5521 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005522 type_T **typep;
5523
Bram Moolenaara5565e42020-05-09 15:44:01 +02005524 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525
Bram Moolenaara5565e42020-05-09 15:44:01 +02005526 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005527 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5528 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005529
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005530 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005531 isn = ((isn_T *)instr->ga_data) + end_idx;
5532 isn->isn_arg.jump.jump_where = instr->ga_len;
5533 }
5534
5535 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 }
5537 return OK;
5538}
5539
5540/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005541 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005542 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5543 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005544 */
5545 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005546compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005547{
5548 ppconst_T ppconst;
5549
5550 CLEAR_FIELD(ppconst);
5551 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5552 {
5553 clear_ppconst(&ppconst);
5554 return FAIL;
5555 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005556 if (is_const != NULL)
5557 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005558 if (generate_ppconst(cctx, &ppconst) == FAIL)
5559 return FAIL;
5560 return OK;
5561}
5562
5563/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005564 * Toplevel expression.
5565 */
5566 static int
5567compile_expr0(char_u **arg, cctx_T *cctx)
5568{
5569 return compile_expr0_ext(arg, cctx, NULL);
5570}
5571
5572/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005573 * Compile "return [expr]".
5574 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005575 */
5576 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005577compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578{
5579 char_u *p = arg;
5580 garray_T *stack = &cctx->ctx_type_stack;
5581 type_T *stack_type;
5582
5583 if (*p != NUL && *p != '|' && *p != '\n')
5584 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005585 if (legacy)
5586 {
5587 int save_flags = cmdmod.cmod_flags;
5588
5589 generate_LEGACY_EVAL(cctx, p);
5590 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5591 0, cctx, FALSE, FALSE) == FAIL)
5592 return NULL;
5593 cmdmod.cmod_flags |= CMOD_LEGACY;
5594 (void)skip_expr(&p, NULL);
5595 cmdmod.cmod_flags = save_flags;
5596 }
5597 else
5598 {
5599 // compile return argument into instructions
5600 if (compile_expr0(&p, cctx) == FAIL)
5601 return NULL;
5602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005604 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005605 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005606 // "check_return_type" with uf_ret_type set to &t_unknown is used
5607 // for an inline function without a specified return type. Set the
5608 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005609 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005610 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005611 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5612 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005613 || (!check_return_type
5614 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005615 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005616 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005617 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005618 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005619 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005620 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5621 && stack_type->tt_type != VAR_VOID
5622 && stack_type->tt_type != VAR_UNKNOWN)
5623 {
5624 emsg(_(e_returning_value_in_function_without_return_type));
5625 return NULL;
5626 }
5627 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005628 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005629 return NULL;
5630 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632 }
5633 else
5634 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005635 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005636 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005637 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5638 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005640 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 return NULL;
5642 }
5643
5644 // No argument, return zero.
5645 generate_PUSHNR(cctx, 0);
5646 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005647
5648 // Undo any command modifiers.
5649 generate_undo_cmdmods(cctx);
5650
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005651 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 return NULL;
5653
5654 // "return val | endif" is possible
5655 return skipwhite(p);
5656}
5657
5658/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005659 * Get a line from the compilation context, compatible with exarg_T getline().
5660 * Return a pointer to the line in allocated memory.
5661 * Return NULL for end-of-file or some error.
5662 */
5663 static char_u *
5664exarg_getline(
5665 int c UNUSED,
5666 void *cookie,
5667 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005668 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005669{
5670 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005671 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005672
Bram Moolenaar66250c92020-08-20 15:02:42 +02005673 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005674 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005675 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005676 return NULL;
5677 ++cctx->ctx_lnum;
5678 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5679 // Comment lines result in NULL pointers, skip them.
5680 if (p != NULL)
5681 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005682 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005683}
5684
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005685 void
5686fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5687{
5688 eap->getline = exarg_getline;
5689 eap->cookie = cctx;
5690}
5691
Bram Moolenaar04b12692020-05-04 23:24:44 +02005692/*
5693 * Compile a nested :def command.
5694 */
5695 static char_u *
5696compile_nested_function(exarg_T *eap, cctx_T *cctx)
5697{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005698 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005699 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005700 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005701 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005702 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005703 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005704 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005705
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005706 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005707 {
5708 emsg(_(e_cannot_use_bang_with_nested_def));
5709 return NULL;
5710 }
5711
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005712 if (*name_start == '/')
5713 {
5714 name_end = skip_regexp(name_start + 1, '/', TRUE);
5715 if (*name_end == '/')
5716 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005717 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005718 }
5719 if (name_end == name_start || *skipwhite(name_end) != '(')
5720 {
5721 if (!ends_excmd2(name_start, name_end))
5722 {
5723 semsg(_(e_invalid_command_str), eap->cmd);
5724 return NULL;
5725 }
5726
5727 // "def" or "def Name": list functions
5728 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5729 return NULL;
5730 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5731 }
5732
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005733 // Only g:Func() can use a namespace.
5734 if (name_start[1] == ':' && !is_global)
5735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005736 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005737 return NULL;
5738 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005739 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005740 return NULL;
5741
Bram Moolenaar04b12692020-05-04 23:24:44 +02005742 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005743 fill_exarg_from_cctx(eap, cctx);
5744
Bram Moolenaar04b12692020-05-04 23:24:44 +02005745 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005746 lambda_name = vim_strsave(get_lambda_name());
5747 if (lambda_name == NULL)
5748 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005749 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005750
Bram Moolenaar822ba242020-05-24 23:00:18 +02005751 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005752 {
5753 r = eap->skip ? OK : FAIL;
5754 goto theend;
5755 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005756
5757 // copy over the block scope IDs before compiling
5758 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5759 {
5760 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5761
5762 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5763 if (ufunc->uf_block_ids != NULL)
5764 {
5765 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5766 sizeof(int) * block_depth);
5767 ufunc->uf_block_depth = block_depth;
5768 }
5769 }
5770
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005771 compile_type = COMPILE_TYPE(ufunc);
5772#ifdef FEAT_PROFILE
5773 // If the outer function is profiled, also compile the nested function for
5774 // profiling.
5775 if (cctx->ctx_compile_type == CT_PROFILE)
5776 compile_type = CT_PROFILE;
5777#endif
5778 if (func_needs_compiling(ufunc, compile_type)
5779 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005780 {
5781 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005782 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005783 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005784
Bram Moolenaar648594e2021-07-11 17:55:01 +02005785#ifdef FEAT_PROFILE
5786 // When the outer function is compiled for profiling, the nested function
5787 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005788 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005789 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5790#endif
5791
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005792 if (is_global)
5793 {
5794 char_u *func_name = vim_strnsave(name_start + 2,
5795 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005796
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005797 if (func_name == NULL)
5798 r = FAIL;
5799 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005800 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005801 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005802 lambda_name = NULL;
5803 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005804 }
5805 else
5806 {
5807 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005808 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005809 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005810
Bram Moolenaareef21022020-08-01 22:16:43 +02005811 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005812 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005813 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005814 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005815 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5816 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005817
5818theend:
5819 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005820 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005821}
5822
5823/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005824 * Return the length of an assignment operator, or zero if there isn't one.
5825 */
5826 int
5827assignment_len(char_u *p, int *heredoc)
5828{
5829 if (*p == '=')
5830 {
5831 if (p[1] == '<' && p[2] == '<')
5832 {
5833 *heredoc = TRUE;
5834 return 3;
5835 }
5836 return 1;
5837 }
5838 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5839 return 2;
5840 if (STRNCMP(p, "..=", 3) == 0)
5841 return 3;
5842 return 0;
5843}
5844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005846 * Generate the load instruction for "name".
5847 */
5848 static void
5849generate_loadvar(
5850 cctx_T *cctx,
5851 assign_dest_T dest,
5852 char_u *name,
5853 lvar_T *lvar,
5854 type_T *type)
5855{
5856 switch (dest)
5857 {
5858 case dest_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005859 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5860 break;
5861 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005862 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5863 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5864 else
5865 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005866 break;
5867 case dest_buffer:
5868 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5869 break;
5870 case dest_window:
5871 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5872 break;
5873 case dest_tab:
5874 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5875 break;
5876 case dest_script:
5877 compile_load_scriptvar(cctx,
5878 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5879 break;
5880 case dest_env:
5881 // Include $ in the name here
5882 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5883 break;
5884 case dest_reg:
5885 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5886 break;
5887 case dest_vimvar:
5888 generate_LOADV(cctx, name + 2, TRUE);
5889 break;
5890 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005891 if (lvar->lv_from_outer > 0)
5892 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5893 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005894 else
5895 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5896 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005897 case dest_expr:
5898 // list or dict value should already be on the stack.
5899 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005900 }
5901}
5902
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005903/*
5904 * Skip over "[expr]" or ".member".
5905 * Does not check for any errors.
5906 */
5907 static char_u *
5908skip_index(char_u *start)
5909{
5910 char_u *p = start;
5911
5912 if (*p == '[')
5913 {
5914 p = skipwhite(p + 1);
5915 (void)skip_expr(&p, NULL);
5916 p = skipwhite(p);
5917 if (*p == ']')
5918 return p + 1;
5919 return p;
5920 }
5921 // if (*p == '.')
5922 return to_name_end(p + 1, TRUE);
5923}
5924
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005925 void
5926vim9_declare_error(char_u *name)
5927{
5928 char *scope = "";
5929
5930 switch (*name)
5931 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005932 case 'g': scope = _("global"); break;
5933 case 'b': scope = _("buffer"); break;
5934 case 'w': scope = _("window"); break;
5935 case 't': scope = _("tab"); break;
5936 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005937 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005938 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005939 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005940 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005941 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005942 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005943 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005944 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005945 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005946}
5947
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005948/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005949 * For one assignment figure out the type of destination. Return it in "dest".
5950 * When not recognized "dest" is not set.
5951 * For an option "opt_flags" is set.
5952 * For a v:var "vimvaridx" is set.
5953 * "type" is set to the destination type if known, unchanted otherwise.
5954 * Return FAIL if an error message was given.
5955 */
5956 static int
5957get_var_dest(
5958 char_u *name,
5959 assign_dest_T *dest,
5960 int cmdidx,
5961 int *opt_flags,
5962 int *vimvaridx,
5963 type_T **type,
5964 cctx_T *cctx)
5965{
5966 char_u *p;
5967
5968 if (*name == '&')
5969 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005970 int cc;
5971 long numval;
5972 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005973
5974 *dest = dest_option;
5975 if (cmdidx == CMD_final || cmdidx == CMD_const)
5976 {
5977 emsg(_(e_const_option));
5978 return FAIL;
5979 }
5980 p = name;
5981 p = find_option_end(&p, opt_flags);
5982 if (p == NULL)
5983 {
5984 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005985 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005986 return FAIL;
5987 }
5988 cc = *p;
5989 *p = NUL;
5990 opt_type = get_option_value(skip_option_env_lead(name),
5991 &numval, NULL, *opt_flags);
5992 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005993 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005994 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005995 case gov_unknown:
5996 semsg(_(e_unknown_option), name);
5997 return FAIL;
5998 case gov_string:
5999 case gov_hidden_string:
6000 *type = &t_string;
6001 break;
6002 case gov_bool:
6003 case gov_hidden_bool:
6004 *type = &t_bool;
6005 break;
6006 case gov_number:
6007 case gov_hidden_number:
6008 *type = &t_number;
6009 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006010 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006011 }
6012 else if (*name == '$')
6013 {
6014 *dest = dest_env;
6015 *type = &t_string;
6016 }
6017 else if (*name == '@')
6018 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006019 if (name[1] != '@'
6020 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006021 {
6022 emsg_invreg(name[1]);
6023 return FAIL;
6024 }
6025 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006026 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006027 }
6028 else if (STRNCMP(name, "g:", 2) == 0)
6029 {
6030 *dest = dest_global;
6031 }
6032 else if (STRNCMP(name, "b:", 2) == 0)
6033 {
6034 *dest = dest_buffer;
6035 }
6036 else if (STRNCMP(name, "w:", 2) == 0)
6037 {
6038 *dest = dest_window;
6039 }
6040 else if (STRNCMP(name, "t:", 2) == 0)
6041 {
6042 *dest = dest_tab;
6043 }
6044 else if (STRNCMP(name, "v:", 2) == 0)
6045 {
6046 typval_T *vtv;
6047 int di_flags;
6048
6049 *vimvaridx = find_vim_var(name + 2, &di_flags);
6050 if (*vimvaridx < 0)
6051 {
6052 semsg(_(e_variable_not_found_str), name);
6053 return FAIL;
6054 }
6055 // We use the current value of "sandbox" here, is that OK?
6056 if (var_check_ro(di_flags, name, FALSE))
6057 return FAIL;
6058 *dest = dest_vimvar;
6059 vtv = get_vim_var_tv(*vimvaridx);
6060 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6061 }
6062 return OK;
6063}
6064
6065/*
6066 * Generate a STORE instruction for "dest", not being "dest_local".
6067 * Return FAIL when out of memory.
6068 */
6069 static int
6070generate_store_var(
6071 cctx_T *cctx,
6072 assign_dest_T dest,
6073 int opt_flags,
6074 int vimvaridx,
6075 int scriptvar_idx,
6076 int scriptvar_sid,
6077 type_T *type,
6078 char_u *name)
6079{
6080 switch (dest)
6081 {
6082 case dest_option:
6083 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6084 opt_flags);
6085 case dest_global:
6086 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006087 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6088 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006089 case dest_buffer:
6090 // include b: with the name, easier to execute that way
6091 return generate_STORE(cctx, ISN_STOREB, 0, name);
6092 case dest_window:
6093 // include w: with the name, easier to execute that way
6094 return generate_STORE(cctx, ISN_STOREW, 0, name);
6095 case dest_tab:
6096 // include t: with the name, easier to execute that way
6097 return generate_STORE(cctx, ISN_STORET, 0, name);
6098 case dest_env:
6099 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6100 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006101 return generate_STORE(cctx, ISN_STOREREG,
6102 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006103 case dest_vimvar:
6104 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6105 case dest_script:
6106 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006107 // "s:" may be included in the name.
6108 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006109 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006110 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6111 scriptvar_sid, scriptvar_idx, type);
6112 case dest_local:
6113 case dest_expr:
6114 // cannot happen
6115 break;
6116 }
6117 return FAIL;
6118}
6119
Bram Moolenaar752fc692021-01-04 21:57:11 +01006120 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006121generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6122{
6123 if (lhs->lhs_dest != dest_local)
6124 return generate_store_var(cctx, lhs->lhs_dest,
6125 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6126 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6127 lhs->lhs_type, lhs->lhs_name);
6128
6129 if (lhs->lhs_lvar != NULL)
6130 {
6131 garray_T *instr = &cctx->ctx_instr;
6132 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6133
6134 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6135 // ISN_STORENR
6136 if (lhs->lhs_lvar->lv_from_outer == 0
6137 && instr->ga_len == instr_count + 1
6138 && isn->isn_type == ISN_PUSHNR)
6139 {
6140 varnumber_T val = isn->isn_arg.number;
6141 garray_T *stack = &cctx->ctx_type_stack;
6142
6143 isn->isn_type = ISN_STORENR;
6144 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6145 isn->isn_arg.storenr.stnr_val = val;
6146 if (stack->ga_len > 0)
6147 --stack->ga_len;
6148 }
6149 else if (lhs->lhs_lvar->lv_from_outer > 0)
6150 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6151 lhs->lhs_lvar->lv_from_outer);
6152 else
6153 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6154 }
6155 return OK;
6156}
6157
6158 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006159is_decl_command(int cmdidx)
6160{
6161 return cmdidx == CMD_let || cmdidx == CMD_var
6162 || cmdidx == CMD_final || cmdidx == CMD_const;
6163}
6164
6165/*
6166 * Figure out the LHS type and other properties for an assignment or one item
6167 * of ":unlet" with an index.
6168 * Returns OK or FAIL.
6169 */
6170 static int
6171compile_lhs(
6172 char_u *var_start,
6173 lhs_T *lhs,
6174 int cmdidx,
6175 int heredoc,
6176 int oplen,
6177 cctx_T *cctx)
6178{
6179 char_u *var_end;
6180 int is_decl = is_decl_command(cmdidx);
6181
6182 CLEAR_POINTER(lhs);
6183 lhs->lhs_dest = dest_local;
6184 lhs->lhs_vimvaridx = -1;
6185 lhs->lhs_scriptvar_idx = -1;
6186
6187 // "dest_end" is the end of the destination, including "[expr]" or
6188 // ".name".
6189 // "var_end" is the end of the variable/option/etc. name.
6190 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6191 if (*var_start == '@')
6192 var_end = var_start + 2;
6193 else
6194 {
6195 // skip over the leading "&", "&l:", "&g:" and "$"
6196 var_end = skip_option_env_lead(var_start);
6197 var_end = to_name_end(var_end, TRUE);
6198 }
6199
6200 // "a: type" is declaring variable "a" with a type, not dict "a:".
6201 if (is_decl && lhs->lhs_dest_end == var_start + 2
6202 && lhs->lhs_dest_end[-1] == ':')
6203 --lhs->lhs_dest_end;
6204 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6205 --var_end;
6206
6207 // compute the length of the destination without "[expr]" or ".name"
6208 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006209 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006210 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6211 if (lhs->lhs_name == NULL)
6212 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006213
6214 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6215 // Something follows after the variable: "var[idx]" or "var.key".
6216 lhs->lhs_has_index = TRUE;
6217
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 if (heredoc)
6219 lhs->lhs_type = &t_list_string;
6220 else
6221 lhs->lhs_type = &t_any;
6222
6223 if (cctx->ctx_skip != SKIP_YES)
6224 {
6225 int declare_error = FALSE;
6226
6227 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6228 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6229 &lhs->lhs_type, cctx) == FAIL)
6230 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006231 if (lhs->lhs_dest != dest_local
6232 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006233 {
6234 // Specific kind of variable recognized.
6235 declare_error = is_decl;
6236 }
6237 else
6238 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006239 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006240 if (check_reserved_name(lhs->lhs_name) == FAIL)
6241 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006242
6243 if (lookup_local(var_start, lhs->lhs_varlen,
6244 &lhs->lhs_local_lvar, cctx) == OK)
6245 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6246 else
6247 {
6248 CLEAR_FIELD(lhs->lhs_arg_lvar);
6249 if (arg_exists(var_start, lhs->lhs_varlen,
6250 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6251 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6252 {
6253 if (is_decl)
6254 {
6255 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6256 return FAIL;
6257 }
6258 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6259 }
6260 }
6261 if (lhs->lhs_lvar != NULL)
6262 {
6263 if (is_decl)
6264 {
6265 semsg(_(e_variable_already_declared), lhs->lhs_name);
6266 return FAIL;
6267 }
6268 }
6269 else
6270 {
6271 int script_namespace = lhs->lhs_varlen > 1
6272 && STRNCMP(var_start, "s:", 2) == 0;
6273 int script_var = (script_namespace
6274 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006275 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006276 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006277 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006278 imported_T *import =
6279 find_imported(var_start, lhs->lhs_varlen, cctx);
6280
6281 if (script_namespace || script_var || import != NULL)
6282 {
6283 char_u *rawname = lhs->lhs_name
6284 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6285
6286 if (is_decl)
6287 {
6288 if (script_namespace)
6289 semsg(_(e_cannot_declare_script_variable_in_function),
6290 lhs->lhs_name);
6291 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006292 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006293 lhs->lhs_name);
6294 return FAIL;
6295 }
6296 else if (cctx->ctx_ufunc->uf_script_ctx_version
6297 == SCRIPT_VERSION_VIM9
6298 && script_namespace
6299 && !script_var && import == NULL)
6300 {
6301 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6302 return FAIL;
6303 }
6304
6305 lhs->lhs_dest = dest_script;
6306
6307 // existing script-local variables should have a type
6308 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6309 if (import != NULL)
6310 lhs->lhs_scriptvar_sid = import->imp_sid;
6311 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6312 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006313 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006314 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006315 lhs->lhs_scriptvar_sid, rawname,
6316 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6317 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006318 if (lhs->lhs_scriptvar_idx >= 0)
6319 {
6320 scriptitem_T *si = SCRIPT_ITEM(
6321 lhs->lhs_scriptvar_sid);
6322 svar_T *sv =
6323 ((svar_T *)si->sn_var_vals.ga_data)
6324 + lhs->lhs_scriptvar_idx;
6325 lhs->lhs_type = sv->sv_type;
6326 }
6327 }
6328 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006329 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006330 == FAIL)
6331 return FAIL;
6332 }
6333 }
6334
6335 if (declare_error)
6336 {
6337 vim9_declare_error(lhs->lhs_name);
6338 return FAIL;
6339 }
6340 }
6341
6342 // handle "a:name" as a name, not index "name" on "a"
6343 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6344 var_end = lhs->lhs_dest_end;
6345
6346 if (lhs->lhs_dest != dest_option)
6347 {
6348 if (is_decl && *var_end == ':')
6349 {
6350 char_u *p;
6351
6352 // parse optional type: "let var: type = expr"
6353 if (!VIM_ISWHITE(var_end[1]))
6354 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006355 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006356 return FAIL;
6357 }
6358 p = skipwhite(var_end + 1);
6359 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6360 if (lhs->lhs_type == NULL)
6361 return FAIL;
6362 lhs->lhs_has_type = TRUE;
6363 }
6364 else if (lhs->lhs_lvar != NULL)
6365 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6366 }
6367
Bram Moolenaare42939a2021-04-05 17:11:17 +02006368 if (oplen == 3 && !heredoc
6369 && lhs->lhs_dest != dest_global
6370 && !lhs->lhs_has_index
6371 && lhs->lhs_type->tt_type != VAR_STRING
6372 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006373 {
6374 emsg(_(e_can_only_concatenate_to_string));
6375 return FAIL;
6376 }
6377
6378 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6379 && cctx->ctx_skip != SKIP_YES)
6380 {
6381 if (oplen > 1 && !heredoc)
6382 {
6383 // +=, /=, etc. require an existing variable
6384 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6385 return FAIL;
6386 }
6387 if (!is_decl)
6388 {
6389 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6390 return FAIL;
6391 }
6392
Bram Moolenaar3f327882021-03-17 20:56:38 +01006393 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006394 if ((lhs->lhs_type->tt_type == VAR_FUNC
6395 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006396 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006397 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006398
6399 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006400 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6401 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6402 if (lhs->lhs_lvar == NULL)
6403 return FAIL;
6404 lhs->lhs_new_local = TRUE;
6405 }
6406
6407 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006408 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006409 {
6410 // Something follows after the variable: "var[idx]" or "var.key".
6411 // TODO: should we also handle "->func()" here?
6412 if (is_decl)
6413 {
6414 emsg(_(e_cannot_use_index_when_declaring_variable));
6415 return FAIL;
6416 }
6417
6418 if (var_start[lhs->lhs_varlen] == '['
6419 || var_start[lhs->lhs_varlen] == '.')
6420 {
6421 char_u *after = var_start + lhs->lhs_varlen;
6422 char_u *p;
6423
6424 // Only the last index is used below, if there are others
6425 // before it generate code for the expression. Thus for
6426 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6427 for (;;)
6428 {
6429 p = skip_index(after);
6430 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006431 {
6432 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006433 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006434 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006435 after = p;
6436 }
6437 if (after > var_start + lhs->lhs_varlen)
6438 {
6439 lhs->lhs_varlen = after - var_start;
6440 lhs->lhs_dest = dest_expr;
6441 // We don't know the type before evaluating the expression,
6442 // use "any" until then.
6443 lhs->lhs_type = &t_any;
6444 }
6445
Bram Moolenaar752fc692021-01-04 21:57:11 +01006446 if (lhs->lhs_type->tt_member == NULL)
6447 lhs->lhs_member_type = &t_any;
6448 else
6449 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6450 }
6451 else
6452 {
6453 semsg("Not supported yet: %s", var_start);
6454 return FAIL;
6455 }
6456 }
6457 return OK;
6458}
6459
6460/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006461 * Figure out the LHS and check a few errors.
6462 */
6463 static int
6464compile_assign_lhs(
6465 char_u *var_start,
6466 lhs_T *lhs,
6467 int cmdidx,
6468 int is_decl,
6469 int heredoc,
6470 int oplen,
6471 cctx_T *cctx)
6472{
6473 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6474 return FAIL;
6475
6476 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6477 {
6478 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6479 return FAIL;
6480 }
6481 if (!is_decl && lhs->lhs_lvar != NULL
6482 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6483 {
6484 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6485 return FAIL;
6486 }
6487 return OK;
6488}
6489
6490/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006491 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6492 */
6493 static int
6494has_list_index(char_u *idx_start, cctx_T *cctx)
6495{
6496 char_u *p = idx_start;
6497 int save_skip;
6498
6499 if (*p != '[')
6500 return FALSE;
6501
6502 p = skipwhite(p + 1);
6503 if (*p == ':')
6504 return TRUE;
6505
6506 save_skip = cctx->ctx_skip;
6507 cctx->ctx_skip = SKIP_YES;
6508 (void)compile_expr0(&p, cctx);
6509 cctx->ctx_skip = save_skip;
6510 return *skipwhite(p) == ':';
6511}
6512
6513/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006514 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6515 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006516 */
6517 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006518compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006519 char_u *var_start,
6520 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006521 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006522 cctx_T *cctx)
6523{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006524 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006525 char_u *p;
6526 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006527 int need_white_before = TRUE;
6528 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006529
Bram Moolenaar752fc692021-01-04 21:57:11 +01006530 p = var_start + varlen;
6531 if (*p == '[')
6532 {
6533 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006534 if (*p == ':')
6535 {
6536 // empty first index, push zero
6537 r = generate_PUSHNR(cctx, 0);
6538 need_white_before = FALSE;
6539 }
6540 else
6541 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006542
6543 if (r == OK && *skipwhite(p) == ':')
6544 {
6545 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006546 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006547 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006548 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006549 empty_second = *skipwhite(p + 1) == ']';
6550 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6551 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006552 {
6553 semsg(_(e_white_space_required_before_and_after_str_at_str),
6554 ":", p);
6555 return FAIL;
6556 }
6557 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006558 if (*p == ']')
6559 // empty second index, push "none"
6560 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6561 else
6562 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006563 }
6564
Bram Moolenaar752fc692021-01-04 21:57:11 +01006565 if (r == OK && *skipwhite(p) != ']')
6566 {
6567 // this should not happen
6568 emsg(_(e_missbrac));
6569 r = FAIL;
6570 }
6571 }
6572 else // if (*p == '.')
6573 {
6574 char_u *key_end = to_name_end(p + 1, TRUE);
6575 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6576
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006577 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006578 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006579 return r;
6580}
6581
6582/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006583 * For a LHS with an index, load the variable to be indexed.
6584 */
6585 static int
6586compile_load_lhs(
6587 lhs_T *lhs,
6588 char_u *var_start,
6589 type_T *rhs_type,
6590 cctx_T *cctx)
6591{
6592 if (lhs->lhs_dest == dest_expr)
6593 {
6594 size_t varlen = lhs->lhs_varlen;
6595 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006596 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006597 char_u *p = var_start;
6598 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006599 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006600
Bram Moolenaare97976b2021-08-01 13:17:17 +02006601 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6602 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006603 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006604 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6605 res = compile_expr0(&p, cctx);
6606 var_start[varlen] = c;
6607 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6608 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006609 {
6610 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006611 if (res != FAIL)
6612 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006613 return FAIL;
6614 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006615
6616 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6617 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6618 // now we can properly check the type
6619 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6620 && rhs_type != &t_void
6621 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6622 FALSE, FALSE) == FAIL)
6623 return FAIL;
6624 }
6625 else
6626 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6627 lhs->lhs_lvar, lhs->lhs_type);
6628 return OK;
6629}
6630
6631/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006632 * Produce code for loading "lhs" and also take care of an index.
6633 * Return OK/FAIL.
6634 */
6635 static int
6636compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6637{
6638 compile_load_lhs(lhs, var_start, NULL, cctx);
6639
6640 if (lhs->lhs_has_index)
6641 {
6642 int range = FALSE;
6643
6644 // Get member from list or dict. First compile the
6645 // index value.
6646 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6647 return FAIL;
6648 if (range)
6649 {
6650 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6651 var_start);
6652 return FAIL;
6653 }
6654
6655 // Get the member.
6656 if (compile_member(FALSE, cctx) == FAIL)
6657 return FAIL;
6658 }
6659 return OK;
6660}
6661
6662/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006663 * Assignment to a list or dict member, or ":unlet" for the item, using the
6664 * information in "lhs".
6665 * Returns OK or FAIL.
6666 */
6667 static int
6668compile_assign_unlet(
6669 char_u *var_start,
6670 lhs_T *lhs,
6671 int is_assign,
6672 type_T *rhs_type,
6673 cctx_T *cctx)
6674{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006675 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006676 garray_T *stack = &cctx->ctx_type_stack;
6677 int range = FALSE;
6678
Bram Moolenaar68452172021-04-12 21:21:02 +02006679 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006680 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006681 if (is_assign && range
6682 && lhs->lhs_type->tt_type != VAR_LIST
6683 && lhs->lhs_type != &t_blob
6684 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006685 {
6686 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6687 return FAIL;
6688 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006689
6690 if (lhs->lhs_type == &t_any)
6691 {
6692 // Index on variable of unknown type: check at runtime.
6693 dest_type = VAR_ANY;
6694 }
6695 else
6696 {
6697 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006698 if (dest_type == VAR_DICT && range)
6699 {
6700 emsg(e_cannot_use_range_with_dictionary);
6701 return FAIL;
6702 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006703 if (dest_type == VAR_DICT
6704 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006705 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006706 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006707 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006708 type_T *type;
6709
6710 if (range)
6711 {
6712 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6713 if (need_type(type, &t_number,
6714 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006715 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006716 }
6717 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006718 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006719 && need_type(type, &t_number,
6720 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006721 return FAIL;
6722 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006723 }
6724
6725 // Load the dict or list. On the stack we then have:
6726 // - value (for assignment, not for :unlet)
6727 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006728 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006729 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006730 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6731 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006732
Bram Moolenaar68452172021-04-12 21:21:02 +02006733 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6734 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006735 {
6736 if (is_assign)
6737 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006738 if (range)
6739 {
6740 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6741 return FAIL;
6742 }
6743 else
6744 {
6745 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006746
Bram Moolenaar68452172021-04-12 21:21:02 +02006747 if (isn == NULL)
6748 return FAIL;
6749 isn->isn_arg.vartype = dest_type;
6750 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006751 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006752 else if (range)
6753 {
6754 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6755 return FAIL;
6756 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006757 else
6758 {
6759 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6760 return FAIL;
6761 }
6762 }
6763 else
6764 {
6765 emsg(_(e_indexable_type_required));
6766 return FAIL;
6767 }
6768
6769 return OK;
6770}
6771
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006772/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006773 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006774 * "let name"
6775 * "var name = expr"
6776 * "final name = expr"
6777 * "const name = expr"
6778 * "name = expr"
6779 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006780 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006781 * Return NULL for an error.
6782 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 */
6784 static char_u *
6785compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6786{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006787 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006789 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 char_u *ret = NULL;
6791 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006792 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006794 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006796 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 int oplen = 0;
6799 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006800 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006801 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 int is_decl = is_decl_command(cmdidx);
6804 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006805 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006807 // Skip over the "var" or "[var, var]" to get to any "=".
6808 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6809 if (p == NULL)
6810 return *arg == '[' ? arg : NULL;
6811
6812 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006814 // TODO: should we allow this, and figure out type inference from list
6815 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006816 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006817 return NULL;
6818 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006819 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821 sp = p;
6822 p = skipwhite(p);
6823 op = p;
6824 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006825
6826 if (var_count > 0 && oplen == 0)
6827 // can be something like "[1, 2]->func()"
6828 return arg;
6829
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006830 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006832 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006833 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006834 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006835 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6836 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006837 if (VIM_ISWHITE(eap->cmd[2]))
6838 {
6839 semsg(_(e_no_white_space_allowed_after_str_str),
6840 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6841 return NULL;
6842 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006843 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6844 oplen = 2;
6845 incdec = TRUE;
6846 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848 if (heredoc)
6849 {
6850 list_T *l;
6851 listitem_T *li;
6852
6853 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006854 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006856 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006857 if (l == NULL)
6858 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006859
Bram Moolenaar078269b2020-09-21 20:35:55 +02006860 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006862 // Push each line and the create the list.
6863 FOR_ALL_LIST_ITEMS(l, li)
6864 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006865 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006866 li->li_tv.vval.v_string = NULL;
6867 }
6868 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 list_free(l);
6871 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006872 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006873 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006874 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006876 char_u *wp;
6877
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006878 // for "[var, var] = expr" evaluate the expression here, loop over the
6879 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006880 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006881
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006882 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006883 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006884 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006885 if (compile_expr0(&p, cctx) == FAIL)
6886 return NULL;
6887 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006889 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006891 type_T *stacktype;
6892
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006893 stacktype = stack->ga_len == 0 ? &t_void
6894 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006895 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006897 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006898 goto theend;
6899 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006900 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006901 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006902 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006903 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006904 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6905 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006906 if (stacktype->tt_member != NULL)
6907 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006908 }
6909 }
6910
6911 /*
6912 * Loop over variables in "[var, var] = expr".
6913 * For "var = expr" and "let var: type" this is done only once.
6914 */
6915 if (var_count > 0)
6916 var_start = skipwhite(arg + 1); // skip over the "["
6917 else
6918 var_start = arg;
6919 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6920 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006921 int instr_count = -1;
6922 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006923
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006924 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6925 {
6926 // Ignore underscore in "[a, _, b] = list".
6927 if (var_count > 0)
6928 {
6929 var_start = skipwhite(var_start + 2);
6930 continue;
6931 }
6932 emsg(_(e_cannot_use_underscore_here));
6933 goto theend;
6934 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006935 vim_free(lhs.lhs_name);
6936
6937 /*
6938 * Figure out the LHS type and other properties.
6939 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006940 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6941 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006942 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02006943 if (heredoc)
6944 {
6945 SOURCING_LNUM = start_lnum;
6946 if (lhs.lhs_has_type
6947 && need_type(&t_list_string, lhs.lhs_type,
6948 -1, 0, cctx, FALSE, FALSE) == FAIL)
6949 goto theend;
6950 }
6951 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006952 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006953 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006954 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006955 if (oplen > 0 && var_count == 0)
6956 {
6957 // skip over the "=" and the expression
6958 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006959 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006960 }
6961 }
6962 else if (oplen > 0)
6963 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006964 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006965 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006966
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006967 // for "+=", "*=", "..=" etc. first load the current value
6968 if (*op != '='
6969 && compile_load_lhs_with_index(&lhs, var_start,
6970 cctx) == FAIL)
6971 goto theend;
6972
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006973 // For "var = expr" evaluate the expression.
6974 if (var_count == 0)
6975 {
6976 int r;
6977
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006978 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006979 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006980 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006981 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006982 r = generate_PUSHNR(cctx, 1);
6983 }
6984 else
6985 {
6986 // Temporarily hide the new local variable here, it is
6987 // not available to this expression.
6988 if (lhs.lhs_new_local)
6989 --cctx->ctx_locals.ga_len;
6990 wp = op + oplen;
6991 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6992 {
6993 if (lhs.lhs_new_local)
6994 ++cctx->ctx_locals.ga_len;
6995 goto theend;
6996 }
6997 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006998 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006999 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007000 if (r == FAIL)
7001 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007002 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007003 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007004 else if (semicolon && var_idx == var_count - 1)
7005 {
7006 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007007 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007008 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7009 goto theend;
7010 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007011 else
7012 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007013 // For "[var, var] = expr" get the "var_idx" item from the
7014 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007015 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007016 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007017 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007018
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007019 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007020 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007021 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007022 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007023 if ((rhs_type->tt_type == VAR_FUNC
7024 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007025 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007026 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007027 goto theend;
7028
Bram Moolenaar752fc692021-01-04 21:57:11 +01007029 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007030 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007031 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007032 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007033 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007034 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007035 }
7036 else
7037 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007038 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007039 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007040 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007041 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007042 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007043 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007044 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007045 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007046 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007047 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007048 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007049 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007050 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007051 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007052 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007053 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007054
Bram Moolenaar77709b12021-04-03 21:01:01 +02007055 // Without operator check type here, otherwise below.
7056 // Use the line number of the assignment.
7057 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007058 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7059 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007060 // If assigning to a list or dict member, use the
7061 // member type. Not for "list[:] =".
7062 if (lhs.lhs_has_index
7063 && !has_list_index(var_start + lhs.lhs_varlen,
7064 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007065 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007066 if (need_type_where(rhs_type, use_type, -1, where,
7067 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007068 goto theend;
7069 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007070 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007071 else
7072 {
7073 type_T *lhs_type = lhs.lhs_member_type;
7074
7075 // Special case: assigning to @# can use a number or a
7076 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007077 // Also: can assign a number to a float.
7078 if ((lhs_type == &t_number_or_string
7079 || lhs_type == &t_float)
7080 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007081 lhs_type = &t_number;
7082 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007083 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007084 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007087 else if (cmdidx == CMD_final)
7088 {
7089 emsg(_(e_final_requires_a_value));
7090 goto theend;
7091 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007092 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007094 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007095 goto theend;
7096 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007097 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007098 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007099 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007100 goto theend;
7101 }
7102 else
7103 {
7104 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007105 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007106 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007107 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007108 {
7109 case VAR_BOOL:
7110 generate_PUSHBOOL(cctx, VVAL_FALSE);
7111 break;
7112 case VAR_FLOAT:
7113#ifdef FEAT_FLOAT
7114 generate_PUSHF(cctx, 0.0);
7115#endif
7116 break;
7117 case VAR_STRING:
7118 generate_PUSHS(cctx, NULL);
7119 break;
7120 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007121 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007122 break;
7123 case VAR_FUNC:
7124 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7125 break;
7126 case VAR_LIST:
7127 generate_NEWLIST(cctx, 0);
7128 break;
7129 case VAR_DICT:
7130 generate_NEWDICT(cctx, 0);
7131 break;
7132 case VAR_JOB:
7133 generate_PUSHJOB(cctx, NULL);
7134 break;
7135 case VAR_CHANNEL:
7136 generate_PUSHCHANNEL(cctx, NULL);
7137 break;
7138 case VAR_NUMBER:
7139 case VAR_UNKNOWN:
7140 case VAR_ANY:
7141 case VAR_PARTIAL:
7142 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007143 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007144 case VAR_SPECIAL: // cannot happen
7145 generate_PUSHNR(cctx, 0);
7146 break;
7147 }
7148 }
7149 if (var_count == 0)
7150 end = p;
7151 }
7152
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007153 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007154 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007155 break;
7156
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007157 if (oplen > 0 && *op != '=')
7158 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007159 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007160 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007161
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007162 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007163 {
7164 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7165 goto theend;
7166 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007167 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007168 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007169 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007170 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7171 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007172#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007173 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007174 !(expected == &t_float && (stacktype == &t_number
7175 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007176#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007177 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007178 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007179 goto theend;
7180 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007181
7182 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007183 {
7184 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7185 goto theend;
7186 }
7187 else if (*op == '+')
7188 {
7189 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007190 operator_type(lhs.lhs_member_type, stacktype),
7191 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007192 goto theend;
7193 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007194 else if (generate_two_op(cctx, op) == FAIL)
7195 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007198 // Use the line number of the assignment for store instruction.
7199 save_lnum = cctx->ctx_lnum;
7200 cctx->ctx_lnum = start_lnum - 1;
7201
Bram Moolenaar752fc692021-01-04 21:57:11 +01007202 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007203 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007204 // Use the info in "lhs" to store the value at the index in the
7205 // list or dict.
7206 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7207 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007208 {
7209 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007210 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007211 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007212 }
7213 else
7214 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007215 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007216 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007217 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007218 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007219 generate_LOCKCONST(cctx);
7220
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007221 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007222 && (lhs.lhs_type->tt_type == VAR_DICT
7223 || lhs.lhs_type->tt_type == VAR_LIST)
7224 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007225 && !(lhs.lhs_type->tt_member == &t_any
7226 && oplen > 0
7227 && rhs_type != NULL
7228 && rhs_type->tt_type == lhs.lhs_type->tt_type
7229 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007230 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007231 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007232 // also in legacy script. Not for "list<any> = val", then the
7233 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007234 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007235
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007236 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007237 {
7238 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007239 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007240 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007241 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007242 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007243
7244 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007245 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007247
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007248 // For "[var, var] = expr" drop the "expr" value.
7249 // Also for "[var, var; _] = expr".
7250 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007251 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007252 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007253 goto theend;
7254 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007255
Bram Moolenaarb2097502020-07-19 17:17:02 +02007256 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257
7258theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007259 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 return ret;
7261}
7262
7263/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007264 * Check for an assignment at "eap->cmd", compile it if found.
7265 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7266 */
7267 static int
7268may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7269{
7270 char_u *pskip;
7271 char_u *p;
7272
7273 // Assuming the command starts with a variable or function name,
7274 // find what follows.
7275 // Skip over "var.member", "var[idx]" and the like.
7276 // Also "&opt = val", "$ENV = val" and "@r = val".
7277 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7278 ? eap->cmd + 1 : eap->cmd;
7279 p = to_name_end(pskip, TRUE);
7280 if (p > eap->cmd && *p != NUL)
7281 {
7282 char_u *var_end;
7283 int oplen;
7284 int heredoc;
7285
7286 if (eap->cmd[0] == '@')
7287 var_end = eap->cmd + 2;
7288 else
7289 var_end = find_name_end(pskip, NULL, NULL,
7290 FNE_CHECK_START | FNE_INCL_BR);
7291 oplen = assignment_len(skipwhite(var_end), &heredoc);
7292 if (oplen > 0)
7293 {
7294 size_t len = p - eap->cmd;
7295
7296 // Recognize an assignment if we recognize the variable
7297 // name:
7298 // "g:var = expr"
7299 // "local = expr" where "local" is a local var.
7300 // "script = expr" where "script" is a script-local var.
7301 // "import = expr" where "import" is an imported var
7302 // "&opt = expr"
7303 // "$ENV = expr"
7304 // "@r = expr"
7305 if (*eap->cmd == '&'
7306 || *eap->cmd == '$'
7307 || *eap->cmd == '@'
7308 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007309 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007310 {
7311 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7312 if (*line == NULL || *line == eap->cmd)
7313 return FAIL;
7314 return OK;
7315 }
7316 }
7317 }
7318
7319 if (*eap->cmd == '[')
7320 {
7321 // [var, var] = expr
7322 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7323 if (*line == NULL)
7324 return FAIL;
7325 if (*line != eap->cmd)
7326 return OK;
7327 }
7328 return NOTDONE;
7329}
7330
7331/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007332 * Check if "name" can be "unlet".
7333 */
7334 int
7335check_vim9_unlet(char_u *name)
7336{
7337 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7338 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007339 // "unlet s:var" is allowed in legacy script.
7340 if (*name == 's' && !script_is_vim9())
7341 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007342 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007343 return FAIL;
7344 }
7345 return OK;
7346}
7347
7348/*
7349 * Callback passed to ex_unletlock().
7350 */
7351 static int
7352compile_unlet(
7353 lval_T *lvp,
7354 char_u *name_end,
7355 exarg_T *eap,
7356 int deep UNUSED,
7357 void *coookie)
7358{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007359 cctx_T *cctx = coookie;
7360 char_u *p = lvp->ll_name;
7361 int cc = *name_end;
7362 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007363
Bram Moolenaar752fc692021-01-04 21:57:11 +01007364 if (cctx->ctx_skip == SKIP_YES)
7365 return OK;
7366
7367 *name_end = NUL;
7368 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007369 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007370 // :unlet $ENV_VAR
7371 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7372 }
7373 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7374 {
7375 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007376
Bram Moolenaar752fc692021-01-04 21:57:11 +01007377 // This is similar to assigning: lookup the list/dict, compile the
7378 // idx/key. Then instead of storing the value unlet the item.
7379 // unlet {list}[idx]
7380 // unlet {dict}[key] dict.key
7381 //
7382 // Figure out the LHS type and other properties.
7383 //
7384 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7385
7386 // : unlet an indexed item
7387 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007388 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007389 iemsg("called compile_lhs() without an index");
7390 ret = FAIL;
7391 }
7392 else
7393 {
7394 // Use the info in "lhs" to unlet the item at the index in the
7395 // list or dict.
7396 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007397 }
7398
Bram Moolenaar752fc692021-01-04 21:57:11 +01007399 vim_free(lhs.lhs_name);
7400 }
7401 else if (check_vim9_unlet(p) == FAIL)
7402 {
7403 ret = FAIL;
7404 }
7405 else
7406 {
7407 // Normal name. Only supports g:, w:, t: and b: namespaces.
7408 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007409 }
7410
Bram Moolenaar752fc692021-01-04 21:57:11 +01007411 *name_end = cc;
7412 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007413}
7414
7415/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007416 * Callback passed to ex_unletlock().
7417 */
7418 static int
7419compile_lock_unlock(
7420 lval_T *lvp,
7421 char_u *name_end,
7422 exarg_T *eap,
7423 int deep UNUSED,
7424 void *coookie)
7425{
7426 cctx_T *cctx = coookie;
7427 int cc = *name_end;
7428 char_u *p = lvp->ll_name;
7429 int ret = OK;
7430 size_t len;
7431 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007432 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007433
7434 if (cctx->ctx_skip == SKIP_YES)
7435 return OK;
7436
7437 // Cannot use :lockvar and :unlockvar on local variables.
7438 if (p[1] != ':')
7439 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007440 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007441
7442 if (lookup_local(p, end - p, NULL, cctx) == OK)
7443 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007444 char_u *s = p;
7445
7446 if (*end != '.' && *end != '[')
7447 {
7448 emsg(_(e_cannot_lock_unlock_local_variable));
7449 return FAIL;
7450 }
7451
7452 // For "d.member" put the local variable on the stack, it will be
7453 // passed to ex_lockvar() indirectly.
7454 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7455 return FAIL;
7456 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007457 }
7458 }
7459
7460 // Checking is done at runtime.
7461 *name_end = NUL;
7462 len = name_end - p + 20;
7463 buf = alloc(len);
7464 if (buf == NULL)
7465 ret = FAIL;
7466 else
7467 {
7468 vim_snprintf((char *)buf, len, "%s %s",
7469 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7470 p);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007471 ret = generate_EXEC(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007472
7473 vim_free(buf);
7474 *name_end = cc;
7475 }
7476 return ret;
7477}
7478
7479/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007480 * compile "unlet var", "lock var" and "unlock var"
7481 * "arg" points to "var".
7482 */
7483 static char_u *
7484compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7485{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007486 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7487 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7488 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007489 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7490}
7491
7492/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7494 */
7495 static int
7496compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7497{
7498 garray_T *instr = &cctx->ctx_instr;
7499 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7500
7501 if (endlabel == NULL)
7502 return FAIL;
7503 endlabel->el_next = *el;
7504 *el = endlabel;
7505 endlabel->el_end_label = instr->ga_len;
7506
7507 generate_JUMP(cctx, when, 0);
7508 return OK;
7509}
7510
7511 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007512compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513{
7514 garray_T *instr = &cctx->ctx_instr;
7515
7516 while (*el != NULL)
7517 {
7518 endlabel_T *cur = (*el);
7519 isn_T *isn;
7520
7521 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007522 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007523 *el = cur->el_next;
7524 vim_free(cur);
7525 }
7526}
7527
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007528 static void
7529compile_free_jump_to_end(endlabel_T **el)
7530{
7531 while (*el != NULL)
7532 {
7533 endlabel_T *cur = (*el);
7534
7535 *el = cur->el_next;
7536 vim_free(cur);
7537 }
7538}
7539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007540/*
7541 * Create a new scope and set up the generic items.
7542 */
7543 static scope_T *
7544new_scope(cctx_T *cctx, scopetype_T type)
7545{
7546 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7547
7548 if (scope == NULL)
7549 return NULL;
7550 scope->se_outer = cctx->ctx_scope;
7551 cctx->ctx_scope = scope;
7552 scope->se_type = type;
7553 scope->se_local_count = cctx->ctx_locals.ga_len;
7554 return scope;
7555}
7556
7557/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007558 * Free the current scope and go back to the outer scope.
7559 */
7560 static void
7561drop_scope(cctx_T *cctx)
7562{
7563 scope_T *scope = cctx->ctx_scope;
7564
7565 if (scope == NULL)
7566 {
7567 iemsg("calling drop_scope() without a scope");
7568 return;
7569 }
7570 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007571 switch (scope->se_type)
7572 {
7573 case IF_SCOPE:
7574 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7575 case FOR_SCOPE:
7576 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7577 case WHILE_SCOPE:
7578 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7579 case TRY_SCOPE:
7580 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7581 case NO_SCOPE:
7582 case BLOCK_SCOPE:
7583 break;
7584 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007585 vim_free(scope);
7586}
7587
7588/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 * compile "if expr"
7590 *
7591 * "if expr" Produces instructions:
7592 * EVAL expr Push result of "expr"
7593 * JUMP_IF_FALSE end
7594 * ... body ...
7595 * end:
7596 *
7597 * "if expr | else" Produces instructions:
7598 * EVAL expr Push result of "expr"
7599 * JUMP_IF_FALSE else
7600 * ... body ...
7601 * JUMP_ALWAYS end
7602 * else:
7603 * ... body ...
7604 * end:
7605 *
7606 * "if expr1 | elseif expr2 | else" Produces instructions:
7607 * EVAL expr Push result of "expr"
7608 * JUMP_IF_FALSE elseif
7609 * ... body ...
7610 * JUMP_ALWAYS end
7611 * elseif:
7612 * EVAL expr Push result of "expr"
7613 * JUMP_IF_FALSE else
7614 * ... body ...
7615 * JUMP_ALWAYS end
7616 * else:
7617 * ... body ...
7618 * end:
7619 */
7620 static char_u *
7621compile_if(char_u *arg, cctx_T *cctx)
7622{
7623 char_u *p = arg;
7624 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007625 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007627 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007628 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629
Bram Moolenaara5565e42020-05-09 15:44:01 +02007630 CLEAR_FIELD(ppconst);
7631 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007632 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007633 clear_ppconst(&ppconst);
7634 return NULL;
7635 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007636 if (!ends_excmd2(arg, skipwhite(p)))
7637 {
7638 semsg(_(e_trailing_arg), p);
7639 return NULL;
7640 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007641 if (cctx->ctx_skip == SKIP_YES)
7642 clear_ppconst(&ppconst);
7643 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007644 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007645 int error = FALSE;
7646 int v;
7647
Bram Moolenaara5565e42020-05-09 15:44:01 +02007648 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007649 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007650 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007651 if (error)
7652 return NULL;
7653 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007654 }
7655 else
7656 {
7657 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007658 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007659 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007660 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007661 if (bool_on_stack(cctx) == FAIL)
7662 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007664
Bram Moolenaara91a7132021-03-25 21:12:15 +01007665 // CMDMOD_REV must come before the jump
7666 generate_undo_cmdmods(cctx);
7667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668 scope = new_scope(cctx, IF_SCOPE);
7669 if (scope == NULL)
7670 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007671 scope->se_skip_save = skip_save;
7672 // "is_had_return" will be reset if any block does not end in :return
7673 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007675 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007676 {
7677 // "where" is set when ":elseif", "else" or ":endif" is found
7678 scope->se_u.se_if.is_if_label = instr->ga_len;
7679 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7680 }
7681 else
7682 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683
Bram Moolenaarced68a02021-01-24 17:53:47 +01007684#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007685 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007686 && skip_save != SKIP_YES)
7687 {
7688 // generated a profile start, need to generate a profile end, since it
7689 // won't be done after returning
7690 cctx->ctx_skip = SKIP_NOT;
7691 generate_instr(cctx, ISN_PROF_END);
7692 cctx->ctx_skip = SKIP_YES;
7693 }
7694#endif
7695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 return p;
7697}
7698
7699 static char_u *
7700compile_elseif(char_u *arg, cctx_T *cctx)
7701{
7702 char_u *p = arg;
7703 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007704 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 isn_T *isn;
7706 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007707 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007708 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709
7710 if (scope == NULL || scope->se_type != IF_SCOPE)
7711 {
7712 emsg(_(e_elseif_without_if));
7713 return NULL;
7714 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007715 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007716 if (!cctx->ctx_had_return)
7717 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718
Bram Moolenaarced68a02021-01-24 17:53:47 +01007719 if (cctx->ctx_skip == SKIP_NOT)
7720 {
7721 // previous block was executed, this one and following will not
7722 cctx->ctx_skip = SKIP_YES;
7723 scope->se_u.se_if.is_seen_skip_not = TRUE;
7724 }
7725 if (scope->se_u.se_if.is_seen_skip_not)
7726 {
7727 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007728 // Do not count the "elseif" for profiling and cmdmod
7729 instr->ga_len = current_instr_idx(cctx);
7730
Bram Moolenaarced68a02021-01-24 17:53:47 +01007731 skip_expr_cctx(&p, cctx);
7732 return p;
7733 }
7734
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007735 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007736 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007737 int moved_cmdmod = FALSE;
7738
7739 // Move any CMDMOD instruction to after the jump
7740 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7741 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007742 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007743 return NULL;
7744 ((isn_T *)instr->ga_data)[instr->ga_len] =
7745 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7746 --instr->ga_len;
7747 moved_cmdmod = TRUE;
7748 }
7749
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007750 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007752 return NULL;
7753 // previous "if" or "elseif" jumps here
7754 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7755 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007756 if (moved_cmdmod)
7757 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007758 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007760 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007761 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007762 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007763 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007764 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007765#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007766 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007767 {
7768 // the previous block was skipped, need to profile this line
7769 generate_instr(cctx, ISN_PROF_START);
7770 instr_count = instr->ga_len;
7771 }
7772#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007773 if (cctx->ctx_compile_type == CT_DEBUG)
7774 {
7775 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007776 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007777 instr_count = instr->ga_len;
7778 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007779 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007780 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007781 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007782 clear_ppconst(&ppconst);
7783 return NULL;
7784 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007785 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007786 if (!ends_excmd2(arg, skipwhite(p)))
7787 {
7788 semsg(_(e_trailing_arg), p);
7789 return NULL;
7790 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007791 if (scope->se_skip_save == SKIP_YES)
7792 clear_ppconst(&ppconst);
7793 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007794 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007795 int error = FALSE;
7796 int v;
7797
Bram Moolenaar7f141552020-05-09 17:35:53 +02007798 // The expression results in a constant.
7799 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007800 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7801 if (error)
7802 return NULL;
7803 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007804 clear_ppconst(&ppconst);
7805 scope->se_u.se_if.is_if_label = -1;
7806 }
7807 else
7808 {
7809 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007810 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007811 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007812 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007813 if (bool_on_stack(cctx) == FAIL)
7814 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815
Bram Moolenaara91a7132021-03-25 21:12:15 +01007816 // CMDMOD_REV must come before the jump
7817 generate_undo_cmdmods(cctx);
7818
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007819 // "where" is set when ":elseif", "else" or ":endif" is found
7820 scope->se_u.se_if.is_if_label = instr->ga_len;
7821 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823
7824 return p;
7825}
7826
7827 static char_u *
7828compile_else(char_u *arg, cctx_T *cctx)
7829{
7830 char_u *p = arg;
7831 garray_T *instr = &cctx->ctx_instr;
7832 isn_T *isn;
7833 scope_T *scope = cctx->ctx_scope;
7834
7835 if (scope == NULL || scope->se_type != IF_SCOPE)
7836 {
7837 emsg(_(e_else_without_if));
7838 return NULL;
7839 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007840 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007841 if (!cctx->ctx_had_return)
7842 scope->se_u.se_if.is_had_return = FALSE;
7843 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844
Bram Moolenaarced68a02021-01-24 17:53:47 +01007845#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007846 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007847 {
7848 if (cctx->ctx_skip == SKIP_NOT
7849 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7850 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007851 // the previous block was executed, do not count "else" for
7852 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007853 --instr->ga_len;
7854 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7855 {
7856 // the previous block was not executed, this one will, do count the
7857 // "else" for profiling
7858 cctx->ctx_skip = SKIP_NOT;
7859 generate_instr(cctx, ISN_PROF_END);
7860 generate_instr(cctx, ISN_PROF_START);
7861 cctx->ctx_skip = SKIP_YES;
7862 }
7863 }
7864#endif
7865
7866 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007867 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007868 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007869 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007870 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007871 if (!cctx->ctx_had_return
7872 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7873 JUMP_ALWAYS, cctx) == FAIL)
7874 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007875 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007876
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007877 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007878 {
7879 if (scope->se_u.se_if.is_if_label >= 0)
7880 {
7881 // previous "if" or "elseif" jumps here
7882 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7883 isn->isn_arg.jump.jump_where = instr->ga_len;
7884 scope->se_u.se_if.is_if_label = -1;
7885 }
7886 }
7887
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007888 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007889 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007891
7892 return p;
7893}
7894
7895 static char_u *
7896compile_endif(char_u *arg, cctx_T *cctx)
7897{
7898 scope_T *scope = cctx->ctx_scope;
7899 ifscope_T *ifscope;
7900 garray_T *instr = &cctx->ctx_instr;
7901 isn_T *isn;
7902
Bram Moolenaarfa984412021-03-25 22:15:28 +01007903 if (misplaced_cmdmod(cctx))
7904 return NULL;
7905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007906 if (scope == NULL || scope->se_type != IF_SCOPE)
7907 {
7908 emsg(_(e_endif_without_if));
7909 return NULL;
7910 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007911 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007912 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007913 if (!cctx->ctx_had_return)
7914 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007915
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007916 if (scope->se_u.se_if.is_if_label >= 0)
7917 {
7918 // previous "if" or "elseif" jumps here
7919 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7920 isn->isn_arg.jump.jump_where = instr->ga_len;
7921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007923 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007924
7925#ifdef FEAT_PROFILE
7926 // even when skipping we count the endif as executed, unless the block it's
7927 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007928 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007929 && scope->se_skip_save != SKIP_YES)
7930 {
7931 cctx->ctx_skip = SKIP_NOT;
7932 generate_instr(cctx, ISN_PROF_START);
7933 }
7934#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007935 cctx->ctx_skip = scope->se_skip_save;
7936
7937 // If all the blocks end in :return and there is an :else then the
7938 // had_return flag is set.
7939 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007941 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942 return arg;
7943}
7944
7945/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007946 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947 *
7948 * Produces instructions:
7949 * PUSHNR -1
7950 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007951 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7953 * - if beyond end, jump to "end"
7954 * - otherwise get item from list and push it
7955 * STORE var Store item in "var"
7956 * ... body ...
7957 * JUMP top Jump back to repeat
7958 * end: DROP Drop the result of "expr"
7959 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007960 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7961 * UNPACK 2 Split item in 2
7962 * STORE var1 Store item in "var1"
7963 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007964 */
7965 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007966compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007968 char_u *arg;
7969 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007970 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007971 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007972 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007973 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007974 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007975 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007976 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007978 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007979 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007980 lvar_T *loop_lvar; // loop iteration variable
7981 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007983 type_T *item_type = &t_any;
7984 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007985 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007986
Bram Moolenaar792f7862020-11-23 08:31:18 +01007987 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007988 if (p == NULL)
7989 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007990 if (var_count == 0)
7991 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007992 else
7993 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007994
7995 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007996 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007997 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7998 return NULL;
7999 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008001 if (*p == ':' && wp != p)
8002 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8003 else
8004 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005 return NULL;
8006 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008007 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008008 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8009 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008011 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8012 // instruction.
8013 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8014 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8015 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008016 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008017 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008018 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8019 .isn_arg.debug.dbg_break_lnum;
8020 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022 scope = new_scope(cctx, FOR_SCOPE);
8023 if (scope == NULL)
8024 return NULL;
8025
Bram Moolenaar792f7862020-11-23 08:31:18 +01008026 // Reserve a variable to store the loop iteration counter and initialize it
8027 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008028 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8029 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008030 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008031 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008032 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008033 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008034 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008035 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008036
8037 // compile "expr", it remains on the stack until "endfor"
8038 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008039 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008040 {
8041 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008042 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008043 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008044 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008045
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008046 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01008047 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008048 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01008049 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008050 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01008052 semsg(_(e_for_loop_on_str_not_supported),
8053 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008054 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 return NULL;
8056 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008057
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008058 if (vartype->tt_type == VAR_STRING)
8059 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008060 else if (vartype->tt_type == VAR_BLOB)
8061 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008062 else if (vartype->tt_type == VAR_LIST
8063 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008064 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02008065 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008066 item_type = vartype->tt_member;
8067 else if (vartype->tt_member->tt_type == VAR_LIST
8068 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008069 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01008070 item_type = vartype->tt_member->tt_member;
8071 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008072
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008073 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01008074 generate_undo_cmdmods(cctx);
8075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01008077 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008078
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008079 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080
Bram Moolenaar792f7862020-11-23 08:31:18 +01008081 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008082 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008083 {
8084 generate_UNPACK(cctx, var_count, semicolon);
8085 arg = skipwhite(arg + 1); // skip white after '['
8086
8087 // the list item is replaced by a number of items
Bram Moolenaar35578162021-08-02 19:10:38 +02008088 if (GA_GROW_FAILS(stack, var_count - 1))
Bram Moolenaar792f7862020-11-23 08:31:18 +01008089 {
8090 drop_scope(cctx);
8091 return NULL;
8092 }
8093 --stack->ga_len;
8094 for (idx = 0; idx < var_count; ++idx)
8095 {
8096 ((type_T **)stack->ga_data)[stack->ga_len] =
8097 (semicolon && idx == 0) ? vartype : item_type;
8098 ++stack->ga_len;
8099 }
8100 }
8101
8102 for (idx = 0; idx < var_count; ++idx)
8103 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008104 assign_dest_T dest = dest_local;
8105 int opt_flags = 0;
8106 int vimvaridx = -1;
8107 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008108 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02008109 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008110
8111 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008112 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008113 name = vim_strnsave(arg, varlen);
8114 if (name == NULL)
8115 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008116 if (*p == ':')
8117 {
8118 p = skipwhite(p + 1);
8119 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8120 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008121
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008122 // TODO: script var not supported?
8123 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
8124 &vimvaridx, &type, cctx) == FAIL)
8125 goto failed;
8126 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008127 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008128 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8129 0, 0, type, name) == FAIL)
8130 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008131 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008132 else if (varlen == 1 && *arg == '_')
8133 {
8134 // Assigning to "_": drop the value.
8135 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8136 goto failed;
8137 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008138 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008139 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008140 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008141 {
8142 semsg(_(e_variable_already_declared), arg);
8143 goto failed;
8144 }
8145
Bram Moolenaarea870692020-12-02 14:24:30 +01008146 if (STRNCMP(name, "s:", 2) == 0)
8147 {
8148 semsg(_(e_cannot_declare_script_variable_in_function), name);
8149 goto failed;
8150 }
8151
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008152 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008153 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008154 where.wt_variable = TRUE;
8155 if (lhs_type == &t_any)
8156 lhs_type = item_type;
8157 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008158 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008159 ? need_type(item_type, lhs_type,
8160 -1, 0, cctx, FALSE, FALSE)
8161 : check_type(lhs_type, item_type, TRUE, where))
8162 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008163 goto failed;
8164 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008165 if (var_lvar == NULL)
8166 // out of memory or used as an argument
8167 goto failed;
8168
8169 if (semicolon && idx == var_count - 1)
8170 var_lvar->lv_type = vartype;
8171 else
8172 var_lvar->lv_type = item_type;
8173 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8174 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008175
8176 if (*p == ',' || *p == ';')
8177 ++p;
8178 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008179 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008180 }
8181
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008182 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008183 {
8184 int save_prev_lnum = cctx->ctx_prev_lnum;
8185
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008186 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008187 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8188 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008189 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008190 cctx->ctx_prev_lnum = save_prev_lnum;
8191 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008192
Bram Moolenaar792f7862020-11-23 08:31:18 +01008193 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008194
8195failed:
8196 vim_free(name);
8197 drop_scope(cctx);
8198 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199}
8200
8201/*
8202 * compile "endfor"
8203 */
8204 static char_u *
8205compile_endfor(char_u *arg, cctx_T *cctx)
8206{
8207 garray_T *instr = &cctx->ctx_instr;
8208 scope_T *scope = cctx->ctx_scope;
8209 forscope_T *forscope;
8210 isn_T *isn;
8211
Bram Moolenaarfa984412021-03-25 22:15:28 +01008212 if (misplaced_cmdmod(cctx))
8213 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008215 if (scope == NULL || scope->se_type != FOR_SCOPE)
8216 {
8217 emsg(_(e_for));
8218 return NULL;
8219 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008220 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008222 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008223
8224 // At end of ":for" scope jump back to the FOR instruction.
8225 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8226
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008227 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8229 isn->isn_arg.forloop.for_end = instr->ga_len;
8230
8231 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008232 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008233
8234 // Below the ":for" scope drop the "expr" list from the stack.
8235 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8236 return NULL;
8237
8238 vim_free(scope);
8239
8240 return arg;
8241}
8242
8243/*
8244 * compile "while expr"
8245 *
8246 * Produces instructions:
8247 * top: EVAL expr Push result of "expr"
8248 * JUMP_IF_FALSE end jump if false
8249 * ... body ...
8250 * JUMP top Jump back to repeat
8251 * end:
8252 *
8253 */
8254 static char_u *
8255compile_while(char_u *arg, cctx_T *cctx)
8256{
8257 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008258 scope_T *scope;
8259
8260 scope = new_scope(cctx, WHILE_SCOPE);
8261 if (scope == NULL)
8262 return NULL;
8263
Bram Moolenaara91a7132021-03-25 21:12:15 +01008264 // "endwhile" jumps back here, one before when profiling or using cmdmods
8265 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266
8267 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008268 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008269 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008270 if (!ends_excmd2(arg, skipwhite(p)))
8271 {
8272 semsg(_(e_trailing_arg), p);
8273 return NULL;
8274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275
Bram Moolenaar13106602020-10-04 16:06:05 +02008276 if (bool_on_stack(cctx) == FAIL)
8277 return FAIL;
8278
Bram Moolenaara91a7132021-03-25 21:12:15 +01008279 // CMDMOD_REV must come before the jump
8280 generate_undo_cmdmods(cctx);
8281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008282 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008283 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008284 JUMP_IF_FALSE, cctx) == FAIL)
8285 return FAIL;
8286
8287 return p;
8288}
8289
8290/*
8291 * compile "endwhile"
8292 */
8293 static char_u *
8294compile_endwhile(char_u *arg, cctx_T *cctx)
8295{
8296 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008297 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008298
Bram Moolenaarfa984412021-03-25 22:15:28 +01008299 if (misplaced_cmdmod(cctx))
8300 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8302 {
8303 emsg(_(e_while));
8304 return NULL;
8305 }
8306 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008307 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008308
Bram Moolenaarf002a412021-01-24 13:34:18 +01008309#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008310 // count the endwhile before jumping
8311 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008312#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008315 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316
8317 // Fill in the "end" label in the WHILE statement so it can jump here.
8318 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008319 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8320 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321
8322 vim_free(scope);
8323
8324 return arg;
8325}
8326
8327/*
8328 * compile "continue"
8329 */
8330 static char_u *
8331compile_continue(char_u *arg, cctx_T *cctx)
8332{
8333 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008334 int try_scopes = 0;
8335 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336
8337 for (;;)
8338 {
8339 if (scope == NULL)
8340 {
8341 emsg(_(e_continue));
8342 return NULL;
8343 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008344 if (scope->se_type == FOR_SCOPE)
8345 {
8346 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008348 }
8349 if (scope->se_type == WHILE_SCOPE)
8350 {
8351 loop_label = scope->se_u.se_while.ws_top_label;
8352 break;
8353 }
8354 if (scope->se_type == TRY_SCOPE)
8355 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356 scope = scope->se_outer;
8357 }
8358
Bram Moolenaarc150c092021-02-13 15:02:46 +01008359 if (try_scopes > 0)
8360 // Inside one or more try/catch blocks we first need to jump to the
8361 // "finally" or "endtry" to cleanup.
8362 generate_TRYCONT(cctx, try_scopes, loop_label);
8363 else
8364 // Jump back to the FOR or WHILE instruction.
8365 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008367 return arg;
8368}
8369
8370/*
8371 * compile "break"
8372 */
8373 static char_u *
8374compile_break(char_u *arg, cctx_T *cctx)
8375{
8376 scope_T *scope = cctx->ctx_scope;
8377 endlabel_T **el;
8378
8379 for (;;)
8380 {
8381 if (scope == NULL)
8382 {
8383 emsg(_(e_break));
8384 return NULL;
8385 }
8386 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8387 break;
8388 scope = scope->se_outer;
8389 }
8390
8391 // Jump to the end of the FOR or WHILE loop.
8392 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008393 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008395 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8397 return FAIL;
8398
8399 return arg;
8400}
8401
8402/*
8403 * compile "{" start of block
8404 */
8405 static char_u *
8406compile_block(char_u *arg, cctx_T *cctx)
8407{
8408 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8409 return NULL;
8410 return skipwhite(arg + 1);
8411}
8412
8413/*
8414 * compile end of block: drop one scope
8415 */
8416 static void
8417compile_endblock(cctx_T *cctx)
8418{
8419 scope_T *scope = cctx->ctx_scope;
8420
8421 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008422 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423 vim_free(scope);
8424}
8425
8426/*
8427 * compile "try"
8428 * Creates a new scope for the try-endtry, pointing to the first catch and
8429 * finally.
8430 * Creates another scope for the "try" block itself.
8431 * TRY instruction sets up exception handling at runtime.
8432 *
8433 * "try"
8434 * TRY -> catch1, -> finally push trystack entry
8435 * ... try block
8436 * "throw {exception}"
8437 * EVAL {exception}
8438 * THROW create exception
8439 * ... try block
8440 * " catch {expr}"
8441 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008442 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008443 * EVAL {expr}
8444 * MATCH
8445 * JUMP nomatch -> catch2
8446 * CATCH remove exception
8447 * ... catch block
8448 * " catch"
8449 * JUMP -> finally
8450 * catch2: CATCH remove exception
8451 * ... catch block
8452 * " finally"
8453 * finally:
8454 * ... finally block
8455 * " endtry"
8456 * ENDTRY pop trystack entry, may rethrow
8457 */
8458 static char_u *
8459compile_try(char_u *arg, cctx_T *cctx)
8460{
8461 garray_T *instr = &cctx->ctx_instr;
8462 scope_T *try_scope;
8463 scope_T *scope;
8464
Bram Moolenaarfa984412021-03-25 22:15:28 +01008465 if (misplaced_cmdmod(cctx))
8466 return NULL;
8467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008468 // scope that holds the jumps that go to catch/finally/endtry
8469 try_scope = new_scope(cctx, TRY_SCOPE);
8470 if (try_scope == NULL)
8471 return NULL;
8472
Bram Moolenaar69f70502021-01-01 16:10:46 +01008473 if (cctx->ctx_skip != SKIP_YES)
8474 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008475 isn_T *isn;
8476
8477 // "try_catch" is set when the first ":catch" is found or when no catch
8478 // is found and ":finally" is found.
8479 // "try_finally" is set when ":finally" is found
8480 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008481 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008482 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8483 return NULL;
8484 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8485 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008486 return NULL;
8487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488
8489 // scope for the try block itself
8490 scope = new_scope(cctx, BLOCK_SCOPE);
8491 if (scope == NULL)
8492 return NULL;
8493
8494 return arg;
8495}
8496
8497/*
8498 * compile "catch {expr}"
8499 */
8500 static char_u *
8501compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8502{
8503 scope_T *scope = cctx->ctx_scope;
8504 garray_T *instr = &cctx->ctx_instr;
8505 char_u *p;
8506 isn_T *isn;
8507
Bram Moolenaarfa984412021-03-25 22:15:28 +01008508 if (misplaced_cmdmod(cctx))
8509 return NULL;
8510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008511 // end block scope from :try or :catch
8512 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8513 compile_endblock(cctx);
8514 scope = cctx->ctx_scope;
8515
8516 // Error if not in a :try scope
8517 if (scope == NULL || scope->se_type != TRY_SCOPE)
8518 {
8519 emsg(_(e_catch));
8520 return NULL;
8521 }
8522
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008523 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008524 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008525 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008526 return NULL;
8527 }
8528
Bram Moolenaar69f70502021-01-01 16:10:46 +01008529 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008530 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008531#ifdef FEAT_PROFILE
8532 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008533 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008534 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008535 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008536 .isn_type == ISN_PROF_START)
8537 --instr->ga_len;
8538#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008539 // Jump from end of previous block to :finally or :endtry
8540 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8541 JUMP_ALWAYS, cctx) == FAIL)
8542 return NULL;
8543
8544 // End :try or :catch scope: set value in ISN_TRY instruction
8545 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008546 if (isn->isn_arg.try.try_ref->try_catch == 0)
8547 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008548 if (scope->se_u.se_try.ts_catch_label != 0)
8549 {
8550 // Previous catch without match jumps here
8551 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8552 isn->isn_arg.jump.jump_where = instr->ga_len;
8553 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008554#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008555 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008556 {
8557 // a "throw" that jumps here needs to be counted
8558 generate_instr(cctx, ISN_PROF_END);
8559 // the "catch" is also counted
8560 generate_instr(cctx, ISN_PROF_START);
8561 }
8562#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008563 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008564 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008565 }
8566
8567 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008568 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008569 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008570 scope->se_u.se_try.ts_caught_all = TRUE;
8571 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008572 }
8573 else
8574 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008575 char_u *end;
8576 char_u *pat;
8577 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008578 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008579 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008581 // Push v:exception, push {expr} and MATCH
8582 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8583
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008584 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008585 if (*end != *p)
8586 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008587 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008588 vim_free(tofree);
8589 return FAIL;
8590 }
8591 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008592 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008593 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008594 len = (int)(end - tofree);
8595 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008596 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008597 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008598 if (pat == NULL)
8599 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008600 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008601 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008603 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8604 return NULL;
8605
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008606 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008607 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8608 return NULL;
8609 }
8610
Bram Moolenaar69f70502021-01-01 16:10:46 +01008611 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612 return NULL;
8613
8614 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8615 return NULL;
8616 return p;
8617}
8618
8619 static char_u *
8620compile_finally(char_u *arg, cctx_T *cctx)
8621{
8622 scope_T *scope = cctx->ctx_scope;
8623 garray_T *instr = &cctx->ctx_instr;
8624 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008625 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008626
Bram Moolenaarfa984412021-03-25 22:15:28 +01008627 if (misplaced_cmdmod(cctx))
8628 return NULL;
8629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008630 // end block scope from :try or :catch
8631 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8632 compile_endblock(cctx);
8633 scope = cctx->ctx_scope;
8634
8635 // Error if not in a :try scope
8636 if (scope == NULL || scope->se_type != TRY_SCOPE)
8637 {
8638 emsg(_(e_finally));
8639 return NULL;
8640 }
8641
rbtnn84934992021-08-07 13:26:53 +02008642 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643 {
rbtnn84934992021-08-07 13:26:53 +02008644 // End :catch or :finally scope: set value in ISN_TRY instruction
8645 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8646 if (isn->isn_arg.try.try_ref->try_finally != 0)
8647 {
8648 emsg(_(e_finally_dup));
8649 return NULL;
8650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651
rbtnn84934992021-08-07 13:26:53 +02008652 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008653#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008654 if (cctx->ctx_compile_type == CT_PROFILE
8655 && ((isn_T *)instr->ga_data)[this_instr - 1]
8656 .isn_type == ISN_PROF_START)
8657 {
8658 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008659 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008660
8661 // jump to the profile end above it
8662 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8663 .isn_type == ISN_PROF_END)
8664 --this_instr;
8665 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008666#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008667
rbtnn84934992021-08-07 13:26:53 +02008668 // Fill in the "end" label in jumps at the end of the blocks.
8669 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8670 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008671
rbtnn84934992021-08-07 13:26:53 +02008672 // If there is no :catch then an exception jumps to :finally.
8673 if (isn->isn_arg.try.try_ref->try_catch == 0)
8674 isn->isn_arg.try.try_ref->try_catch = this_instr;
8675 isn->isn_arg.try.try_ref->try_finally = this_instr;
8676 if (scope->se_u.se_try.ts_catch_label != 0)
8677 {
8678 // Previous catch without match jumps here
8679 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8680 isn->isn_arg.jump.jump_where = this_instr;
8681 scope->se_u.se_try.ts_catch_label = 0;
8682 }
8683 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8684 return NULL;
8685
8686 // TODO: set index in ts_finally_label jumps
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008688
8689 return arg;
8690}
8691
8692 static char_u *
8693compile_endtry(char_u *arg, cctx_T *cctx)
8694{
8695 scope_T *scope = cctx->ctx_scope;
8696 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008697 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008698
Bram Moolenaarfa984412021-03-25 22:15:28 +01008699 if (misplaced_cmdmod(cctx))
8700 return NULL;
8701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008702 // end block scope from :catch or :finally
8703 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8704 compile_endblock(cctx);
8705 scope = cctx->ctx_scope;
8706
8707 // Error if not in a :try scope
8708 if (scope == NULL || scope->se_type != TRY_SCOPE)
8709 {
8710 if (scope == NULL)
8711 emsg(_(e_no_endtry));
8712 else if (scope->se_type == WHILE_SCOPE)
8713 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008714 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715 emsg(_(e_endfor));
8716 else
8717 emsg(_(e_endif));
8718 return NULL;
8719 }
8720
Bram Moolenaarc150c092021-02-13 15:02:46 +01008721 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008722 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008723 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008724 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8725 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008726 {
8727 emsg(_(e_missing_catch_or_finally));
8728 return NULL;
8729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008730
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008731#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008732 if (cctx->ctx_compile_type == CT_PROFILE
8733 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008734 .isn_type == ISN_PROF_START)
8735 // move the profile start after "endtry" so that it's not counted when
8736 // the exception is rethrown.
8737 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008738#endif
8739
Bram Moolenaar69f70502021-01-01 16:10:46 +01008740 // Fill in the "end" label in jumps at the end of the blocks, if not
8741 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008742 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8743 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008744
Bram Moolenaar69f70502021-01-01 16:10:46 +01008745 if (scope->se_u.se_try.ts_catch_label != 0)
8746 {
8747 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008748 isn_T *isn = ((isn_T *)instr->ga_data)
8749 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008750 isn->isn_arg.jump.jump_where = instr->ga_len;
8751 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008752 }
8753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008754 compile_endblock(cctx);
8755
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008756 if (cctx->ctx_skip != SKIP_YES)
8757 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008758 // End :catch or :finally scope: set instruction index in ISN_TRY
8759 // instruction
8760 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008761 if (cctx->ctx_skip != SKIP_YES
8762 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8763 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008764#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008765 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008766 generate_instr(cctx, ISN_PROF_START);
8767#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008769 return arg;
8770}
8771
8772/*
8773 * compile "throw {expr}"
8774 */
8775 static char_u *
8776compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8777{
8778 char_u *p = skipwhite(arg);
8779
Bram Moolenaara5565e42020-05-09 15:44:01 +02008780 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008781 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008782 if (cctx->ctx_skip == SKIP_YES)
8783 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008784 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008785 return NULL;
8786 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8787 return NULL;
8788
8789 return p;
8790}
8791
Bram Moolenaarc3235272021-07-10 19:42:03 +02008792 static char_u *
8793compile_eval(char_u *arg, cctx_T *cctx)
8794{
8795 char_u *p = arg;
8796 int name_only;
8797 char_u *alias;
8798 long lnum = SOURCING_LNUM;
8799
8800 // find_ex_command() will consider a variable name an expression, assuming
8801 // that something follows on the next line. Check that something actually
8802 // follows, otherwise it's probably a misplaced command.
8803 get_name_len(&p, &alias, FALSE, FALSE);
8804 name_only = ends_excmd2(arg, skipwhite(p));
8805 vim_free(alias);
8806
8807 p = arg;
8808 if (compile_expr0(&p, cctx) == FAIL)
8809 return NULL;
8810
8811 if (name_only && lnum == SOURCING_LNUM)
8812 {
8813 semsg(_(e_expression_without_effect_str), arg);
8814 return NULL;
8815 }
8816
8817 // drop the result
8818 generate_instr_drop(cctx, ISN_DROP, 1);
8819
8820 return skipwhite(p);
8821}
8822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008823/*
8824 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008825 * compile "echomsg expr"
8826 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008827 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008828 * compile "execute expr"
8829 */
8830 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008831compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008832{
8833 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008834 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008835 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008836 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008837 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008838 garray_T *stack = &cctx->ctx_type_stack;
8839 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008840
8841 for (;;)
8842 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008843 if (ends_excmd2(prev, p))
8844 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008845 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008846 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008847 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008848
8849 if (cctx->ctx_skip != SKIP_YES)
8850 {
8851 // check for non-void type
8852 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8853 if (type->tt_type == VAR_VOID)
8854 {
8855 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8856 return NULL;
8857 }
8858 }
8859
Bram Moolenaarad39c092020-02-26 18:23:43 +01008860 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008861 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008862 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008863 }
8864
Bram Moolenaare4984292020-12-13 14:19:25 +01008865 if (count > 0)
8866 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008867 long save_lnum = cctx->ctx_lnum;
8868
8869 // Use the line number where the command started.
8870 cctx->ctx_lnum = start_ctx_lnum;
8871
Bram Moolenaare4984292020-12-13 14:19:25 +01008872 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8873 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8874 else if (cmdidx == CMD_execute)
8875 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8876 else if (cmdidx == CMD_echomsg)
8877 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02008878 else if (cmdidx == CMD_echoconsole)
8879 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01008880 else
8881 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008882
8883 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008885 return p;
8886}
8887
8888/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008889 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008890 * instruction to compute it and return OK.
8891 * Otherwise return FAIL, the caller must deal with any range.
8892 */
8893 static int
8894compile_variable_range(exarg_T *eap, cctx_T *cctx)
8895{
8896 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8897 char_u *p = skipdigits(eap->cmd);
8898
8899 if (p == range_end)
8900 return FAIL;
8901 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8902}
8903
8904/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008905 * :put r
8906 * :put ={expr}
8907 */
8908 static char_u *
8909compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8910{
8911 char_u *line = arg;
8912 linenr_T lnum;
8913 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008914 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008915
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008916 eap->regname = *line;
8917
8918 if (eap->regname == '=')
8919 {
8920 char_u *p = line + 1;
8921
8922 if (compile_expr0(&p, cctx) == FAIL)
8923 return NULL;
8924 line = p;
8925 }
8926 else if (eap->regname != NUL)
8927 ++line;
8928
Bram Moolenaar08597872020-12-10 19:43:40 +01008929 if (compile_variable_range(eap, cctx) == OK)
8930 {
8931 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8932 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008933 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008934 {
8935 // Either no range or a number.
8936 // "errormsg" will not be set because the range is ADDR_LINES.
8937 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008938 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008939 return NULL;
8940 if (eap->addr_count == 0)
8941 lnum = -1;
8942 else
8943 lnum = eap->line2;
8944 if (above)
8945 --lnum;
8946 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008947
8948 generate_PUT(cctx, eap->regname, lnum);
8949 return line;
8950}
8951
8952/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008953 * A command that is not compiled, execute with legacy code.
8954 */
8955 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008956compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008957{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008958 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02008959 char_u *p;
8960 int has_expr = FALSE;
8961 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02008962 char_u *tofree = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008963
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008964 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008965 goto theend;
8966
Bram Moolenaare729ce22021-06-06 21:38:09 +02008967 // If there was a prececing command modifier, drop it and include it in the
8968 // EXEC command.
8969 if (cctx->ctx_has_cmdmod)
8970 {
8971 garray_T *instr = &cctx->ctx_instr;
8972 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8973
8974 if (isn->isn_type == ISN_CMDMOD)
8975 {
8976 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8977 ->cmod_filter_regmatch.regprog);
8978 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8979 --instr->ga_len;
8980 cctx->ctx_has_cmdmod = FALSE;
8981 }
8982 }
8983
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008984 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008985 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008986 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008987 int usefilter = FALSE;
8988
8989 has_expr = argt & (EX_XFILE | EX_EXPAND);
8990
8991 // If the command can be followed by a bar, find the bar and truncate
8992 // it, so that the following command can be compiled.
8993 // The '|' is overwritten with a NUL, it is put back below.
8994 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8995 && *eap->arg == '!')
8996 // :w !filter or :r !filter or :r! filter
8997 usefilter = TRUE;
8998 if ((argt & EX_TRLBAR) && !usefilter)
8999 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009000 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009001 separate_nextcmd(eap);
9002 if (eap->nextcmd != NULL)
9003 nextcmd = eap->nextcmd;
9004 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009005 else if (eap->cmdidx == CMD_wincmd)
9006 {
9007 p = eap->arg;
9008 if (*p != NUL)
9009 ++p;
9010 if (*p == 'g' || *p == Ctrl_G)
9011 ++p;
9012 p = skipwhite(p);
9013 if (*p == '|')
9014 {
9015 *p = NUL;
9016 nextcmd = p + 1;
9017 }
9018 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009019 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9020 {
9021 // If there is a trailing '{' read lines until the '}'
9022 p = eap->arg + STRLEN(eap->arg) - 1;
9023 while (p > eap->arg && VIM_ISWHITE(*p))
9024 --p;
9025 if (*p == '{')
9026 {
9027 exarg_T ea;
9028 int flags; // unused
9029 int start_lnum = SOURCING_LNUM;
9030
9031 CLEAR_FIELD(ea);
9032 ea.arg = eap->arg;
9033 fill_exarg_from_cctx(&ea, cctx);
9034 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9035 if (tofree != NULL)
9036 {
9037 *p = NUL;
9038 line = concat_str(line, tofree);
9039 if (line == NULL)
9040 goto theend;
9041 vim_free(tofree);
9042 tofree = line;
9043 SOURCING_LNUM = start_lnum;
9044 }
9045 }
9046 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009047 }
9048
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009049 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9050 {
9051 // expand filename in "syntax include [@group] filename"
9052 has_expr = TRUE;
9053 eap->arg = skipwhite(eap->arg + 7);
9054 if (*eap->arg == '@')
9055 eap->arg = skiptowhite(eap->arg);
9056 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009057
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009058 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9059 && STRLEN(eap->arg) > 4)
9060 {
9061 int delim = *eap->arg;
9062
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009063 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009064 if (*p == delim)
9065 {
9066 eap->arg = p + 1;
9067 has_expr = TRUE;
9068 }
9069 }
9070
Bram Moolenaarecac5912021-01-05 19:23:28 +01009071 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
9072 {
9073 // TODO: should only expand when appropriate for the command
9074 eap->arg = skiptowhite(eap->arg);
9075 has_expr = TRUE;
9076 }
9077
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009078 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009079 {
9080 int count = 0;
9081 char_u *start = skipwhite(line);
9082
9083 // :cmd xxx`=expr1`yyy`=expr2`zzz
9084 // PUSHS ":cmd xxx"
9085 // eval expr1
9086 // PUSHS "yyy"
9087 // eval expr2
9088 // PUSHS "zzz"
9089 // EXECCONCAT 5
9090 for (;;)
9091 {
9092 if (p > start)
9093 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009094 char_u *val = vim_strnsave(start, p - start);
9095
9096 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009097 ++count;
9098 }
9099 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009100 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009101 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009102 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009103 ++count;
9104 p = skipwhite(p);
9105 if (*p != '`')
9106 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009107 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009108 return NULL;
9109 }
9110 start = p + 1;
9111
9112 p = (char_u *)strstr((char *)start, "`=");
9113 if (p == NULL)
9114 {
9115 if (*skipwhite(start) != NUL)
9116 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009117 char_u *val = vim_strsave(start);
9118
9119 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009120 ++count;
9121 }
9122 break;
9123 }
9124 }
9125 generate_EXECCONCAT(cctx, count);
9126 }
9127 else
Bram Moolenaaraacc9662021-08-13 19:40:51 +02009128 generate_EXEC(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009129
9130theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009131 if (*nextcmd != NUL)
9132 {
9133 // the parser expects a pointer to the bar, put it back
9134 --nextcmd;
9135 *nextcmd = '|';
9136 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009137 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009138
9139 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009140}
9141
Bram Moolenaar20677332021-06-06 17:02:53 +02009142/*
9143 * A script command with heredoc, e.g.
9144 * ruby << EOF
9145 * command
9146 * EOF
9147 * Has been turned into one long line with NL characters by
9148 * get_function_body():
9149 * ruby << EOF<NL> command<NL>EOF
9150 */
9151 static char_u *
9152compile_script(char_u *line, cctx_T *cctx)
9153{
9154 if (cctx->ctx_skip != SKIP_YES)
9155 {
9156 isn_T *isn;
9157
9158 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9159 return NULL;
9160 isn->isn_arg.string = vim_strsave(line);
9161 }
9162 return (char_u *)"";
9163}
9164
Bram Moolenaar8238f082021-04-20 21:10:48 +02009165
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009166/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009167 * :s/pat/repl/
9168 */
9169 static char_u *
9170compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9171{
9172 char_u *cmd = eap->arg;
9173 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9174
9175 if (expr != NULL)
9176 {
9177 int delimiter = *cmd++;
9178
9179 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009180 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009181 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9182 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009183 garray_T save_ga = cctx->ctx_instr;
9184 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009185 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009186 int trailing_error;
9187 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009188 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009189 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009190
9191 cmd += 3;
9192 end = skip_substitute(cmd, delimiter);
9193
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009194 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009195 // labels are correct.
9196 cctx->ctx_instr.ga_len = 0;
9197 cctx->ctx_instr.ga_maxlen = 0;
9198 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009199 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009200 if (end[-1] == NUL)
9201 end[-1] = delimiter;
9202 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009203 trailing_error = *cmd != delimiter && *cmd != NUL;
9204
Bram Moolenaar169502f2021-04-21 12:19:35 +02009205 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009206 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009207 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009208 if (trailing_error)
9209 semsg(_(e_trailing_arg), cmd);
9210 clear_instr_ga(&cctx->ctx_instr);
9211 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009212 return NULL;
9213 }
9214
Bram Moolenaar8238f082021-04-20 21:10:48 +02009215 // Move the generated instructions into the ISN_SUBSTITUTE
9216 // instructions, then restore the list of instructions before
9217 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009218 instr_count = cctx->ctx_instr.ga_len;
9219 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009220 instr[instr_count].isn_type = ISN_FINISH;
9221
9222 cctx->ctx_instr = save_ga;
9223 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9224 {
9225 int idx;
9226
9227 for (idx = 0; idx < instr_count; ++idx)
9228 delete_instr(instr + idx);
9229 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009230 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009231 }
9232 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9233 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009234
9235 // skip over flags
9236 if (*end == '&')
9237 ++end;
9238 while (ASCII_ISALPHA(*end) || *end == '#')
9239 ++end;
9240 return end;
9241 }
9242 }
9243
9244 return compile_exec(arg, eap, cctx);
9245}
9246
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009247 static char_u *
9248compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9249{
9250 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009251 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009252
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009253 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009254 {
9255 if (STRNCMP(arg, "END", 3) == 0)
9256 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009257 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009258 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009259 // First load the current variable value.
9260 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9261 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009262 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009263 }
9264
9265 // Gets the redirected text and put it on the stack, then store it
9266 // in the variable.
9267 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9268
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009269 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009270 generate_instr_drop(cctx, ISN_CONCAT, 1);
9271
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009272 if (lhs->lhs_has_index)
9273 {
9274 // Use the info in "lhs" to store the value at the index in the
9275 // list or dict.
9276 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9277 &t_string, cctx) == FAIL)
9278 return NULL;
9279 }
9280 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009281 return NULL;
9282
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009283 VIM_CLEAR(lhs->lhs_name);
9284 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009285 return arg + 3;
9286 }
9287 emsg(_(e_cannot_nest_redir));
9288 return NULL;
9289 }
9290
9291 if (arg[0] == '=' && arg[1] == '>')
9292 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009293 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009294
9295 // redirect to a variable is compiled
9296 arg += 2;
9297 if (*arg == '>')
9298 {
9299 ++arg;
9300 append = TRUE;
9301 }
9302 arg = skipwhite(arg);
9303
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009304 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009305 FALSE, FALSE, 1, cctx) == FAIL)
9306 return NULL;
9307 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009308 lhs->lhs_append = append;
9309 if (lhs->lhs_has_index)
9310 {
9311 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9312 if (lhs->lhs_whole == NULL)
9313 return NULL;
9314 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009315
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009316 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009317 }
9318
9319 // other redirects are handled like at script level
9320 return compile_exec(line, eap, cctx);
9321}
9322
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009323#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009324 static char_u *
9325compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9326{
9327 isn_T *isn;
9328 char_u *p;
9329
9330 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9331 if (isn == NULL)
9332 return NULL;
9333 isn->isn_arg.number = eap->cmdidx;
9334
9335 p = eap->arg;
9336 if (compile_expr0(&p, cctx) == FAIL)
9337 return NULL;
9338
9339 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9340 if (isn == NULL)
9341 return NULL;
9342 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9343 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9344 return NULL;
9345 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9346 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9347 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9348
9349 return p;
9350}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009351#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009352
Bram Moolenaar4c137212021-04-19 16:48:48 +02009353/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009354 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009355 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009356 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009357 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009358add_def_function(ufunc_T *ufunc)
9359{
9360 dfunc_T *dfunc;
9361
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009362 if (def_functions.ga_len == 0)
9363 {
9364 // The first position is not used, so that a zero uf_dfunc_idx means it
9365 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009366 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009367 return FAIL;
9368 ++def_functions.ga_len;
9369 }
9370
Bram Moolenaar09689a02020-05-09 22:50:08 +02009371 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009372 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009373 return FAIL;
9374 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9375 CLEAR_POINTER(dfunc);
9376 dfunc->df_idx = def_functions.ga_len;
9377 ufunc->uf_dfunc_idx = dfunc->df_idx;
9378 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009379 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009380 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009381 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009382 ++def_functions.ga_len;
9383 return OK;
9384}
9385
9386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009387 * After ex_function() has collected all the function lines: parse and compile
9388 * the lines into instructions.
9389 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009390 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9391 * the return statement (used for lambda). When uf_ret_type is already set
9392 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009393 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009394 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009395 * This can be used recursively through compile_lambda(), which may reallocate
9396 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009397 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009398 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009399 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009400compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009401 ufunc_T *ufunc,
9402 int check_return_type,
9403 compiletype_T compile_type,
9404 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009405{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009406 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009407 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009408 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009410 cctx_T cctx;
9411 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009412 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009413 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009414 int ret = FAIL;
9415 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009416 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009417 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009418 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009419 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009420#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009421 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009422#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009423 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009424
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009425 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009426 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009427 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009428 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009429 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9430 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009431 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009432
9433 switch (compile_type)
9434 {
9435 case CT_PROFILE:
9436#ifdef FEAT_PROFILE
9437 instr_dest = dfunc->df_instr_prof; break;
9438#endif
9439 case CT_NONE: instr_dest = dfunc->df_instr; break;
9440 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9441 }
9442 if (instr_dest != NULL)
9443 // Was compiled in this mode before: Free old instructions.
9444 delete_def_function_contents(dfunc, FALSE);
9445 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009446 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009447 else
9448 {
9449 if (add_def_function(ufunc) == FAIL)
9450 return FAIL;
9451 new_def_function = TRUE;
9452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009453
Bram Moolenaar985116a2020-07-12 17:31:09 +02009454 ufunc->uf_def_status = UF_COMPILING;
9455
Bram Moolenaara80faa82020-04-12 19:37:17 +02009456 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009457
Bram Moolenaare99d4222021-06-13 14:01:26 +02009458 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009459 cctx.ctx_ufunc = ufunc;
9460 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009461 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009462 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9463 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9464 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9465 cctx.ctx_type_list = &ufunc->uf_type_list;
9466 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9467 instr = &cctx.ctx_instr;
9468
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009469 // Set the context to the function, it may be compiled when called from
9470 // another script. Set the script version to the most modern one.
9471 // The line number will be set in next_line_from_context().
9472 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009473 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9474
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009475 // Don't use the flag from ":legacy" here.
9476 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9477
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009478 // Make sure error messages are OK.
9479 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9480 if (do_estack_push)
9481 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009482 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009483
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009484 if (ufunc->uf_def_args.ga_len > 0)
9485 {
9486 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009487 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009488 int i;
9489 char_u *arg;
9490 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009491 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009492
9493 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009494 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009495 for (i = 0; i < count; ++i)
9496 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009497 garray_T *stack = &cctx.ctx_type_stack;
9498 type_T *val_type;
9499 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009500 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009501 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009502 int jump_instr_idx = instr->ga_len;
9503 isn_T *isn;
9504
9505 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9506 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9507 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009508
9509 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009510 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009511
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009512 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009513 r = compile_expr0(&arg, &cctx);
9514
Bram Moolenaar12bce952021-03-11 20:04:04 +01009515 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009516 goto erret;
9517
9518 // If no type specified use the type of the default value.
9519 // Otherwise check that the default value type matches the
9520 // specified type.
9521 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009522 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009523 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009524 {
9525 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009526 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009527 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009528 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009529 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009530 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009531
9532 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009533 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009534
9535 // set instruction index in JUMP_IF_ARG_SET to here
9536 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9537 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009538 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009539
9540 if (did_set_arg_type)
9541 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009542 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009543 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009544
9545 /*
9546 * Loop over all the lines of the function and generate instructions.
9547 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009548 for (;;)
9549 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009550 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009551 int starts_with_colon = FALSE;
9552 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009553 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009554
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009555 // Bail out on the first error to avoid a flood of errors and report
9556 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009557 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009558 goto erret;
9559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009560 if (line != NULL && *line == '|')
9561 // the line continues after a '|'
9562 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009563 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009564 && !(*line == '#' && (line == cctx.ctx_line_start
9565 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009566 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009567 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009568 goto erret;
9569 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009570 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9571 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009572 else
9573 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009574 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009575 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009576 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009577 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009578#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009579 if (cctx.ctx_skip != SKIP_YES)
9580 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009581#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009582 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009583 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009584 // Make a copy, splitting off nextcmd and removing trailing spaces
9585 // may change it.
9586 if (line != NULL)
9587 {
9588 line = vim_strsave(line);
9589 vim_free(line_to_free);
9590 line_to_free = line;
9591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009592 }
9593
Bram Moolenaara80faa82020-04-12 19:37:17 +02009594 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595 ea.cmdlinep = &line;
9596 ea.cmd = skipwhite(line);
9597
Bram Moolenaarb2049902021-01-24 12:53:53 +01009598 if (*ea.cmd == '#')
9599 {
9600 // "#" starts a comment
9601 line = (char_u *)"";
9602 continue;
9603 }
9604
Bram Moolenaarf002a412021-01-24 13:34:18 +01009605#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009606 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9607 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009608 {
9609 may_generate_prof_end(&cctx, prof_lnum);
9610
9611 prof_lnum = cctx.ctx_lnum;
9612 generate_instr(&cctx, ISN_PROF_START);
9613 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009614#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009615 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9616 && cctx.ctx_skip != SKIP_YES)
9617 {
9618 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009619 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009620 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009621 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009622
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009623 // Some things can be recognized by the first character.
9624 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009625 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009626 case '}':
9627 {
9628 // "}" ends a block scope
9629 scopetype_T stype = cctx.ctx_scope == NULL
9630 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009631
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009632 if (stype == BLOCK_SCOPE)
9633 {
9634 compile_endblock(&cctx);
9635 line = ea.cmd;
9636 }
9637 else
9638 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009639 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009640 goto erret;
9641 }
9642 if (line != NULL)
9643 line = skipwhite(ea.cmd + 1);
9644 continue;
9645 }
9646
9647 case '{':
9648 // "{" starts a block scope
9649 // "{'a': 1}->func() is something else
9650 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9651 {
9652 line = compile_block(ea.cmd, &cctx);
9653 continue;
9654 }
9655 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009656 }
9657
9658 /*
9659 * COMMAND MODIFIERS
9660 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009661 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009662 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9663 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009664 {
9665 if (errormsg != NULL)
9666 goto erret;
9667 // empty line or comment
9668 line = (char_u *)"";
9669 continue;
9670 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009671 generate_cmdmods(&cctx, &local_cmdmod);
9672 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009673
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009674 // Check if there was a colon after the last command modifier or before
9675 // the current position.
9676 for (p = ea.cmd; p >= line; --p)
9677 {
9678 if (*p == ':')
9679 starts_with_colon = TRUE;
9680 if (p < ea.cmd && !VIM_ISWHITE(*p))
9681 break;
9682 }
9683
Bram Moolenaarce024c32021-06-26 13:00:49 +02009684 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009685 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009686 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009687 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009688 if (checkforcmd(&ea.cmd, "call", 3))
9689 {
9690 if (*ea.cmd == '(')
9691 // not for "call()"
9692 ea.cmd = p;
9693 else
9694 ea.cmd = skipwhite(ea.cmd);
9695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696
Bram Moolenaarce024c32021-06-26 13:00:49 +02009697 if (!starts_with_colon)
9698 {
9699 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009700
Bram Moolenaarce024c32021-06-26 13:00:49 +02009701 // Check for assignment after command modifiers.
9702 assign = may_compile_assignment(&ea, &line, &cctx);
9703 if (assign == OK)
9704 goto nextline;
9705 if (assign == FAIL)
9706 goto erret;
9707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009708 }
9709
9710 /*
9711 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009712 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009713 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009714 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009715 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009716 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9717 && (starts_with_colon || !(*cmd == '\''
9718 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009719 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009720 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009721 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009722 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009723 if (!starts_with_colon)
9724 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009725 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009726 goto erret;
9727 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009728 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009729 if (ends_excmd2(line, ea.cmd))
9730 {
9731 // A range without a command: jump to the line.
9732 // TODO: compile to a more efficient command, possibly
9733 // calling parse_cmd_address().
9734 ea.cmdidx = CMD_SIZE;
9735 line = compile_exec(line, &ea, &cctx);
9736 goto nextline;
9737 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009738 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009739 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009740 p = find_ex_command(&ea, NULL,
9741 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009742 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009743
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009744 if (p == NULL)
9745 {
9746 if (cctx.ctx_skip != SKIP_YES)
9747 emsg(_(e_ambiguous_use_of_user_defined_command));
9748 goto erret;
9749 }
9750
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009751 // When using ":legacy cmd" always use compile_exec().
9752 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009753 {
9754 char_u *start = ea.cmd;
9755
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009756 switch (ea.cmdidx)
9757 {
9758 case CMD_if:
9759 case CMD_elseif:
9760 case CMD_else:
9761 case CMD_endif:
9762 case CMD_for:
9763 case CMD_endfor:
9764 case CMD_continue:
9765 case CMD_break:
9766 case CMD_while:
9767 case CMD_endwhile:
9768 case CMD_try:
9769 case CMD_catch:
9770 case CMD_finally:
9771 case CMD_endtry:
9772 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9773 goto erret;
9774 default: break;
9775 }
9776
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009777 // ":legacy return expr" needs to be handled differently.
9778 if (checkforcmd(&start, "return", 4))
9779 ea.cmdidx = CMD_return;
9780 else
9781 ea.cmdidx = CMD_legacy;
9782 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009784 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9785 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009786 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009787 {
9788 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009789 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009790 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009791 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009792 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009793 // CMD_var cannot happen, compile_assignment() above would be
9794 // used. Most likely an assignment to a non-existing variable.
9795 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009796 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009798 }
9799
Bram Moolenaar3988f642020-08-27 22:43:03 +02009800 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009801 && ea.cmdidx != CMD_elseif
9802 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009803 && ea.cmdidx != CMD_endif
9804 && ea.cmdidx != CMD_endfor
9805 && ea.cmdidx != CMD_endwhile
9806 && ea.cmdidx != CMD_catch
9807 && ea.cmdidx != CMD_finally
9808 && ea.cmdidx != CMD_endtry)
9809 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009810 emsg(_(e_unreachable_code_after_return));
9811 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009812 }
9813
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009814 p = skipwhite(p);
9815 if (ea.cmdidx != CMD_SIZE
9816 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9817 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009818 if (ea.cmdidx >= 0)
9819 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009820 if ((ea.argt & EX_BANG) && *p == '!')
9821 {
9822 ea.forceit = TRUE;
9823 p = skipwhite(p + 1);
9824 }
9825 }
9826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009827 switch (ea.cmdidx)
9828 {
9829 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009830 ea.arg = p;
9831 line = compile_nested_function(&ea, &cctx);
9832 break;
9833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009834 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009835 // TODO: should we allow this, e.g. to declare a global
9836 // function?
9837 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009838 goto erret;
9839
9840 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009841 line = compile_return(p, check_return_type,
9842 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009843 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009844 break;
9845
9846 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009847 emsg(_(e_cannot_use_let_in_vim9_script));
9848 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009849 case CMD_var:
9850 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009851 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009852 case CMD_increment:
9853 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009854 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009855 if (line == p)
9856 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009857 break;
9858
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009859 case CMD_unlet:
9860 case CMD_unlockvar:
9861 case CMD_lockvar:
9862 line = compile_unletlock(p, &ea, &cctx);
9863 break;
9864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009865 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009866 emsg(_(e_import_can_only_be_used_in_script));
9867 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009868 break;
9869
9870 case CMD_if:
9871 line = compile_if(p, &cctx);
9872 break;
9873 case CMD_elseif:
9874 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009875 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009876 break;
9877 case CMD_else:
9878 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009879 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009880 break;
9881 case CMD_endif:
9882 line = compile_endif(p, &cctx);
9883 break;
9884
9885 case CMD_while:
9886 line = compile_while(p, &cctx);
9887 break;
9888 case CMD_endwhile:
9889 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009890 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009891 break;
9892
9893 case CMD_for:
9894 line = compile_for(p, &cctx);
9895 break;
9896 case CMD_endfor:
9897 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009898 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009899 break;
9900 case CMD_continue:
9901 line = compile_continue(p, &cctx);
9902 break;
9903 case CMD_break:
9904 line = compile_break(p, &cctx);
9905 break;
9906
9907 case CMD_try:
9908 line = compile_try(p, &cctx);
9909 break;
9910 case CMD_catch:
9911 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009912 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009913 break;
9914 case CMD_finally:
9915 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009916 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009917 break;
9918 case CMD_endtry:
9919 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009920 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009921 break;
9922 case CMD_throw:
9923 line = compile_throw(p, &cctx);
9924 break;
9925
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009926 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009927 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009928 break;
9929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009930 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009931 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009932 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009933 case CMD_echomsg:
9934 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +02009935 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009936 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009937 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009938
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009939 case CMD_put:
9940 ea.cmd = cmd;
9941 line = compile_put(p, &ea, &cctx);
9942 break;
9943
Bram Moolenaar4c137212021-04-19 16:48:48 +02009944 case CMD_substitute:
9945 if (cctx.ctx_skip == SKIP_YES)
9946 line = (char_u *)"";
9947 else
9948 {
9949 ea.arg = p;
9950 line = compile_substitute(line, &ea, &cctx);
9951 }
9952 break;
9953
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009954 case CMD_redir:
9955 ea.arg = p;
9956 line = compile_redir(line, &ea, &cctx);
9957 break;
9958
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009959 case CMD_cexpr:
9960 case CMD_lexpr:
9961 case CMD_caddexpr:
9962 case CMD_laddexpr:
9963 case CMD_cgetexpr:
9964 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009965#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009966 ea.arg = p;
9967 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009968#else
9969 ex_ni(&ea);
9970 line = NULL;
9971#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009972 break;
9973
Bram Moolenaarae616492020-07-28 20:07:27 +02009974 case CMD_append:
9975 case CMD_change:
9976 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009977 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009978 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009979 case CMD_xit:
9980 not_in_vim9(&ea);
9981 goto erret;
9982
Bram Moolenaar002262f2020-07-08 17:47:57 +02009983 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009984 if (cctx.ctx_skip != SKIP_YES)
9985 {
9986 semsg(_(e_invalid_command_str), ea.cmd);
9987 goto erret;
9988 }
9989 // We don't check for a next command here.
9990 line = (char_u *)"";
9991 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009992
Bram Moolenaar20677332021-06-06 17:02:53 +02009993 case CMD_lua:
9994 case CMD_mzscheme:
9995 case CMD_perl:
9996 case CMD_py3:
9997 case CMD_python3:
9998 case CMD_python:
9999 case CMD_pythonx:
10000 case CMD_ruby:
10001 case CMD_tcl:
10002 ea.arg = p;
10003 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010004 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010005 else
10006 // heredoc lines have been concatenated with NL
10007 // characters in get_function_body()
10008 line = compile_script(line, &cctx);
10009 break;
10010
10011 default:
10012 // Not recognized, execute with do_cmdline_cmd().
10013 ea.arg = p;
10014 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010015 break;
10016 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010017nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010018 if (line == NULL)
10019 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010020 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010021
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010022 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010023 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010025 if (cctx.ctx_type_stack.ga_len < 0)
10026 {
10027 iemsg("Type stack underflow");
10028 goto erret;
10029 }
10030 }
10031
10032 if (cctx.ctx_scope != NULL)
10033 {
10034 if (cctx.ctx_scope->se_type == IF_SCOPE)
10035 emsg(_(e_endif));
10036 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10037 emsg(_(e_endwhile));
10038 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10039 emsg(_(e_endfor));
10040 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010041 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010042 goto erret;
10043 }
10044
Bram Moolenaarefd88552020-06-18 20:50:10 +020010045 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010046 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010047 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10048 ufunc->uf_ret_type = &t_void;
10049 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010050 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010051 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010052 goto erret;
10053 }
10054
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010055 // Return void if there is no return at the end.
10056 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010057 }
10058
Bram Moolenaar599410c2021-04-10 14:03:43 +020010059 // When compiled with ":silent!" and there was an error don't consider the
10060 // function compiled.
10061 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010062 {
10063 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10064 + ufunc->uf_dfunc_idx;
10065 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010066 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010067#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010068 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010069 {
10070 dfunc->df_instr_prof = instr->ga_data;
10071 dfunc->df_instr_prof_count = instr->ga_len;
10072 }
10073 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010074#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010075 if (cctx.ctx_compile_type == CT_DEBUG)
10076 {
10077 dfunc->df_instr_debug = instr->ga_data;
10078 dfunc->df_instr_debug_count = instr->ga_len;
10079 }
10080 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010081 {
10082 dfunc->df_instr = instr->ga_data;
10083 dfunc->df_instr_count = instr->ga_len;
10084 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010085 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010086 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010087 if (cctx.ctx_outer_used)
10088 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010089 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010091
10092 ret = OK;
10093
10094erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010095 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010096 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010097 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10098 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010099
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010100 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010101 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010102 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010103 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010104
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010105 // If using the last entry in the table and it was added above, we
10106 // might as well remove it.
10107 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010108 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010109 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010110 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010111 ufunc->uf_dfunc_idx = 0;
10112 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010113 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010114
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010115 while (cctx.ctx_scope != NULL)
10116 drop_scope(&cctx);
10117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010118 if (errormsg != NULL)
10119 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010120 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010121 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010122 }
10123
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010124 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10125 {
10126 if (ret == OK)
10127 {
10128 emsg(_(e_missing_redir_end));
10129 ret = FAIL;
10130 }
10131 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010132 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010133 }
10134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010135 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010136 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010137 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010138 if (do_estack_push)
10139 estack_pop();
10140
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010141 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010142 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010143 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010144 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010145 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010146}
10147
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010148 void
10149set_function_type(ufunc_T *ufunc)
10150{
10151 int varargs = ufunc->uf_va_name != NULL;
10152 int argcount = ufunc->uf_args.ga_len;
10153
10154 // Create a type for the function, with the return type and any
10155 // argument types.
10156 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10157 // The type is included in "tt_args".
10158 if (argcount > 0 || varargs)
10159 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010160 if (ufunc->uf_type_list.ga_itemsize == 0)
10161 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010162 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10163 argcount, &ufunc->uf_type_list);
10164 // Add argument types to the function type.
10165 if (func_type_add_arg_types(ufunc->uf_func_type,
10166 argcount + varargs,
10167 &ufunc->uf_type_list) == FAIL)
10168 return;
10169 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10170 ufunc->uf_func_type->tt_min_argcount =
10171 argcount - ufunc->uf_def_args.ga_len;
10172 if (ufunc->uf_arg_types == NULL)
10173 {
10174 int i;
10175
10176 // lambda does not have argument types.
10177 for (i = 0; i < argcount; ++i)
10178 ufunc->uf_func_type->tt_args[i] = &t_any;
10179 }
10180 else
10181 mch_memmove(ufunc->uf_func_type->tt_args,
10182 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10183 if (varargs)
10184 {
10185 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010186 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010187 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10188 }
10189 }
10190 else
10191 // No arguments, can use a predefined type.
10192 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10193 argcount, &ufunc->uf_type_list);
10194}
10195
10196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010197/*
10198 * Delete an instruction, free what it contains.
10199 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010200 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010201delete_instr(isn_T *isn)
10202{
10203 switch (isn->isn_type)
10204 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010205 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010206 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010207 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010208 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010209 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010210 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010211 case ISN_LOADENV:
10212 case ISN_LOADG:
10213 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010214 case ISN_LOADT:
10215 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010216 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010217 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010218 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010219 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010220 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010221 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010222 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010223 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010224 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010225 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010226 case ISN_STOREW:
10227 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010228 vim_free(isn->isn_arg.string);
10229 break;
10230
Bram Moolenaar4c137212021-04-19 16:48:48 +020010231 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010232 {
10233 int idx;
10234 isn_T *list = isn->isn_arg.subs.subs_instr;
10235
10236 vim_free(isn->isn_arg.subs.subs_cmd);
10237 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10238 delete_instr(list + idx);
10239 vim_free(list);
10240 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010241 break;
10242
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010243 case ISN_INSTR:
10244 {
10245 int idx;
10246 isn_T *list = isn->isn_arg.instr;
10247
10248 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10249 delete_instr(list + idx);
10250 vim_free(list);
10251 }
10252 break;
10253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010254 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010255 case ISN_STORES:
10256 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010257 break;
10258
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010259 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010260 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010261 vim_free(isn->isn_arg.unlet.ul_name);
10262 break;
10263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010264 case ISN_STOREOPT:
10265 vim_free(isn->isn_arg.storeopt.so_name);
10266 break;
10267
10268 case ISN_PUSHBLOB: // push blob isn_arg.blob
10269 blob_unref(isn->isn_arg.blob);
10270 break;
10271
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010272 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010273#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010274 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010275#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010276 break;
10277
10278 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010279#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010280 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010281#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010282 break;
10283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010284 case ISN_UCALL:
10285 vim_free(isn->isn_arg.ufunc.cuf_name);
10286 break;
10287
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010288 case ISN_FUNCREF:
10289 {
10290 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10291 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010292 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010293
Bram Moolenaar077a4232020-12-22 18:33:27 +010010294 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10295 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010296 }
10297 break;
10298
10299 case ISN_DCALL:
10300 {
10301 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10302 + isn->isn_arg.dfunc.cdf_idx;
10303
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010304 if (dfunc->df_ufunc != NULL
10305 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010306 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010307 }
10308 break;
10309
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010310 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010311 {
10312 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10313 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10314
10315 if (ufunc != NULL)
10316 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010317 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010318 func_ptr_unref(ufunc);
10319 }
10320
10321 vim_free(lambda);
10322 vim_free(isn->isn_arg.newfunc.nf_global);
10323 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010324 break;
10325
Bram Moolenaar5e654232020-09-16 15:22:00 +020010326 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010327 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010328 free_type(isn->isn_arg.type.ct_type);
10329 break;
10330
Bram Moolenaar02194d22020-10-24 23:08:38 +020010331 case ISN_CMDMOD:
10332 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10333 ->cmod_filter_regmatch.regprog);
10334 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10335 break;
10336
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010337 case ISN_LOADSCRIPT:
10338 case ISN_STORESCRIPT:
10339 vim_free(isn->isn_arg.script.scriptref);
10340 break;
10341
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010342 case ISN_TRY:
10343 vim_free(isn->isn_arg.try.try_ref);
10344 break;
10345
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010346 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010347 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010348 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10349 break;
10350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010351 case ISN_2BOOL:
10352 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010353 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010354 case ISN_ADDBLOB:
10355 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010356 case ISN_ANYINDEX:
10357 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010358 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010359 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010360 case ISN_BLOBINDEX:
10361 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010362 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010363 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010364 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010365 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010366 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010367 case ISN_COMPAREANY:
10368 case ISN_COMPAREBLOB:
10369 case ISN_COMPAREBOOL:
10370 case ISN_COMPAREDICT:
10371 case ISN_COMPAREFLOAT:
10372 case ISN_COMPAREFUNC:
10373 case ISN_COMPARELIST:
10374 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010375 case ISN_COMPARESPECIAL:
10376 case ISN_COMPARESTRING:
10377 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010378 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010379 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010380 case ISN_DROP:
10381 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010382 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010383 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010384 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010385 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010386 case ISN_EXECCONCAT:
10387 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010388 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010389 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010390 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010391 case ISN_GETITEM:
10392 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010393 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010394 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010395 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010396 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010397 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010398 case ISN_LOADBDICT:
10399 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010400 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010401 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010402 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010403 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010404 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010405 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010406 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010407 case ISN_NEGATENR:
10408 case ISN_NEWDICT:
10409 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010410 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010411 case ISN_OPFLOAT:
10412 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010413 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010414 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010415 case ISN_PROF_END:
10416 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010417 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010418 case ISN_PUSHF:
10419 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010420 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010421 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010422 case ISN_REDIREND:
10423 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010424 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010425 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010426 case ISN_SHUFFLE:
10427 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010428 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010429 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010430 case ISN_STORENR:
10431 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010432 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010433 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010434 case ISN_STOREV:
10435 case ISN_STRINDEX:
10436 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010437 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010438 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010439 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010440 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010441 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010442 // nothing allocated
10443 break;
10444 }
10445}
10446
10447/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010448 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010449 */
10450 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010451delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010452{
10453 int idx;
10454
10455 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010456 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010457
10458 if (dfunc->df_instr != NULL)
10459 {
10460 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10461 delete_instr(dfunc->df_instr + idx);
10462 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010463 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010464 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010465 if (dfunc->df_instr_debug != NULL)
10466 {
10467 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10468 delete_instr(dfunc->df_instr_debug + idx);
10469 VIM_CLEAR(dfunc->df_instr_debug);
10470 dfunc->df_instr_debug = NULL;
10471 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010472#ifdef FEAT_PROFILE
10473 if (dfunc->df_instr_prof != NULL)
10474 {
10475 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10476 delete_instr(dfunc->df_instr_prof + idx);
10477 VIM_CLEAR(dfunc->df_instr_prof);
10478 dfunc->df_instr_prof = NULL;
10479 }
10480#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010481
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010482 if (mark_deleted)
10483 dfunc->df_deleted = TRUE;
10484 if (dfunc->df_ufunc != NULL)
10485 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010486}
10487
10488/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010489 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010490 * function, unless another user function still uses it.
10491 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010492 */
10493 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010494unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010495{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010496 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010497 {
10498 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10499 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010500
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010501 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010502 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010503 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010504 ufunc->uf_dfunc_idx = 0;
10505 if (dfunc->df_ufunc == ufunc)
10506 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010507 }
10508}
10509
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010510/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010511 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010512 */
10513 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010514link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010515{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010516 if (ufunc->uf_dfunc_idx > 0)
10517 {
10518 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10519 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010520
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010521 ++dfunc->df_refcount;
10522 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010523}
10524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010525#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010526/*
10527 * Free all functions defined with ":def".
10528 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010529 void
10530free_def_functions(void)
10531{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010532 int idx;
10533
10534 for (idx = 0; idx < def_functions.ga_len; ++idx)
10535 {
10536 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10537
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010538 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010539 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010540 }
10541
10542 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010543}
10544#endif
10545
10546
10547#endif // FEAT_EVAL