blob: cafbd6b2fc1fdc9b920635cf5f4df0184a6702a5 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100551 if (ga_grow(instr, 1) == FAIL)
552 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
588 if (ga_grow(stack, 1) == FAIL)
589 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 */
Bram Moolenaar193f6202020-11-16 20:08:35 +01001015 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 Moolenaar351ead02021-01-16 16:07:01 +01001042 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001043need_type(
1044 type_T *actual,
1045 type_T *expected,
1046 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001047 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001048 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001049 int silent,
1050 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001051{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001052 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001053
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001054 if (expected == &t_bool && actual != &t_bool
1055 && (actual->tt_flags & TTFLAG_BOOL_OK))
1056 {
1057 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1058 // boolean is OK but requires a conversion.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001059 generate_2BOOL(cctx, FALSE, offset);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02001060 return OK;
1061 }
1062
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001063 where.wt_index = arg_idx;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001064 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001065 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +02001066
1067 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001068 // If it's a constant a runtime check makes no sense.
Bram Moolenaar378697a2021-07-15 19:23:18 +02001069 if ((!actual_is_const || actual == &t_any)
1070 && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001071 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01001072 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001073 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001074 }
Bram Moolenaar5e654232020-09-16 15:22:00 +02001075
1076 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +01001077 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02001078 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001079}
1080
1081/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001082 * Check that the top of the type stack has a type that can be used as a
1083 * condition. Give an error and return FAIL if not.
1084 */
1085 static int
1086bool_on_stack(cctx_T *cctx)
1087{
1088 garray_T *stack = &cctx->ctx_type_stack;
1089 type_T *type;
1090
1091 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1092 if (type == &t_bool)
1093 return OK;
1094
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001095 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001096 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1097 // This requires a runtime type check.
1098 return generate_COND2BOOL(cctx);
1099
Bram Moolenaar351ead02021-01-16 16:07:01 +01001100 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001101}
1102
1103/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 * Generate an ISN_PUSHNR instruction.
1105 */
1106 static int
1107generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1108{
1109 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001110 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111
Bram Moolenaar080457c2020-03-03 21:53:32 +01001112 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1114 return FAIL;
1115 isn->isn_arg.number = number;
1116
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001117 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001118 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001119 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHBOOL instruction.
1125 */
1126 static int
1127generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.number = number;
1135
1136 return OK;
1137}
1138
1139/*
1140 * Generate an ISN_PUSHSPEC instruction.
1141 */
1142 static int
1143generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1149 return FAIL;
1150 isn->isn_arg.number = number;
1151
1152 return OK;
1153}
1154
1155#ifdef FEAT_FLOAT
1156/*
1157 * Generate an ISN_PUSHF instruction.
1158 */
1159 static int
1160generate_PUSHF(cctx_T *cctx, float_T fnumber)
1161{
1162 isn_T *isn;
1163
Bram Moolenaar080457c2020-03-03 21:53:32 +01001164 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1166 return FAIL;
1167 isn->isn_arg.fnumber = fnumber;
1168
1169 return OK;
1170}
1171#endif
1172
1173/*
1174 * Generate an ISN_PUSHS instruction.
1175 * Consumes "str".
1176 */
1177 static int
1178generate_PUSHS(cctx_T *cctx, char_u *str)
1179{
1180 isn_T *isn;
1181
Bram Moolenaar508b5612021-01-02 18:17:26 +01001182 if (cctx->ctx_skip == SKIP_YES)
1183 {
1184 vim_free(str);
1185 return OK;
1186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1188 return FAIL;
1189 isn->isn_arg.string = str;
1190
1191 return OK;
1192}
1193
1194/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001195 * Generate an ISN_PUSHCHANNEL instruction.
1196 * Consumes "channel".
1197 */
1198 static int
1199generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1200{
1201 isn_T *isn;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001204 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1205 return FAIL;
1206 isn->isn_arg.channel = channel;
1207
1208 return OK;
1209}
1210
1211/*
1212 * Generate an ISN_PUSHJOB instruction.
1213 * Consumes "job".
1214 */
1215 static int
1216generate_PUSHJOB(cctx_T *cctx, job_T *job)
1217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001221 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001222 return FAIL;
1223 isn->isn_arg.job = job;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 * Generate an ISN_PUSHBLOB instruction.
1230 * Consumes "blob".
1231 */
1232 static int
1233generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1234{
1235 isn_T *isn;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1239 return FAIL;
1240 isn->isn_arg.blob = blob;
1241
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001246 * Generate an ISN_PUSHFUNC instruction with name "name".
1247 * Consumes "name".
1248 */
1249 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001250generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001251{
1252 isn_T *isn;
1253
Bram Moolenaar080457c2020-03-03 21:53:32 +01001254 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001255 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001256 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001257 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001258
1259 return OK;
1260}
1261
1262/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001263 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001264 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1265 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001266 */
1267 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001268generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001269{
1270 isn_T *isn;
1271 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001272 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1273 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001274 type_T *item_type = &t_any;
1275
1276 RETURN_OK_IF_SKIP(cctx);
1277
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001278 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001279 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001280 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001281 emsg(_(e_listreq));
1282 return FAIL;
1283 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001284 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001285 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1286 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001287 isn->isn_arg.getitem.gi_index = index;
1288 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001289
1290 // add the item type to the type stack
1291 if (ga_grow(stack, 1) == FAIL)
1292 return FAIL;
1293 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1294 ++stack->ga_len;
1295 return OK;
1296}
1297
1298/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001299 * Generate an ISN_SLICE instruction with "count".
1300 */
1301 static int
1302generate_SLICE(cctx_T *cctx, int count)
1303{
1304 isn_T *isn;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.number = count;
1310 return OK;
1311}
1312
1313/*
1314 * Generate an ISN_CHECKLEN instruction with "min_len".
1315 */
1316 static int
1317generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1318{
1319 isn_T *isn;
1320
1321 RETURN_OK_IF_SKIP(cctx);
1322
1323 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1324 return FAIL;
1325 isn->isn_arg.checklen.cl_min_len = min_len;
1326 isn->isn_arg.checklen.cl_more_OK = more_OK;
1327
1328 return OK;
1329}
1330
1331/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 * Generate an ISN_STORE instruction.
1333 */
1334 static int
1335generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1336{
1337 isn_T *isn;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1341 return FAIL;
1342 if (name != NULL)
1343 isn->isn_arg.string = vim_strsave(name);
1344 else
1345 isn->isn_arg.number = idx;
1346
1347 return OK;
1348}
1349
1350/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001351 * Generate an ISN_STOREOUTER instruction.
1352 */
1353 static int
1354generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1355{
1356 isn_T *isn;
1357
1358 RETURN_OK_IF_SKIP(cctx);
1359 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1360 return FAIL;
1361 isn->isn_arg.outer.outer_idx = idx;
1362 isn->isn_arg.outer.outer_depth = level;
1363
1364 return OK;
1365}
1366
1367/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1369 */
1370 static int
1371generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1372{
1373 isn_T *isn;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1377 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001378 isn->isn_arg.storenr.stnr_idx = idx;
1379 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_STOREOPT instruction
1386 */
1387 static int
1388generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1389{
1390 isn_T *isn;
1391
Bram Moolenaar080457c2020-03-03 21:53:32 +01001392 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001393 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 return FAIL;
1395 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1396 isn->isn_arg.storeopt.so_flags = opt_flags;
1397
1398 return OK;
1399}
1400
1401/*
1402 * Generate an ISN_LOAD or similar instruction.
1403 */
1404 static int
1405generate_LOAD(
1406 cctx_T *cctx,
1407 isntype_T isn_type,
1408 int idx,
1409 char_u *name,
1410 type_T *type)
1411{
1412 isn_T *isn;
1413
Bram Moolenaar080457c2020-03-03 21:53:32 +01001414 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1416 return FAIL;
1417 if (name != NULL)
1418 isn->isn_arg.string = vim_strsave(name);
1419 else
1420 isn->isn_arg.number = idx;
1421
1422 return OK;
1423}
1424
1425/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001426 * Generate an ISN_LOADOUTER instruction
1427 */
1428 static int
1429generate_LOADOUTER(
1430 cctx_T *cctx,
1431 int idx,
1432 int nesting,
1433 type_T *type)
1434{
1435 isn_T *isn;
1436
1437 RETURN_OK_IF_SKIP(cctx);
1438 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1439 return FAIL;
1440 isn->isn_arg.outer.outer_idx = idx;
1441 isn->isn_arg.outer.outer_depth = nesting;
1442
1443 return OK;
1444}
1445
1446/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001447 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001448 */
1449 static int
1450generate_LOADV(
1451 cctx_T *cctx,
1452 char_u *name,
1453 int error)
1454{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001455 int di_flags;
1456 int vidx = find_vim_var(name, &di_flags);
1457 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001458
Bram Moolenaar080457c2020-03-03 21:53:32 +01001459 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001460 if (vidx < 0)
1461 {
1462 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001463 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001464 return FAIL;
1465 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001466 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001467
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001468 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001469}
1470
1471/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001472 * Generate an ISN_UNLET instruction.
1473 */
1474 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001475generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001476{
1477 isn_T *isn;
1478
1479 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001480 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001481 return FAIL;
1482 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1483 isn->isn_arg.unlet.ul_forceit = forceit;
1484
1485 return OK;
1486}
1487
1488/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001489 * Generate an ISN_LOCKCONST instruction.
1490 */
1491 static int
1492generate_LOCKCONST(cctx_T *cctx)
1493{
1494 isn_T *isn;
1495
1496 RETURN_OK_IF_SKIP(cctx);
1497 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1498 return FAIL;
1499 return OK;
1500}
1501
1502/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 * Generate an ISN_LOADS instruction.
1504 */
1505 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001506generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001508 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001510 int sid,
1511 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512{
1513 isn_T *isn;
1514
Bram Moolenaar080457c2020-03-03 21:53:32 +01001515 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001516 if (isn_type == ISN_LOADS)
1517 isn = generate_instr_type(cctx, isn_type, type);
1518 else
1519 isn = generate_instr_drop(cctx, isn_type, 1);
1520 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001522 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1523 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524
1525 return OK;
1526}
1527
1528/*
1529 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1530 */
1531 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001532generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 cctx_T *cctx,
1534 isntype_T isn_type,
1535 int sid,
1536 int idx,
1537 type_T *type)
1538{
1539 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001540 scriptref_T *sref;
1541 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
Bram Moolenaar080457c2020-03-03 21:53:32 +01001543 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 if (isn_type == ISN_LOADSCRIPT)
1545 isn = generate_instr_type(cctx, isn_type, type);
1546 else
1547 isn = generate_instr_drop(cctx, isn_type, 1);
1548 if (isn == NULL)
1549 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001550
1551 // This requires three arguments, which doesn't fit in an instruction, thus
1552 // we need to allocate a struct for this.
1553 sref = ALLOC_ONE(scriptref_T);
1554 if (sref == NULL)
1555 return FAIL;
1556 isn->isn_arg.script.scriptref = sref;
1557 sref->sref_sid = sid;
1558 sref->sref_idx = idx;
1559 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001560 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 return OK;
1562}
1563
1564/*
1565 * Generate an ISN_NEWLIST instruction.
1566 */
1567 static int
1568generate_NEWLIST(cctx_T *cctx, int count)
1569{
1570 isn_T *isn;
1571 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 type_T *type;
1573 type_T *member;
1574
Bram Moolenaar080457c2020-03-03 21:53:32 +01001575 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1577 return FAIL;
1578 isn->isn_arg.number = count;
1579
Bram Moolenaar127542b2020-08-09 17:22:04 +02001580 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001581 if (count == 0)
1582 member = &t_void;
1583 else
1584 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001585 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1586 cctx->ctx_type_list);
1587 type = get_list_type(member, cctx->ctx_type_list);
1588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 // drop the value types
1590 stack->ga_len -= count;
1591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 // add the list type to the type stack
1593 if (ga_grow(stack, 1) == FAIL)
1594 return FAIL;
1595 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1596 ++stack->ga_len;
1597
1598 return OK;
1599}
1600
1601/*
1602 * Generate an ISN_NEWDICT instruction.
1603 */
1604 static int
1605generate_NEWDICT(cctx_T *cctx, int count)
1606{
1607 isn_T *isn;
1608 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 type_T *type;
1610 type_T *member;
1611
Bram Moolenaar080457c2020-03-03 21:53:32 +01001612 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1614 return FAIL;
1615 isn->isn_arg.number = count;
1616
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001617 if (count == 0)
1618 member = &t_void;
1619 else
1620 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001621 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1622 cctx->ctx_type_list);
1623 type = get_dict_type(member, cctx->ctx_type_list);
1624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 // drop the key and value types
1626 stack->ga_len -= 2 * count;
1627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 // add the dict type to the type stack
1629 if (ga_grow(stack, 1) == FAIL)
1630 return FAIL;
1631 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1632 ++stack->ga_len;
1633
1634 return OK;
1635}
1636
1637/*
1638 * Generate an ISN_FUNCREF instruction.
1639 */
1640 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001641generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642{
1643 isn_T *isn;
1644 garray_T *stack = &cctx->ctx_type_stack;
1645
Bram Moolenaar080457c2020-03-03 21:53:32 +01001646 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1648 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001649 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001650 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001652 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001653 // the nested context, including this one.
1654 if (ufunc->uf_flags & FC_CLOSURE)
1655 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 if (ga_grow(stack, 1) == FAIL)
1658 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001659 ((type_T **)stack->ga_data)[stack->ga_len] =
1660 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 ++stack->ga_len;
1662
1663 return OK;
1664}
1665
1666/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001667 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001668 * "lambda_name" and "func_name" must be in allocated memory and will be
1669 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001670 */
1671 static int
1672generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1673{
1674 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001675
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001676 if (cctx->ctx_skip == SKIP_YES)
1677 {
1678 vim_free(lambda_name);
1679 vim_free(func_name);
1680 return OK;
1681 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001682 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001683 {
1684 vim_free(lambda_name);
1685 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001686 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001687 }
1688 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001689 isn->isn_arg.newfunc.nf_global = func_name;
1690
1691 return OK;
1692}
1693
1694/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001695 * Generate an ISN_DEF instruction: list functions
1696 */
1697 static int
1698generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1699{
1700 isn_T *isn;
1701
1702 RETURN_OK_IF_SKIP(cctx);
1703 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1704 return FAIL;
1705 if (len > 0)
1706 {
1707 isn->isn_arg.string = vim_strnsave(name, len);
1708 if (isn->isn_arg.string == NULL)
1709 return FAIL;
1710 }
1711 return OK;
1712}
1713
1714/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 * Generate an ISN_JUMP instruction.
1716 */
1717 static int
1718generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1719{
1720 isn_T *isn;
1721 garray_T *stack = &cctx->ctx_type_stack;
1722
Bram Moolenaar080457c2020-03-03 21:53:32 +01001723 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1725 return FAIL;
1726 isn->isn_arg.jump.jump_when = when;
1727 isn->isn_arg.jump.jump_where = where;
1728
1729 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1730 --stack->ga_len;
1731
1732 return OK;
1733}
1734
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001735/*
1736 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1737 */
1738 static int
1739generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1740{
1741 isn_T *isn;
1742
1743 RETURN_OK_IF_SKIP(cctx);
1744 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1745 return FAIL;
1746 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1747 // jump_where is set later
1748 return OK;
1749}
1750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 static int
1752generate_FOR(cctx_T *cctx, int loop_idx)
1753{
1754 isn_T *isn;
1755 garray_T *stack = &cctx->ctx_type_stack;
1756
Bram Moolenaar080457c2020-03-03 21:53:32 +01001757 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1759 return FAIL;
1760 isn->isn_arg.forloop.for_idx = loop_idx;
1761
1762 if (ga_grow(stack, 1) == FAIL)
1763 return FAIL;
1764 // type doesn't matter, will be stored next
1765 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1766 ++stack->ga_len;
1767
1768 return OK;
1769}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001770/*
1771 * Generate an ISN_TRYCONT instruction.
1772 */
1773 static int
1774generate_TRYCONT(cctx_T *cctx, int levels, int where)
1775{
1776 isn_T *isn;
1777
1778 RETURN_OK_IF_SKIP(cctx);
1779 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1780 return FAIL;
1781 isn->isn_arg.trycont.tct_levels = levels;
1782 isn->isn_arg.trycont.tct_where = where;
1783
1784 return OK;
1785}
1786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
1788/*
1789 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001790 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 * Return FAIL if the number of arguments is wrong.
1792 */
1793 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001794generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795{
1796 isn_T *isn;
1797 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001798 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001799 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001800 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001801 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802
Bram Moolenaar080457c2020-03-03 21:53:32 +01001803 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001804 argoff = check_internal_func(func_idx, argcount);
1805 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 return FAIL;
1807
Bram Moolenaar389df252020-07-09 21:20:47 +02001808 if (method_call && argoff > 1)
1809 {
1810 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1811 return FAIL;
1812 isn->isn_arg.shuffle.shfl_item = argcount;
1813 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1814 }
1815
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001816 if (argcount > 0)
1817 {
1818 // Check the types of the arguments.
1819 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001820 if (method_call && argoff > 1)
1821 {
1822 int i;
1823
1824 for (i = 0; i < argcount; ++i)
1825 shuffled_argtypes[i] = (i < argoff - 1)
1826 ? argtypes[i + 1]
1827 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1828 argtypes = shuffled_argtypes;
1829 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001830 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1831 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001832 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001833 if (internal_func_is_map(func_idx))
1834 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001835 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1838 return FAIL;
1839 isn->isn_arg.bfunc.cbf_idx = func_idx;
1840 isn->isn_arg.bfunc.cbf_argcount = argcount;
1841
Bram Moolenaar94738d82020-10-21 14:25:07 +02001842 // Drop the argument types and push the return type.
1843 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 if (ga_grow(stack, 1) == FAIL)
1845 return FAIL;
1846 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001847 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001848 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001850 if (maptype != NULL && maptype->tt_member != NULL
1851 && maptype->tt_member != &t_any)
1852 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001853 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 return OK;
1856}
1857
1858/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001859 * Generate an ISN_LISTAPPEND instruction. Works like add().
1860 * Argument count is already checked.
1861 */
1862 static int
1863generate_LISTAPPEND(cctx_T *cctx)
1864{
1865 garray_T *stack = &cctx->ctx_type_stack;
1866 type_T *list_type;
1867 type_T *item_type;
1868 type_T *expected;
1869
1870 // Caller already checked that list_type is a list.
1871 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1872 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1873 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001874 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001875 return FAIL;
1876
1877 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1878 return FAIL;
1879
1880 --stack->ga_len; // drop the argument
1881 return OK;
1882}
1883
1884/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001885 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1886 * Argument count is already checked.
1887 */
1888 static int
1889generate_BLOBAPPEND(cctx_T *cctx)
1890{
1891 garray_T *stack = &cctx->ctx_type_stack;
1892 type_T *item_type;
1893
1894 // Caller already checked that blob_type is a blob.
1895 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001896 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001897 return FAIL;
1898
1899 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1900 return FAIL;
1901
1902 --stack->ga_len; // drop the argument
1903 return OK;
1904}
1905
1906/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001907 * Return TRUE if "ufunc" should be compiled, taking into account whether
1908 * "profile" indicates profiling is to be done.
1909 */
1910 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001911func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001912{
1913 switch (ufunc->uf_def_status)
1914 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001915 case UF_TO_BE_COMPILED:
1916 return TRUE;
1917
Bram Moolenaarb2049902021-01-24 12:53:53 +01001918 case UF_COMPILED:
1919 {
1920 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1921 + ufunc->uf_dfunc_idx;
1922
Bram Moolenaare99d4222021-06-13 14:01:26 +02001923 switch (compile_type)
1924 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001925 case CT_PROFILE:
1926#ifdef FEAT_PROFILE
1927 return dfunc->df_instr_prof == NULL;
1928#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001929 case CT_NONE:
1930 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001931 case CT_DEBUG:
1932 return dfunc->df_instr_debug == NULL;
1933 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001934 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001935
1936 case UF_NOT_COMPILED:
1937 case UF_COMPILE_ERROR:
1938 case UF_COMPILING:
1939 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001940 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001941 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001942}
1943
1944/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 * Generate an ISN_DCALL or ISN_UCALL instruction.
1946 * Return FAIL if the number of arguments is wrong.
1947 */
1948 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001949generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950{
1951 isn_T *isn;
1952 garray_T *stack = &cctx->ctx_type_stack;
1953 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001954 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
Bram Moolenaar080457c2020-03-03 21:53:32 +01001956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 if (argcount > regular_args && !has_varargs(ufunc))
1958 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001959 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 return FAIL;
1961 }
1962 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1963 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001964 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 return FAIL;
1966 }
1967
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001968 if (ufunc->uf_def_status != UF_NOT_COMPILED
1969 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001970 {
1971 int i;
1972
1973 for (i = 0; i < argcount; ++i)
1974 {
1975 type_T *expected;
1976 type_T *actual;
1977
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001978 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1979 if (actual == &t_special
1980 && i >= regular_args - ufunc->uf_def_args.ga_len)
1981 {
1982 // assume v:none used for default argument value
1983 continue;
1984 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001985 if (i < regular_args)
1986 {
1987 if (ufunc->uf_arg_types == NULL)
1988 continue;
1989 expected = ufunc->uf_arg_types[i];
1990 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001991 else if (ufunc->uf_va_type == NULL
1992 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001993 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001994 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001995 else
1996 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001997 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001998 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001999 {
2000 arg_type_mismatch(expected, actual, i + 1);
2001 return FAIL;
2002 }
2003 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002004 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002005 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002006 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002007 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002008 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002009 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2010 {
2011 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2012 ufunc->uf_name);
2013 return FAIL;
2014 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002017 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002018 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002020 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 {
2022 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2023 isn->isn_arg.dfunc.cdf_argcount = argcount;
2024 }
2025 else
2026 {
2027 // A user function may be deleted and redefined later, can't use the
2028 // ufunc pointer, need to look it up again at runtime.
2029 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2030 isn->isn_arg.ufunc.cuf_argcount = argcount;
2031 }
2032
2033 stack->ga_len -= argcount; // drop the arguments
2034 if (ga_grow(stack, 1) == FAIL)
2035 return FAIL;
2036 // add return value
2037 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2038 ++stack->ga_len;
2039
2040 return OK;
2041}
2042
2043/*
2044 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2045 */
2046 static int
2047generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2048{
2049 isn_T *isn;
2050 garray_T *stack = &cctx->ctx_type_stack;
2051
Bram Moolenaar080457c2020-03-03 21:53:32 +01002052 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2054 return FAIL;
2055 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2056 isn->isn_arg.ufunc.cuf_argcount = argcount;
2057
2058 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002059 if (ga_grow(stack, 1) == FAIL)
2060 return FAIL;
2061 // add return value
2062 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2063 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064
2065 return OK;
2066}
2067
2068/*
2069 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002070 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 */
2072 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002073generate_PCALL(
2074 cctx_T *cctx,
2075 int argcount,
2076 char_u *name,
2077 type_T *type,
2078 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079{
2080 isn_T *isn;
2081 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002082 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
Bram Moolenaar080457c2020-03-03 21:53:32 +01002084 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002085
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002086 if (type->tt_type == VAR_ANY)
2087 ret_type = &t_any;
2088 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002089 {
2090 if (type->tt_argcount != -1)
2091 {
2092 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2093
2094 if (argcount < type->tt_min_argcount - varargs)
2095 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002096 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002097 return FAIL;
2098 }
2099 if (!varargs && argcount > type->tt_argcount)
2100 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002101 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002102 return FAIL;
2103 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002104 if (type->tt_args != NULL)
2105 {
2106 int i;
2107
2108 for (i = 0; i < argcount; ++i)
2109 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002110 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002111 type_T *actual = ((type_T **)stack->ga_data)[
2112 stack->ga_len + offset];
2113 type_T *expected;
2114
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002115 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002116 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002117 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002118 else if (i >= type->tt_min_argcount
2119 && actual == &t_special)
2120 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002121 else
2122 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002123 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002124 cctx, TRUE, FALSE) == FAIL)
2125 {
2126 arg_type_mismatch(expected, actual, i + 1);
2127 return FAIL;
2128 }
2129 }
2130 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002131 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002132 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002133 if (ret_type == &t_unknown)
2134 // return type not known yet, use a runtime check
2135 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002136 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002137 else
2138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002139 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002140 return FAIL;
2141 }
2142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2144 return FAIL;
2145 isn->isn_arg.pfunc.cpf_top = at_top;
2146 isn->isn_arg.pfunc.cpf_argcount = argcount;
2147
2148 stack->ga_len -= argcount; // drop the arguments
2149
2150 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002151 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002153 // If partial is above the arguments it must be cleared and replaced with
2154 // the return value.
2155 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2156 return FAIL;
2157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 return OK;
2159}
2160
2161/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002162 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 */
2164 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002165generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166{
2167 isn_T *isn;
2168 garray_T *stack = &cctx->ctx_type_stack;
2169 type_T *type;
2170
Bram Moolenaar080457c2020-03-03 21:53:32 +01002171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002172 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002174 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002176 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002178 if (type->tt_type != VAR_DICT && type != &t_any)
2179 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002180 char *tofree;
2181
2182 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2183 name, type_name(type, &tofree));
2184 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002185 return FAIL;
2186 }
2187 // change dict type to dict member type
2188 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002189 {
2190 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2191 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193
2194 return OK;
2195}
2196
2197/*
2198 * Generate an ISN_ECHO instruction.
2199 */
2200 static int
2201generate_ECHO(cctx_T *cctx, int with_white, int count)
2202{
2203 isn_T *isn;
2204
Bram Moolenaar080457c2020-03-03 21:53:32 +01002205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2207 return FAIL;
2208 isn->isn_arg.echo.echo_with_white = with_white;
2209 isn->isn_arg.echo.echo_count = count;
2210
2211 return OK;
2212}
2213
Bram Moolenaarad39c092020-02-26 18:23:43 +01002214/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002215 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002216 */
2217 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002218generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002219{
2220 isn_T *isn;
2221
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002222 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002223 return FAIL;
2224 isn->isn_arg.number = count;
2225
2226 return OK;
2227}
2228
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002229/*
2230 * Generate an ISN_PUT instruction.
2231 */
2232 static int
2233generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2234{
2235 isn_T *isn;
2236
2237 RETURN_OK_IF_SKIP(cctx);
2238 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2239 return FAIL;
2240 isn->isn_arg.put.put_regname = regname;
2241 isn->isn_arg.put.put_lnum = lnum;
2242 return OK;
2243}
2244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 static int
2246generate_EXEC(cctx_T *cctx, char_u *line)
2247{
2248 isn_T *isn;
2249
Bram Moolenaar080457c2020-03-03 21:53:32 +01002250 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2252 return FAIL;
2253 isn->isn_arg.string = vim_strsave(line);
2254 return OK;
2255}
2256
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002257 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002258generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2259{
2260 isn_T *isn;
2261 garray_T *stack = &cctx->ctx_type_stack;
2262
2263 RETURN_OK_IF_SKIP(cctx);
2264 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2265 return FAIL;
2266 isn->isn_arg.string = vim_strsave(line);
2267
2268 if (ga_grow(stack, 1) == FAIL)
2269 return FAIL;
2270 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2271 ++stack->ga_len;
2272
2273 return OK;
2274}
2275
2276 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002277generate_EXECCONCAT(cctx_T *cctx, int count)
2278{
2279 isn_T *isn;
2280
2281 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2282 return FAIL;
2283 isn->isn_arg.number = count;
2284 return OK;
2285}
2286
Bram Moolenaar08597872020-12-10 19:43:40 +01002287/*
2288 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2289 */
2290 static int
2291generate_RANGE(cctx_T *cctx, char_u *range)
2292{
2293 isn_T *isn;
2294 garray_T *stack = &cctx->ctx_type_stack;
2295
2296 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2297 return FAIL;
2298 isn->isn_arg.string = range;
2299
2300 if (ga_grow(stack, 1) == FAIL)
2301 return FAIL;
2302 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2303 ++stack->ga_len;
2304 return OK;
2305}
2306
Bram Moolenaar792f7862020-11-23 08:31:18 +01002307 static int
2308generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2309{
2310 isn_T *isn;
2311
2312 RETURN_OK_IF_SKIP(cctx);
2313 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2314 return FAIL;
2315 isn->isn_arg.unpack.unp_count = var_count;
2316 isn->isn_arg.unpack.unp_semicolon = semicolon;
2317 return OK;
2318}
2319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002321 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002322 */
2323 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002324generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002325{
2326 isn_T *isn;
2327
Bram Moolenaarfa984412021-03-25 22:15:28 +01002328 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002329 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002330 cctx->ctx_has_cmdmod = TRUE;
2331
2332 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002333 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002334 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2335 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2336 return FAIL;
2337 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002338 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002339 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002340 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002341
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002342 return OK;
2343}
2344
2345 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002346generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002347{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002348 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2349 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002350 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002351 return OK;
2352}
2353
Bram Moolenaarfa984412021-03-25 22:15:28 +01002354 static int
2355misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002356{
2357 garray_T *instr = &cctx->ctx_instr;
2358
Bram Moolenaara91a7132021-03-25 21:12:15 +01002359 if (cctx->ctx_has_cmdmod
2360 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2361 == ISN_CMDMOD)
2362 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002363 emsg(_(e_misplaced_command_modifier));
2364 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002365 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002366 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002367}
2368
2369/*
2370 * Get the index of the current instruction.
2371 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2372 */
2373 static int
2374current_instr_idx(cctx_T *cctx)
2375{
2376 garray_T *instr = &cctx->ctx_instr;
2377 int idx = instr->ga_len;
2378
Bram Moolenaare99d4222021-06-13 14:01:26 +02002379 while (idx > 0)
2380 {
2381 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002382 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002383 {
2384 --idx;
2385 continue;
2386 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002387#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002388 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2389 {
2390 --idx;
2391 continue;
2392 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002393#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002394 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2395 {
2396 --idx;
2397 continue;
2398 }
2399 break;
2400 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002401 return idx;
2402}
2403
Bram Moolenaarf002a412021-01-24 13:34:18 +01002404#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002405 static void
2406may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2407{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002408 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002409 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002410}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002411#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002412
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002415 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002417 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002418reserve_local(
2419 cctx_T *cctx,
2420 char_u *name,
2421 size_t len,
2422 int isConst,
2423 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002426 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002428 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002430 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002431 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 }
2433
2434 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002435 return NULL;
2436 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002437 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002439 // Every local variable uses the next entry on the stack. We could re-use
2440 // the last ones when leaving a scope, but then variables used in a closure
2441 // might get overwritten. To keep things simple do not re-use stack
2442 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002443 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2444 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002445
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002446 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 lvar->lv_const = isConst;
2448 lvar->lv_type = type;
2449
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002450 // Remember the name for debugging.
2451 if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2452 return NULL;
2453 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2454 vim_strsave(lvar->lv_name);
2455 ++dfunc->df_var_names.ga_len;
2456
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002457 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458}
2459
2460/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002461 * Remove local variables above "new_top".
2462 */
2463 static void
2464unwind_locals(cctx_T *cctx, int new_top)
2465{
2466 if (cctx->ctx_locals.ga_len > new_top)
2467 {
2468 int idx;
2469 lvar_T *lvar;
2470
2471 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2472 {
2473 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2474 vim_free(lvar->lv_name);
2475 }
2476 }
2477 cctx->ctx_locals.ga_len = new_top;
2478}
2479
2480/*
2481 * Free all local variables.
2482 */
2483 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002484free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002485{
2486 unwind_locals(cctx, 0);
2487 ga_clear(&cctx->ctx_locals);
2488}
2489
2490/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002491 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2492 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2493 * error if the variable was defined with :const.
2494 */
2495 static int
2496check_item_writable(svar_T *sv, int check_writable, char_u *name)
2497{
2498 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2499 || (check_writable == ASSIGN_FINAL
2500 && sv->sv_const == ASSIGN_CONST))
2501 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002502 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002503 return FAIL;
2504 }
2505 return OK;
2506}
2507
2508/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002510 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 * Returns the index in "sn_var_vals" if found.
2512 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002513 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 */
2515 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002516get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517{
2518 hashtab_T *ht;
2519 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002520 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002521 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 int idx;
2523
Bram Moolenaare3d46852020-08-29 13:39:17 +02002524 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002526 if (sid == current_sctx.sc_sid)
2527 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002528 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002529
2530 if (sav == NULL)
2531 return -2;
2532 idx = sav->sav_var_vals_idx;
2533 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002534 if (check_item_writable(sv, check_writable, name) == FAIL)
2535 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002536 return idx;
2537 }
2538
2539 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 ht = &SCRIPT_VARS(sid);
2541 di = find_var_in_ht(ht, 0, name, TRUE);
2542 if (di == NULL)
2543 return -2;
2544
2545 // Now find the svar_T index in sn_var_vals.
2546 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2547 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002548 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 if (sv->sv_tv == &di->di_tv)
2550 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002551 if (check_item_writable(sv, check_writable, name) == FAIL)
2552 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 return idx;
2554 }
2555 }
2556 return -1;
2557}
2558
2559/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002560 * Find "name" in imported items of the current script or in "cctx" if not
2561 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 */
2563 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002564find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 int idx;
2567
Bram Moolenaare3d46852020-08-29 13:39:17 +02002568 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002569 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 if (cctx != NULL)
2571 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2572 {
2573 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2574 + idx;
2575
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002576 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2577 : STRLEN(import->imp_name) == len
2578 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 return import;
2580 }
2581
Bram Moolenaarefa94442020-08-08 22:16:00 +02002582 return find_imported_in_script(name, len, current_sctx.sc_sid);
2583}
2584
2585 imported_T *
2586find_imported_in_script(char_u *name, size_t len, int sid)
2587{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002588 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002589 int idx;
2590
Bram Moolenaare3d46852020-08-29 13:39:17 +02002591 if (!SCRIPT_ID_VALID(sid))
2592 return NULL;
2593 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2595 {
2596 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2597
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002598 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2599 : STRLEN(import->imp_name) == len
2600 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 return import;
2602 }
2603 return NULL;
2604}
2605
2606/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002607 * Free all imported variables.
2608 */
2609 static void
2610free_imported(cctx_T *cctx)
2611{
2612 int idx;
2613
2614 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2615 {
2616 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2617
2618 vim_free(import->imp_name);
2619 }
2620 ga_clear(&cctx->ctx_imports);
2621}
2622
2623/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002624 * Return a pointer to the next line that isn't empty or only contains a
2625 * comment. Skips over white space.
2626 * Returns NULL if there is none.
2627 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002628 char_u *
2629peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002630{
2631 int lnum = cctx->ctx_lnum;
2632
2633 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2634 {
2635 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002636 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002637
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002638 // ignore NULLs inserted for continuation lines
2639 if (line != NULL)
2640 {
2641 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002642 if (vim9_bad_comment(p))
2643 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002644 if (*p != NUL && !vim9_comment_start(p))
2645 return p;
2646 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002647 }
2648 return NULL;
2649}
2650
2651/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002652 * Called when checking for a following operator at "arg". When the rest of
2653 * the line is empty or only a comment, peek the next line. If there is a next
2654 * line return a pointer to it and set "nextp".
2655 * Otherwise skip over white space.
2656 */
2657 static char_u *
2658may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2659{
2660 char_u *p = skipwhite(arg);
2661
2662 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002663 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002664 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002665 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002666 if (*nextp != NULL)
2667 return *nextp;
2668 }
2669 return p;
2670}
2671
2672/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002673 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002674 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002675 * Returns NULL when at the end.
2676 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002677 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002678next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002679{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002680 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002681
2682 do
2683 {
2684 ++cctx->ctx_lnum;
2685 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002686 {
2687 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002688 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002689 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002690 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002691 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002692 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002693 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002694 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002695 return line;
2696}
2697
2698/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002699 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002700 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002701 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002702 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2703 */
2704 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002705may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002706{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002707 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002708 if (vim9_bad_comment(*arg))
2709 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002710 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002711 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002712 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002713
2714 if (next == NULL)
2715 return FAIL;
2716 *arg = skipwhite(next);
2717 }
2718 return OK;
2719}
2720
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002721/*
2722 * Idem, and give an error when failed.
2723 */
2724 static int
2725may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2726{
2727 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2728 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002729 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002730 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002731 return FAIL;
2732 }
2733 return OK;
2734}
2735
2736
Bram Moolenaara5565e42020-05-09 15:44:01 +02002737// Structure passed between the compile_expr* functions to keep track of
2738// constants that have been parsed but for which no code was produced yet. If
2739// possible expressions on these constants are applied at compile time. If
2740// that is not possible, the code to push the constants needs to be generated
2741// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002742// Using 50 should be more than enough of 5 levels of ().
2743#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002744typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002745 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002746 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002747 int pp_is_const; // all generated code was constants, used for a
2748 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002749} ppconst_T;
2750
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002751static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002752static int compile_expr0(char_u **arg, cctx_T *cctx);
2753static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2754
Bram Moolenaara5565e42020-05-09 15:44:01 +02002755/*
2756 * Generate a PUSH instruction for "tv".
2757 * "tv" will be consumed or cleared.
2758 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2759 */
2760 static int
2761generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2762{
2763 if (tv != NULL)
2764 {
2765 switch (tv->v_type)
2766 {
2767 case VAR_UNKNOWN:
2768 break;
2769 case VAR_BOOL:
2770 generate_PUSHBOOL(cctx, tv->vval.v_number);
2771 break;
2772 case VAR_SPECIAL:
2773 generate_PUSHSPEC(cctx, tv->vval.v_number);
2774 break;
2775 case VAR_NUMBER:
2776 generate_PUSHNR(cctx, tv->vval.v_number);
2777 break;
2778#ifdef FEAT_FLOAT
2779 case VAR_FLOAT:
2780 generate_PUSHF(cctx, tv->vval.v_float);
2781 break;
2782#endif
2783 case VAR_BLOB:
2784 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2785 tv->vval.v_blob = NULL;
2786 break;
2787 case VAR_STRING:
2788 generate_PUSHS(cctx, tv->vval.v_string);
2789 tv->vval.v_string = NULL;
2790 break;
2791 default:
2792 iemsg("constant type not supported");
2793 clear_tv(tv);
2794 return FAIL;
2795 }
2796 tv->v_type = VAR_UNKNOWN;
2797 }
2798 return OK;
2799}
2800
2801/*
2802 * Generate code for any ppconst entries.
2803 */
2804 static int
2805generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2806{
2807 int i;
2808 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002809 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002810
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002811 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002812 for (i = 0; i < ppconst->pp_used; ++i)
2813 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2814 ret = FAIL;
2815 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002816 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002817 return ret;
2818}
2819
2820/*
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002821 * Check that the last item of "ppconst" is a bool.
2822 */
2823 static int
2824check_ppconst_bool(ppconst_T *ppconst)
2825{
2826 if (ppconst->pp_used > 0)
2827 {
2828 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002829 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002830
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002831 return check_typval_type(&t_bool, tv, where);
2832 }
2833 return OK;
2834}
2835
2836/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002837 * Clear ppconst constants. Used when failing.
2838 */
2839 static void
2840clear_ppconst(ppconst_T *ppconst)
2841{
2842 int i;
2843
2844 for (i = 0; i < ppconst->pp_used; ++i)
2845 clear_tv(&ppconst->pp_tv[i]);
2846 ppconst->pp_used = 0;
2847}
2848
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002849/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002850 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002851 * indexable value and the index or the two indexes of a slice.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002852 */
2853 static int
2854compile_member(int is_slice, cctx_T *cctx)
2855{
2856 type_T **typep;
2857 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002858 vartype_T vartype;
2859 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002860
Bram Moolenaar261417b2021-05-06 21:04:55 +02002861 // We can index a list, dict and blob. If we don't know the type
2862 // we can use the index value type. If we still don't know use an "ANY"
2863 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002864 typep = ((type_T **)stack->ga_data) + stack->ga_len
2865 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002866 vartype = (*typep)->tt_type;
2867 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002868 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002869 if (*typep == &t_any && idxtype == &t_string)
2870 vartype = VAR_DICT;
2871 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002872 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002873 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002874 return FAIL;
2875 if (is_slice)
2876 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002877 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2878 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002879 FALSE, FALSE) == FAIL)
2880 return FAIL;
2881 }
2882 }
2883
Bram Moolenaar261417b2021-05-06 21:04:55 +02002884 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002885 {
2886 if (is_slice)
2887 {
2888 emsg(_(e_cannot_slice_dictionary));
2889 return FAIL;
2890 }
2891 if ((*typep)->tt_type == VAR_DICT)
2892 {
2893 *typep = (*typep)->tt_member;
2894 if (*typep == &t_unknown)
2895 // empty dict was used
2896 *typep = &t_any;
2897 }
2898 else
2899 {
2900 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2901 FALSE, FALSE) == FAIL)
2902 return FAIL;
2903 *typep = &t_any;
2904 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002905 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002906 return FAIL;
2907 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2908 return FAIL;
2909 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002910 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002911 {
2912 *typep = &t_string;
2913 if ((is_slice
2914 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2915 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2916 return FAIL;
2917 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002918 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002919 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002920 if (is_slice)
2921 {
2922 *typep = &t_blob;
2923 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2924 return FAIL;
2925 }
2926 else
2927 {
2928 *typep = &t_number;
2929 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2930 return FAIL;
2931 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002932 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002933 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002934 {
2935 if (is_slice)
2936 {
2937 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002938 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002939 2) == FAIL)
2940 return FAIL;
2941 }
2942 else
2943 {
2944 if ((*typep)->tt_type == VAR_LIST)
2945 {
2946 *typep = (*typep)->tt_member;
2947 if (*typep == &t_unknown)
2948 // empty list was used
2949 *typep = &t_any;
2950 }
2951 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002952 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2953 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002954 return FAIL;
2955 }
2956 }
2957 else
2958 {
2959 emsg(_(e_string_list_dict_or_blob_required));
2960 return FAIL;
2961 }
2962 return OK;
2963}
2964
2965/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002966 * Generate an instruction to load script-local variable "name", without the
2967 * leading "s:".
2968 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 */
2970 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002971compile_load_scriptvar(
2972 cctx_T *cctx,
2973 char_u *name, // variable NUL terminated
2974 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002975 char_u **end, // end of variable
2976 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002978 scriptitem_T *si;
2979 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 imported_T *import;
2981
Bram Moolenaare3d46852020-08-29 13:39:17 +02002982 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2983 return FAIL;
2984 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002985 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002986 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002988 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002989 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2990 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 }
2992 if (idx >= 0)
2993 {
2994 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2995
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002996 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 current_sctx.sc_sid, idx, sv->sv_type);
2998 return OK;
2999 }
3000
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003001 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 if (import != NULL)
3003 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003004 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003005 {
3006 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003007 char_u *exp_name;
3008 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003009 ufunc_T *ufunc;
3010 type_T *type;
3011
3012 // Used "import * as Name", need to lookup the member.
3013 if (*p != '.')
3014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003015 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003016 return FAIL;
3017 }
3018 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003019 if (VIM_ISWHITE(*p))
3020 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003021 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003022 return FAIL;
3023 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003024
Bram Moolenaar1c991142020-07-04 13:15:31 +02003025 // isolate one name
3026 exp_name = p;
3027 while (eval_isnamec(*p))
3028 ++p;
3029 cc = *p;
3030 *p = NUL;
3031
Bram Moolenaaredba7072021-03-13 21:14:18 +01003032 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3033 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003034 *p = cc;
3035 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003036 *end = p;
3037
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003038 if (idx < 0)
3039 {
3040 if (*p == '(' && ufunc != NULL)
3041 {
3042 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3043 return OK;
3044 }
3045 return FAIL;
3046 }
3047
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003048 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3049 import->imp_sid,
3050 idx,
3051 type);
3052 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003053 else if (import->imp_funcname != NULL)
3054 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003055 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003056 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3057 import->imp_sid,
3058 import->imp_var_vals_idx,
3059 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 return OK;
3061 }
3062
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003063 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003064 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 return FAIL;
3066}
3067
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003068 static int
3069generate_funcref(cctx_T *cctx, char_u *name)
3070{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003071 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003072
3073 if (ufunc == NULL)
3074 return FAIL;
3075
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003076 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003077 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3078 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003079 == FAIL)
3080 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003081 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003082}
3083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084/*
3085 * Compile a variable name into a load instruction.
3086 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003087 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 * When "error" is FALSE do not give an error when not found.
3089 */
3090 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003091compile_load(
3092 char_u **arg,
3093 char_u *end_arg,
3094 cctx_T *cctx,
3095 int is_expr,
3096 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097{
3098 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003099 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003100 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003102 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103
3104 if (*(*arg + 1) == ':')
3105 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003106 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003107 {
3108 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109
Bram Moolenaarfa596382021-04-07 21:58:16 +02003110 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003111 switch (**arg)
3112 {
3113 case 'g': isn_type = ISN_LOADGDICT; break;
3114 case 'w': isn_type = ISN_LOADWDICT; break;
3115 case 't': isn_type = ISN_LOADTDICT; break;
3116 case 'b': isn_type = ISN_LOADBDICT; break;
3117 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003118 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003119 goto theend;
3120 }
3121 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3122 goto theend;
3123 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 else
3126 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003127 isntype_T isn_type = ISN_DROP;
3128
Bram Moolenaarfa596382021-04-07 21:58:16 +02003129 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003130 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3131 if (name == NULL)
3132 return FAIL;
3133
3134 switch (**arg)
3135 {
3136 case 'v': res = generate_LOADV(cctx, name, error);
3137 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003138 case 's': if (is_expr && ASCII_ISUPPER(*name)
3139 && find_func(name, FALSE, cctx) != NULL)
3140 res = generate_funcref(cctx, name);
3141 else
3142 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003143 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003144 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003145 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003146 {
3147 if (is_expr && ASCII_ISUPPER(*name)
3148 && find_func(name, FALSE, cctx) != NULL)
3149 res = generate_funcref(cctx, name);
3150 else
3151 isn_type = ISN_LOADG;
3152 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003153 else
3154 {
3155 isn_type = ISN_LOADAUTO;
3156 vim_free(name);
3157 name = vim_strnsave(*arg, end - *arg);
3158 if (name == NULL)
3159 return FAIL;
3160 }
3161 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003162 case 'w': isn_type = ISN_LOADW; break;
3163 case 't': isn_type = ISN_LOADT; break;
3164 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003165 default: // cannot happen, just in case
3166 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003167 goto theend;
3168 }
3169 if (isn_type != ISN_DROP)
3170 {
3171 // Global, Buffer-local, Window-local and Tabpage-local
3172 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003173 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003174 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177 }
3178 else
3179 {
3180 size_t len = end - *arg;
3181 int idx;
3182 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003183 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184
3185 name = vim_strnsave(*arg, end - *arg);
3186 if (name == NULL)
3187 return FAIL;
3188
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003189 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3190 {
3191 script_autoload(name, FALSE);
3192 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3193 }
3194 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3195 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003197 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003198 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 }
3200 else
3201 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003202 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003203
Bram Moolenaar709664c2020-12-12 14:33:41 +01003204 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003206 type = lvar.lv_type;
3207 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003208 if (lvar.lv_from_outer != 0)
3209 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003210 else
3211 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213 else
3214 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003215 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003216 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003217 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003218 || find_imported(name, 0, cctx) != NULL)
3219 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003220
Bram Moolenaar0f769812020-09-12 18:32:34 +02003221 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003222 // uppercase letter it can be a user defined function.
3223 // generate_funcref() will fail if the function can't be found.
3224 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003225 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 }
3227 }
3228 if (gen_load)
3229 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003230 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003231 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003232 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003233 cctx->ctx_outer_used = TRUE;
3234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 }
3236
3237 *arg = end;
3238
3239theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003240 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003241 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 vim_free(name);
3243 return res;
3244}
3245
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003246 static void
3247clear_instr_ga(garray_T *gap)
3248{
3249 int idx;
3250
3251 for (idx = 0; idx < gap->ga_len; ++idx)
3252 delete_instr(((isn_T *)gap->ga_data) + idx);
3253 ga_clear(gap);
3254}
3255
3256/*
3257 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3258 * Returns FAIL if compilation fails.
3259 */
3260 static int
3261compile_string(isn_T *isn, cctx_T *cctx)
3262{
3263 char_u *s = isn->isn_arg.string;
3264 garray_T save_ga = cctx->ctx_instr;
3265 int expr_res;
3266 int trailing_error;
3267 int instr_count;
3268 isn_T *instr = NULL;
3269
Bram Moolenaarcd268012021-07-22 19:11:08 +02003270 // Remove the string type from the stack.
3271 --cctx->ctx_type_stack.ga_len;
3272
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003273 // Temporarily reset the list of instructions so that the jump labels are
3274 // correct.
3275 cctx->ctx_instr.ga_len = 0;
3276 cctx->ctx_instr.ga_maxlen = 0;
3277 cctx->ctx_instr.ga_data = NULL;
3278 expr_res = compile_expr0(&s, cctx);
3279 s = skipwhite(s);
3280 trailing_error = *s != NUL;
3281
Bram Moolenaarff652882021-05-16 15:24:49 +02003282 if (expr_res == FAIL || trailing_error
3283 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003284 {
3285 if (trailing_error)
3286 semsg(_(e_trailing_arg), s);
3287 clear_instr_ga(&cctx->ctx_instr);
3288 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003289 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003290 return FAIL;
3291 }
3292
3293 // Move the generated instructions into the ISN_INSTR instruction, then
3294 // restore the list of instructions.
3295 instr_count = cctx->ctx_instr.ga_len;
3296 instr = cctx->ctx_instr.ga_data;
3297 instr[instr_count].isn_type = ISN_FINISH;
3298
3299 cctx->ctx_instr = save_ga;
3300 vim_free(isn->isn_arg.string);
3301 isn->isn_type = ISN_INSTR;
3302 isn->isn_arg.instr = instr;
3303 return OK;
3304}
3305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306/*
3307 * Compile the argument expressions.
3308 * "arg" points to just after the "(" and is advanced to after the ")"
3309 */
3310 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003311compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003313 char_u *p = *arg;
3314 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003315 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003316 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317
Bram Moolenaare6085c52020-04-12 20:19:16 +02003318 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003320 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3321 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003322 if (*p == ')')
3323 {
3324 *arg = p + 1;
3325 return OK;
3326 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003327 if (must_end)
3328 {
3329 semsg(_(e_missing_comma_before_argument_str), p);
3330 return FAIL;
3331 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003332
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003333 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003334 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 return FAIL;
3336 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003337
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003338 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003339 && cctx->ctx_instr.ga_len == instr_count + 1)
3340 {
3341 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3342
3343 // {skip} argument of searchpair() can be compiled if not empty
3344 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3345 compile_string(isn, cctx);
3346 }
3347
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003348 if (*p != ',' && *skipwhite(p) == ',')
3349 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003350 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003351 p = skipwhite(p);
3352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003354 {
3355 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003356 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003357 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003358 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003359 else
3360 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003361 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003362 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003364failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003365 emsg(_(e_missing_close));
3366 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367}
3368
3369/*
3370 * Compile a function call: name(arg1, arg2)
3371 * "arg" points to "name", "arg + varlen" to the "(".
3372 * "argcount_init" is 1 for "value->method()"
3373 * Instructions:
3374 * EVAL arg1
3375 * EVAL arg2
3376 * BCALL / DCALL / UCALL
3377 */
3378 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003379compile_call(
3380 char_u **arg,
3381 size_t varlen,
3382 cctx_T *cctx,
3383 ppconst_T *ppconst,
3384 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385{
3386 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003387 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 int argcount = argcount_init;
3389 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003390 char_u fname_buf[FLEN_FIXED + 1];
3391 char_u *tofree = NULL;
3392 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003393 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003394 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003395 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003396 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397
Bram Moolenaara5565e42020-05-09 15:44:01 +02003398 // we can evaluate "has('name')" at compile time
3399 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3400 {
3401 char_u *s = skipwhite(*arg + varlen + 1);
3402 typval_T argvars[2];
3403
3404 argvars[0].v_type = VAR_UNKNOWN;
3405 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003406 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003407 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003408 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003409 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003410 if (*s == ')' && argvars[0].v_type == VAR_STRING
3411 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003412 {
3413 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3414
3415 *arg = s + 1;
3416 argvars[1].v_type = VAR_UNKNOWN;
3417 tv->v_type = VAR_NUMBER;
3418 tv->vval.v_number = 0;
3419 f_has(argvars, tv);
3420 clear_tv(&argvars[0]);
3421 ++ppconst->pp_used;
3422 return OK;
3423 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003424 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003425 }
3426
3427 if (generate_ppconst(cctx, ppconst) == FAIL)
3428 return FAIL;
3429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 if (varlen >= sizeof(namebuf))
3431 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003432 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 return FAIL;
3434 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003435 vim_strncpy(namebuf, *arg, varlen);
3436 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003438 // We handle the "skip" argument of searchpair() and searchpairpos()
3439 // differently.
3440 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3441 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3442 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3443 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003446 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003447 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
Bram Moolenaar03290b82020-12-19 16:30:44 +01003449 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003450 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 {
3452 int idx;
3453
3454 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003455 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003457 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003458 if (STRCMP(name, "flatten") == 0)
3459 {
3460 emsg(_(e_cannot_use_flatten_in_vim9_script));
3461 goto theend;
3462 }
3463
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003464 if (STRCMP(name, "add") == 0 && argcount == 2)
3465 {
3466 garray_T *stack = &cctx->ctx_type_stack;
3467 type_T *type = ((type_T **)stack->ga_data)[
3468 stack->ga_len - 2];
3469
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003470 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003471 if (type->tt_type == VAR_LIST)
3472 {
3473 // inline "add(list, item)" so that the type can be checked
3474 res = generate_LISTAPPEND(cctx);
3475 idx = -1;
3476 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003477 else if (type->tt_type == VAR_BLOB)
3478 {
3479 // inline "add(blob, nr)" so that the type can be checked
3480 res = generate_BLOBAPPEND(cctx);
3481 idx = -1;
3482 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003483 }
3484
3485 if (idx >= 0)
3486 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3487 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003488 else
3489 semsg(_(e_unknownfunc), namebuf);
3490 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
3492
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003493 // An argument or local variable can be a function reference, this
3494 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003495 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003496 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003497 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003498 // If we can find the function by name generate the right call.
3499 // Skip global functions here, a local funcref takes precedence.
3500 ufunc = find_func(name, FALSE, cctx);
3501 if (ufunc != NULL && !func_is_global(ufunc))
3502 {
3503 res = generate_CALL(cctx, ufunc, argcount);
3504 goto theend;
3505 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507
3508 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003509 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003510 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003512 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003513 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003514 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003515 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003516 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003517
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003518 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003519 goto theend;
3520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521
Bram Moolenaar0f769812020-09-12 18:32:34 +02003522 // If we can find a global function by name generate the right call.
3523 if (ufunc != NULL)
3524 {
3525 res = generate_CALL(cctx, ufunc, argcount);
3526 goto theend;
3527 }
3528
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003529 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003530 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003531 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003532 res = generate_UCALL(cctx, name, argcount);
3533 else
3534 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003535
3536theend:
3537 vim_free(tofree);
3538 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539}
3540
3541// like NAMESPACE_CHAR but with 'a' and 'l'.
3542#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3543
3544/*
3545 * Find the end of a variable or function name. Unlike find_name_end() this
3546 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003547 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 * Return a pointer to just after the name. Equal to "arg" if there is no
3549 * valid name.
3550 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003551 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003552to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553{
3554 char_u *p;
3555
3556 // Quick check for valid starting character.
3557 if (!eval_isnamec1(*arg))
3558 return arg;
3559
3560 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3561 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3562 // and can be used in slice "[n:]".
3563 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003564 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3566 break;
3567 return p;
3568}
3569
3570/*
3571 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003572 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003573 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 */
3575 char_u *
3576to_name_const_end(char_u *arg)
3577{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003578 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 typval_T rettv;
3580
Bram Moolenaar678b2072021-07-26 21:10:11 +02003581 if (STRNCMP(p, "<SNR>", 5) == 0)
3582 p = skipdigits(p + 5);
3583 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 if (p == arg && *arg == '[')
3585 {
3586
3587 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003588 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 p = arg;
3590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 return p;
3592}
3593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594/*
3595 * parse a list: [expr, expr]
3596 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003597 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 */
3599 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003600compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601{
3602 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003603 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003605 int is_const;
3606 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003608 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003610 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003611 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003612 semsg(_(e_list_end), *arg);
3613 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003614 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003615 if (*p == ',')
3616 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003617 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003618 return FAIL;
3619 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003620 if (*p == ']')
3621 {
3622 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003623 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003624 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003625 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003626 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003627 if (!is_const)
3628 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 ++count;
3630 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003631 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003633 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3634 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003635 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003636 return FAIL;
3637 }
3638 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003639 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 p = skipwhite(p);
3641 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003642 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003644 ppconst->pp_is_const = is_all_const;
3645 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646}
3647
3648/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003649 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003650 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003651 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 */
3653 static int
3654compile_lambda(char_u **arg, cctx_T *cctx)
3655{
Bram Moolenaare462f522020-12-27 14:43:30 +01003656 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 typval_T rettv;
3658 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003659 evalarg_T evalarg;
3660
3661 CLEAR_FIELD(evalarg);
3662 evalarg.eval_flags = EVAL_EVALUATE;
3663 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664
3665 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003666 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3667 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003668 {
3669 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003670 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003671 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003672
Bram Moolenaar65c44152020-12-24 15:14:01 +01003673 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003675 ++ufunc->uf_refcount;
3676 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
Bram Moolenaara9931532021-06-12 15:58:16 +02003678 // Compile it here to get the return type. The return type is optional,
3679 // when it's missing use t_unknown. This is recognized in
3680 // compile_return().
3681 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3682 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003683 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684
Bram Moolenaar648594e2021-07-11 17:55:01 +02003685#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003686 // When the outer function is compiled for profiling, the lambda may be
3687 // called without profiling. Compile it here in the right context.
3688 if (cctx->ctx_compile_type == CT_PROFILE)
3689 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003690#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003691
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003692 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3693 // points into it. Point to the original line to avoid a dangling pointer.
3694 if (evalarg.eval_tofree_cmdline != NULL)
3695 {
3696 size_t off = *arg - evalarg.eval_tofree_cmdline;
3697
3698 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3699 + off;
3700 }
3701
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003702 clear_evalarg(&evalarg, NULL);
3703
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003704 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003705 {
3706 // The return type will now be known.
3707 set_function_type(ufunc);
3708
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003709 // The function reference count will be 1. When the ISN_FUNCREF
3710 // instruction is deleted the reference count is decremented and the
3711 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003712 return generate_FUNCREF(cctx, ufunc);
3713 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003714
3715 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 return FAIL;
3717}
3718
3719/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003720 * Get a lambda and compile it. Uses Vim9 syntax.
3721 */
3722 int
3723get_lambda_tv_and_compile(
3724 char_u **arg,
3725 typval_T *rettv,
3726 int types_optional,
3727 evalarg_T *evalarg)
3728{
3729 int r;
3730 ufunc_T *ufunc;
3731 int save_sc_version = current_sctx.sc_version;
3732
3733 // Get the funcref in "rettv".
3734 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3735 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3736 current_sctx.sc_version = save_sc_version;
3737 if (r != OK)
3738 return r;
3739
3740 // "rettv" will now be a partial referencing the function.
3741 ufunc = rettv->vval.v_partial->pt_func;
3742
3743 // Compile it here to get the return type. The return type is optional,
3744 // when it's missing use t_unknown. This is recognized in
3745 // compile_return().
3746 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3747 ufunc->uf_ret_type = &t_unknown;
3748 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3749
3750 if (ufunc->uf_def_status == UF_COMPILED)
3751 {
3752 // The return type will now be known.
3753 set_function_type(ufunc);
3754 return OK;
3755 }
3756 clear_tv(rettv);
3757 return FAIL;
3758}
3759
3760/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003761 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003763 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 */
3765 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003766compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767{
3768 garray_T *instr = &cctx->ctx_instr;
3769 int count = 0;
3770 dict_T *d = dict_alloc();
3771 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003772 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003773 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003774 int is_const;
3775 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776
3777 if (d == NULL)
3778 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003779 if (generate_ppconst(cctx, ppconst) == FAIL)
3780 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003781 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003783 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784
Bram Moolenaar23c55272020-06-21 16:58:13 +02003785 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003786 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003787 *arg = NULL;
3788 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003789 }
3790
3791 if (**arg == '}')
3792 break;
3793
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003794 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003796 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003798 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003799 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003800 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003803 if (isn->isn_type == ISN_PUSHNR)
3804 {
3805 char buf[NUMBUFLEN];
3806
3807 // Convert to string at compile time.
3808 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3809 isn->isn_type = ISN_PUSHS;
3810 isn->isn_arg.string = vim_strsave((char_u *)buf);
3811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 if (isn->isn_type == ISN_PUSHS)
3813 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003814 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003815 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003816 *arg = skipwhite(*arg);
3817 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003818 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003819 emsg(_(e_missing_matching_bracket_after_dict_key));
3820 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003821 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003822 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003824 else
3825 {
3826 // {"name": value},
3827 // {'name': value},
3828 // {name: value} use "name" as a literal key
3829 key = get_literal_key(arg);
3830 if (key == NULL)
3831 return FAIL;
3832 if (generate_PUSHS(cctx, key) == FAIL)
3833 return FAIL;
3834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835
3836 // Check for duplicate keys, if using string keys.
3837 if (key != NULL)
3838 {
3839 item = dict_find(d, key, -1);
3840 if (item != NULL)
3841 {
3842 semsg(_(e_duplicate_key), key);
3843 goto failret;
3844 }
3845 item = dictitem_alloc(key);
3846 if (item != NULL)
3847 {
3848 item->di_tv.v_type = VAR_UNKNOWN;
3849 item->di_tv.v_lock = 0;
3850 if (dict_add(d, item) == FAIL)
3851 dictitem_free(item);
3852 }
3853 }
3854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 if (**arg != ':')
3856 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003857 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003858 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003859 else
3860 semsg(_(e_missing_dict_colon), *arg);
3861 return FAIL;
3862 }
3863 whitep = *arg + 1;
3864 if (!IS_WHITE_OR_NUL(*whitep))
3865 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003866 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 return FAIL;
3868 }
3869
Bram Moolenaar23c55272020-06-21 16:58:13 +02003870 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003871 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003872 *arg = NULL;
3873 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003874 }
3875
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003876 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003878 if (!is_const)
3879 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 ++count;
3881
Bram Moolenaar2c330432020-04-13 14:41:35 +02003882 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003883 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003884 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003885 *arg = NULL;
3886 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 if (**arg == '}')
3889 break;
3890 if (**arg != ',')
3891 {
3892 semsg(_(e_missing_dict_comma), *arg);
3893 goto failret;
3894 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003895 if (IS_WHITE_OR_NUL(*whitep))
3896 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003897 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003898 return FAIL;
3899 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003900 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003901 if (!IS_WHITE_OR_NUL(*whitep))
3902 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003903 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003904 return FAIL;
3905 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003906 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 }
3908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 *arg = *arg + 1;
3910
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003911 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003912 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003913 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003914 *arg += STRLEN(*arg);
3915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003917 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 return generate_NEWDICT(cctx, count);
3919
3920failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003921 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003922 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003923 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003924 *arg = (char_u *)"";
3925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 dict_unref(d);
3927 return FAIL;
3928}
3929
3930/*
3931 * Compile "&option".
3932 */
3933 static int
3934compile_get_option(char_u **arg, cctx_T *cctx)
3935{
3936 typval_T rettv;
3937 char_u *start = *arg;
3938 int ret;
3939
3940 // parse the option and get the current value to get the type.
3941 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003942 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 if (ret == OK)
3944 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003945 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003946 char_u *name = vim_strnsave(start, *arg - start);
3947 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3948 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949
3950 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3951 vim_free(name);
3952 }
3953 clear_tv(&rettv);
3954
3955 return ret;
3956}
3957
3958/*
3959 * Compile "$VAR".
3960 */
3961 static int
3962compile_get_env(char_u **arg, cctx_T *cctx)
3963{
3964 char_u *start = *arg;
3965 int len;
3966 int ret;
3967 char_u *name;
3968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 ++*arg;
3970 len = get_env_len(arg);
3971 if (len == 0)
3972 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003973 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 return FAIL;
3975 }
3976
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003977 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 name = vim_strnsave(start, len + 1);
3979 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3980 vim_free(name);
3981 return ret;
3982}
3983
3984/*
3985 * Compile "@r".
3986 */
3987 static int
3988compile_get_register(char_u **arg, cctx_T *cctx)
3989{
3990 int ret;
3991
3992 ++*arg;
3993 if (**arg == NUL)
3994 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003995 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 return FAIL;
3997 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003998 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 {
4000 emsg_invreg(**arg);
4001 return FAIL;
4002 }
4003 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4004 ++*arg;
4005 return ret;
4006}
4007
4008/*
4009 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004010 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 */
4012 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004013apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004015 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
4017 // this works from end to start
4018 while (p > start)
4019 {
4020 --p;
4021 if (*p == '-' || *p == '+')
4022 {
4023 // only '-' has an effect, for '+' we only check the type
4024#ifdef FEAT_FLOAT
4025 if (rettv->v_type == VAR_FLOAT)
4026 {
4027 if (*p == '-')
4028 rettv->vval.v_float = -rettv->vval.v_float;
4029 }
4030 else
4031#endif
4032 {
4033 varnumber_T val;
4034 int error = FALSE;
4035
4036 // tv_get_number_chk() accepts a string, but we don't want that
4037 // here
4038 if (check_not_string(rettv) == FAIL)
4039 return FAIL;
4040 val = tv_get_number_chk(rettv, &error);
4041 clear_tv(rettv);
4042 if (error)
4043 return FAIL;
4044 if (*p == '-')
4045 val = -val;
4046 rettv->v_type = VAR_NUMBER;
4047 rettv->vval.v_number = val;
4048 }
4049 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004050 else if (numeric_only)
4051 {
4052 ++p;
4053 break;
4054 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004055 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 {
4057 int v = tv2bool(rettv);
4058
4059 // '!' is permissive in the type.
4060 clear_tv(rettv);
4061 rettv->v_type = VAR_BOOL;
4062 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4063 }
4064 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004065 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 return OK;
4067}
4068
4069/*
4070 * Recognize v: variables that are constants and set "rettv".
4071 */
4072 static void
4073get_vim_constant(char_u **arg, typval_T *rettv)
4074{
4075 if (STRNCMP(*arg, "v:true", 6) == 0)
4076 {
4077 rettv->v_type = VAR_BOOL;
4078 rettv->vval.v_number = VVAL_TRUE;
4079 *arg += 6;
4080 }
4081 else if (STRNCMP(*arg, "v:false", 7) == 0)
4082 {
4083 rettv->v_type = VAR_BOOL;
4084 rettv->vval.v_number = VVAL_FALSE;
4085 *arg += 7;
4086 }
4087 else if (STRNCMP(*arg, "v:null", 6) == 0)
4088 {
4089 rettv->v_type = VAR_SPECIAL;
4090 rettv->vval.v_number = VVAL_NULL;
4091 *arg += 6;
4092 }
4093 else if (STRNCMP(*arg, "v:none", 6) == 0)
4094 {
4095 rettv->v_type = VAR_SPECIAL;
4096 rettv->vval.v_number = VVAL_NONE;
4097 *arg += 6;
4098 }
4099}
4100
Bram Moolenaar657137c2021-01-09 15:45:23 +01004101 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004102get_compare_type(char_u *p, int *len, int *type_is)
4103{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004104 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004105 int i;
4106
4107 switch (p[0])
4108 {
4109 case '=': if (p[1] == '=')
4110 type = EXPR_EQUAL;
4111 else if (p[1] == '~')
4112 type = EXPR_MATCH;
4113 break;
4114 case '!': if (p[1] == '=')
4115 type = EXPR_NEQUAL;
4116 else if (p[1] == '~')
4117 type = EXPR_NOMATCH;
4118 break;
4119 case '>': if (p[1] != '=')
4120 {
4121 type = EXPR_GREATER;
4122 *len = 1;
4123 }
4124 else
4125 type = EXPR_GEQUAL;
4126 break;
4127 case '<': if (p[1] != '=')
4128 {
4129 type = EXPR_SMALLER;
4130 *len = 1;
4131 }
4132 else
4133 type = EXPR_SEQUAL;
4134 break;
4135 case 'i': if (p[1] == 's')
4136 {
4137 // "is" and "isnot"; but not a prefix of a name
4138 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4139 *len = 5;
4140 i = p[*len];
4141 if (!isalnum(i) && i != '_')
4142 {
4143 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4144 *type_is = TRUE;
4145 }
4146 }
4147 break;
4148 }
4149 return type;
4150}
4151
4152/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004153 * Skip over an expression, ignoring most errors.
4154 */
4155 static void
4156skip_expr_cctx(char_u **arg, cctx_T *cctx)
4157{
4158 evalarg_T evalarg;
4159
4160 CLEAR_FIELD(evalarg);
4161 evalarg.eval_cctx = cctx;
4162 skip_expr(arg, &evalarg);
4163}
4164
4165/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004167 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 */
4169 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004170compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004172 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173
4174 // this works from end to start
4175 while (p > start)
4176 {
4177 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004178 while (VIM_ISWHITE(*p))
4179 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 if (*p == '-' || *p == '+')
4181 {
4182 int negate = *p == '-';
4183 isn_T *isn;
4184
4185 // TODO: check type
4186 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4187 {
4188 --p;
4189 if (*p == '-')
4190 negate = !negate;
4191 }
4192 // only '-' has an effect, for '+' we only check the type
4193 if (negate)
4194 isn = generate_instr(cctx, ISN_NEGATENR);
4195 else
4196 isn = generate_instr(cctx, ISN_CHECKNR);
4197 if (isn == NULL)
4198 return FAIL;
4199 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004200 else if (numeric_only)
4201 {
4202 ++p;
4203 break;
4204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 else
4206 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004207 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004209 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004211 if (p[-1] == '!')
4212 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004215 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 return FAIL;
4217 }
4218 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004219 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 return OK;
4221}
4222
4223/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004224 * Compile "(expression)": recursive!
4225 * Return FAIL/OK.
4226 */
4227 static int
4228compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4229{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004230 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004231 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004232
Bram Moolenaar24156692021-01-14 20:35:49 +01004233 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4234 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004235 if (ppconst->pp_used <= PPSIZE - 10)
4236 {
4237 ret = compile_expr1(arg, cctx, ppconst);
4238 }
4239 else
4240 {
4241 // Not enough space in ppconst, flush constants.
4242 if (generate_ppconst(cctx, ppconst) == FAIL)
4243 return FAIL;
4244 ret = compile_expr0(arg, cctx);
4245 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004246 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4247 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004248 if (**arg == ')')
4249 ++*arg;
4250 else if (ret == OK)
4251 {
4252 emsg(_(e_missing_close));
4253 ret = FAIL;
4254 }
4255 return ret;
4256}
4257
4258/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004260 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 */
4262 static int
4263compile_subscript(
4264 char_u **arg,
4265 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004266 char_u *start_leader,
4267 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004268 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004270 char_u *name_start = *end_leader;
4271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 for (;;)
4273 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004274 char_u *p = skipwhite(*arg);
4275
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004276 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004277 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004278 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004279
4280 // If a following line starts with "->{" or "->X" advance to that
4281 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004282 // Also if a following line starts with ".x".
4283 if (next != NULL &&
4284 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004285 && (next[2] == '{'
4286 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004287 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004288 {
4289 next = next_line_from_context(cctx, TRUE);
4290 if (next == NULL)
4291 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004292 *arg = next;
4293 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004294 }
4295 }
4296
Bram Moolenaarcd268012021-07-22 19:11:08 +02004297 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4298 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004299 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004301 garray_T *stack = &cctx->ctx_type_stack;
4302 type_T *type;
4303 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004305 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004307 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004310 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4311
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004312 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004313 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004315 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 return FAIL;
4317 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004318 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004320 char_u *pstart = p;
4321
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004322 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004323 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004324 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 // something->method()
4327 // Apply the '!', '-' and '+' first:
4328 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004329 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004332 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004333 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004334 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004335 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004336 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004337 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004338 garray_T *stack = &cctx->ctx_type_stack;
4339 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004340 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004341 int expr_isn_start = cctx->ctx_instr.ga_len;
4342 int expr_isn_end;
4343 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004344
4345 // Funcref call: list->(Refs[2])(arg)
4346 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004347 //
4348 // Fist compile the function expression.
4349 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004350 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004351
4352 // Remember the next instruction index, where the instructions
4353 // for arguments are being written.
4354 expr_isn_end = cctx->ctx_instr.ga_len;
4355
4356 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004357 if (**arg != '(')
4358 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004359 if (*skipwhite(*arg) == '(')
4360 emsg(_(e_nowhitespace));
4361 else
4362 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004363 return FAIL;
4364 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004365 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004366 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004367 return FAIL;
4368
Bram Moolenaar2927c072021-04-05 19:41:21 +02004369 // Move the instructions for the arguments to before the
4370 // instructions of the expression and move the type of the
4371 // expression after the argument types. This is what ISN_PCALL
4372 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004373 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004374 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4375 if (arg_isn_count > 0)
4376 {
4377 int expr_isn_count = expr_isn_end - expr_isn_start;
4378 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4379
4380 if (isn == NULL)
4381 return FAIL;
4382 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4383 + expr_isn_start,
4384 sizeof(isn_T) * expr_isn_count);
4385 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4386 + expr_isn_start,
4387 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4388 sizeof(isn_T) * arg_isn_count);
4389 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4390 + expr_isn_start + arg_isn_count,
4391 isn, sizeof(isn_T) * expr_isn_count);
4392 vim_free(isn);
4393
4394 type = ((type_T **)stack->ga_data)[type_idx_start];
4395 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4396 ((type_T **)stack->ga_data) + type_idx_start + 1,
4397 sizeof(type_T *)
4398 * (stack->ga_len - type_idx_start - 1));
4399 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4400 }
4401
Bram Moolenaar7e368202020-12-25 21:56:57 +01004402 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004403 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 return FAIL;
4405 }
4406 else
4407 {
4408 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004409 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004410 if (!eval_isnamec1(*p))
4411 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004412 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004413 return FAIL;
4414 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004415 if (ASCII_ISALPHA(*p) && p[1] == ':')
4416 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004417 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 ;
4419 if (*p != '(')
4420 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004421 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 return FAIL;
4423 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004424 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 return FAIL;
4426 }
4427 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004428 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004430 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004431
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004432 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004433 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004434 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004435 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004436 // TODO: more arguments
4437 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004438 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004439 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004440 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004441
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004442 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004443 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004444 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004445 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004446 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004447 // missing first index is equal to zero
4448 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004449 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004450 else
4451 {
4452 if (compile_expr0(arg, cctx) == FAIL)
4453 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004454 if (**arg == ':')
4455 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004456 semsg(_(e_white_space_required_before_and_after_str_at_str),
4457 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004458 return FAIL;
4459 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004460 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004461 return FAIL;
4462 *arg = skipwhite(*arg);
4463 }
4464 if (**arg == ':')
4465 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004466 is_slice = TRUE;
4467 ++*arg;
4468 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4469 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004470 semsg(_(e_white_space_required_before_and_after_str_at_str),
4471 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004472 return FAIL;
4473 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004474 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004475 return FAIL;
4476 if (**arg == ']')
4477 // missing second index is equal to end of string
4478 generate_PUSHNR(cctx, -1);
4479 else
4480 {
4481 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004482 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004483 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004484 return FAIL;
4485 *arg = skipwhite(*arg);
4486 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488
4489 if (**arg != ']')
4490 {
4491 emsg(_(e_missbrac));
4492 return FAIL;
4493 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004494 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495
Bram Moolenaare42939a2021-04-05 17:11:17 +02004496 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004497 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004499 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004501 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004502 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004503 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004504 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004505
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004506 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004507 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004508 {
4509 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004510 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004511 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004512 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004513 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 while (eval_isnamec(*p))
4515 MB_PTR_ADV(p);
4516 if (p == *arg)
4517 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004518 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 return FAIL;
4520 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004521 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 return FAIL;
4523 *arg = p;
4524 }
4525 else
4526 break;
4527 }
4528
4529 // TODO - see handle_subscript():
4530 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4531 // Don't do this when "Func" is already a partial that was bound
4532 // explicitly (pt_auto is FALSE).
4533
4534 return OK;
4535}
4536
4537/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004538 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4539 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004541 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004542 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004543 *
4544 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 */
4546
4547/*
4548 * number number constant
4549 * 0zFFFFFFFF Blob constant
4550 * "string" string constant
4551 * 'string' literal string constant
4552 * &option-name option value
4553 * @r register contents
4554 * identifier variable value
4555 * function() function call
4556 * $VAR environment variable
4557 * (expression) nested expression
4558 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004559 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 *
4561 * Also handle:
4562 * ! in front logical NOT
4563 * - in front unary minus
4564 * + in front unary plus (ignored)
4565 * trailing (arg) funcref/partial call
4566 * trailing [] subscript in String or List
4567 * trailing .name entry in Dictionary
4568 * trailing ->name() method call
4569 */
4570 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004571compile_expr7(
4572 char_u **arg,
4573 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004574 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 char_u *start_leader, *end_leader;
4577 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004578 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004579 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004581 ppconst->pp_is_const = FALSE;
4582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 /*
4584 * Skip '!', '-' and '+' characters. They are handled later.
4585 */
4586 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004587 if (eval_leader(arg, TRUE) == FAIL)
4588 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 end_leader = *arg;
4590
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004591 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 switch (**arg)
4593 {
4594 /*
4595 * Number constant.
4596 */
4597 case '0': // also for blob starting with 0z
4598 case '1':
4599 case '2':
4600 case '3':
4601 case '4':
4602 case '5':
4603 case '6':
4604 case '7':
4605 case '8':
4606 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004607 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004609 // Apply "-" and "+" just before the number now, right to
4610 // left. Matters especially when "->" follows. Stops at
4611 // '!'.
4612 if (apply_leader(rettv, TRUE,
4613 start_leader, &end_leader) == FAIL)
4614 {
4615 clear_tv(rettv);
4616 return FAIL;
4617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 break;
4619
4620 /*
4621 * String constant: "string".
4622 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004623 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 return FAIL;
4625 break;
4626
4627 /*
4628 * Literal string constant: 'str''ing'.
4629 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004630 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 return FAIL;
4632 break;
4633
4634 /*
4635 * Constant Vim variable.
4636 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004637 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 ret = NOTDONE;
4639 break;
4640
4641 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004642 * "true" constant
4643 */
4644 case 't': if (STRNCMP(*arg, "true", 4) == 0
4645 && !eval_isnamec((*arg)[4]))
4646 {
4647 *arg += 4;
4648 rettv->v_type = VAR_BOOL;
4649 rettv->vval.v_number = VVAL_TRUE;
4650 }
4651 else
4652 ret = NOTDONE;
4653 break;
4654
4655 /*
4656 * "false" constant
4657 */
4658 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4659 && !eval_isnamec((*arg)[5]))
4660 {
4661 *arg += 5;
4662 rettv->v_type = VAR_BOOL;
4663 rettv->vval.v_number = VVAL_FALSE;
4664 }
4665 else
4666 ret = NOTDONE;
4667 break;
4668
4669 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004670 * "null" constant
4671 */
4672 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004673 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004674 {
4675 *arg += 4;
4676 rettv->v_type = VAR_SPECIAL;
4677 rettv->vval.v_number = VVAL_NULL;
4678 }
4679 else
4680 ret = NOTDONE;
4681 break;
4682
4683 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 * List: [expr, expr]
4685 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004686 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4687 return FAIL;
4688 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 break;
4690
4691 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 * Dictionary: {'key': val, 'key': val}
4693 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004694 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4695 return FAIL;
4696 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 break;
4698
4699 /*
4700 * Option value: &name
4701 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004702 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4703 return FAIL;
4704 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 break;
4706
4707 /*
4708 * Environment variable: $VAR.
4709 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004710 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4711 return FAIL;
4712 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 break;
4714
4715 /*
4716 * Register contents: @r.
4717 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004718 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4719 return FAIL;
4720 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 break;
4722 /*
4723 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004724 * lambda: (arg, arg) => expr
4725 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004727 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4728 ret = compile_lambda(arg, cctx);
4729 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004730 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 break;
4732
4733 default: ret = NOTDONE;
4734 break;
4735 }
4736 if (ret == FAIL)
4737 return FAIL;
4738
Bram Moolenaar1c747212020-05-09 18:28:34 +02004739 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004741 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004742 clear_tv(rettv);
4743 else
4744 // A constant expression can possibly be handled compile time,
4745 // return the value instead of generating code.
4746 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 }
4748 else if (ret == NOTDONE)
4749 {
4750 char_u *p;
4751 int r;
4752
4753 if (!eval_isnamec1(**arg))
4754 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004755 if (!vim9_bad_comment(*arg))
4756 {
4757 if (ends_excmd(*skipwhite(*arg)))
4758 semsg(_(e_empty_expression_str), *arg);
4759 else
4760 semsg(_(e_name_expected_str), *arg);
4761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 return FAIL;
4763 }
4764
4765 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004766 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004767 if (p - *arg == (size_t)1 && **arg == '_')
4768 {
4769 emsg(_(e_cannot_use_underscore_here));
4770 return FAIL;
4771 }
4772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004774 {
4775 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 {
4779 if (generate_ppconst(cctx, ppconst) == FAIL)
4780 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004781 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 if (r == FAIL)
4784 return FAIL;
4785 }
4786
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004787 // Handle following "[]", ".member", etc.
4788 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004789 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004790 ppconst) == FAIL)
4791 return FAIL;
4792 if (ppconst->pp_used > 0)
4793 {
4794 // apply the '!', '-' and '+' before the constant
4795 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004796 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797 return FAIL;
4798 return OK;
4799 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004800 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004802 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803}
4804
4805/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004806 * Give the "white on both sides" error, taking the operator from "p[len]".
4807 */
4808 void
4809error_white_both(char_u *op, int len)
4810{
4811 char_u buf[10];
4812
4813 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004814 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004815}
4816
4817/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004818 * <type>expr7: runtime type check / conversion
4819 */
4820 static int
4821compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4822{
4823 type_T *want_type = NULL;
4824
4825 // Recognize <type>
4826 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4827 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004828 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004829 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4830 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004831 return FAIL;
4832
4833 if (**arg != '>')
4834 {
4835 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004836 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004837 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004838 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004839 return FAIL;
4840 }
4841 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004842 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004843 return FAIL;
4844 }
4845
4846 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4847 return FAIL;
4848
4849 if (want_type != NULL)
4850 {
4851 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004852 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004853 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004854
Bram Moolenaard1103582020-08-14 22:44:25 +02004855 generate_ppconst(cctx, ppconst);
4856 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004857 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004858 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004859 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004860 return FAIL;
4861 }
4862 }
4863
4864 return OK;
4865}
4866
4867/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 * * number multiplication
4869 * / number division
4870 * % number modulo
4871 */
4872 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874{
4875 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004876 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004877 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004879 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004880 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 return FAIL;
4882
4883 /*
4884 * Repeat computing, until no "*", "/" or "%" is following.
4885 */
4886 for (;;)
4887 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004888 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 if (*op != '*' && *op != '/' && *op != '%')
4890 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004891 if (next != NULL)
4892 {
4893 *arg = next_line_from_context(cctx, TRUE);
4894 op = skipwhite(*arg);
4895 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004896
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004897 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004899 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004900 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004902 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004903 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004905 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004906 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004908
4909 if (ppconst->pp_used == ppconst_used + 2
4910 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4911 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004912 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004913 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4914 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4915 varnumber_T res = 0;
4916 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004918 // both are numbers: compute the result
4919 switch (*op)
4920 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004921 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004922 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004923 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004924 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004925 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004926 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004927 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004928 break;
4929 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004930 if (failed)
4931 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004932 tv1->vval.v_number = res;
4933 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004934 }
4935 else
4936 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004937 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004938 generate_two_op(cctx, op);
4939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 }
4941
4942 return OK;
4943}
4944
4945/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004946 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 * - number subtraction
4948 * .. string concatenation
4949 */
4950 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004951compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952{
4953 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004954 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004956 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957
4958 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004959 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 return FAIL;
4961
4962 /*
4963 * Repeat computing, until no "+", "-" or ".." is following.
4964 */
4965 for (;;)
4966 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004967 op = may_peek_next_line(cctx, *arg, &next);
4968 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004970 if (op[0] == op[1] && *op != '.' && next)
4971 // Finding "++" or "--" on the next line is a separate command.
4972 // But ".." is concatenation.
4973 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004975 if (next != NULL)
4976 {
4977 *arg = next_line_from_context(cctx, TRUE);
4978 op = skipwhite(*arg);
4979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004981 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004983 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004984 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 }
4986
Bram Moolenaare0de1712020-12-02 17:36:54 +01004987 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004988 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004990 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004991 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 return FAIL;
4993
Bram Moolenaara5565e42020-05-09 15:44:01 +02004994 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004995 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004996 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4997 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4998 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4999 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5002 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005003
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005004 // concat/subtract/add constant numbers
5005 if (*op == '+')
5006 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5007 else if (*op == '-')
5008 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5009 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005010 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005011 // concatenate constant strings
5012 char_u *s1 = tv1->vval.v_string;
5013 char_u *s2 = tv2->vval.v_string;
5014 size_t len1 = STRLEN(s1);
5015
5016 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5017 if (tv1->vval.v_string == NULL)
5018 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005019 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005020 return FAIL;
5021 }
5022 mch_memmove(tv1->vval.v_string, s1, len1);
5023 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005024 vim_free(s1);
5025 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005026 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 }
5029 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005030 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005031 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005032 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005033 if (*op == '.')
5034 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005035 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5036 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005037 return FAIL;
5038 generate_instr_drop(cctx, ISN_CONCAT, 1);
5039 }
5040 else
5041 generate_two_op(cctx, op);
5042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 }
5044
5045 return OK;
5046}
5047
5048/*
5049 * expr5a == expr5b
5050 * expr5a =~ expr5b
5051 * expr5a != expr5b
5052 * expr5a !~ expr5b
5053 * expr5a > expr5b
5054 * expr5a >= expr5b
5055 * expr5a < expr5b
5056 * expr5a <= expr5b
5057 * expr5a is expr5b
5058 * expr5a isnot expr5b
5059 *
5060 * Produces instructions:
5061 * EVAL expr5a Push result of "expr5a"
5062 * EVAL expr5b Push result of "expr5b"
5063 * COMPARE one of the compare instructions
5064 */
5065 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005066compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005068 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005070 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005073 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074
5075 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005076 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 return FAIL;
5078
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005079 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005080 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081
5082 /*
5083 * If there is a comparative operator, use it.
5084 */
5085 if (type != EXPR_UNKNOWN)
5086 {
5087 int ic = FALSE; // Default: do not ignore case
5088
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005089 if (next != NULL)
5090 {
5091 *arg = next_line_from_context(cctx, TRUE);
5092 p = skipwhite(*arg);
5093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 if (type_is && (p[len] == '?' || p[len] == '#'))
5095 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005096 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 return FAIL;
5098 }
5099 // extra question mark appended: ignore case
5100 if (p[len] == '?')
5101 {
5102 ic = TRUE;
5103 ++len;
5104 }
5105 // extra '#' appended: match case (ignored)
5106 else if (p[len] == '#')
5107 ++len;
5108 // nothing appended: match case
5109
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005110 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005112 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005113 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 }
5115
5116 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005117 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005118 return FAIL;
5119
Bram Moolenaara5565e42020-05-09 15:44:01 +02005120 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 return FAIL;
5122
Bram Moolenaara5565e42020-05-09 15:44:01 +02005123 if (ppconst->pp_used == ppconst_used + 2)
5124 {
5125 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5126 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5127 int ret;
5128
5129 // Both sides are a constant, compute the result now.
5130 // First check for a valid combination of types, this is more
5131 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005132 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133 ret = FAIL;
5134 else
5135 {
5136 ret = typval_compare(tv1, tv2, type, ic);
5137 tv1->v_type = VAR_BOOL;
5138 tv1->vval.v_number = tv1->vval.v_number
5139 ? VVAL_TRUE : VVAL_FALSE;
5140 clear_tv(tv2);
5141 --ppconst->pp_used;
5142 }
5143 return ret;
5144 }
5145
5146 generate_ppconst(cctx, ppconst);
5147 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 }
5149
5150 return OK;
5151}
5152
Bram Moolenaar7f141552020-05-09 17:35:53 +02005153static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155/*
5156 * Compile || or &&.
5157 */
5158 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005159compile_and_or(
5160 char_u **arg,
5161 cctx_T *cctx,
5162 char *op,
5163 ppconst_T *ppconst,
5164 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005166 char_u *next;
5167 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 int opchar = *op;
5169
5170 if (p[0] == opchar && p[1] == opchar)
5171 {
5172 garray_T *instr = &cctx->ctx_instr;
5173 garray_T end_ga;
5174
5175 /*
5176 * Repeat until there is no following "||" or "&&"
5177 */
5178 ga_init2(&end_ga, sizeof(int), 10);
5179 while (p[0] == opchar && p[1] == opchar)
5180 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005181 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005182 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005183 int start_ctx_lnum = cctx->ctx_lnum;
5184 int save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005185 int status;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005186
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005187 if (next != NULL)
5188 {
5189 *arg = next_line_from_context(cctx, TRUE);
5190 p = skipwhite(*arg);
5191 }
5192
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005193 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5194 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005195 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005196 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005197 return FAIL;
5198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005200 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005201 SOURCING_LNUM = start_lnum;
5202 save_lnum = cctx->ctx_lnum;
5203 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005204
5205 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005206 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005207 {
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005208 // TODO: use ppconst if the value is a constant
5209 generate_ppconst(cctx, ppconst);
5210
5211 // Every part must evaluate to a bool.
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005212 status = bool_on_stack(cctx);
5213 if (status != FAIL)
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005214 status = ga_grow(&end_ga, 1);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005215 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02005216 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005217 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 {
5219 ga_clear(&end_ga);
5220 return FAIL;
5221 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5224 ++end_ga.ga_len;
5225 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005226 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227
5228 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005229 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005230 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005231 {
5232 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005233 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005234 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005235
Bram Moolenaara5565e42020-05-09 15:44:01 +02005236 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5237 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238 {
5239 ga_clear(&end_ga);
5240 return FAIL;
5241 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005242
5243 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005245
5246 if (check_ppconst_bool(ppconst) == FAIL)
5247 {
5248 ga_clear(&end_ga);
5249 return FAIL;
5250 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005251 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005253 // Every part must evaluate to a bool.
5254 if (bool_on_stack(cctx) == FAIL)
5255 {
5256 ga_clear(&end_ga);
5257 return FAIL;
5258 }
5259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 // Fill in the end label in all jumps.
5261 while (end_ga.ga_len > 0)
5262 {
5263 isn_T *isn;
5264
5265 --end_ga.ga_len;
5266 isn = ((isn_T *)instr->ga_data)
5267 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5268 isn->isn_arg.jump.jump_where = instr->ga_len;
5269 }
5270 ga_clear(&end_ga);
5271 }
5272
5273 return OK;
5274}
5275
5276/*
5277 * expr4a && expr4a && expr4a logical AND
5278 *
5279 * Produces instructions:
5280 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005281 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005282 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005284 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 * EVAL expr4c Push result of "expr4c"
5286 * end:
5287 */
5288 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005289compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005291 int ppconst_used = ppconst->pp_used;
5292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005294 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 return FAIL;
5296
5297 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005298 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299}
5300
5301/*
5302 * expr3a || expr3b || expr3c logical OR
5303 *
5304 * Produces instructions:
5305 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005306 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005307 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005309 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 * EVAL expr3c Push result of "expr3c"
5311 * end:
5312 */
5313 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005314compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005316 int ppconst_used = ppconst->pp_used;
5317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005319 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 return FAIL;
5321
5322 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005323 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324}
5325
5326/*
5327 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005329 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330 * JUMP_IF_FALSE alt jump if false
5331 * EVAL expr1a
5332 * JUMP_ALWAYS end
5333 * alt: EVAL expr1b
5334 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005335 *
5336 * Toplevel expression: expr2 ?? expr1
5337 * Produces instructions:
5338 * EVAL expr2 Push result of "expr2"
5339 * JUMP_AND_KEEP_IF_TRUE end jump if true
5340 * EVAL expr1
5341 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 */
5343 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005344compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345{
5346 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005347 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005348 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005349
Bram Moolenaar3988f642020-08-27 22:43:03 +02005350 // Ignore all kinds of errors when not producing code.
5351 if (cctx->ctx_skip == SKIP_YES)
5352 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005353 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005354 return OK;
5355 }
5356
Bram Moolenaar61a89812020-05-07 16:58:17 +02005357 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005358 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359 return FAIL;
5360
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005361 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 if (*p == '?')
5363 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005364 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 garray_T *instr = &cctx->ctx_instr;
5366 garray_T *stack = &cctx->ctx_type_stack;
5367 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005368 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005370 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005371 int has_const_expr = FALSE;
5372 int const_value = FALSE;
5373 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005375 if (next != NULL)
5376 {
5377 *arg = next_line_from_context(cctx, TRUE);
5378 p = skipwhite(*arg);
5379 }
5380
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005381 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005382 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005383 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005384 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005385 return FAIL;
5386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387
Bram Moolenaara5565e42020-05-09 15:44:01 +02005388 if (ppconst->pp_used == ppconst_used + 1)
5389 {
5390 // the condition is a constant, we know whether the ? or the :
5391 // expression is to be evaluated.
5392 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005393 if (op_falsy)
5394 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5395 else
5396 {
5397 int error = FALSE;
5398
5399 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5400 &error);
5401 if (error)
5402 return FAIL;
5403 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005404 cctx->ctx_skip = save_skip == SKIP_YES ||
5405 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5406
5407 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5408 // "left ?? right" and "left" is truthy: produce "left"
5409 generate_ppconst(cctx, ppconst);
5410 else
5411 {
5412 clear_tv(&ppconst->pp_tv[ppconst_used]);
5413 --ppconst->pp_used;
5414 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005415 }
5416 else
5417 {
5418 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005419 if (op_falsy)
5420 end_idx = instr->ga_len;
5421 generate_JUMP(cctx, op_falsy
5422 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5423 if (op_falsy)
5424 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426
5427 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005428 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005429 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005430 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005431 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432
Bram Moolenaara5565e42020-05-09 15:44:01 +02005433 if (!has_const_expr)
5434 {
5435 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005437 if (!op_falsy)
5438 {
5439 // remember the type and drop it
5440 --stack->ga_len;
5441 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005443 end_idx = instr->ga_len;
5444 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005445
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005446 // jump here from JUMP_IF_FALSE
5447 isn = ((isn_T *)instr->ga_data) + alt_idx;
5448 isn->isn_arg.jump.jump_where = instr->ga_len;
5449 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005452 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005454 // Check for the ":".
5455 p = may_peek_next_line(cctx, *arg, &next);
5456 if (*p != ':')
5457 {
5458 emsg(_(e_missing_colon));
5459 return FAIL;
5460 }
5461 if (next != NULL)
5462 {
5463 *arg = next_line_from_context(cctx, TRUE);
5464 p = skipwhite(*arg);
5465 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005466
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005467 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5468 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005469 semsg(_(e_white_space_required_before_and_after_str_at_str),
5470 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005471 return FAIL;
5472 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005474 // evaluate the third expression
5475 if (has_const_expr)
5476 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005477 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005478 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005479 return FAIL;
5480 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5481 return FAIL;
5482 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483
Bram Moolenaara5565e42020-05-09 15:44:01 +02005484 if (!has_const_expr)
5485 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005486 type_T **typep;
5487
Bram Moolenaara5565e42020-05-09 15:44:01 +02005488 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489
Bram Moolenaara5565e42020-05-09 15:44:01 +02005490 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005491 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5492 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005493
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005494 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005495 isn = ((isn_T *)instr->ga_data) + end_idx;
5496 isn->isn_arg.jump.jump_where = instr->ga_len;
5497 }
5498
5499 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500 }
5501 return OK;
5502}
5503
5504/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005505 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005506 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5507 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005508 */
5509 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005510compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005511{
5512 ppconst_T ppconst;
5513
5514 CLEAR_FIELD(ppconst);
5515 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5516 {
5517 clear_ppconst(&ppconst);
5518 return FAIL;
5519 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005520 if (is_const != NULL)
5521 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005522 if (generate_ppconst(cctx, &ppconst) == FAIL)
5523 return FAIL;
5524 return OK;
5525}
5526
5527/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005528 * Toplevel expression.
5529 */
5530 static int
5531compile_expr0(char_u **arg, cctx_T *cctx)
5532{
5533 return compile_expr0_ext(arg, cctx, NULL);
5534}
5535
5536/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005537 * Compile "return [expr]".
5538 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539 */
5540 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005541compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542{
5543 char_u *p = arg;
5544 garray_T *stack = &cctx->ctx_type_stack;
5545 type_T *stack_type;
5546
5547 if (*p != NUL && *p != '|' && *p != '\n')
5548 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005549 if (legacy)
5550 {
5551 int save_flags = cmdmod.cmod_flags;
5552
5553 generate_LEGACY_EVAL(cctx, p);
5554 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5555 0, cctx, FALSE, FALSE) == FAIL)
5556 return NULL;
5557 cmdmod.cmod_flags |= CMOD_LEGACY;
5558 (void)skip_expr(&p, NULL);
5559 cmdmod.cmod_flags = save_flags;
5560 }
5561 else
5562 {
5563 // compile return argument into instructions
5564 if (compile_expr0(&p, cctx) == FAIL)
5565 return NULL;
5566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005568 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005569 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005570 // "check_return_type" with uf_ret_type set to &t_unknown is used
5571 // for an inline function without a specified return type. Set the
5572 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005573 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005574 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005575 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5576 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005577 || (!check_return_type
5578 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005579 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005580 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005581 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005582 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005583 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005584 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5585 && stack_type->tt_type != VAR_VOID
5586 && stack_type->tt_type != VAR_UNKNOWN)
5587 {
5588 emsg(_(e_returning_value_in_function_without_return_type));
5589 return NULL;
5590 }
5591 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005592 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005593 return NULL;
5594 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 }
5597 else
5598 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005599 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005600 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005601 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5602 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005604 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 return NULL;
5606 }
5607
5608 // No argument, return zero.
5609 generate_PUSHNR(cctx, 0);
5610 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005611
5612 // Undo any command modifiers.
5613 generate_undo_cmdmods(cctx);
5614
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005615 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616 return NULL;
5617
5618 // "return val | endif" is possible
5619 return skipwhite(p);
5620}
5621
5622/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005623 * Get a line from the compilation context, compatible with exarg_T getline().
5624 * Return a pointer to the line in allocated memory.
5625 * Return NULL for end-of-file or some error.
5626 */
5627 static char_u *
5628exarg_getline(
5629 int c UNUSED,
5630 void *cookie,
5631 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005632 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005633{
5634 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005635 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005636
Bram Moolenaar66250c92020-08-20 15:02:42 +02005637 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005638 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005639 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005640 return NULL;
5641 ++cctx->ctx_lnum;
5642 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5643 // Comment lines result in NULL pointers, skip them.
5644 if (p != NULL)
5645 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005646 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005647}
5648
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005649 void
5650fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5651{
5652 eap->getline = exarg_getline;
5653 eap->cookie = cctx;
5654}
5655
Bram Moolenaar04b12692020-05-04 23:24:44 +02005656/*
5657 * Compile a nested :def command.
5658 */
5659 static char_u *
5660compile_nested_function(exarg_T *eap, cctx_T *cctx)
5661{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005662 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005663 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005664 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005665 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005666 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005667 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005668 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005669
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005670 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005671 {
5672 emsg(_(e_cannot_use_bang_with_nested_def));
5673 return NULL;
5674 }
5675
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005676 if (*name_start == '/')
5677 {
5678 name_end = skip_regexp(name_start + 1, '/', TRUE);
5679 if (*name_end == '/')
5680 ++name_end;
5681 eap->nextcmd = check_nextcmd(name_end);
5682 }
5683 if (name_end == name_start || *skipwhite(name_end) != '(')
5684 {
5685 if (!ends_excmd2(name_start, name_end))
5686 {
5687 semsg(_(e_invalid_command_str), eap->cmd);
5688 return NULL;
5689 }
5690
5691 // "def" or "def Name": list functions
5692 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5693 return NULL;
5694 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5695 }
5696
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005697 // Only g:Func() can use a namespace.
5698 if (name_start[1] == ':' && !is_global)
5699 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005700 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005701 return NULL;
5702 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005703 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005704 return NULL;
5705
Bram Moolenaar04b12692020-05-04 23:24:44 +02005706 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005707 fill_exarg_from_cctx(eap, cctx);
5708
Bram Moolenaar04b12692020-05-04 23:24:44 +02005709 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005710 lambda_name = vim_strsave(get_lambda_name());
5711 if (lambda_name == NULL)
5712 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005713 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005714
Bram Moolenaar822ba242020-05-24 23:00:18 +02005715 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005716 {
5717 r = eap->skip ? OK : FAIL;
5718 goto theend;
5719 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005720
5721 // copy over the block scope IDs before compiling
5722 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5723 {
5724 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5725
5726 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5727 if (ufunc->uf_block_ids != NULL)
5728 {
5729 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5730 sizeof(int) * block_depth);
5731 ufunc->uf_block_depth = block_depth;
5732 }
5733 }
5734
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005735 compile_type = COMPILE_TYPE(ufunc);
5736#ifdef FEAT_PROFILE
5737 // If the outer function is profiled, also compile the nested function for
5738 // profiling.
5739 if (cctx->ctx_compile_type == CT_PROFILE)
5740 compile_type = CT_PROFILE;
5741#endif
5742 if (func_needs_compiling(ufunc, compile_type)
5743 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005744 {
5745 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005746 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005747 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005748
Bram Moolenaar648594e2021-07-11 17:55:01 +02005749#ifdef FEAT_PROFILE
5750 // When the outer function is compiled for profiling, the nested function
5751 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005752 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005753 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5754#endif
5755
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005756 if (is_global)
5757 {
5758 char_u *func_name = vim_strnsave(name_start + 2,
5759 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005760
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005761 if (func_name == NULL)
5762 r = FAIL;
5763 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005764 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005765 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005766 lambda_name = NULL;
5767 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005768 }
5769 else
5770 {
5771 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005772 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005773 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005774
Bram Moolenaareef21022020-08-01 22:16:43 +02005775 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005776 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005777 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005778 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005779 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5780 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005781 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005782
5783theend:
5784 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005785 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005786}
5787
5788/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789 * Return the length of an assignment operator, or zero if there isn't one.
5790 */
5791 int
5792assignment_len(char_u *p, int *heredoc)
5793{
5794 if (*p == '=')
5795 {
5796 if (p[1] == '<' && p[2] == '<')
5797 {
5798 *heredoc = TRUE;
5799 return 3;
5800 }
5801 return 1;
5802 }
5803 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5804 return 2;
5805 if (STRNCMP(p, "..=", 3) == 0)
5806 return 3;
5807 return 0;
5808}
5809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005811 * Generate the load instruction for "name".
5812 */
5813 static void
5814generate_loadvar(
5815 cctx_T *cctx,
5816 assign_dest_T dest,
5817 char_u *name,
5818 lvar_T *lvar,
5819 type_T *type)
5820{
5821 switch (dest)
5822 {
5823 case dest_option:
5824 // TODO: check the option exists
5825 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5826 break;
5827 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005828 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5829 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5830 else
5831 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005832 break;
5833 case dest_buffer:
5834 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5835 break;
5836 case dest_window:
5837 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5838 break;
5839 case dest_tab:
5840 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5841 break;
5842 case dest_script:
5843 compile_load_scriptvar(cctx,
5844 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5845 break;
5846 case dest_env:
5847 // Include $ in the name here
5848 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5849 break;
5850 case dest_reg:
5851 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5852 break;
5853 case dest_vimvar:
5854 generate_LOADV(cctx, name + 2, TRUE);
5855 break;
5856 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005857 if (lvar->lv_from_outer > 0)
5858 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5859 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005860 else
5861 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5862 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005863 case dest_expr:
5864 // list or dict value should already be on the stack.
5865 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005866 }
5867}
5868
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005869/*
5870 * Skip over "[expr]" or ".member".
5871 * Does not check for any errors.
5872 */
5873 static char_u *
5874skip_index(char_u *start)
5875{
5876 char_u *p = start;
5877
5878 if (*p == '[')
5879 {
5880 p = skipwhite(p + 1);
5881 (void)skip_expr(&p, NULL);
5882 p = skipwhite(p);
5883 if (*p == ']')
5884 return p + 1;
5885 return p;
5886 }
5887 // if (*p == '.')
5888 return to_name_end(p + 1, TRUE);
5889}
5890
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005891 void
5892vim9_declare_error(char_u *name)
5893{
5894 char *scope = "";
5895
5896 switch (*name)
5897 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005898 case 'g': scope = _("global"); break;
5899 case 'b': scope = _("buffer"); break;
5900 case 'w': scope = _("window"); break;
5901 case 't': scope = _("tab"); break;
5902 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005903 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005904 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005905 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005906 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005907 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005908 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005909 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005910 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005911 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005912}
5913
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005914/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005915 * For one assignment figure out the type of destination. Return it in "dest".
5916 * When not recognized "dest" is not set.
5917 * For an option "opt_flags" is set.
5918 * For a v:var "vimvaridx" is set.
5919 * "type" is set to the destination type if known, unchanted otherwise.
5920 * Return FAIL if an error message was given.
5921 */
5922 static int
5923get_var_dest(
5924 char_u *name,
5925 assign_dest_T *dest,
5926 int cmdidx,
5927 int *opt_flags,
5928 int *vimvaridx,
5929 type_T **type,
5930 cctx_T *cctx)
5931{
5932 char_u *p;
5933
5934 if (*name == '&')
5935 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005936 int cc;
5937 long numval;
5938 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005939
5940 *dest = dest_option;
5941 if (cmdidx == CMD_final || cmdidx == CMD_const)
5942 {
5943 emsg(_(e_const_option));
5944 return FAIL;
5945 }
5946 p = name;
5947 p = find_option_end(&p, opt_flags);
5948 if (p == NULL)
5949 {
5950 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02005951 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005952 return FAIL;
5953 }
5954 cc = *p;
5955 *p = NUL;
5956 opt_type = get_option_value(skip_option_env_lead(name),
5957 &numval, NULL, *opt_flags);
5958 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005959 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005960 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005961 case gov_unknown:
5962 semsg(_(e_unknown_option), name);
5963 return FAIL;
5964 case gov_string:
5965 case gov_hidden_string:
5966 *type = &t_string;
5967 break;
5968 case gov_bool:
5969 case gov_hidden_bool:
5970 *type = &t_bool;
5971 break;
5972 case gov_number:
5973 case gov_hidden_number:
5974 *type = &t_number;
5975 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005976 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005977 }
5978 else if (*name == '$')
5979 {
5980 *dest = dest_env;
5981 *type = &t_string;
5982 }
5983 else if (*name == '@')
5984 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02005985 if (name[1] != '@'
5986 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005987 {
5988 emsg_invreg(name[1]);
5989 return FAIL;
5990 }
5991 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02005992 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005993 }
5994 else if (STRNCMP(name, "g:", 2) == 0)
5995 {
5996 *dest = dest_global;
5997 }
5998 else if (STRNCMP(name, "b:", 2) == 0)
5999 {
6000 *dest = dest_buffer;
6001 }
6002 else if (STRNCMP(name, "w:", 2) == 0)
6003 {
6004 *dest = dest_window;
6005 }
6006 else if (STRNCMP(name, "t:", 2) == 0)
6007 {
6008 *dest = dest_tab;
6009 }
6010 else if (STRNCMP(name, "v:", 2) == 0)
6011 {
6012 typval_T *vtv;
6013 int di_flags;
6014
6015 *vimvaridx = find_vim_var(name + 2, &di_flags);
6016 if (*vimvaridx < 0)
6017 {
6018 semsg(_(e_variable_not_found_str), name);
6019 return FAIL;
6020 }
6021 // We use the current value of "sandbox" here, is that OK?
6022 if (var_check_ro(di_flags, name, FALSE))
6023 return FAIL;
6024 *dest = dest_vimvar;
6025 vtv = get_vim_var_tv(*vimvaridx);
6026 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6027 }
6028 return OK;
6029}
6030
6031/*
6032 * Generate a STORE instruction for "dest", not being "dest_local".
6033 * Return FAIL when out of memory.
6034 */
6035 static int
6036generate_store_var(
6037 cctx_T *cctx,
6038 assign_dest_T dest,
6039 int opt_flags,
6040 int vimvaridx,
6041 int scriptvar_idx,
6042 int scriptvar_sid,
6043 type_T *type,
6044 char_u *name)
6045{
6046 switch (dest)
6047 {
6048 case dest_option:
6049 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6050 opt_flags);
6051 case dest_global:
6052 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006053 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6054 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006055 case dest_buffer:
6056 // include b: with the name, easier to execute that way
6057 return generate_STORE(cctx, ISN_STOREB, 0, name);
6058 case dest_window:
6059 // include w: with the name, easier to execute that way
6060 return generate_STORE(cctx, ISN_STOREW, 0, name);
6061 case dest_tab:
6062 // include t: with the name, easier to execute that way
6063 return generate_STORE(cctx, ISN_STORET, 0, name);
6064 case dest_env:
6065 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6066 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006067 return generate_STORE(cctx, ISN_STOREREG,
6068 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006069 case dest_vimvar:
6070 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6071 case dest_script:
6072 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006073 // "s:" may be included in the name.
6074 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006075 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006076 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6077 scriptvar_sid, scriptvar_idx, type);
6078 case dest_local:
6079 case dest_expr:
6080 // cannot happen
6081 break;
6082 }
6083 return FAIL;
6084}
6085
Bram Moolenaar752fc692021-01-04 21:57:11 +01006086 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006087generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6088{
6089 if (lhs->lhs_dest != dest_local)
6090 return generate_store_var(cctx, lhs->lhs_dest,
6091 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6092 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6093 lhs->lhs_type, lhs->lhs_name);
6094
6095 if (lhs->lhs_lvar != NULL)
6096 {
6097 garray_T *instr = &cctx->ctx_instr;
6098 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6099
6100 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6101 // ISN_STORENR
6102 if (lhs->lhs_lvar->lv_from_outer == 0
6103 && instr->ga_len == instr_count + 1
6104 && isn->isn_type == ISN_PUSHNR)
6105 {
6106 varnumber_T val = isn->isn_arg.number;
6107 garray_T *stack = &cctx->ctx_type_stack;
6108
6109 isn->isn_type = ISN_STORENR;
6110 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6111 isn->isn_arg.storenr.stnr_val = val;
6112 if (stack->ga_len > 0)
6113 --stack->ga_len;
6114 }
6115 else if (lhs->lhs_lvar->lv_from_outer > 0)
6116 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6117 lhs->lhs_lvar->lv_from_outer);
6118 else
6119 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6120 }
6121 return OK;
6122}
6123
6124 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006125is_decl_command(int cmdidx)
6126{
6127 return cmdidx == CMD_let || cmdidx == CMD_var
6128 || cmdidx == CMD_final || cmdidx == CMD_const;
6129}
6130
6131/*
6132 * Figure out the LHS type and other properties for an assignment or one item
6133 * of ":unlet" with an index.
6134 * Returns OK or FAIL.
6135 */
6136 static int
6137compile_lhs(
6138 char_u *var_start,
6139 lhs_T *lhs,
6140 int cmdidx,
6141 int heredoc,
6142 int oplen,
6143 cctx_T *cctx)
6144{
6145 char_u *var_end;
6146 int is_decl = is_decl_command(cmdidx);
6147
6148 CLEAR_POINTER(lhs);
6149 lhs->lhs_dest = dest_local;
6150 lhs->lhs_vimvaridx = -1;
6151 lhs->lhs_scriptvar_idx = -1;
6152
6153 // "dest_end" is the end of the destination, including "[expr]" or
6154 // ".name".
6155 // "var_end" is the end of the variable/option/etc. name.
6156 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6157 if (*var_start == '@')
6158 var_end = var_start + 2;
6159 else
6160 {
6161 // skip over the leading "&", "&l:", "&g:" and "$"
6162 var_end = skip_option_env_lead(var_start);
6163 var_end = to_name_end(var_end, TRUE);
6164 }
6165
6166 // "a: type" is declaring variable "a" with a type, not dict "a:".
6167 if (is_decl && lhs->lhs_dest_end == var_start + 2
6168 && lhs->lhs_dest_end[-1] == ':')
6169 --lhs->lhs_dest_end;
6170 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6171 --var_end;
6172
6173 // compute the length of the destination without "[expr]" or ".name"
6174 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006175 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006176 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6177 if (lhs->lhs_name == NULL)
6178 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006179
6180 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6181 // Something follows after the variable: "var[idx]" or "var.key".
6182 lhs->lhs_has_index = TRUE;
6183
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 if (heredoc)
6185 lhs->lhs_type = &t_list_string;
6186 else
6187 lhs->lhs_type = &t_any;
6188
6189 if (cctx->ctx_skip != SKIP_YES)
6190 {
6191 int declare_error = FALSE;
6192
6193 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6194 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6195 &lhs->lhs_type, cctx) == FAIL)
6196 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006197 if (lhs->lhs_dest != dest_local
6198 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006199 {
6200 // Specific kind of variable recognized.
6201 declare_error = is_decl;
6202 }
6203 else
6204 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006205 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006206 if (check_reserved_name(lhs->lhs_name) == FAIL)
6207 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006208
6209 if (lookup_local(var_start, lhs->lhs_varlen,
6210 &lhs->lhs_local_lvar, cctx) == OK)
6211 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6212 else
6213 {
6214 CLEAR_FIELD(lhs->lhs_arg_lvar);
6215 if (arg_exists(var_start, lhs->lhs_varlen,
6216 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6217 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6218 {
6219 if (is_decl)
6220 {
6221 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6222 return FAIL;
6223 }
6224 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6225 }
6226 }
6227 if (lhs->lhs_lvar != NULL)
6228 {
6229 if (is_decl)
6230 {
6231 semsg(_(e_variable_already_declared), lhs->lhs_name);
6232 return FAIL;
6233 }
6234 }
6235 else
6236 {
6237 int script_namespace = lhs->lhs_varlen > 1
6238 && STRNCMP(var_start, "s:", 2) == 0;
6239 int script_var = (script_namespace
6240 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006241 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006242 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006243 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006244 imported_T *import =
6245 find_imported(var_start, lhs->lhs_varlen, cctx);
6246
6247 if (script_namespace || script_var || import != NULL)
6248 {
6249 char_u *rawname = lhs->lhs_name
6250 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6251
6252 if (is_decl)
6253 {
6254 if (script_namespace)
6255 semsg(_(e_cannot_declare_script_variable_in_function),
6256 lhs->lhs_name);
6257 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006258 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006259 lhs->lhs_name);
6260 return FAIL;
6261 }
6262 else if (cctx->ctx_ufunc->uf_script_ctx_version
6263 == SCRIPT_VERSION_VIM9
6264 && script_namespace
6265 && !script_var && import == NULL)
6266 {
6267 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6268 return FAIL;
6269 }
6270
6271 lhs->lhs_dest = dest_script;
6272
6273 // existing script-local variables should have a type
6274 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6275 if (import != NULL)
6276 lhs->lhs_scriptvar_sid = import->imp_sid;
6277 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6278 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006279 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006280 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006281 lhs->lhs_scriptvar_sid, rawname,
6282 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6283 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 if (lhs->lhs_scriptvar_idx >= 0)
6285 {
6286 scriptitem_T *si = SCRIPT_ITEM(
6287 lhs->lhs_scriptvar_sid);
6288 svar_T *sv =
6289 ((svar_T *)si->sn_var_vals.ga_data)
6290 + lhs->lhs_scriptvar_idx;
6291 lhs->lhs_type = sv->sv_type;
6292 }
6293 }
6294 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006295 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006296 == FAIL)
6297 return FAIL;
6298 }
6299 }
6300
6301 if (declare_error)
6302 {
6303 vim9_declare_error(lhs->lhs_name);
6304 return FAIL;
6305 }
6306 }
6307
6308 // handle "a:name" as a name, not index "name" on "a"
6309 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6310 var_end = lhs->lhs_dest_end;
6311
6312 if (lhs->lhs_dest != dest_option)
6313 {
6314 if (is_decl && *var_end == ':')
6315 {
6316 char_u *p;
6317
6318 // parse optional type: "let var: type = expr"
6319 if (!VIM_ISWHITE(var_end[1]))
6320 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006321 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006322 return FAIL;
6323 }
6324 p = skipwhite(var_end + 1);
6325 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6326 if (lhs->lhs_type == NULL)
6327 return FAIL;
6328 lhs->lhs_has_type = TRUE;
6329 }
6330 else if (lhs->lhs_lvar != NULL)
6331 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6332 }
6333
Bram Moolenaare42939a2021-04-05 17:11:17 +02006334 if (oplen == 3 && !heredoc
6335 && lhs->lhs_dest != dest_global
6336 && !lhs->lhs_has_index
6337 && lhs->lhs_type->tt_type != VAR_STRING
6338 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339 {
6340 emsg(_(e_can_only_concatenate_to_string));
6341 return FAIL;
6342 }
6343
6344 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6345 && cctx->ctx_skip != SKIP_YES)
6346 {
6347 if (oplen > 1 && !heredoc)
6348 {
6349 // +=, /=, etc. require an existing variable
6350 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6351 return FAIL;
6352 }
6353 if (!is_decl)
6354 {
6355 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6356 return FAIL;
6357 }
6358
Bram Moolenaar3f327882021-03-17 20:56:38 +01006359 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006360 if ((lhs->lhs_type->tt_type == VAR_FUNC
6361 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006362 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006363 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006364
6365 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006366 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6367 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6368 if (lhs->lhs_lvar == NULL)
6369 return FAIL;
6370 lhs->lhs_new_local = TRUE;
6371 }
6372
6373 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006374 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006375 {
6376 // Something follows after the variable: "var[idx]" or "var.key".
6377 // TODO: should we also handle "->func()" here?
6378 if (is_decl)
6379 {
6380 emsg(_(e_cannot_use_index_when_declaring_variable));
6381 return FAIL;
6382 }
6383
6384 if (var_start[lhs->lhs_varlen] == '['
6385 || var_start[lhs->lhs_varlen] == '.')
6386 {
6387 char_u *after = var_start + lhs->lhs_varlen;
6388 char_u *p;
6389
6390 // Only the last index is used below, if there are others
6391 // before it generate code for the expression. Thus for
6392 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6393 for (;;)
6394 {
6395 p = skip_index(after);
6396 if (*p != '[' && *p != '.')
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006397 {
6398 lhs->lhs_varlen_total = p - var_start;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006399 break;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006400 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006401 after = p;
6402 }
6403 if (after > var_start + lhs->lhs_varlen)
6404 {
6405 lhs->lhs_varlen = after - var_start;
6406 lhs->lhs_dest = dest_expr;
6407 // We don't know the type before evaluating the expression,
6408 // use "any" until then.
6409 lhs->lhs_type = &t_any;
6410 }
6411
Bram Moolenaar752fc692021-01-04 21:57:11 +01006412 if (lhs->lhs_type->tt_member == NULL)
6413 lhs->lhs_member_type = &t_any;
6414 else
6415 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6416 }
6417 else
6418 {
6419 semsg("Not supported yet: %s", var_start);
6420 return FAIL;
6421 }
6422 }
6423 return OK;
6424}
6425
6426/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006427 * Figure out the LHS and check a few errors.
6428 */
6429 static int
6430compile_assign_lhs(
6431 char_u *var_start,
6432 lhs_T *lhs,
6433 int cmdidx,
6434 int is_decl,
6435 int heredoc,
6436 int oplen,
6437 cctx_T *cctx)
6438{
6439 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6440 return FAIL;
6441
6442 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6443 {
6444 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6445 return FAIL;
6446 }
6447 if (!is_decl && lhs->lhs_lvar != NULL
6448 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6449 {
6450 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6451 return FAIL;
6452 }
6453 return OK;
6454}
6455
6456/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006457 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6458 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006459 */
6460 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006461compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 char_u *var_start,
6463 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006464 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006465 cctx_T *cctx)
6466{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006467 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006468 char_u *p;
6469 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006470 int need_white_before = TRUE;
6471 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006472
Bram Moolenaar752fc692021-01-04 21:57:11 +01006473 p = var_start + varlen;
6474 if (*p == '[')
6475 {
6476 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006477 if (*p == ':')
6478 {
6479 // empty first index, push zero
6480 r = generate_PUSHNR(cctx, 0);
6481 need_white_before = FALSE;
6482 }
6483 else
6484 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006485
6486 if (r == OK && *skipwhite(p) == ':')
6487 {
6488 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006489 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006490 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006491 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006492 empty_second = *skipwhite(p + 1) == ']';
6493 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6494 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006495 {
6496 semsg(_(e_white_space_required_before_and_after_str_at_str),
6497 ":", p);
6498 return FAIL;
6499 }
6500 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006501 if (*p == ']')
6502 // empty second index, push "none"
6503 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6504 else
6505 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006506 }
6507
Bram Moolenaar752fc692021-01-04 21:57:11 +01006508 if (r == OK && *skipwhite(p) != ']')
6509 {
6510 // this should not happen
6511 emsg(_(e_missbrac));
6512 r = FAIL;
6513 }
6514 }
6515 else // if (*p == '.')
6516 {
6517 char_u *key_end = to_name_end(p + 1, TRUE);
6518 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6519
6520 r = generate_PUSHS(cctx, key);
6521 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006522 return r;
6523}
6524
6525/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006526 * For a LHS with an index, load the variable to be indexed.
6527 */
6528 static int
6529compile_load_lhs(
6530 lhs_T *lhs,
6531 char_u *var_start,
6532 type_T *rhs_type,
6533 cctx_T *cctx)
6534{
6535 if (lhs->lhs_dest == dest_expr)
6536 {
6537 size_t varlen = lhs->lhs_varlen;
6538 int c = var_start[varlen];
6539 char_u *p = var_start;
6540 garray_T *stack = &cctx->ctx_type_stack;
6541
6542 // Evaluate "ll[expr]" of "ll[expr][idx]"
6543 var_start[varlen] = NUL;
6544 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6545 {
6546 // this should not happen
6547 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006548 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006549 return FAIL;
6550 }
6551 var_start[varlen] = c;
6552
6553 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6554 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6555 // now we can properly check the type
6556 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6557 && rhs_type != &t_void
6558 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6559 FALSE, FALSE) == FAIL)
6560 return FAIL;
6561 }
6562 else
6563 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6564 lhs->lhs_lvar, lhs->lhs_type);
6565 return OK;
6566}
6567
6568/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006569 * Produce code for loading "lhs" and also take care of an index.
6570 * Return OK/FAIL.
6571 */
6572 static int
6573compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6574{
6575 compile_load_lhs(lhs, var_start, NULL, cctx);
6576
6577 if (lhs->lhs_has_index)
6578 {
6579 int range = FALSE;
6580
6581 // Get member from list or dict. First compile the
6582 // index value.
6583 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6584 return FAIL;
6585 if (range)
6586 {
6587 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6588 var_start);
6589 return FAIL;
6590 }
6591
6592 // Get the member.
6593 if (compile_member(FALSE, cctx) == FAIL)
6594 return FAIL;
6595 }
6596 return OK;
6597}
6598
6599/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006600 * Assignment to a list or dict member, or ":unlet" for the item, using the
6601 * information in "lhs".
6602 * Returns OK or FAIL.
6603 */
6604 static int
6605compile_assign_unlet(
6606 char_u *var_start,
6607 lhs_T *lhs,
6608 int is_assign,
6609 type_T *rhs_type,
6610 cctx_T *cctx)
6611{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006612 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006613 garray_T *stack = &cctx->ctx_type_stack;
6614 int range = FALSE;
6615
Bram Moolenaar68452172021-04-12 21:21:02 +02006616 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006617 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006618 if (is_assign && range && lhs->lhs_type != &t_blob
6619 && lhs->lhs_type != &t_any)
6620 {
6621 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6622 return FAIL;
6623 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006624
6625 if (lhs->lhs_type == &t_any)
6626 {
6627 // Index on variable of unknown type: check at runtime.
6628 dest_type = VAR_ANY;
6629 }
6630 else
6631 {
6632 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006633 if (dest_type == VAR_DICT && range)
6634 {
6635 emsg(e_cannot_use_range_with_dictionary);
6636 return FAIL;
6637 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006638 if (dest_type == VAR_DICT
6639 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006640 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006641 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006642 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006643 type_T *type;
6644
6645 if (range)
6646 {
6647 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6648 if (need_type(type, &t_number,
6649 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006650 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006651 }
6652 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006653 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006654 && need_type(type, &t_number,
6655 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006656 return FAIL;
6657 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006658 }
6659
6660 // Load the dict or list. On the stack we then have:
6661 // - value (for assignment, not for :unlet)
6662 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006663 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006664 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006665 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6666 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006667
Bram Moolenaar68452172021-04-12 21:21:02 +02006668 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6669 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006670 {
6671 if (is_assign)
6672 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006673 if (range)
6674 {
6675 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6676 return FAIL;
6677 }
6678 else
6679 {
6680 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006681
Bram Moolenaar68452172021-04-12 21:21:02 +02006682 if (isn == NULL)
6683 return FAIL;
6684 isn->isn_arg.vartype = dest_type;
6685 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006686 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006687 else if (range)
6688 {
6689 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6690 return FAIL;
6691 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006692 else
6693 {
6694 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6695 return FAIL;
6696 }
6697 }
6698 else
6699 {
6700 emsg(_(e_indexable_type_required));
6701 return FAIL;
6702 }
6703
6704 return OK;
6705}
6706
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006707/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006708 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006709 * "let name"
6710 * "var name = expr"
6711 * "final name = expr"
6712 * "const name = expr"
6713 * "name = expr"
6714 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006715 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006716 * Return NULL for an error.
6717 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 */
6719 static char_u *
6720compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6721{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006722 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006724 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 char_u *ret = NULL;
6726 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006727 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006729 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006731 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733 int oplen = 0;
6734 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006735 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006736 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006738 int is_decl = is_decl_command(cmdidx);
6739 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006740 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006742 // Skip over the "var" or "[var, var]" to get to any "=".
6743 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6744 if (p == NULL)
6745 return *arg == '[' ? arg : NULL;
6746
6747 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006749 // TODO: should we allow this, and figure out type inference from list
6750 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006751 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 return NULL;
6753 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006754 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 sp = p;
6757 p = skipwhite(p);
6758 op = p;
6759 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006760
6761 if (var_count > 0 && oplen == 0)
6762 // can be something like "[1, 2]->func()"
6763 return arg;
6764
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006765 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006767 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006768 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006769 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006770 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6771 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006772 if (VIM_ISWHITE(eap->cmd[2]))
6773 {
6774 semsg(_(e_no_white_space_allowed_after_str_str),
6775 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6776 return NULL;
6777 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006778 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6779 oplen = 2;
6780 incdec = TRUE;
6781 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 if (heredoc)
6784 {
6785 list_T *l;
6786 listitem_T *li;
6787
6788 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006789 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006791 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006792 if (l == NULL)
6793 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794
Bram Moolenaar078269b2020-09-21 20:35:55 +02006795 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006797 // Push each line and the create the list.
6798 FOR_ALL_LIST_ITEMS(l, li)
6799 {
6800 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6801 li->li_tv.vval.v_string = NULL;
6802 }
6803 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 list_free(l);
6806 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006807 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006809 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006811 char_u *wp;
6812
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006813 // for "[var, var] = expr" evaluate the expression here, loop over the
6814 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006815 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006816
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006817 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006818 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006819 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006820 if (compile_expr0(&p, cctx) == FAIL)
6821 return NULL;
6822 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006823
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006824 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006826 type_T *stacktype;
6827
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006828 stacktype = stack->ga_len == 0 ? &t_void
6829 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006830 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006832 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006833 goto theend;
6834 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006835 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006836 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006838 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006839 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6840 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006841 if (stacktype->tt_member != NULL)
6842 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006843 }
6844 }
6845
6846 /*
6847 * Loop over variables in "[var, var] = expr".
6848 * For "var = expr" and "let var: type" this is done only once.
6849 */
6850 if (var_count > 0)
6851 var_start = skipwhite(arg + 1); // skip over the "["
6852 else
6853 var_start = arg;
6854 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6855 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02006856 int instr_count = -1;
6857 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006858
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006859 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6860 {
6861 // Ignore underscore in "[a, _, b] = list".
6862 if (var_count > 0)
6863 {
6864 var_start = skipwhite(var_start + 2);
6865 continue;
6866 }
6867 emsg(_(e_cannot_use_underscore_here));
6868 goto theend;
6869 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006870 vim_free(lhs.lhs_name);
6871
6872 /*
6873 * Figure out the LHS type and other properties.
6874 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006875 if (compile_assign_lhs(var_start, &lhs, cmdidx,
6876 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006877 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02006878 if (heredoc)
6879 {
6880 SOURCING_LNUM = start_lnum;
6881 if (lhs.lhs_has_type
6882 && need_type(&t_list_string, lhs.lhs_type,
6883 -1, 0, cctx, FALSE, FALSE) == FAIL)
6884 goto theend;
6885 }
6886 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006887 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006888 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006889 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006890 if (oplen > 0 && var_count == 0)
6891 {
6892 // skip over the "=" and the expression
6893 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02006894 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006895 }
6896 }
6897 else if (oplen > 0)
6898 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006899 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006900 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006901
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006902 // for "+=", "*=", "..=" etc. first load the current value
6903 if (*op != '='
6904 && compile_load_lhs_with_index(&lhs, var_start,
6905 cctx) == FAIL)
6906 goto theend;
6907
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006908 // For "var = expr" evaluate the expression.
6909 if (var_count == 0)
6910 {
6911 int r;
6912
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006913 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006914 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006915 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006916 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006917 r = generate_PUSHNR(cctx, 1);
6918 }
6919 else
6920 {
6921 // Temporarily hide the new local variable here, it is
6922 // not available to this expression.
6923 if (lhs.lhs_new_local)
6924 --cctx->ctx_locals.ga_len;
6925 wp = op + oplen;
6926 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6927 {
6928 if (lhs.lhs_new_local)
6929 ++cctx->ctx_locals.ga_len;
6930 goto theend;
6931 }
6932 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006933 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006934 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006935 if (r == FAIL)
6936 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006937 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006938 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006939 else if (semicolon && var_idx == var_count - 1)
6940 {
6941 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006942 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006943 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6944 goto theend;
6945 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006946 else
6947 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006948 // For "[var, var] = expr" get the "var_idx" item from the
6949 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006950 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006951 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006952 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006953
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006954 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006955 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006956 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006957 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006958 if ((rhs_type->tt_type == VAR_FUNC
6959 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006960 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006961 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006962 goto theend;
6963
Bram Moolenaar752fc692021-01-04 21:57:11 +01006964 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006965 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006966 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006967 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006968 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006969 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006970 }
6971 else
6972 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006973 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006974 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006975 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006976 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006977 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006978 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006979 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006980 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006981 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006982 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006983 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006984 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006985 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006986 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006987 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006988
Bram Moolenaar77709b12021-04-03 21:01:01 +02006989 // Without operator check type here, otherwise below.
6990 // Use the line number of the assignment.
6991 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006992 if (lhs.lhs_has_index)
6993 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006994 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006995 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006996 goto theend;
6997 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006998 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006999 else
7000 {
7001 type_T *lhs_type = lhs.lhs_member_type;
7002
7003 // Special case: assigning to @# can use a number or a
7004 // string.
7005 if (lhs_type == &t_number_or_string
7006 && rhs_type->tt_type == VAR_NUMBER)
7007 lhs_type = &t_number;
7008 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007009 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007010 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007013 else if (cmdidx == CMD_final)
7014 {
7015 emsg(_(e_final_requires_a_value));
7016 goto theend;
7017 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007018 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007020 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007021 goto theend;
7022 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007023 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007024 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007025 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007026 goto theend;
7027 }
7028 else
7029 {
7030 // variables are always initialized
7031 if (ga_grow(instr, 1) == FAIL)
7032 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007033 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007034 {
7035 case VAR_BOOL:
7036 generate_PUSHBOOL(cctx, VVAL_FALSE);
7037 break;
7038 case VAR_FLOAT:
7039#ifdef FEAT_FLOAT
7040 generate_PUSHF(cctx, 0.0);
7041#endif
7042 break;
7043 case VAR_STRING:
7044 generate_PUSHS(cctx, NULL);
7045 break;
7046 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007047 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007048 break;
7049 case VAR_FUNC:
7050 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7051 break;
7052 case VAR_LIST:
7053 generate_NEWLIST(cctx, 0);
7054 break;
7055 case VAR_DICT:
7056 generate_NEWDICT(cctx, 0);
7057 break;
7058 case VAR_JOB:
7059 generate_PUSHJOB(cctx, NULL);
7060 break;
7061 case VAR_CHANNEL:
7062 generate_PUSHCHANNEL(cctx, NULL);
7063 break;
7064 case VAR_NUMBER:
7065 case VAR_UNKNOWN:
7066 case VAR_ANY:
7067 case VAR_PARTIAL:
7068 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007069 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007070 case VAR_SPECIAL: // cannot happen
7071 generate_PUSHNR(cctx, 0);
7072 break;
7073 }
7074 }
7075 if (var_count == 0)
7076 end = p;
7077 }
7078
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007079 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007080 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007081 break;
7082
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007083 if (oplen > 0 && *op != '=')
7084 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007085 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007086 type_T *stacktype;
7087
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007088 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007089 {
7090 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7091 goto theend;
7092 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007093 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007094 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007095 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007096 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7097 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007098#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007099 // If variable is float operation with number is OK.
7100 !(expected == &t_float && stacktype == &t_number) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007101#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007102 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007103 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007104 goto theend;
7105 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007106
7107 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007108 {
7109 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7110 goto theend;
7111 }
7112 else if (*op == '+')
7113 {
7114 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007115 operator_type(lhs.lhs_member_type, stacktype),
7116 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007117 goto theend;
7118 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007119 else if (generate_two_op(cctx, op) == FAIL)
7120 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007123 // Use the line number of the assignment for store instruction.
7124 save_lnum = cctx->ctx_lnum;
7125 cctx->ctx_lnum = start_lnum - 1;
7126
Bram Moolenaar752fc692021-01-04 21:57:11 +01007127 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007128 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007129 // Use the info in "lhs" to store the value at the index in the
7130 // list or dict.
7131 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7132 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007133 {
7134 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007135 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007136 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007137 }
7138 else
7139 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007140 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007141 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007142 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007143 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007144 generate_LOCKCONST(cctx);
7145
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007146 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007147 && (lhs.lhs_type->tt_type == VAR_DICT
7148 || lhs.lhs_type->tt_type == VAR_LIST)
7149 && lhs.lhs_type->tt_member != NULL
7150 && lhs.lhs_type->tt_member != &t_any
7151 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007152 // Set the type in the list or dict, so that it can be checked,
7153 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007154 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007155
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007156 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007157 {
7158 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007159 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007160 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007161 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007162 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007163
7164 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007165 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007167
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007168 // For "[var, var] = expr" drop the "expr" value.
7169 // Also for "[var, var; _] = expr".
7170 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007171 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007172 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007173 goto theend;
7174 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007175
Bram Moolenaarb2097502020-07-19 17:17:02 +02007176 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
7178theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007179 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 return ret;
7181}
7182
7183/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007184 * Check for an assignment at "eap->cmd", compile it if found.
7185 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7186 */
7187 static int
7188may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7189{
7190 char_u *pskip;
7191 char_u *p;
7192
7193 // Assuming the command starts with a variable or function name,
7194 // find what follows.
7195 // Skip over "var.member", "var[idx]" and the like.
7196 // Also "&opt = val", "$ENV = val" and "@r = val".
7197 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7198 ? eap->cmd + 1 : eap->cmd;
7199 p = to_name_end(pskip, TRUE);
7200 if (p > eap->cmd && *p != NUL)
7201 {
7202 char_u *var_end;
7203 int oplen;
7204 int heredoc;
7205
7206 if (eap->cmd[0] == '@')
7207 var_end = eap->cmd + 2;
7208 else
7209 var_end = find_name_end(pskip, NULL, NULL,
7210 FNE_CHECK_START | FNE_INCL_BR);
7211 oplen = assignment_len(skipwhite(var_end), &heredoc);
7212 if (oplen > 0)
7213 {
7214 size_t len = p - eap->cmd;
7215
7216 // Recognize an assignment if we recognize the variable
7217 // name:
7218 // "g:var = expr"
7219 // "local = expr" where "local" is a local var.
7220 // "script = expr" where "script" is a script-local var.
7221 // "import = expr" where "import" is an imported var
7222 // "&opt = expr"
7223 // "$ENV = expr"
7224 // "@r = expr"
7225 if (*eap->cmd == '&'
7226 || *eap->cmd == '$'
7227 || *eap->cmd == '@'
7228 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007229 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007230 {
7231 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7232 if (*line == NULL || *line == eap->cmd)
7233 return FAIL;
7234 return OK;
7235 }
7236 }
7237 }
7238
7239 if (*eap->cmd == '[')
7240 {
7241 // [var, var] = expr
7242 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7243 if (*line == NULL)
7244 return FAIL;
7245 if (*line != eap->cmd)
7246 return OK;
7247 }
7248 return NOTDONE;
7249}
7250
7251/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007252 * Check if "name" can be "unlet".
7253 */
7254 int
7255check_vim9_unlet(char_u *name)
7256{
7257 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7258 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007259 // "unlet s:var" is allowed in legacy script.
7260 if (*name == 's' && !script_is_vim9())
7261 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007262 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007263 return FAIL;
7264 }
7265 return OK;
7266}
7267
7268/*
7269 * Callback passed to ex_unletlock().
7270 */
7271 static int
7272compile_unlet(
7273 lval_T *lvp,
7274 char_u *name_end,
7275 exarg_T *eap,
7276 int deep UNUSED,
7277 void *coookie)
7278{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007279 cctx_T *cctx = coookie;
7280 char_u *p = lvp->ll_name;
7281 int cc = *name_end;
7282 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007283
Bram Moolenaar752fc692021-01-04 21:57:11 +01007284 if (cctx->ctx_skip == SKIP_YES)
7285 return OK;
7286
7287 *name_end = NUL;
7288 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007289 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007290 // :unlet $ENV_VAR
7291 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7292 }
7293 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7294 {
7295 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007296
Bram Moolenaar752fc692021-01-04 21:57:11 +01007297 // This is similar to assigning: lookup the list/dict, compile the
7298 // idx/key. Then instead of storing the value unlet the item.
7299 // unlet {list}[idx]
7300 // unlet {dict}[key] dict.key
7301 //
7302 // Figure out the LHS type and other properties.
7303 //
7304 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7305
7306 // : unlet an indexed item
7307 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007308 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007309 iemsg("called compile_lhs() without an index");
7310 ret = FAIL;
7311 }
7312 else
7313 {
7314 // Use the info in "lhs" to unlet the item at the index in the
7315 // list or dict.
7316 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007317 }
7318
Bram Moolenaar752fc692021-01-04 21:57:11 +01007319 vim_free(lhs.lhs_name);
7320 }
7321 else if (check_vim9_unlet(p) == FAIL)
7322 {
7323 ret = FAIL;
7324 }
7325 else
7326 {
7327 // Normal name. Only supports g:, w:, t: and b: namespaces.
7328 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007329 }
7330
Bram Moolenaar752fc692021-01-04 21:57:11 +01007331 *name_end = cc;
7332 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007333}
7334
7335/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007336 * Callback passed to ex_unletlock().
7337 */
7338 static int
7339compile_lock_unlock(
7340 lval_T *lvp,
7341 char_u *name_end,
7342 exarg_T *eap,
7343 int deep UNUSED,
7344 void *coookie)
7345{
7346 cctx_T *cctx = coookie;
7347 int cc = *name_end;
7348 char_u *p = lvp->ll_name;
7349 int ret = OK;
7350 size_t len;
7351 char_u *buf;
7352
7353 if (cctx->ctx_skip == SKIP_YES)
7354 return OK;
7355
7356 // Cannot use :lockvar and :unlockvar on local variables.
7357 if (p[1] != ':')
7358 {
7359 char_u *end = skip_var_one(p, FALSE);
7360
7361 if (lookup_local(p, end - p, NULL, cctx) == OK)
7362 {
7363 emsg(_(e_cannot_lock_unlock_local_variable));
7364 return FAIL;
7365 }
7366 }
7367
7368 // Checking is done at runtime.
7369 *name_end = NUL;
7370 len = name_end - p + 20;
7371 buf = alloc(len);
7372 if (buf == NULL)
7373 ret = FAIL;
7374 else
7375 {
7376 vim_snprintf((char *)buf, len, "%s %s",
7377 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7378 p);
7379 ret = generate_EXEC(cctx, buf);
7380
7381 vim_free(buf);
7382 *name_end = cc;
7383 }
7384 return ret;
7385}
7386
7387/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007388 * compile "unlet var", "lock var" and "unlock var"
7389 * "arg" points to "var".
7390 */
7391 static char_u *
7392compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7393{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007394 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7395 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7396 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007397 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7398}
7399
7400/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7402 */
7403 static int
7404compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7405{
7406 garray_T *instr = &cctx->ctx_instr;
7407 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7408
7409 if (endlabel == NULL)
7410 return FAIL;
7411 endlabel->el_next = *el;
7412 *el = endlabel;
7413 endlabel->el_end_label = instr->ga_len;
7414
7415 generate_JUMP(cctx, when, 0);
7416 return OK;
7417}
7418
7419 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007420compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421{
7422 garray_T *instr = &cctx->ctx_instr;
7423
7424 while (*el != NULL)
7425 {
7426 endlabel_T *cur = (*el);
7427 isn_T *isn;
7428
7429 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007430 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 *el = cur->el_next;
7432 vim_free(cur);
7433 }
7434}
7435
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007436 static void
7437compile_free_jump_to_end(endlabel_T **el)
7438{
7439 while (*el != NULL)
7440 {
7441 endlabel_T *cur = (*el);
7442
7443 *el = cur->el_next;
7444 vim_free(cur);
7445 }
7446}
7447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448/*
7449 * Create a new scope and set up the generic items.
7450 */
7451 static scope_T *
7452new_scope(cctx_T *cctx, scopetype_T type)
7453{
7454 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7455
7456 if (scope == NULL)
7457 return NULL;
7458 scope->se_outer = cctx->ctx_scope;
7459 cctx->ctx_scope = scope;
7460 scope->se_type = type;
7461 scope->se_local_count = cctx->ctx_locals.ga_len;
7462 return scope;
7463}
7464
7465/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007466 * Free the current scope and go back to the outer scope.
7467 */
7468 static void
7469drop_scope(cctx_T *cctx)
7470{
7471 scope_T *scope = cctx->ctx_scope;
7472
7473 if (scope == NULL)
7474 {
7475 iemsg("calling drop_scope() without a scope");
7476 return;
7477 }
7478 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007479 switch (scope->se_type)
7480 {
7481 case IF_SCOPE:
7482 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7483 case FOR_SCOPE:
7484 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7485 case WHILE_SCOPE:
7486 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7487 case TRY_SCOPE:
7488 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7489 case NO_SCOPE:
7490 case BLOCK_SCOPE:
7491 break;
7492 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007493 vim_free(scope);
7494}
7495
7496/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 * compile "if expr"
7498 *
7499 * "if expr" Produces instructions:
7500 * EVAL expr Push result of "expr"
7501 * JUMP_IF_FALSE end
7502 * ... body ...
7503 * end:
7504 *
7505 * "if expr | else" Produces instructions:
7506 * EVAL expr Push result of "expr"
7507 * JUMP_IF_FALSE else
7508 * ... body ...
7509 * JUMP_ALWAYS end
7510 * else:
7511 * ... body ...
7512 * end:
7513 *
7514 * "if expr1 | elseif expr2 | else" Produces instructions:
7515 * EVAL expr Push result of "expr"
7516 * JUMP_IF_FALSE elseif
7517 * ... body ...
7518 * JUMP_ALWAYS end
7519 * elseif:
7520 * EVAL expr Push result of "expr"
7521 * JUMP_IF_FALSE else
7522 * ... body ...
7523 * JUMP_ALWAYS end
7524 * else:
7525 * ... body ...
7526 * end:
7527 */
7528 static char_u *
7529compile_if(char_u *arg, cctx_T *cctx)
7530{
7531 char_u *p = arg;
7532 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007533 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007535 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007536 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537
Bram Moolenaara5565e42020-05-09 15:44:01 +02007538 CLEAR_FIELD(ppconst);
7539 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007540 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007541 clear_ppconst(&ppconst);
7542 return NULL;
7543 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007544 if (!ends_excmd2(arg, skipwhite(p)))
7545 {
7546 semsg(_(e_trailing_arg), p);
7547 return NULL;
7548 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007549 if (cctx->ctx_skip == SKIP_YES)
7550 clear_ppconst(&ppconst);
7551 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007552 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007553 int error = FALSE;
7554 int v;
7555
Bram Moolenaara5565e42020-05-09 15:44:01 +02007556 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007557 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007558 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007559 if (error)
7560 return NULL;
7561 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007562 }
7563 else
7564 {
7565 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007566 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007567 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007568 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007569 if (bool_on_stack(cctx) == FAIL)
7570 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572
Bram Moolenaara91a7132021-03-25 21:12:15 +01007573 // CMDMOD_REV must come before the jump
7574 generate_undo_cmdmods(cctx);
7575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 scope = new_scope(cctx, IF_SCOPE);
7577 if (scope == NULL)
7578 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007579 scope->se_skip_save = skip_save;
7580 // "is_had_return" will be reset if any block does not end in :return
7581 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007583 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007584 {
7585 // "where" is set when ":elseif", "else" or ":endif" is found
7586 scope->se_u.se_if.is_if_label = instr->ga_len;
7587 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7588 }
7589 else
7590 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591
Bram Moolenaarced68a02021-01-24 17:53:47 +01007592#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007593 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007594 && skip_save != SKIP_YES)
7595 {
7596 // generated a profile start, need to generate a profile end, since it
7597 // won't be done after returning
7598 cctx->ctx_skip = SKIP_NOT;
7599 generate_instr(cctx, ISN_PROF_END);
7600 cctx->ctx_skip = SKIP_YES;
7601 }
7602#endif
7603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 return p;
7605}
7606
7607 static char_u *
7608compile_elseif(char_u *arg, cctx_T *cctx)
7609{
7610 char_u *p = arg;
7611 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007612 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613 isn_T *isn;
7614 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007615 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007616 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617
7618 if (scope == NULL || scope->se_type != IF_SCOPE)
7619 {
7620 emsg(_(e_elseif_without_if));
7621 return NULL;
7622 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007623 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007624 if (!cctx->ctx_had_return)
7625 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626
Bram Moolenaarced68a02021-01-24 17:53:47 +01007627 if (cctx->ctx_skip == SKIP_NOT)
7628 {
7629 // previous block was executed, this one and following will not
7630 cctx->ctx_skip = SKIP_YES;
7631 scope->se_u.se_if.is_seen_skip_not = TRUE;
7632 }
7633 if (scope->se_u.se_if.is_seen_skip_not)
7634 {
7635 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007636 // Do not count the "elseif" for profiling and cmdmod
7637 instr->ga_len = current_instr_idx(cctx);
7638
Bram Moolenaarced68a02021-01-24 17:53:47 +01007639 skip_expr_cctx(&p, cctx);
7640 return p;
7641 }
7642
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007643 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007644 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007645 int moved_cmdmod = FALSE;
7646
7647 // Move any CMDMOD instruction to after the jump
7648 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7649 {
7650 if (ga_grow(instr, 1) == FAIL)
7651 return NULL;
7652 ((isn_T *)instr->ga_data)[instr->ga_len] =
7653 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7654 --instr->ga_len;
7655 moved_cmdmod = TRUE;
7656 }
7657
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007658 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007660 return NULL;
7661 // previous "if" or "elseif" jumps here
7662 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7663 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007664 if (moved_cmdmod)
7665 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007668 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007669 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007670 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007671 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007672 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007673#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007674 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007675 {
7676 // the previous block was skipped, need to profile this line
7677 generate_instr(cctx, ISN_PROF_START);
7678 instr_count = instr->ga_len;
7679 }
7680#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007681 if (cctx->ctx_compile_type == CT_DEBUG)
7682 {
7683 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007684 generate_instr_debug(cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007685 instr_count = instr->ga_len;
7686 }
Bram Moolenaarced68a02021-01-24 17:53:47 +01007687 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007688 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007689 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007690 clear_ppconst(&ppconst);
7691 return NULL;
7692 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007693 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007694 if (!ends_excmd2(arg, skipwhite(p)))
7695 {
7696 semsg(_(e_trailing_arg), p);
7697 return NULL;
7698 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007699 if (scope->se_skip_save == SKIP_YES)
7700 clear_ppconst(&ppconst);
7701 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007702 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007703 int error = FALSE;
7704 int v;
7705
Bram Moolenaar7f141552020-05-09 17:35:53 +02007706 // The expression results in a constant.
7707 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007708 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7709 if (error)
7710 return NULL;
7711 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007712 clear_ppconst(&ppconst);
7713 scope->se_u.se_if.is_if_label = -1;
7714 }
7715 else
7716 {
7717 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007718 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007719 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007720 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007721 if (bool_on_stack(cctx) == FAIL)
7722 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007723
Bram Moolenaara91a7132021-03-25 21:12:15 +01007724 // CMDMOD_REV must come before the jump
7725 generate_undo_cmdmods(cctx);
7726
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007727 // "where" is set when ":elseif", "else" or ":endif" is found
7728 scope->se_u.se_if.is_if_label = instr->ga_len;
7729 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731
7732 return p;
7733}
7734
7735 static char_u *
7736compile_else(char_u *arg, cctx_T *cctx)
7737{
7738 char_u *p = arg;
7739 garray_T *instr = &cctx->ctx_instr;
7740 isn_T *isn;
7741 scope_T *scope = cctx->ctx_scope;
7742
7743 if (scope == NULL || scope->se_type != IF_SCOPE)
7744 {
7745 emsg(_(e_else_without_if));
7746 return NULL;
7747 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007748 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007749 if (!cctx->ctx_had_return)
7750 scope->se_u.se_if.is_had_return = FALSE;
7751 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752
Bram Moolenaarced68a02021-01-24 17:53:47 +01007753#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007754 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007755 {
7756 if (cctx->ctx_skip == SKIP_NOT
7757 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7758 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007759 // the previous block was executed, do not count "else" for
7760 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007761 --instr->ga_len;
7762 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7763 {
7764 // the previous block was not executed, this one will, do count the
7765 // "else" for profiling
7766 cctx->ctx_skip = SKIP_NOT;
7767 generate_instr(cctx, ISN_PROF_END);
7768 generate_instr(cctx, ISN_PROF_START);
7769 cctx->ctx_skip = SKIP_YES;
7770 }
7771 }
7772#endif
7773
7774 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007775 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007776 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007777 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007778 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007779 if (!cctx->ctx_had_return
7780 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7781 JUMP_ALWAYS, cctx) == FAIL)
7782 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007783 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007784
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007785 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007786 {
7787 if (scope->se_u.se_if.is_if_label >= 0)
7788 {
7789 // previous "if" or "elseif" jumps here
7790 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7791 isn->isn_arg.jump.jump_where = instr->ga_len;
7792 scope->se_u.se_if.is_if_label = -1;
7793 }
7794 }
7795
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007796 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007797 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799
7800 return p;
7801}
7802
7803 static char_u *
7804compile_endif(char_u *arg, cctx_T *cctx)
7805{
7806 scope_T *scope = cctx->ctx_scope;
7807 ifscope_T *ifscope;
7808 garray_T *instr = &cctx->ctx_instr;
7809 isn_T *isn;
7810
Bram Moolenaarfa984412021-03-25 22:15:28 +01007811 if (misplaced_cmdmod(cctx))
7812 return NULL;
7813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 if (scope == NULL || scope->se_type != IF_SCOPE)
7815 {
7816 emsg(_(e_endif_without_if));
7817 return NULL;
7818 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007819 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007820 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007821 if (!cctx->ctx_had_return)
7822 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007824 if (scope->se_u.se_if.is_if_label >= 0)
7825 {
7826 // previous "if" or "elseif" jumps here
7827 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7828 isn->isn_arg.jump.jump_where = instr->ga_len;
7829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007831 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007832
7833#ifdef FEAT_PROFILE
7834 // even when skipping we count the endif as executed, unless the block it's
7835 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02007836 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007837 && scope->se_skip_save != SKIP_YES)
7838 {
7839 cctx->ctx_skip = SKIP_NOT;
7840 generate_instr(cctx, ISN_PROF_START);
7841 }
7842#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007843 cctx->ctx_skip = scope->se_skip_save;
7844
7845 // If all the blocks end in :return and there is an :else then the
7846 // had_return flag is set.
7847 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007849 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 return arg;
7851}
7852
7853/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007854 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007855 *
7856 * Produces instructions:
7857 * PUSHNR -1
7858 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007859 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007860 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7861 * - if beyond end, jump to "end"
7862 * - otherwise get item from list and push it
7863 * STORE var Store item in "var"
7864 * ... body ...
7865 * JUMP top Jump back to repeat
7866 * end: DROP Drop the result of "expr"
7867 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007868 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7869 * UNPACK 2 Split item in 2
7870 * STORE var1 Store item in "var1"
7871 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007872 */
7873 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007874compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007876 char_u *arg;
7877 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007878 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007880 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007881 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007882 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007883 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007886 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007887 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007888 lvar_T *loop_lvar; // loop iteration variable
7889 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007891 type_T *item_type = &t_any;
7892 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007893 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894
Bram Moolenaar792f7862020-11-23 08:31:18 +01007895 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007896 if (p == NULL)
7897 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007898 if (var_count == 0)
7899 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007900 else
7901 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902
7903 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007904 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007905 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7906 return NULL;
7907 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02007909 if (*p == ':' && wp != p)
7910 semsg(_(e_no_white_space_allowed_before_colon_str), p);
7911 else
7912 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 return NULL;
7914 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007915 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007916 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7917 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007918
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007919 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7920 // instruction.
7921 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7922 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7923 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007924 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007925 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02007926 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
7927 .isn_arg.debug.dbg_break_lnum;
7928 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007930 scope = new_scope(cctx, FOR_SCOPE);
7931 if (scope == NULL)
7932 return NULL;
7933
Bram Moolenaar792f7862020-11-23 08:31:18 +01007934 // Reserve a variable to store the loop iteration counter and initialize it
7935 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007936 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7937 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007938 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007939 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007940 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007941 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007942 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007943 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944
7945 // compile "expr", it remains on the stack until "endfor"
7946 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007947 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007948 {
7949 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007951 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007952 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007953
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007954 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007955 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007957 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007958 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007959 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007960 semsg(_(e_for_loop_on_str_not_supported),
7961 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007962 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963 return NULL;
7964 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007965
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007966 if (vartype->tt_type == VAR_STRING)
7967 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007968 else if (vartype->tt_type == VAR_BLOB)
7969 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007970 else if (vartype->tt_type == VAR_LIST
7971 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007972 {
Bram Moolenaar444d8782021-06-26 12:40:56 +02007973 if (!var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007974 item_type = vartype->tt_member;
7975 else if (vartype->tt_member->tt_type == VAR_LIST
7976 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007977 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007978 item_type = vartype->tt_member->tt_member;
7979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007980
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007981 // CMDMOD_REV must come before the FOR instruction.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007982 generate_undo_cmdmods(cctx);
7983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007984 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007985 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02007986
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007987 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007988
Bram Moolenaar792f7862020-11-23 08:31:18 +01007989 arg = arg_start;
Bram Moolenaar444d8782021-06-26 12:40:56 +02007990 if (var_list)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007991 {
7992 generate_UNPACK(cctx, var_count, semicolon);
7993 arg = skipwhite(arg + 1); // skip white after '['
7994
7995 // the list item is replaced by a number of items
7996 if (ga_grow(stack, var_count - 1) == FAIL)
7997 {
7998 drop_scope(cctx);
7999 return NULL;
8000 }
8001 --stack->ga_len;
8002 for (idx = 0; idx < var_count; ++idx)
8003 {
8004 ((type_T **)stack->ga_data)[stack->ga_len] =
8005 (semicolon && idx == 0) ? vartype : item_type;
8006 ++stack->ga_len;
8007 }
8008 }
8009
8010 for (idx = 0; idx < var_count; ++idx)
8011 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008012 assign_dest_T dest = dest_local;
8013 int opt_flags = 0;
8014 int vimvaridx = -1;
8015 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008016 type_T *lhs_type = &t_any;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02008017 where_T where = WHERE_INIT;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008018
8019 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008020 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008021 name = vim_strnsave(arg, varlen);
8022 if (name == NULL)
8023 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008024 if (*p == ':')
8025 {
8026 p = skipwhite(p + 1);
8027 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8028 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008029
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008030 // TODO: script var not supported?
8031 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
8032 &vimvaridx, &type, cctx) == FAIL)
8033 goto failed;
8034 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008035 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008036 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
8037 0, 0, type, name) == FAIL)
8038 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008039 }
Bram Moolenaarb777da92021-05-22 21:40:39 +02008040 else if (varlen == 1 && *arg == '_')
8041 {
8042 // Assigning to "_": drop the value.
8043 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8044 goto failed;
8045 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008046 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008047 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01008048 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008049 {
8050 semsg(_(e_variable_already_declared), arg);
8051 goto failed;
8052 }
8053
Bram Moolenaarea870692020-12-02 14:24:30 +01008054 if (STRNCMP(name, "s:", 2) == 0)
8055 {
8056 semsg(_(e_cannot_declare_script_variable_in_function), name);
8057 goto failed;
8058 }
8059
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008060 // Reserve a variable to store "var".
Bram Moolenaar444d8782021-06-26 12:40:56 +02008061 where.wt_index = var_list ? idx + 1 : 0;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008062 where.wt_variable = TRUE;
8063 if (lhs_type == &t_any)
8064 lhs_type = item_type;
8065 else if (item_type != &t_unknown
Bram Moolenaar5ede5b22021-07-07 21:55:25 +02008066 && (item_type == &t_any
Bram Moolenaarefc5db52021-07-07 21:21:30 +02008067 ? need_type(item_type, lhs_type,
8068 -1, 0, cctx, FALSE, FALSE)
8069 : check_type(lhs_type, item_type, TRUE, where))
8070 == FAIL)
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02008071 goto failed;
8072 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008073 if (var_lvar == NULL)
8074 // out of memory or used as an argument
8075 goto failed;
8076
8077 if (semicolon && idx == var_count - 1)
8078 var_lvar->lv_type = vartype;
8079 else
8080 var_lvar->lv_type = item_type;
8081 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8082 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008083
8084 if (*p == ',' || *p == ';')
8085 ++p;
8086 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008087 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01008088 }
8089
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008090 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008091 {
8092 int save_prev_lnum = cctx->ctx_prev_lnum;
8093
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008094 // Add ISN_DEBUG here, so that the loop variables can be inspected.
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008095 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8096 cctx->ctx_prev_lnum = prev_lnum;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008097 generate_instr_debug(cctx);
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008098 cctx->ctx_prev_lnum = save_prev_lnum;
8099 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008100
Bram Moolenaar792f7862020-11-23 08:31:18 +01008101 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008102
8103failed:
8104 vim_free(name);
8105 drop_scope(cctx);
8106 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107}
8108
8109/*
8110 * compile "endfor"
8111 */
8112 static char_u *
8113compile_endfor(char_u *arg, cctx_T *cctx)
8114{
8115 garray_T *instr = &cctx->ctx_instr;
8116 scope_T *scope = cctx->ctx_scope;
8117 forscope_T *forscope;
8118 isn_T *isn;
8119
Bram Moolenaarfa984412021-03-25 22:15:28 +01008120 if (misplaced_cmdmod(cctx))
8121 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 if (scope == NULL || scope->se_type != FOR_SCOPE)
8124 {
8125 emsg(_(e_for));
8126 return NULL;
8127 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008128 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008130 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008131
8132 // At end of ":for" scope jump back to the FOR instruction.
8133 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
8134
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008135 // Fill in the "end" label in the FOR statement so it can jump here.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8137 isn->isn_arg.forloop.for_end = instr->ga_len;
8138
8139 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008140 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141
8142 // Below the ":for" scope drop the "expr" list from the stack.
8143 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8144 return NULL;
8145
8146 vim_free(scope);
8147
8148 return arg;
8149}
8150
8151/*
8152 * compile "while expr"
8153 *
8154 * Produces instructions:
8155 * top: EVAL expr Push result of "expr"
8156 * JUMP_IF_FALSE end jump if false
8157 * ... body ...
8158 * JUMP top Jump back to repeat
8159 * end:
8160 *
8161 */
8162 static char_u *
8163compile_while(char_u *arg, cctx_T *cctx)
8164{
8165 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 scope_T *scope;
8167
8168 scope = new_scope(cctx, WHILE_SCOPE);
8169 if (scope == NULL)
8170 return NULL;
8171
Bram Moolenaara91a7132021-03-25 21:12:15 +01008172 // "endwhile" jumps back here, one before when profiling or using cmdmods
8173 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174
8175 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008176 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008178 if (!ends_excmd2(arg, skipwhite(p)))
8179 {
8180 semsg(_(e_trailing_arg), p);
8181 return NULL;
8182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183
Bram Moolenaar13106602020-10-04 16:06:05 +02008184 if (bool_on_stack(cctx) == FAIL)
8185 return FAIL;
8186
Bram Moolenaara91a7132021-03-25 21:12:15 +01008187 // CMDMOD_REV must come before the jump
8188 generate_undo_cmdmods(cctx);
8189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008191 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192 JUMP_IF_FALSE, cctx) == FAIL)
8193 return FAIL;
8194
8195 return p;
8196}
8197
8198/*
8199 * compile "endwhile"
8200 */
8201 static char_u *
8202compile_endwhile(char_u *arg, cctx_T *cctx)
8203{
8204 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008205 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008206
Bram Moolenaarfa984412021-03-25 22:15:28 +01008207 if (misplaced_cmdmod(cctx))
8208 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008209 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8210 {
8211 emsg(_(e_while));
8212 return NULL;
8213 }
8214 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008215 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008216
Bram Moolenaarf002a412021-01-24 13:34:18 +01008217#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008218 // count the endwhile before jumping
8219 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008220#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008223 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224
8225 // Fill in the "end" label in the WHILE statement so it can jump here.
8226 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008227 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8228 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008229
8230 vim_free(scope);
8231
8232 return arg;
8233}
8234
8235/*
8236 * compile "continue"
8237 */
8238 static char_u *
8239compile_continue(char_u *arg, cctx_T *cctx)
8240{
8241 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008242 int try_scopes = 0;
8243 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244
8245 for (;;)
8246 {
8247 if (scope == NULL)
8248 {
8249 emsg(_(e_continue));
8250 return NULL;
8251 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008252 if (scope->se_type == FOR_SCOPE)
8253 {
8254 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008255 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008256 }
8257 if (scope->se_type == WHILE_SCOPE)
8258 {
8259 loop_label = scope->se_u.se_while.ws_top_label;
8260 break;
8261 }
8262 if (scope->se_type == TRY_SCOPE)
8263 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264 scope = scope->se_outer;
8265 }
8266
Bram Moolenaarc150c092021-02-13 15:02:46 +01008267 if (try_scopes > 0)
8268 // Inside one or more try/catch blocks we first need to jump to the
8269 // "finally" or "endtry" to cleanup.
8270 generate_TRYCONT(cctx, try_scopes, loop_label);
8271 else
8272 // Jump back to the FOR or WHILE instruction.
8273 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275 return arg;
8276}
8277
8278/*
8279 * compile "break"
8280 */
8281 static char_u *
8282compile_break(char_u *arg, cctx_T *cctx)
8283{
8284 scope_T *scope = cctx->ctx_scope;
8285 endlabel_T **el;
8286
8287 for (;;)
8288 {
8289 if (scope == NULL)
8290 {
8291 emsg(_(e_break));
8292 return NULL;
8293 }
8294 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8295 break;
8296 scope = scope->se_outer;
8297 }
8298
8299 // Jump to the end of the FOR or WHILE loop.
8300 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008301 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008303 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008304 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8305 return FAIL;
8306
8307 return arg;
8308}
8309
8310/*
8311 * compile "{" start of block
8312 */
8313 static char_u *
8314compile_block(char_u *arg, cctx_T *cctx)
8315{
8316 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8317 return NULL;
8318 return skipwhite(arg + 1);
8319}
8320
8321/*
8322 * compile end of block: drop one scope
8323 */
8324 static void
8325compile_endblock(cctx_T *cctx)
8326{
8327 scope_T *scope = cctx->ctx_scope;
8328
8329 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008330 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331 vim_free(scope);
8332}
8333
8334/*
8335 * compile "try"
8336 * Creates a new scope for the try-endtry, pointing to the first catch and
8337 * finally.
8338 * Creates another scope for the "try" block itself.
8339 * TRY instruction sets up exception handling at runtime.
8340 *
8341 * "try"
8342 * TRY -> catch1, -> finally push trystack entry
8343 * ... try block
8344 * "throw {exception}"
8345 * EVAL {exception}
8346 * THROW create exception
8347 * ... try block
8348 * " catch {expr}"
8349 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008350 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008351 * EVAL {expr}
8352 * MATCH
8353 * JUMP nomatch -> catch2
8354 * CATCH remove exception
8355 * ... catch block
8356 * " catch"
8357 * JUMP -> finally
8358 * catch2: CATCH remove exception
8359 * ... catch block
8360 * " finally"
8361 * finally:
8362 * ... finally block
8363 * " endtry"
8364 * ENDTRY pop trystack entry, may rethrow
8365 */
8366 static char_u *
8367compile_try(char_u *arg, cctx_T *cctx)
8368{
8369 garray_T *instr = &cctx->ctx_instr;
8370 scope_T *try_scope;
8371 scope_T *scope;
8372
Bram Moolenaarfa984412021-03-25 22:15:28 +01008373 if (misplaced_cmdmod(cctx))
8374 return NULL;
8375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008376 // scope that holds the jumps that go to catch/finally/endtry
8377 try_scope = new_scope(cctx, TRY_SCOPE);
8378 if (try_scope == NULL)
8379 return NULL;
8380
Bram Moolenaar69f70502021-01-01 16:10:46 +01008381 if (cctx->ctx_skip != SKIP_YES)
8382 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008383 isn_T *isn;
8384
8385 // "try_catch" is set when the first ":catch" is found or when no catch
8386 // is found and ":finally" is found.
8387 // "try_finally" is set when ":finally" is found
8388 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008389 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008390 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8391 return NULL;
8392 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8393 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008394 return NULL;
8395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396
8397 // scope for the try block itself
8398 scope = new_scope(cctx, BLOCK_SCOPE);
8399 if (scope == NULL)
8400 return NULL;
8401
8402 return arg;
8403}
8404
8405/*
8406 * compile "catch {expr}"
8407 */
8408 static char_u *
8409compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8410{
8411 scope_T *scope = cctx->ctx_scope;
8412 garray_T *instr = &cctx->ctx_instr;
8413 char_u *p;
8414 isn_T *isn;
8415
Bram Moolenaarfa984412021-03-25 22:15:28 +01008416 if (misplaced_cmdmod(cctx))
8417 return NULL;
8418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419 // end block scope from :try or :catch
8420 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8421 compile_endblock(cctx);
8422 scope = cctx->ctx_scope;
8423
8424 // Error if not in a :try scope
8425 if (scope == NULL || scope->se_type != TRY_SCOPE)
8426 {
8427 emsg(_(e_catch));
8428 return NULL;
8429 }
8430
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008431 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008432 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008433 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434 return NULL;
8435 }
8436
Bram Moolenaar69f70502021-01-01 16:10:46 +01008437 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008439#ifdef FEAT_PROFILE
8440 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008441 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008442 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008443 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008444 .isn_type == ISN_PROF_START)
8445 --instr->ga_len;
8446#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008447 // Jump from end of previous block to :finally or :endtry
8448 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8449 JUMP_ALWAYS, cctx) == FAIL)
8450 return NULL;
8451
8452 // End :try or :catch scope: set value in ISN_TRY instruction
8453 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008454 if (isn->isn_arg.try.try_ref->try_catch == 0)
8455 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008456 if (scope->se_u.se_try.ts_catch_label != 0)
8457 {
8458 // Previous catch without match jumps here
8459 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8460 isn->isn_arg.jump.jump_where = instr->ga_len;
8461 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008462#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008463 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008464 {
8465 // a "throw" that jumps here needs to be counted
8466 generate_instr(cctx, ISN_PROF_END);
8467 // the "catch" is also counted
8468 generate_instr(cctx, ISN_PROF_START);
8469 }
8470#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008471 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008472 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473 }
8474
8475 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008476 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008478 scope->se_u.se_try.ts_caught_all = TRUE;
8479 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008480 }
8481 else
8482 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008483 char_u *end;
8484 char_u *pat;
8485 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008486 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008487 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008489 // Push v:exception, push {expr} and MATCH
8490 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8491
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008492 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008493 if (*end != *p)
8494 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008495 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008496 vim_free(tofree);
8497 return FAIL;
8498 }
8499 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008500 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008501 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008502 len = (int)(end - tofree);
8503 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008504 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008505 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008506 if (pat == NULL)
8507 return FAIL;
8508 if (generate_PUSHS(cctx, pat) == FAIL)
8509 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008511 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8512 return NULL;
8513
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008514 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008515 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8516 return NULL;
8517 }
8518
Bram Moolenaar69f70502021-01-01 16:10:46 +01008519 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008520 return NULL;
8521
8522 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8523 return NULL;
8524 return p;
8525}
8526
8527 static char_u *
8528compile_finally(char_u *arg, cctx_T *cctx)
8529{
8530 scope_T *scope = cctx->ctx_scope;
8531 garray_T *instr = &cctx->ctx_instr;
8532 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008533 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534
Bram Moolenaarfa984412021-03-25 22:15:28 +01008535 if (misplaced_cmdmod(cctx))
8536 return NULL;
8537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008538 // end block scope from :try or :catch
8539 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8540 compile_endblock(cctx);
8541 scope = cctx->ctx_scope;
8542
8543 // Error if not in a :try scope
8544 if (scope == NULL || scope->se_type != TRY_SCOPE)
8545 {
8546 emsg(_(e_finally));
8547 return NULL;
8548 }
8549
8550 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008551 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008552 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008553 {
8554 emsg(_(e_finally_dup));
8555 return NULL;
8556 }
8557
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008558 this_instr = instr->ga_len;
8559#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008560 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar834193a2021-06-30 20:39:15 +02008561 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008562 .isn_type == ISN_PROF_START)
Bram Moolenaar834193a2021-06-30 20:39:15 +02008563 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008564 // jump to the profile start of the "finally"
8565 --this_instr;
Bram Moolenaar834193a2021-06-30 20:39:15 +02008566
8567 // jump to the profile end above it
8568 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
8569 .isn_type == ISN_PROF_END)
8570 --this_instr;
8571 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008572#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008574 // Fill in the "end" label in jumps at the end of the blocks.
8575 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8576 this_instr, cctx);
8577
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008578 // If there is no :catch then an exception jumps to :finally.
8579 if (isn->isn_arg.try.try_ref->try_catch == 0)
8580 isn->isn_arg.try.try_ref->try_catch = this_instr;
8581 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008582 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 {
8584 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008585 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008586 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008587 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008588 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008589 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8590 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008592 // TODO: set index in ts_finally_label jumps
8593
8594 return arg;
8595}
8596
8597 static char_u *
8598compile_endtry(char_u *arg, cctx_T *cctx)
8599{
8600 scope_T *scope = cctx->ctx_scope;
8601 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008602 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008603
Bram Moolenaarfa984412021-03-25 22:15:28 +01008604 if (misplaced_cmdmod(cctx))
8605 return NULL;
8606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008607 // end block scope from :catch or :finally
8608 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8609 compile_endblock(cctx);
8610 scope = cctx->ctx_scope;
8611
8612 // Error if not in a :try scope
8613 if (scope == NULL || scope->se_type != TRY_SCOPE)
8614 {
8615 if (scope == NULL)
8616 emsg(_(e_no_endtry));
8617 else if (scope->se_type == WHILE_SCOPE)
8618 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008619 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008620 emsg(_(e_endfor));
8621 else
8622 emsg(_(e_endif));
8623 return NULL;
8624 }
8625
Bram Moolenaarc150c092021-02-13 15:02:46 +01008626 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008627 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008628 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008629 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8630 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008631 {
8632 emsg(_(e_missing_catch_or_finally));
8633 return NULL;
8634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008636#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008637 if (cctx->ctx_compile_type == CT_PROFILE
8638 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008639 .isn_type == ISN_PROF_START)
8640 // move the profile start after "endtry" so that it's not counted when
8641 // the exception is rethrown.
8642 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008643#endif
8644
Bram Moolenaar69f70502021-01-01 16:10:46 +01008645 // Fill in the "end" label in jumps at the end of the blocks, if not
8646 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008647 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8648 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649
Bram Moolenaar69f70502021-01-01 16:10:46 +01008650 if (scope->se_u.se_try.ts_catch_label != 0)
8651 {
8652 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008653 isn_T *isn = ((isn_T *)instr->ga_data)
8654 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008655 isn->isn_arg.jump.jump_where = instr->ga_len;
8656 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008657 }
8658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008659 compile_endblock(cctx);
8660
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008661 if (cctx->ctx_skip != SKIP_YES)
8662 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008663 // End :catch or :finally scope: set instruction index in ISN_TRY
8664 // instruction
8665 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008666 if (cctx->ctx_skip != SKIP_YES
8667 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8668 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008669#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008670 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008671 generate_instr(cctx, ISN_PROF_START);
8672#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008674 return arg;
8675}
8676
8677/*
8678 * compile "throw {expr}"
8679 */
8680 static char_u *
8681compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8682{
8683 char_u *p = skipwhite(arg);
8684
Bram Moolenaara5565e42020-05-09 15:44:01 +02008685 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008686 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008687 if (cctx->ctx_skip == SKIP_YES)
8688 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008689 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008690 return NULL;
8691 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8692 return NULL;
8693
8694 return p;
8695}
8696
Bram Moolenaarc3235272021-07-10 19:42:03 +02008697 static char_u *
8698compile_eval(char_u *arg, cctx_T *cctx)
8699{
8700 char_u *p = arg;
8701 int name_only;
8702 char_u *alias;
8703 long lnum = SOURCING_LNUM;
8704
8705 // find_ex_command() will consider a variable name an expression, assuming
8706 // that something follows on the next line. Check that something actually
8707 // follows, otherwise it's probably a misplaced command.
8708 get_name_len(&p, &alias, FALSE, FALSE);
8709 name_only = ends_excmd2(arg, skipwhite(p));
8710 vim_free(alias);
8711
8712 p = arg;
8713 if (compile_expr0(&p, cctx) == FAIL)
8714 return NULL;
8715
8716 if (name_only && lnum == SOURCING_LNUM)
8717 {
8718 semsg(_(e_expression_without_effect_str), arg);
8719 return NULL;
8720 }
8721
8722 // drop the result
8723 generate_instr_drop(cctx, ISN_DROP, 1);
8724
8725 return skipwhite(p);
8726}
8727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008728/*
8729 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008730 * compile "echomsg expr"
8731 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008732 * compile "execute expr"
8733 */
8734 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008735compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008736{
8737 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008738 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008739 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008740 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008741 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008742 garray_T *stack = &cctx->ctx_type_stack;
8743 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008744
8745 for (;;)
8746 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008747 if (ends_excmd2(prev, p))
8748 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008749 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008750 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008751 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008752
8753 if (cctx->ctx_skip != SKIP_YES)
8754 {
8755 // check for non-void type
8756 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8757 if (type->tt_type == VAR_VOID)
8758 {
8759 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8760 return NULL;
8761 }
8762 }
8763
Bram Moolenaarad39c092020-02-26 18:23:43 +01008764 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008765 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008766 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008767 }
8768
Bram Moolenaare4984292020-12-13 14:19:25 +01008769 if (count > 0)
8770 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008771 long save_lnum = cctx->ctx_lnum;
8772
8773 // Use the line number where the command started.
8774 cctx->ctx_lnum = start_ctx_lnum;
8775
Bram Moolenaare4984292020-12-13 14:19:25 +01008776 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8777 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8778 else if (cmdidx == CMD_execute)
8779 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8780 else if (cmdidx == CMD_echomsg)
8781 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8782 else
8783 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008784
8785 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787 return p;
8788}
8789
8790/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008791 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008792 * instruction to compute it and return OK.
8793 * Otherwise return FAIL, the caller must deal with any range.
8794 */
8795 static int
8796compile_variable_range(exarg_T *eap, cctx_T *cctx)
8797{
8798 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8799 char_u *p = skipdigits(eap->cmd);
8800
8801 if (p == range_end)
8802 return FAIL;
8803 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8804}
8805
8806/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008807 * :put r
8808 * :put ={expr}
8809 */
8810 static char_u *
8811compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8812{
8813 char_u *line = arg;
8814 linenr_T lnum;
8815 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008816 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008817
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008818 eap->regname = *line;
8819
8820 if (eap->regname == '=')
8821 {
8822 char_u *p = line + 1;
8823
8824 if (compile_expr0(&p, cctx) == FAIL)
8825 return NULL;
8826 line = p;
8827 }
8828 else if (eap->regname != NUL)
8829 ++line;
8830
Bram Moolenaar08597872020-12-10 19:43:40 +01008831 if (compile_variable_range(eap, cctx) == OK)
8832 {
8833 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8834 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008835 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008836 {
8837 // Either no range or a number.
8838 // "errormsg" will not be set because the range is ADDR_LINES.
8839 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008840 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008841 return NULL;
8842 if (eap->addr_count == 0)
8843 lnum = -1;
8844 else
8845 lnum = eap->line2;
8846 if (above)
8847 --lnum;
8848 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008849
8850 generate_PUT(cctx, eap->regname, lnum);
8851 return line;
8852}
8853
8854/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008855 * A command that is not compiled, execute with legacy code.
8856 */
8857 static char_u *
8858compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8859{
Bram Moolenaare729ce22021-06-06 21:38:09 +02008860 char_u *p;
8861 int has_expr = FALSE;
8862 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008863
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008864 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008865 goto theend;
8866
Bram Moolenaare729ce22021-06-06 21:38:09 +02008867 // If there was a prececing command modifier, drop it and include it in the
8868 // EXEC command.
8869 if (cctx->ctx_has_cmdmod)
8870 {
8871 garray_T *instr = &cctx->ctx_instr;
8872 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8873
8874 if (isn->isn_type == ISN_CMDMOD)
8875 {
8876 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8877 ->cmod_filter_regmatch.regprog);
8878 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8879 --instr->ga_len;
8880 cctx->ctx_has_cmdmod = FALSE;
8881 }
8882 }
8883
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008884 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008885 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008886 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008887 int usefilter = FALSE;
8888
8889 has_expr = argt & (EX_XFILE | EX_EXPAND);
8890
8891 // If the command can be followed by a bar, find the bar and truncate
8892 // it, so that the following command can be compiled.
8893 // The '|' is overwritten with a NUL, it is put back below.
8894 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8895 && *eap->arg == '!')
8896 // :w !filter or :r !filter or :r! filter
8897 usefilter = TRUE;
8898 if ((argt & EX_TRLBAR) && !usefilter)
8899 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008900 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008901 separate_nextcmd(eap);
8902 if (eap->nextcmd != NULL)
8903 nextcmd = eap->nextcmd;
8904 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008905 else if (eap->cmdidx == CMD_wincmd)
8906 {
8907 p = eap->arg;
8908 if (*p != NUL)
8909 ++p;
8910 if (*p == 'g' || *p == Ctrl_G)
8911 ++p;
8912 p = skipwhite(p);
8913 if (*p == '|')
8914 {
8915 *p = NUL;
8916 nextcmd = p + 1;
8917 }
8918 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008919 }
8920
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008921 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8922 {
8923 // expand filename in "syntax include [@group] filename"
8924 has_expr = TRUE;
8925 eap->arg = skipwhite(eap->arg + 7);
8926 if (*eap->arg == '@')
8927 eap->arg = skiptowhite(eap->arg);
8928 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008929
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008930 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8931 && STRLEN(eap->arg) > 4)
8932 {
8933 int delim = *eap->arg;
8934
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008935 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008936 if (*p == delim)
8937 {
8938 eap->arg = p + 1;
8939 has_expr = TRUE;
8940 }
8941 }
8942
Bram Moolenaarecac5912021-01-05 19:23:28 +01008943 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8944 {
8945 // TODO: should only expand when appropriate for the command
8946 eap->arg = skiptowhite(eap->arg);
8947 has_expr = TRUE;
8948 }
8949
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008950 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008951 {
8952 int count = 0;
8953 char_u *start = skipwhite(line);
8954
8955 // :cmd xxx`=expr1`yyy`=expr2`zzz
8956 // PUSHS ":cmd xxx"
8957 // eval expr1
8958 // PUSHS "yyy"
8959 // eval expr2
8960 // PUSHS "zzz"
8961 // EXECCONCAT 5
8962 for (;;)
8963 {
8964 if (p > start)
8965 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008966 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008967 ++count;
8968 }
8969 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008970 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008971 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008972 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008973 ++count;
8974 p = skipwhite(p);
8975 if (*p != '`')
8976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008977 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008978 return NULL;
8979 }
8980 start = p + 1;
8981
8982 p = (char_u *)strstr((char *)start, "`=");
8983 if (p == NULL)
8984 {
8985 if (*skipwhite(start) != NUL)
8986 {
8987 generate_PUSHS(cctx, vim_strsave(start));
8988 ++count;
8989 }
8990 break;
8991 }
8992 }
8993 generate_EXECCONCAT(cctx, count);
8994 }
8995 else
8996 generate_EXEC(cctx, line);
8997
8998theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008999 if (*nextcmd != NUL)
9000 {
9001 // the parser expects a pointer to the bar, put it back
9002 --nextcmd;
9003 *nextcmd = '|';
9004 }
9005
9006 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009007}
9008
Bram Moolenaar20677332021-06-06 17:02:53 +02009009/*
9010 * A script command with heredoc, e.g.
9011 * ruby << EOF
9012 * command
9013 * EOF
9014 * Has been turned into one long line with NL characters by
9015 * get_function_body():
9016 * ruby << EOF<NL> command<NL>EOF
9017 */
9018 static char_u *
9019compile_script(char_u *line, cctx_T *cctx)
9020{
9021 if (cctx->ctx_skip != SKIP_YES)
9022 {
9023 isn_T *isn;
9024
9025 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9026 return NULL;
9027 isn->isn_arg.string = vim_strsave(line);
9028 }
9029 return (char_u *)"";
9030}
9031
Bram Moolenaar8238f082021-04-20 21:10:48 +02009032
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009033/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009034 * :s/pat/repl/
9035 */
9036 static char_u *
9037compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9038{
9039 char_u *cmd = eap->arg;
9040 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9041
9042 if (expr != NULL)
9043 {
9044 int delimiter = *cmd++;
9045
9046 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009047 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009048 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9049 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009050 garray_T save_ga = cctx->ctx_instr;
9051 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009052 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009053 int trailing_error;
9054 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009055 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009056 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009057
9058 cmd += 3;
9059 end = skip_substitute(cmd, delimiter);
9060
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009061 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009062 // labels are correct.
9063 cctx->ctx_instr.ga_len = 0;
9064 cctx->ctx_instr.ga_maxlen = 0;
9065 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009066 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009067 if (end[-1] == NUL)
9068 end[-1] = delimiter;
9069 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009070 trailing_error = *cmd != delimiter && *cmd != NUL;
9071
Bram Moolenaar169502f2021-04-21 12:19:35 +02009072 if (expr_res == FAIL || trailing_error
9073 || ga_grow(&cctx->ctx_instr, 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02009074 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009075 if (trailing_error)
9076 semsg(_(e_trailing_arg), cmd);
9077 clear_instr_ga(&cctx->ctx_instr);
9078 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009079 return NULL;
9080 }
9081
Bram Moolenaar8238f082021-04-20 21:10:48 +02009082 // Move the generated instructions into the ISN_SUBSTITUTE
9083 // instructions, then restore the list of instructions before
9084 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009085 instr_count = cctx->ctx_instr.ga_len;
9086 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009087 instr[instr_count].isn_type = ISN_FINISH;
9088
9089 cctx->ctx_instr = save_ga;
9090 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9091 {
9092 int idx;
9093
9094 for (idx = 0; idx < instr_count; ++idx)
9095 delete_instr(instr + idx);
9096 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009097 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009098 }
9099 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9100 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009101
9102 // skip over flags
9103 if (*end == '&')
9104 ++end;
9105 while (ASCII_ISALPHA(*end) || *end == '#')
9106 ++end;
9107 return end;
9108 }
9109 }
9110
9111 return compile_exec(arg, eap, cctx);
9112}
9113
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009114 static char_u *
9115compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9116{
9117 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009118 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009119
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009120 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009121 {
9122 if (STRNCMP(arg, "END", 3) == 0)
9123 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009124 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009125 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009126 // First load the current variable value.
9127 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9128 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009129 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009130 }
9131
9132 // Gets the redirected text and put it on the stack, then store it
9133 // in the variable.
9134 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9135
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009136 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009137 generate_instr_drop(cctx, ISN_CONCAT, 1);
9138
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009139 if (lhs->lhs_has_index)
9140 {
9141 // Use the info in "lhs" to store the value at the index in the
9142 // list or dict.
9143 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9144 &t_string, cctx) == FAIL)
9145 return NULL;
9146 }
9147 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009148 return NULL;
9149
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009150 VIM_CLEAR(lhs->lhs_name);
9151 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009152 return arg + 3;
9153 }
9154 emsg(_(e_cannot_nest_redir));
9155 return NULL;
9156 }
9157
9158 if (arg[0] == '=' && arg[1] == '>')
9159 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009160 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009161
9162 // redirect to a variable is compiled
9163 arg += 2;
9164 if (*arg == '>')
9165 {
9166 ++arg;
9167 append = TRUE;
9168 }
9169 arg = skipwhite(arg);
9170
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009171 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009172 FALSE, FALSE, 1, cctx) == FAIL)
9173 return NULL;
9174 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009175 lhs->lhs_append = append;
9176 if (lhs->lhs_has_index)
9177 {
9178 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9179 if (lhs->lhs_whole == NULL)
9180 return NULL;
9181 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009182
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009183 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009184 }
9185
9186 // other redirects are handled like at script level
9187 return compile_exec(line, eap, cctx);
9188}
9189
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009190#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009191 static char_u *
9192compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9193{
9194 isn_T *isn;
9195 char_u *p;
9196
9197 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9198 if (isn == NULL)
9199 return NULL;
9200 isn->isn_arg.number = eap->cmdidx;
9201
9202 p = eap->arg;
9203 if (compile_expr0(&p, cctx) == FAIL)
9204 return NULL;
9205
9206 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9207 if (isn == NULL)
9208 return NULL;
9209 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9210 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9211 return NULL;
9212 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9213 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9214 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9215
9216 return p;
9217}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009218#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009219
Bram Moolenaar4c137212021-04-19 16:48:48 +02009220/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009221 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009222 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009223 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009224 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009225add_def_function(ufunc_T *ufunc)
9226{
9227 dfunc_T *dfunc;
9228
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009229 if (def_functions.ga_len == 0)
9230 {
9231 // The first position is not used, so that a zero uf_dfunc_idx means it
9232 // wasn't set.
9233 if (ga_grow(&def_functions, 1) == FAIL)
9234 return FAIL;
9235 ++def_functions.ga_len;
9236 }
9237
Bram Moolenaar09689a02020-05-09 22:50:08 +02009238 // Add the function to "def_functions".
9239 if (ga_grow(&def_functions, 1) == FAIL)
9240 return FAIL;
9241 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9242 CLEAR_POINTER(dfunc);
9243 dfunc->df_idx = def_functions.ga_len;
9244 ufunc->uf_dfunc_idx = dfunc->df_idx;
9245 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009246 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009247 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009248 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009249 ++def_functions.ga_len;
9250 return OK;
9251}
9252
9253/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254 * After ex_function() has collected all the function lines: parse and compile
9255 * the lines into instructions.
9256 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009257 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9258 * the return statement (used for lambda). When uf_ret_type is already set
9259 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009260 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009261 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009262 * This can be used recursively through compile_lambda(), which may reallocate
9263 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009264 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009265 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009266 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009267compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009268 ufunc_T *ufunc,
9269 int check_return_type,
9270 compiletype_T compile_type,
9271 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009272{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009273 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009274 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009275 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009276 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009277 cctx_T cctx;
9278 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009279 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009280 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281 int ret = FAIL;
9282 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009283 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009284 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009285 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009286 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009287#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009288 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009289#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009290 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009291
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009292 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009293 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009294 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009295 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009296 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9297 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009298 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009299
9300 switch (compile_type)
9301 {
9302 case CT_PROFILE:
9303#ifdef FEAT_PROFILE
9304 instr_dest = dfunc->df_instr_prof; break;
9305#endif
9306 case CT_NONE: instr_dest = dfunc->df_instr; break;
9307 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9308 }
9309 if (instr_dest != NULL)
9310 // Was compiled in this mode before: Free old instructions.
9311 delete_def_function_contents(dfunc, FALSE);
9312 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009313 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009314 else
9315 {
9316 if (add_def_function(ufunc) == FAIL)
9317 return FAIL;
9318 new_def_function = TRUE;
9319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009320
Bram Moolenaar985116a2020-07-12 17:31:09 +02009321 ufunc->uf_def_status = UF_COMPILING;
9322
Bram Moolenaara80faa82020-04-12 19:37:17 +02009323 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009324
Bram Moolenaare99d4222021-06-13 14:01:26 +02009325 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009326 cctx.ctx_ufunc = ufunc;
9327 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009328 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9330 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9331 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9332 cctx.ctx_type_list = &ufunc->uf_type_list;
9333 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9334 instr = &cctx.ctx_instr;
9335
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009336 // Set the context to the function, it may be compiled when called from
9337 // another script. Set the script version to the most modern one.
9338 // The line number will be set in next_line_from_context().
9339 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009340 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9341
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009342 // Don't use the flag from ":legacy" here.
9343 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9344
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009345 // Make sure error messages are OK.
9346 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9347 if (do_estack_push)
9348 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009349 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009350
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009351 if (ufunc->uf_def_args.ga_len > 0)
9352 {
9353 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009354 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009355 int i;
9356 char_u *arg;
9357 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009358 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009359
9360 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009361 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009362 for (i = 0; i < count; ++i)
9363 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009364 garray_T *stack = &cctx.ctx_type_stack;
9365 type_T *val_type;
9366 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009367 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009368 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009369 int jump_instr_idx = instr->ga_len;
9370 isn_T *isn;
9371
9372 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9373 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9374 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009375
9376 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009377 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009378
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009379 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009380 r = compile_expr0(&arg, &cctx);
9381
Bram Moolenaar12bce952021-03-11 20:04:04 +01009382 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009383 goto erret;
9384
9385 // If no type specified use the type of the default value.
9386 // Otherwise check that the default value type matches the
9387 // specified type.
9388 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009389 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009390 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009391 {
9392 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009393 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009394 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009395 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009396 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009397 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009398
9399 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009400 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009401
9402 // set instruction index in JUMP_IF_ARG_SET to here
9403 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9404 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009405 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009406
9407 if (did_set_arg_type)
9408 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009409 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009410 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009411
9412 /*
9413 * Loop over all the lines of the function and generate instructions.
9414 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009415 for (;;)
9416 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009417 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009418 int starts_with_colon = FALSE;
9419 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009420 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009421
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009422 // Bail out on the first error to avoid a flood of errors and report
9423 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009424 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009425 goto erret;
9426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009427 if (line != NULL && *line == '|')
9428 // the line continues after a '|'
9429 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009430 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009431 && !(*line == '#' && (line == cctx.ctx_line_start
9432 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009433 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009434 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009435 goto erret;
9436 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009437 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9438 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009439 else
9440 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009441 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009442 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009443 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009444 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009445#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009446 if (cctx.ctx_skip != SKIP_YES)
9447 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009448#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009449 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009450 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009451 // Make a copy, splitting off nextcmd and removing trailing spaces
9452 // may change it.
9453 if (line != NULL)
9454 {
9455 line = vim_strsave(line);
9456 vim_free(line_to_free);
9457 line_to_free = line;
9458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009459 }
9460
Bram Moolenaara80faa82020-04-12 19:37:17 +02009461 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009462 ea.cmdlinep = &line;
9463 ea.cmd = skipwhite(line);
9464
Bram Moolenaarb2049902021-01-24 12:53:53 +01009465 if (*ea.cmd == '#')
9466 {
9467 // "#" starts a comment
9468 line = (char_u *)"";
9469 continue;
9470 }
9471
Bram Moolenaarf002a412021-01-24 13:34:18 +01009472#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009473 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9474 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009475 {
9476 may_generate_prof_end(&cctx, prof_lnum);
9477
9478 prof_lnum = cctx.ctx_lnum;
9479 generate_instr(&cctx, ISN_PROF_START);
9480 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009481#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009482 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9483 && cctx.ctx_skip != SKIP_YES)
9484 {
9485 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009486 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009487 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009488 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009489
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009490 // Some things can be recognized by the first character.
9491 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009492 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009493 case '}':
9494 {
9495 // "}" ends a block scope
9496 scopetype_T stype = cctx.ctx_scope == NULL
9497 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009498
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009499 if (stype == BLOCK_SCOPE)
9500 {
9501 compile_endblock(&cctx);
9502 line = ea.cmd;
9503 }
9504 else
9505 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009506 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009507 goto erret;
9508 }
9509 if (line != NULL)
9510 line = skipwhite(ea.cmd + 1);
9511 continue;
9512 }
9513
9514 case '{':
9515 // "{" starts a block scope
9516 // "{'a': 1}->func() is something else
9517 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9518 {
9519 line = compile_block(ea.cmd, &cctx);
9520 continue;
9521 }
9522 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009523 }
9524
9525 /*
9526 * COMMAND MODIFIERS
9527 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009528 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009529 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9530 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009531 {
9532 if (errormsg != NULL)
9533 goto erret;
9534 // empty line or comment
9535 line = (char_u *)"";
9536 continue;
9537 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009538 generate_cmdmods(&cctx, &local_cmdmod);
9539 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009540
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009541 // Check if there was a colon after the last command modifier or before
9542 // the current position.
9543 for (p = ea.cmd; p >= line; --p)
9544 {
9545 if (*p == ':')
9546 starts_with_colon = TRUE;
9547 if (p < ea.cmd && !VIM_ISWHITE(*p))
9548 break;
9549 }
9550
Bram Moolenaarce024c32021-06-26 13:00:49 +02009551 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009552 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009553 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009554 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009555 if (checkforcmd(&ea.cmd, "call", 3))
9556 {
9557 if (*ea.cmd == '(')
9558 // not for "call()"
9559 ea.cmd = p;
9560 else
9561 ea.cmd = skipwhite(ea.cmd);
9562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009563
Bram Moolenaarce024c32021-06-26 13:00:49 +02009564 if (!starts_with_colon)
9565 {
9566 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009567
Bram Moolenaarce024c32021-06-26 13:00:49 +02009568 // Check for assignment after command modifiers.
9569 assign = may_compile_assignment(&ea, &line, &cctx);
9570 if (assign == OK)
9571 goto nextline;
9572 if (assign == FAIL)
9573 goto erret;
9574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009575 }
9576
9577 /*
9578 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009579 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009580 * "++nr" and "--nr" are eval commands
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009581 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009582 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009583 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9584 && (starts_with_colon || !(*cmd == '\''
9585 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009586 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009587 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009588 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009589 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009590 if (!starts_with_colon)
9591 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009592 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009593 goto erret;
9594 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009595 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009596 if (ends_excmd2(line, ea.cmd))
9597 {
9598 // A range without a command: jump to the line.
9599 // TODO: compile to a more efficient command, possibly
9600 // calling parse_cmd_address().
9601 ea.cmdidx = CMD_SIZE;
9602 line = compile_exec(line, &ea, &cctx);
9603 goto nextline;
9604 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009605 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009606 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009607 p = find_ex_command(&ea, NULL,
9608 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009609 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009610
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009611 if (p == NULL)
9612 {
9613 if (cctx.ctx_skip != SKIP_YES)
9614 emsg(_(e_ambiguous_use_of_user_defined_command));
9615 goto erret;
9616 }
9617
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009618 // When using ":legacy cmd" always use compile_exec().
9619 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009620 {
9621 char_u *start = ea.cmd;
9622
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009623 switch (ea.cmdidx)
9624 {
9625 case CMD_if:
9626 case CMD_elseif:
9627 case CMD_else:
9628 case CMD_endif:
9629 case CMD_for:
9630 case CMD_endfor:
9631 case CMD_continue:
9632 case CMD_break:
9633 case CMD_while:
9634 case CMD_endwhile:
9635 case CMD_try:
9636 case CMD_catch:
9637 case CMD_finally:
9638 case CMD_endtry:
9639 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9640 goto erret;
9641 default: break;
9642 }
9643
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009644 // ":legacy return expr" needs to be handled differently.
9645 if (checkforcmd(&start, "return", 4))
9646 ea.cmdidx = CMD_return;
9647 else
9648 ea.cmdidx = CMD_legacy;
9649 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009651 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9652 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009653 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009654 {
9655 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009656 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009657 }
9658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009659 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009660 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009661 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009662 // CMD_var cannot happen, compile_assignment() above would be
9663 // used. Most likely an assignment to a non-existing variable.
9664 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009665 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009667 }
9668
Bram Moolenaar3988f642020-08-27 22:43:03 +02009669 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009670 && ea.cmdidx != CMD_elseif
9671 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009672 && ea.cmdidx != CMD_endif
9673 && ea.cmdidx != CMD_endfor
9674 && ea.cmdidx != CMD_endwhile
9675 && ea.cmdidx != CMD_catch
9676 && ea.cmdidx != CMD_finally
9677 && ea.cmdidx != CMD_endtry)
9678 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009679 emsg(_(e_unreachable_code_after_return));
9680 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009681 }
9682
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009683 p = skipwhite(p);
9684 if (ea.cmdidx != CMD_SIZE
9685 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9686 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009687 if (ea.cmdidx >= 0)
9688 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009689 if ((ea.argt & EX_BANG) && *p == '!')
9690 {
9691 ea.forceit = TRUE;
9692 p = skipwhite(p + 1);
9693 }
9694 }
9695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009696 switch (ea.cmdidx)
9697 {
9698 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009699 ea.arg = p;
9700 line = compile_nested_function(&ea, &cctx);
9701 break;
9702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009703 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009704 // TODO: should we allow this, e.g. to declare a global
9705 // function?
9706 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009707 goto erret;
9708
9709 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009710 line = compile_return(p, check_return_type,
9711 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009712 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009713 break;
9714
9715 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009716 emsg(_(e_cannot_use_let_in_vim9_script));
9717 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009718 case CMD_var:
9719 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009720 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009721 case CMD_increment:
9722 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009723 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009724 if (line == p)
9725 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009726 break;
9727
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009728 case CMD_unlet:
9729 case CMD_unlockvar:
9730 case CMD_lockvar:
9731 line = compile_unletlock(p, &ea, &cctx);
9732 break;
9733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009734 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02009735 emsg(_(e_import_can_only_be_used_in_script));
9736 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009737 break;
9738
9739 case CMD_if:
9740 line = compile_if(p, &cctx);
9741 break;
9742 case CMD_elseif:
9743 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009744 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009745 break;
9746 case CMD_else:
9747 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009748 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009749 break;
9750 case CMD_endif:
9751 line = compile_endif(p, &cctx);
9752 break;
9753
9754 case CMD_while:
9755 line = compile_while(p, &cctx);
9756 break;
9757 case CMD_endwhile:
9758 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009759 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009760 break;
9761
9762 case CMD_for:
9763 line = compile_for(p, &cctx);
9764 break;
9765 case CMD_endfor:
9766 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009767 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009768 break;
9769 case CMD_continue:
9770 line = compile_continue(p, &cctx);
9771 break;
9772 case CMD_break:
9773 line = compile_break(p, &cctx);
9774 break;
9775
9776 case CMD_try:
9777 line = compile_try(p, &cctx);
9778 break;
9779 case CMD_catch:
9780 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009781 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009782 break;
9783 case CMD_finally:
9784 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009785 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009786 break;
9787 case CMD_endtry:
9788 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009789 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009790 break;
9791 case CMD_throw:
9792 line = compile_throw(p, &cctx);
9793 break;
9794
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009795 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02009796 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009797 break;
9798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009799 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009800 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009801 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009802 case CMD_echomsg:
9803 case CMD_echoerr:
9804 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009805 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009806
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009807 case CMD_put:
9808 ea.cmd = cmd;
9809 line = compile_put(p, &ea, &cctx);
9810 break;
9811
Bram Moolenaar4c137212021-04-19 16:48:48 +02009812 case CMD_substitute:
9813 if (cctx.ctx_skip == SKIP_YES)
9814 line = (char_u *)"";
9815 else
9816 {
9817 ea.arg = p;
9818 line = compile_substitute(line, &ea, &cctx);
9819 }
9820 break;
9821
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009822 case CMD_redir:
9823 ea.arg = p;
9824 line = compile_redir(line, &ea, &cctx);
9825 break;
9826
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009827 case CMD_cexpr:
9828 case CMD_lexpr:
9829 case CMD_caddexpr:
9830 case CMD_laddexpr:
9831 case CMD_cgetexpr:
9832 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009833#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009834 ea.arg = p;
9835 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009836#else
9837 ex_ni(&ea);
9838 line = NULL;
9839#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009840 break;
9841
Bram Moolenaar3988f642020-08-27 22:43:03 +02009842 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009843
Bram Moolenaarae616492020-07-28 20:07:27 +02009844 case CMD_append:
9845 case CMD_change:
9846 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009847 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009848 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009849 case CMD_xit:
9850 not_in_vim9(&ea);
9851 goto erret;
9852
Bram Moolenaar002262f2020-07-08 17:47:57 +02009853 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009854 if (cctx.ctx_skip != SKIP_YES)
9855 {
9856 semsg(_(e_invalid_command_str), ea.cmd);
9857 goto erret;
9858 }
9859 // We don't check for a next command here.
9860 line = (char_u *)"";
9861 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009862
Bram Moolenaar20677332021-06-06 17:02:53 +02009863 case CMD_lua:
9864 case CMD_mzscheme:
9865 case CMD_perl:
9866 case CMD_py3:
9867 case CMD_python3:
9868 case CMD_python:
9869 case CMD_pythonx:
9870 case CMD_ruby:
9871 case CMD_tcl:
9872 ea.arg = p;
9873 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009874 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02009875 else
9876 // heredoc lines have been concatenated with NL
9877 // characters in get_function_body()
9878 line = compile_script(line, &cctx);
9879 break;
9880
9881 default:
9882 // Not recognized, execute with do_cmdline_cmd().
9883 ea.arg = p;
9884 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009885 break;
9886 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009887nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009888 if (line == NULL)
9889 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009890 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009891
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009892 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009893 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009895 if (cctx.ctx_type_stack.ga_len < 0)
9896 {
9897 iemsg("Type stack underflow");
9898 goto erret;
9899 }
9900 }
9901
9902 if (cctx.ctx_scope != NULL)
9903 {
9904 if (cctx.ctx_scope->se_type == IF_SCOPE)
9905 emsg(_(e_endif));
9906 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9907 emsg(_(e_endwhile));
9908 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9909 emsg(_(e_endfor));
9910 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009911 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009912 goto erret;
9913 }
9914
Bram Moolenaarefd88552020-06-18 20:50:10 +02009915 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009916 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02009917 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9918 ufunc->uf_ret_type = &t_void;
9919 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009920 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009921 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009922 goto erret;
9923 }
9924
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02009925 // Return void if there is no return at the end.
9926 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009927 }
9928
Bram Moolenaar599410c2021-04-10 14:03:43 +02009929 // When compiled with ":silent!" and there was an error don't consider the
9930 // function compiled.
9931 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009932 {
9933 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9934 + ufunc->uf_dfunc_idx;
9935 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009936 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009937#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009938 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009939 {
9940 dfunc->df_instr_prof = instr->ga_data;
9941 dfunc->df_instr_prof_count = instr->ga_len;
9942 }
9943 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009944#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009945 if (cctx.ctx_compile_type == CT_DEBUG)
9946 {
9947 dfunc->df_instr_debug = instr->ga_data;
9948 dfunc->df_instr_debug_count = instr->ga_len;
9949 }
9950 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01009951 {
9952 dfunc->df_instr = instr->ga_data;
9953 dfunc->df_instr_count = instr->ga_len;
9954 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009955 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009956 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009957 if (cctx.ctx_outer_used)
9958 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009959 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009961
9962 ret = OK;
9963
9964erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009965 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009966 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009967 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9968 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009969
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009970 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009971 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009972 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02009973 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009974
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009975 // If using the last entry in the table and it was added above, we
9976 // might as well remove it.
9977 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009978 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009979 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009980 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009981 ufunc->uf_dfunc_idx = 0;
9982 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009983 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009984
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009985 while (cctx.ctx_scope != NULL)
9986 drop_scope(&cctx);
9987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009988 if (errormsg != NULL)
9989 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009990 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009991 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009992 }
9993
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009994 if (cctx.ctx_redir_lhs.lhs_name != NULL)
9995 {
9996 if (ret == OK)
9997 {
9998 emsg(_(e_missing_redir_end));
9999 ret = FAIL;
10000 }
10001 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010002 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010003 }
10004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010005 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010006 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010007 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010008 if (do_estack_push)
10009 estack_pop();
10010
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010011 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010012 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010013 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010014 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010015 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010016}
10017
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010018 void
10019set_function_type(ufunc_T *ufunc)
10020{
10021 int varargs = ufunc->uf_va_name != NULL;
10022 int argcount = ufunc->uf_args.ga_len;
10023
10024 // Create a type for the function, with the return type and any
10025 // argument types.
10026 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10027 // The type is included in "tt_args".
10028 if (argcount > 0 || varargs)
10029 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010030 if (ufunc->uf_type_list.ga_itemsize == 0)
10031 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010032 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10033 argcount, &ufunc->uf_type_list);
10034 // Add argument types to the function type.
10035 if (func_type_add_arg_types(ufunc->uf_func_type,
10036 argcount + varargs,
10037 &ufunc->uf_type_list) == FAIL)
10038 return;
10039 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10040 ufunc->uf_func_type->tt_min_argcount =
10041 argcount - ufunc->uf_def_args.ga_len;
10042 if (ufunc->uf_arg_types == NULL)
10043 {
10044 int i;
10045
10046 // lambda does not have argument types.
10047 for (i = 0; i < argcount; ++i)
10048 ufunc->uf_func_type->tt_args[i] = &t_any;
10049 }
10050 else
10051 mch_memmove(ufunc->uf_func_type->tt_args,
10052 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10053 if (varargs)
10054 {
10055 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010056 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010057 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10058 }
10059 }
10060 else
10061 // No arguments, can use a predefined type.
10062 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10063 argcount, &ufunc->uf_type_list);
10064}
10065
10066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010067/*
10068 * Delete an instruction, free what it contains.
10069 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010070 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010071delete_instr(isn_T *isn)
10072{
10073 switch (isn->isn_type)
10074 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010075 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010077 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010078 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010079 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010080 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010081 case ISN_LOADENV:
10082 case ISN_LOADG:
10083 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010084 case ISN_LOADT:
10085 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010086 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010087 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010088 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010089 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010090 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010091 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010092 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010093 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010094 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010095 case ISN_STOREW:
10096 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010097 vim_free(isn->isn_arg.string);
10098 break;
10099
Bram Moolenaar4c137212021-04-19 16:48:48 +020010100 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010101 {
10102 int idx;
10103 isn_T *list = isn->isn_arg.subs.subs_instr;
10104
10105 vim_free(isn->isn_arg.subs.subs_cmd);
10106 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10107 delete_instr(list + idx);
10108 vim_free(list);
10109 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010110 break;
10111
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010112 case ISN_INSTR:
10113 {
10114 int idx;
10115 isn_T *list = isn->isn_arg.instr;
10116
10117 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10118 delete_instr(list + idx);
10119 vim_free(list);
10120 }
10121 break;
10122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010123 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010124 case ISN_STORES:
10125 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010126 break;
10127
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010128 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010129 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010130 vim_free(isn->isn_arg.unlet.ul_name);
10131 break;
10132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010133 case ISN_STOREOPT:
10134 vim_free(isn->isn_arg.storeopt.so_name);
10135 break;
10136
10137 case ISN_PUSHBLOB: // push blob isn_arg.blob
10138 blob_unref(isn->isn_arg.blob);
10139 break;
10140
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010141 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010143 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010144#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010145 break;
10146
10147 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010148#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010149 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010150#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010151 break;
10152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010153 case ISN_UCALL:
10154 vim_free(isn->isn_arg.ufunc.cuf_name);
10155 break;
10156
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010157 case ISN_FUNCREF:
10158 {
10159 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10160 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +010010161 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010162
Bram Moolenaar077a4232020-12-22 18:33:27 +010010163 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10164 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +020010165 }
10166 break;
10167
10168 case ISN_DCALL:
10169 {
10170 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10171 + isn->isn_arg.dfunc.cdf_idx;
10172
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010173 if (dfunc->df_ufunc != NULL
10174 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010175 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010176 }
10177 break;
10178
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010179 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010180 {
10181 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10182 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10183
10184 if (ufunc != NULL)
10185 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010186 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010187 func_ptr_unref(ufunc);
10188 }
10189
10190 vim_free(lambda);
10191 vim_free(isn->isn_arg.newfunc.nf_global);
10192 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010193 break;
10194
Bram Moolenaar5e654232020-09-16 15:22:00 +020010195 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010196 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010197 free_type(isn->isn_arg.type.ct_type);
10198 break;
10199
Bram Moolenaar02194d22020-10-24 23:08:38 +020010200 case ISN_CMDMOD:
10201 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10202 ->cmod_filter_regmatch.regprog);
10203 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10204 break;
10205
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010206 case ISN_LOADSCRIPT:
10207 case ISN_STORESCRIPT:
10208 vim_free(isn->isn_arg.script.scriptref);
10209 break;
10210
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010211 case ISN_TRY:
10212 vim_free(isn->isn_arg.try.try_ref);
10213 break;
10214
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010215 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010216 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010217 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10218 break;
10219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010220 case ISN_2BOOL:
10221 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010222 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010223 case ISN_ADDBLOB:
10224 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010225 case ISN_ANYINDEX:
10226 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010227 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010228 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010229 case ISN_BLOBINDEX:
10230 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010231 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010232 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010233 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010234 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010235 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010236 case ISN_COMPAREANY:
10237 case ISN_COMPAREBLOB:
10238 case ISN_COMPAREBOOL:
10239 case ISN_COMPAREDICT:
10240 case ISN_COMPAREFLOAT:
10241 case ISN_COMPAREFUNC:
10242 case ISN_COMPARELIST:
10243 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010244 case ISN_COMPARESPECIAL:
10245 case ISN_COMPARESTRING:
10246 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010247 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010248 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010249 case ISN_DROP:
10250 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010251 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010252 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010253 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010254 case ISN_EXECCONCAT:
10255 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010256 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010257 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010258 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010259 case ISN_GETITEM:
10260 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010261 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010262 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010263 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010264 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010265 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010266 case ISN_LOADBDICT:
10267 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010268 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010269 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010270 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010271 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010272 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010273 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010274 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010275 case ISN_NEGATENR:
10276 case ISN_NEWDICT:
10277 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010278 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010279 case ISN_OPFLOAT:
10280 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010281 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010282 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010283 case ISN_PROF_END:
10284 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010285 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010286 case ISN_PUSHF:
10287 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010288 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010289 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010290 case ISN_REDIREND:
10291 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010292 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010293 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010294 case ISN_SHUFFLE:
10295 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010296 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010297 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010298 case ISN_STORENR:
10299 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010300 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010301 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010302 case ISN_STOREV:
10303 case ISN_STRINDEX:
10304 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010305 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010306 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010307 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010308 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010309 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010310 // nothing allocated
10311 break;
10312 }
10313}
10314
10315/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010316 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010317 */
10318 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010319delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010320{
10321 int idx;
10322
10323 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010324 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010325
10326 if (dfunc->df_instr != NULL)
10327 {
10328 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10329 delete_instr(dfunc->df_instr + idx);
10330 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010331 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010332 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010333 if (dfunc->df_instr_debug != NULL)
10334 {
10335 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10336 delete_instr(dfunc->df_instr_debug + idx);
10337 VIM_CLEAR(dfunc->df_instr_debug);
10338 dfunc->df_instr_debug = NULL;
10339 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010340#ifdef FEAT_PROFILE
10341 if (dfunc->df_instr_prof != NULL)
10342 {
10343 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10344 delete_instr(dfunc->df_instr_prof + idx);
10345 VIM_CLEAR(dfunc->df_instr_prof);
10346 dfunc->df_instr_prof = NULL;
10347 }
10348#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010349
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010350 if (mark_deleted)
10351 dfunc->df_deleted = TRUE;
10352 if (dfunc->df_ufunc != NULL)
10353 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010354}
10355
10356/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010357 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010358 * function, unless another user function still uses it.
10359 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010360 */
10361 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010362unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010363{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010364 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010365 {
10366 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10367 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010368
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010369 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010370 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010371 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010372 ufunc->uf_dfunc_idx = 0;
10373 if (dfunc->df_ufunc == ufunc)
10374 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010375 }
10376}
10377
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010378/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010379 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010380 */
10381 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010382link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010383{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010384 if (ufunc->uf_dfunc_idx > 0)
10385 {
10386 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10387 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010388
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010389 ++dfunc->df_refcount;
10390 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010391}
10392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010393#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010394/*
10395 * Free all functions defined with ":def".
10396 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010397 void
10398free_def_functions(void)
10399{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010400 int idx;
10401
10402 for (idx = 0; idx < def_functions.ga_len; ++idx)
10403 {
10404 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10405
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010406 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010407 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010408 }
10409
10410 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010411}
10412#endif
10413
10414
10415#endif // FEAT_EVAL