blob: 52e5ac8cc3c1196c1306027740fbf9060c65d1b8 [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".
Bram Moolenaarab36e6a2021-11-30 16:14:49 +0000147 char_u *lhs_end; // end including any type
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200148
149 int lhs_has_index; // has "[expr]" or ".name"
150
151 int lhs_new_local; // create new local variable
152 int lhs_opt_flags; // for when destination is an option
153 int lhs_vimvaridx; // for when destination is a v:var
154
155 lvar_T lhs_local_lvar; // used for existing local destination
156 lvar_T lhs_arg_lvar; // used for argument destination
157 lvar_T *lhs_lvar; // points to destination lvar
158 int lhs_scriptvar_sid;
159 int lhs_scriptvar_idx;
160
161 int lhs_has_type; // type was specified
162 type_T *lhs_type;
163 type_T *lhs_member_type;
164
165 int lhs_append; // used by ISN_REDIREND
166} lhs_T;
167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168/*
169 * Context for compiling lines of Vim script.
170 * Stores info about the local variables and condition stack.
171 */
172struct cctx_S {
173 ufunc_T *ctx_ufunc; // current function
174 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200175 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176 garray_T ctx_instr; // generated instructions
177
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200178 int ctx_prev_lnum; // line number below previous command, for
179 // debugging
180
Bram Moolenaare99d4222021-06-13 14:01:26 +0200181 compiletype_T ctx_compile_type;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183 garray_T ctx_locals; // currently visible local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200185 int ctx_has_closure; // set to one if a closures was created in
186 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 garray_T ctx_imports; // imported items
189
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200190 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100191 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200192 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 cctx_T *ctx_outer; // outer scope for lambda or nested
195 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200196 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200199 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200200
Bram Moolenaar02194d22020-10-24 23:08:38 +0200201 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +0200202
203 lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
204 // lhs_name is not NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205};
206
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100207static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
209/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100210 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100211 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100212 * If "lvar" is NULL only check if the variable can be found.
213 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100215 static int
216lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217{
218 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100219 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100221 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100222 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200223
224 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
226 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100227 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
228 if (STRNCMP(name, lvp->lv_name, len) == 0
229 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200230 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100231 if (lvar != NULL)
232 {
233 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100234 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100235 }
236 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200239
240 // Find local in outer function scope.
241 if (cctx->ctx_outer != NULL)
242 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100243 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200244 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100245 if (lvar != NULL)
246 {
247 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100248 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100249 }
250 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200251 }
252 }
253
Bram Moolenaar709664c2020-12-12 14:33:41 +0100254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255}
256
257/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 * Lookup an argument in the current function and an enclosing function.
259 * Returns the argument index in "idxp"
260 * Returns the argument type in "type"
261 * Sets "gen_load_outer" to TRUE if found in outer scope.
262 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 */
264 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200265arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 char_u *name,
267 size_t len,
268 int *idxp,
269 type_T **type,
270 int *gen_load_outer,
271 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272{
273 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200274 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100276 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200277 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200278 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
280 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
281
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200282 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
283 {
284 if (idxp != NULL)
285 {
286 // Arguments are located above the frame pointer. One further
287 // if there is a vararg argument
288 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
289 + STACK_FRAME_SIZE)
290 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
291
292 if (cctx->ctx_ufunc->uf_arg_types != NULL)
293 *type = cctx->ctx_ufunc->uf_arg_types[idx];
294 else
295 *type = &t_any;
296 }
297 return OK;
298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200301 va_name = cctx->ctx_ufunc->uf_va_name;
302 if (va_name != NULL
303 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
304 {
305 if (idxp != NULL)
306 {
307 // varargs is always the last argument
308 *idxp = -STACK_FRAME_SIZE - 1;
309 *type = cctx->ctx_ufunc->uf_va_type;
310 }
311 return OK;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200314 if (cctx->ctx_outer != NULL)
315 {
316 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200317 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200318 == OK)
319 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100320 if (gen_load_outer != NULL)
321 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200322 return OK;
323 }
324 }
325
326 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a script-local variable in the current script, possibly defined in a
331 * block that contains the function "cctx->ctx_ufunc".
332 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100333 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Return NULL when not found.
335 */
336 static sallvar_T *
337find_script_var(char_u *name, size_t len, cctx_T *cctx)
338{
339 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
340 hashitem_T *hi;
341 int cc;
342 sallvar_T *sav;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200343 sallvar_T *found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 ufunc_T *ufunc;
345
346 // Find the list of all script variables with the right name.
347 if (len > 0)
348 {
349 cc = name[len];
350 name[len] = NUL;
351 }
352 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
353 if (len > 0)
354 name[len] = cc;
355 if (HASHITEM_EMPTY(hi))
356 return NULL;
357
358 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200359 if (sav->sav_block_id == 0)
360 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200361 return sav;
362
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200363 if (cctx == NULL)
364 {
365 // Not in a function scope, find variable with block id equal to or
366 // smaller than the current block id.
367 while (sav != NULL)
368 {
369 if (sav->sav_block_id <= si->sn_current_block_id)
370 break;
371 sav = sav->sav_next;
372 }
373 return sav;
374 }
375
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200376 // Go over the variables with this name and find one that was visible
377 // from the function.
378 ufunc = cctx->ctx_ufunc;
Bram Moolenaar88421d62021-07-24 14:14:52 +0200379 found_sav = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200380 while (sav != NULL)
381 {
382 int idx;
383
384 // Go over the blocks that this function was defined in. If the
385 // variable block ID matches it was visible to the function.
386 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
387 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
388 return sav;
389 sav = sav->sav_next;
390 }
391
Bram Moolenaar88421d62021-07-24 14:14:52 +0200392 // Not found, assume variable at script level was visible.
393 return found_sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200394}
395
396/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100397 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200398 */
399 static int
400script_is_vim9()
401{
402 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
403}
404
405/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200406 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200407 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100408 * Returns OK or FAIL.
409 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200410 static int
411script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200413 if (current_sctx.sc_sid <= 0)
414 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200415 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200416 {
417 // Check script variables that were visible where the function was
418 // defined.
419 if (find_script_var(name, len, cctx) != NULL)
420 return OK;
421 }
422 else
423 {
424 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
425 dictitem_T *di;
426 int cc;
427
428 // Check script variables that are currently visible
429 cc = name[len];
430 name[len] = NUL;
431 di = find_var_in_ht(ht, 0, name, TRUE);
432 name[len] = cc;
433 if (di != NULL)
434 return OK;
435 }
436
437 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438}
439
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100440/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100441 * Return TRUE if "name" is a local variable, argument, script variable or
442 * imported.
443 */
444 static int
445variable_exists(char_u *name, size_t len, cctx_T *cctx)
446{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100447 return (cctx != NULL
448 && (lookup_local(name, len, NULL, cctx) == OK
449 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200450 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100451 || find_imported(name, len, cctx) != NULL;
452}
453
454/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100455 * Return TRUE if "name" is a local variable, argument, script variable,
456 * imported or function.
457 */
458 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100459item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100460{
461 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100462 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100463
464 if (variable_exists(name, len, cctx))
465 return TRUE;
466
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100467 // This is similar to what is in lookup_scriptitem():
468 // Find a function, so that a following "->" works.
469 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
470 // a function call.
471 p = skipwhite(name + len);
472
473 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
474 {
475 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200476 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100477 // Skip "g:" before a function name.
478 is_global = (name[0] == 'g' && name[1] == ':');
479 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
480 }
481 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100482}
483
484/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100485 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200486 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200487 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100488 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100489 * Return FAIL and give an error if it defined.
490 */
491 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100492check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100493{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200494 int c = p[len];
495 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200496
Bram Moolenaarda479c72021-04-10 21:01:38 +0200497 // underscore argument is OK
498 if (len == 1 && *p == '_')
499 return OK;
500
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200501 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100502 {
503 if (is_arg)
504 semsg(_(e_argument_already_declared_in_script_str), p);
505 else
506 semsg(_(e_variable_already_declared_in_script_str), p);
507 return FAIL;
508 }
509
Bram Moolenaarad486a02020-08-01 23:22:18 +0200510 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100511 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100512 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200513 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200514 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200515 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100516 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200517 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200518 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
519 && (!func_is_global(ufunc)
520 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200521 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100522 if (is_arg)
523 semsg(_(e_argument_name_shadows_existing_variable_str), p);
524 else
525 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200526 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200527 return FAIL;
528 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100529 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200530 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100531 return OK;
532}
533
Bram Moolenaar65b95452020-07-19 14:03:09 +0200534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535/////////////////////////////////////////////////////////////////////
536// Following generate_ functions expect the caller to call ga_grow().
537
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200538#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
539#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541/*
542 * Generate an instruction without arguments.
543 * Returns a pointer to the new instruction, NULL if failed.
544 */
545 static isn_T *
546generate_instr(cctx_T *cctx, isntype_T isn_type)
547{
548 garray_T *instr = &cctx->ctx_instr;
549 isn_T *isn;
550
Bram Moolenaar080457c2020-03-03 21:53:32 +0100551 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar35578162021-08-02 19:10:38 +0200552 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 return NULL;
554 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
555 isn->isn_type = isn_type;
556 isn->isn_lnum = cctx->ctx_lnum + 1;
557 ++instr->ga_len;
558
559 return isn;
560}
561
562/*
563 * Generate an instruction without arguments.
564 * "drop" will be removed from the stack.
565 * Returns a pointer to the new instruction, NULL if failed.
566 */
567 static isn_T *
568generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
569{
570 garray_T *stack = &cctx->ctx_type_stack;
571
Bram Moolenaar080457c2020-03-03 21:53:32 +0100572 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 stack->ga_len -= drop;
574 return generate_instr(cctx, isn_type);
575}
576
577/*
578 * Generate instruction "isn_type" and put "type" on the type stack.
579 */
580 static isn_T *
581generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
582{
583 isn_T *isn;
584 garray_T *stack = &cctx->ctx_type_stack;
585
586 if ((isn = generate_instr(cctx, isn_type)) == NULL)
587 return NULL;
588
Bram Moolenaar35578162021-08-02 19:10:38 +0200589 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200591 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 ++stack->ga_len;
593
594 return isn;
595}
596
597/*
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200598 * Generate an ISN_DEBUG instruction.
599 */
600 static isn_T *
601generate_instr_debug(cctx_T *cctx)
602{
603 isn_T *isn;
604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
605 + cctx->ctx_ufunc->uf_dfunc_idx;
606
607 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
608 return NULL;
Bram Moolenaar8cec9272021-06-23 20:20:53 +0200609 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
610 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200611 return isn;
612}
613
614/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200616 * But only for simple types.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200617 * When "tolerant" is TRUE convert most types to string, e.g. a List.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 */
619 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200620may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621{
622 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200623 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100625 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100627 RETURN_OK_IF_SKIP(cctx);
628 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200629 switch ((*type)->tt_type)
630 {
631 // nothing to be done
632 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200634 // conversion possible
635 case VAR_SPECIAL:
636 case VAR_BOOL:
637 case VAR_NUMBER:
638 case VAR_FLOAT:
639 break;
640
641 // conversion possible (with runtime check)
642 case VAR_ANY:
643 case VAR_UNKNOWN:
644 isntype = ISN_2STRING_ANY;
645 break;
646
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200647 // conversion possible when tolerant
648 case VAR_LIST:
649 if (tolerant)
650 {
651 isntype = ISN_2STRING_ANY;
652 break;
653 }
654 // FALLTHROUGH
655
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200656 // conversion not possible
657 case VAR_VOID:
658 case VAR_BLOB:
659 case VAR_FUNC:
660 case VAR_PARTIAL:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200661 case VAR_DICT:
662 case VAR_JOB:
663 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200664 case VAR_INSTR:
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200665 to_string_error((*type)->tt_type);
666 return FAIL;
667 }
668
669 *type = &t_string;
670 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200672 isn->isn_arg.tostring.offset = offset;
673 isn->isn_arg.tostring.tolerant = tolerant;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674
675 return OK;
676}
677
678 static int
679check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
680{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200681 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200683 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 {
685 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200686 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100687 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200688 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 return FAIL;
690 }
691 return OK;
692}
693
Bram Moolenaar07802042021-09-09 23:01:14 +0200694/*
695 * Generate instruction for "+". For a list this creates a new list.
696 */
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200697 static int
698generate_add_instr(
699 cctx_T *cctx,
700 vartype_T vartype,
701 type_T *type1,
Bram Moolenaar07802042021-09-09 23:01:14 +0200702 type_T *type2,
703 exprtype_T expr_type)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200704{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100705 garray_T *stack = &cctx->ctx_type_stack;
706 isn_T *isn = generate_instr_drop(cctx,
707 vartype == VAR_NUMBER ? ISN_OPNR
708 : vartype == VAR_LIST ? ISN_ADDLIST
709 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200710#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100711 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200712#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100713 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200714
715 if (vartype != VAR_LIST && vartype != VAR_BLOB
716 && type1->tt_type != VAR_ANY
717 && type2->tt_type != VAR_ANY
718 && check_number_or_float(
719 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
720 return FAIL;
721
722 if (isn != NULL)
Bram Moolenaar07802042021-09-09 23:01:14 +0200723 {
724 if (isn->isn_type == ISN_ADDLIST)
725 isn->isn_arg.op.op_type = expr_type;
726 else
727 isn->isn_arg.op.op_type = EXPR_ADD;
728 }
Bram Moolenaar399ea812020-12-15 21:28:57 +0100729
730 // When concatenating two lists with different member types the member type
731 // becomes "any".
732 if (vartype == VAR_LIST
733 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
734 && type1->tt_member != type2->tt_member)
735 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
736
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200737 return isn == NULL ? FAIL : OK;
738}
739
740/*
741 * Get the type to use for an instruction for an operation on "type1" and
742 * "type2". If they are matching use a type-specific instruction. Otherwise
743 * fall back to runtime type checking.
744 */
745 static vartype_T
746operator_type(type_T *type1, type_T *type2)
747{
748 if (type1->tt_type == type2->tt_type
749 && (type1->tt_type == VAR_NUMBER
750 || type1->tt_type == VAR_LIST
751#ifdef FEAT_FLOAT
752 || type1->tt_type == VAR_FLOAT
753#endif
754 || type1->tt_type == VAR_BLOB))
755 return type1->tt_type;
756 return VAR_ANY;
757}
758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759/*
760 * Generate an instruction with two arguments. The instruction depends on the
761 * type of the arguments.
762 */
763 static int
764generate_two_op(cctx_T *cctx, char_u *op)
765{
766 garray_T *stack = &cctx->ctx_type_stack;
767 type_T *type1;
768 type_T *type2;
769 vartype_T vartype;
770 isn_T *isn;
771
Bram Moolenaar080457c2020-03-03 21:53:32 +0100772 RETURN_OK_IF_SKIP(cctx);
773
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200774 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
776 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200777 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778
779 switch (*op)
780 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200781 case '+':
Bram Moolenaar07802042021-09-09 23:01:14 +0200782 if (generate_add_instr(cctx, vartype, type1, type2,
783 EXPR_COPY) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 break;
786
787 case '-':
788 case '*':
789 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
790 op) == FAIL)
791 return FAIL;
792 if (vartype == VAR_NUMBER)
793 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
794#ifdef FEAT_FLOAT
795 else if (vartype == VAR_FLOAT)
796 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
797#endif
798 else
799 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
800 if (isn != NULL)
801 isn->isn_arg.op.op_type = *op == '*'
802 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
803 break;
804
Bram Moolenaar4c683752020-04-05 21:38:23 +0200805 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200807 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 && type2->tt_type != VAR_NUMBER))
809 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200810 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 return FAIL;
812 }
813 isn = generate_instr_drop(cctx,
814 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
815 if (isn != NULL)
816 isn->isn_arg.op.op_type = EXPR_REM;
817 break;
818 }
819
820 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200821 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 {
823 type_T *type = &t_any;
824
825#ifdef FEAT_FLOAT
826 // float+number and number+float results in float
827 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
828 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
829 type = &t_float;
830#endif
831 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
832 }
833
834 return OK;
835}
836
837/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200838 * Get the instruction to use for comparing "type1" with "type2"
839 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200841 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100842get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843{
844 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845
Bram Moolenaar4c683752020-04-05 21:38:23 +0200846 if (type1 == VAR_UNKNOWN)
847 type1 = VAR_ANY;
848 if (type2 == VAR_UNKNOWN)
849 type2 = VAR_ANY;
850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 if (type1 == type2)
852 {
853 switch (type1)
854 {
855 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
856 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
857 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
858 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
859 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
860 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
861 case VAR_LIST: isntype = ISN_COMPARELIST; break;
862 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
863 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 default: isntype = ISN_COMPAREANY; break;
865 }
866 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200867 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100869 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 isntype = ISN_COMPAREANY;
871
Bram Moolenaar657137c2021-01-09 15:45:23 +0100872 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 && (isntype == ISN_COMPAREBOOL
874 || isntype == ISN_COMPARESPECIAL
875 || isntype == ISN_COMPARENR
876 || isntype == ISN_COMPAREFLOAT))
877 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200878 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100879 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200880 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 }
882 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100883 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
885 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100886 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
887 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 && (type1 == VAR_BLOB || type2 == VAR_BLOB
889 || type1 == VAR_LIST || type2 == VAR_LIST))))
890 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200891 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200893 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200895 return isntype;
896}
897
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200898 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100899check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200900{
901 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
902 return FAIL;
903 return OK;
904}
905
Bram Moolenaara5565e42020-05-09 15:44:01 +0200906/*
907 * Generate an ISN_COMPARE* instruction with a boolean result.
908 */
909 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100910generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200911{
912 isntype_T isntype;
913 isn_T *isn;
914 garray_T *stack = &cctx->ctx_type_stack;
915 vartype_T type1;
916 vartype_T type2;
917
918 RETURN_OK_IF_SKIP(cctx);
919
920 // Get the known type of the two items on the stack. If they are matching
921 // use a type-specific instruction. Otherwise fall back to runtime type
922 // checking.
923 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
924 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100925 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200926 if (isntype == ISN_DROP)
927 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928
929 if ((isn = generate_instr(cctx, isntype)) == NULL)
930 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100931 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 isn->isn_arg.op.op_ic = ic;
933
934 // takes two arguments, puts one bool back
935 if (stack->ga_len >= 2)
936 {
937 --stack->ga_len;
938 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
939 }
940
941 return OK;
942}
943
944/*
945 * Generate an ISN_2BOOL instruction.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200946 * "offset" is the offset in the type stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 */
948 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200949generate_2BOOL(cctx_T *cctx, int invert, int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950{
951 isn_T *isn;
952 garray_T *stack = &cctx->ctx_type_stack;
953
Bram Moolenaar080457c2020-03-03 21:53:32 +0100954 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
956 return FAIL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200957 isn->isn_arg.tobool.invert = invert;
958 isn->isn_arg.tobool.offset = offset;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 // type becomes bool
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200961 ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962
963 return OK;
964}
965
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200966/*
967 * Generate an ISN_COND2BOOL instruction.
968 */
969 static int
970generate_COND2BOOL(cctx_T *cctx)
971{
972 isn_T *isn;
973 garray_T *stack = &cctx->ctx_type_stack;
974
975 RETURN_OK_IF_SKIP(cctx);
976 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
977 return FAIL;
978
979 // type becomes bool
980 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
981
982 return OK;
983}
984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200986generate_TYPECHECK(
987 cctx_T *cctx,
988 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100989 int offset,
990 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991{
992 isn_T *isn;
993 garray_T *stack = &cctx->ctx_type_stack;
994
Bram Moolenaar080457c2020-03-03 21:53:32 +0100995 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
997 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200998 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100999 isn->isn_arg.type.ct_off = (int8_T)offset;
1000 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001
Bram Moolenaar5e654232020-09-16 15:22:00 +02001002 // type becomes expected
1003 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
1005 return OK;
1006}
1007
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001008 static int
1009generate_SETTYPE(
1010 cctx_T *cctx,
1011 type_T *expected)
1012{
1013 isn_T *isn;
1014
1015 RETURN_OK_IF_SKIP(cctx);
1016 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
1017 return FAIL;
1018 isn->isn_arg.type.ct_type = alloc_type(expected);
1019 return OK;
1020}
1021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001023 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
1024 * used. Return FALSE if the types will never match.
1025 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +02001026 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001027use_typecheck(type_T *actual, type_T *expected)
1028{
1029 if (actual->tt_type == VAR_ANY
1030 || actual->tt_type == VAR_UNKNOWN
1031 || (actual->tt_type == VAR_FUNC
1032 && (expected->tt_type == VAR_FUNC
1033 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +01001034 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
1035 && ((actual->tt_member == &t_void)
1036 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001037 return TRUE;
1038 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1039 && actual->tt_type == expected->tt_type)
1040 // This takes care of a nested list or dict.
1041 return use_typecheck(actual->tt_member, expected->tt_member);
1042 return FALSE;
1043}
1044
1045/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001046 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +02001047 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001048 * - "actual" is a type that can be "expected" type: add a runtime check; or
1049 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001050 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1051 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001052 */
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001053 static int
1054need_type_where(
1055 type_T *actual,
1056 type_T *expected,
1057 int offset,
1058 where_T where,
1059 cctx_T *cctx,
1060 int silent,
1061 int actual_is_const)
1062{
1063 if (expected == &t_bool && actual != &t_bool
1064 && (actual->tt_flags & TTFLAG_BOOL_OK))
1065 {
1066 // Using "0", "1" or the result of an expression with "&&" or "||" as a
1067 // boolean is OK but requires a conversion.
1068 generate_2BOOL(cctx, FALSE, offset);
1069 return OK;
1070 }
1071
1072 if (check_type(expected, actual, FALSE, where) == OK)
1073 return OK;
1074
1075 // If the actual type can be the expected type add a runtime check.
1076 // If it's a constant a runtime check makes no sense.
1077 if ((!actual_is_const || actual == &t_any)
1078 && use_typecheck(actual, expected))
1079 {
1080 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
1081 return OK;
1082 }
1083
1084 if (!silent)
1085 type_mismatch_where(expected, actual, where);
1086 return FAIL;
1087}
1088
Bram Moolenaar351ead02021-01-16 16:07:01 +01001089 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001090need_type(
1091 type_T *actual,
1092 type_T *expected,
1093 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +01001094 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001095 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001096 int silent,
1097 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001098{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001099 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001100
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001101 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02001102 return need_type_where(actual, expected, offset, where,
1103 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001104}
1105
1106/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001107 * Check that the top of the type stack has a type that can be used as a
1108 * condition. Give an error and return FAIL if not.
1109 */
1110 static int
1111bool_on_stack(cctx_T *cctx)
1112{
1113 garray_T *stack = &cctx->ctx_type_stack;
1114 type_T *type;
1115
1116 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1117 if (type == &t_bool)
1118 return OK;
1119
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +02001120 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001121 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1122 // This requires a runtime type check.
1123 return generate_COND2BOOL(cctx);
1124
Bram Moolenaar351ead02021-01-16 16:07:01 +01001125 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001126}
1127
1128/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 * Generate an ISN_PUSHNR instruction.
1130 */
1131 static int
1132generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1133{
1134 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001135 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136
Bram Moolenaar080457c2020-03-03 21:53:32 +01001137 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.number = number;
1141
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001142 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001143 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001144 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 return OK;
1146}
1147
1148/*
1149 * Generate an ISN_PUSHBOOL instruction.
1150 */
1151 static int
1152generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1153{
1154 isn_T *isn;
1155
Bram Moolenaar080457c2020-03-03 21:53:32 +01001156 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1158 return FAIL;
1159 isn->isn_arg.number = number;
1160
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_PUSHSPEC instruction.
1166 */
1167 static int
1168generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1169{
1170 isn_T *isn;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.number = number;
1176
1177 return OK;
1178}
1179
1180#ifdef FEAT_FLOAT
1181/*
1182 * Generate an ISN_PUSHF instruction.
1183 */
1184 static int
1185generate_PUSHF(cctx_T *cctx, float_T fnumber)
1186{
1187 isn_T *isn;
1188
Bram Moolenaar080457c2020-03-03 21:53:32 +01001189 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1191 return FAIL;
1192 isn->isn_arg.fnumber = fnumber;
1193
1194 return OK;
1195}
1196#endif
1197
1198/*
1199 * Generate an ISN_PUSHS instruction.
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001200 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 */
1202 static int
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001203generate_PUSHS(cctx_T *cctx, char_u **str)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204{
1205 isn_T *isn;
1206
Bram Moolenaar508b5612021-01-02 18:17:26 +01001207 if (cctx->ctx_skip == SKIP_YES)
1208 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001209 if (str != NULL)
1210 VIM_CLEAR(*str);
Bram Moolenaar508b5612021-01-02 18:17:26 +01001211 return OK;
1212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001214 {
1215 if (str != NULL)
1216 VIM_CLEAR(*str);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001218 }
1219 isn->isn_arg.string = str == NULL ? NULL : *str;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001225 * Generate an ISN_PUSHCHANNEL instruction.
1226 * Consumes "channel".
1227 */
1228 static int
1229generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1230{
1231 isn_T *isn;
1232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001234 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1235 return FAIL;
1236 isn->isn_arg.channel = channel;
1237
1238 return OK;
1239}
1240
1241/*
1242 * Generate an ISN_PUSHJOB instruction.
1243 * Consumes "job".
1244 */
1245 static int
1246generate_PUSHJOB(cctx_T *cctx, job_T *job)
1247{
1248 isn_T *isn;
1249
Bram Moolenaar080457c2020-03-03 21:53:32 +01001250 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001251 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001252 return FAIL;
1253 isn->isn_arg.job = job;
1254
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 * Generate an ISN_PUSHBLOB instruction.
1260 * Consumes "blob".
1261 */
1262 static int
1263generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1264{
1265 isn_T *isn;
1266
Bram Moolenaar080457c2020-03-03 21:53:32 +01001267 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1269 return FAIL;
1270 isn->isn_arg.blob = blob;
1271
1272 return OK;
1273}
1274
1275/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001276 * Generate an ISN_PUSHFUNC instruction with name "name".
1277 * Consumes "name".
1278 */
1279 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001280generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001281{
1282 isn_T *isn;
1283
Bram Moolenaar080457c2020-03-03 21:53:32 +01001284 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001285 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001286 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001287 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001288
1289 return OK;
1290}
1291
1292/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001293 * Generate an ISN_GETITEM instruction with "index".
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001294 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1295 * value below the list with values.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001296 */
1297 static int
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001298generate_GETITEM(cctx_T *cctx, int index, int with_op)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001299{
1300 isn_T *isn;
1301 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001302 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
1303 - (with_op ? 2 : 1)];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001304 type_T *item_type = &t_any;
1305
1306 RETURN_OK_IF_SKIP(cctx);
1307
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001308 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001309 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001310 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001311 emsg(_(e_listreq));
1312 return FAIL;
1313 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001314 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001315 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1316 return FAIL;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02001317 isn->isn_arg.getitem.gi_index = index;
1318 isn->isn_arg.getitem.gi_with_op = with_op;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001319
1320 // add the item type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001321 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001322 return FAIL;
1323 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1324 ++stack->ga_len;
1325 return OK;
1326}
1327
1328/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001329 * Generate an ISN_SLICE instruction with "count".
1330 */
1331 static int
1332generate_SLICE(cctx_T *cctx, int count)
1333{
1334 isn_T *isn;
1335
1336 RETURN_OK_IF_SKIP(cctx);
1337 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1338 return FAIL;
1339 isn->isn_arg.number = count;
1340 return OK;
1341}
1342
1343/*
1344 * Generate an ISN_CHECKLEN instruction with "min_len".
1345 */
1346 static int
1347generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1348{
1349 isn_T *isn;
1350
1351 RETURN_OK_IF_SKIP(cctx);
1352
1353 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1354 return FAIL;
1355 isn->isn_arg.checklen.cl_min_len = min_len;
1356 isn->isn_arg.checklen.cl_more_OK = more_OK;
1357
1358 return OK;
1359}
1360
1361/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 * Generate an ISN_STORE instruction.
1363 */
1364 static int
1365generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1366{
1367 isn_T *isn;
1368
Bram Moolenaar080457c2020-03-03 21:53:32 +01001369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1371 return FAIL;
1372 if (name != NULL)
1373 isn->isn_arg.string = vim_strsave(name);
1374 else
1375 isn->isn_arg.number = idx;
1376
1377 return OK;
1378}
1379
1380/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001381 * Generate an ISN_STOREOUTER instruction.
1382 */
1383 static int
1384generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1385{
1386 isn_T *isn;
1387
1388 RETURN_OK_IF_SKIP(cctx);
1389 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1390 return FAIL;
1391 isn->isn_arg.outer.outer_idx = idx;
1392 isn->isn_arg.outer.outer_depth = level;
1393
1394 return OK;
1395}
1396
1397/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1399 */
1400 static int
1401generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1407 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001408 isn->isn_arg.storenr.stnr_idx = idx;
1409 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410
1411 return OK;
1412}
1413
1414/*
1415 * Generate an ISN_STOREOPT instruction
1416 */
1417 static int
1418generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1419{
1420 isn_T *isn;
1421
Bram Moolenaar080457c2020-03-03 21:53:32 +01001422 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001423 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 return FAIL;
1425 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1426 isn->isn_arg.storeopt.so_flags = opt_flags;
1427
1428 return OK;
1429}
1430
1431/*
1432 * Generate an ISN_LOAD or similar instruction.
1433 */
1434 static int
1435generate_LOAD(
1436 cctx_T *cctx,
1437 isntype_T isn_type,
1438 int idx,
1439 char_u *name,
1440 type_T *type)
1441{
1442 isn_T *isn;
1443
Bram Moolenaar080457c2020-03-03 21:53:32 +01001444 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1446 return FAIL;
1447 if (name != NULL)
1448 isn->isn_arg.string = vim_strsave(name);
1449 else
1450 isn->isn_arg.number = idx;
1451
1452 return OK;
1453}
1454
1455/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001456 * Generate an ISN_LOADOUTER instruction
1457 */
1458 static int
1459generate_LOADOUTER(
1460 cctx_T *cctx,
1461 int idx,
1462 int nesting,
1463 type_T *type)
1464{
1465 isn_T *isn;
1466
1467 RETURN_OK_IF_SKIP(cctx);
1468 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1469 return FAIL;
1470 isn->isn_arg.outer.outer_idx = idx;
1471 isn->isn_arg.outer.outer_depth = nesting;
1472
1473 return OK;
1474}
1475
1476/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001477 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001478 */
1479 static int
1480generate_LOADV(
1481 cctx_T *cctx,
1482 char_u *name,
1483 int error)
1484{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001485 int di_flags;
1486 int vidx = find_vim_var(name, &di_flags);
1487 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001488
Bram Moolenaar080457c2020-03-03 21:53:32 +01001489 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001490 if (vidx < 0)
1491 {
1492 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001493 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001494 return FAIL;
1495 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001496 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001497
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001498 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001499}
1500
1501/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001502 * Generate an ISN_UNLET instruction.
1503 */
1504 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001505generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001506{
1507 isn_T *isn;
1508
1509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001510 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001511 return FAIL;
1512 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1513 isn->isn_arg.unlet.ul_forceit = forceit;
1514
1515 return OK;
1516}
1517
1518/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001519 * Generate an ISN_LOCKCONST instruction.
1520 */
1521 static int
1522generate_LOCKCONST(cctx_T *cctx)
1523{
1524 isn_T *isn;
1525
1526 RETURN_OK_IF_SKIP(cctx);
1527 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1528 return FAIL;
1529 return OK;
1530}
1531
1532/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 * Generate an ISN_LOADS instruction.
1534 */
1535 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001536generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001538 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001540 int sid,
1541 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542{
1543 isn_T *isn;
1544
Bram Moolenaar080457c2020-03-03 21:53:32 +01001545 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001546 if (isn_type == ISN_LOADS)
1547 isn = generate_instr_type(cctx, isn_type, type);
1548 else
1549 isn = generate_instr_drop(cctx, isn_type, 1);
1550 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001552 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1553 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554
1555 return OK;
1556}
1557
1558/*
1559 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1560 */
1561 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001562generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 cctx_T *cctx,
1564 isntype_T isn_type,
1565 int sid,
1566 int idx,
1567 type_T *type)
1568{
1569 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001570 scriptref_T *sref;
1571 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572
Bram Moolenaar080457c2020-03-03 21:53:32 +01001573 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 if (isn_type == ISN_LOADSCRIPT)
1575 isn = generate_instr_type(cctx, isn_type, type);
1576 else
1577 isn = generate_instr_drop(cctx, isn_type, 1);
1578 if (isn == NULL)
1579 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001580
1581 // This requires three arguments, which doesn't fit in an instruction, thus
1582 // we need to allocate a struct for this.
1583 sref = ALLOC_ONE(scriptref_T);
1584 if (sref == NULL)
1585 return FAIL;
1586 isn->isn_arg.script.scriptref = sref;
1587 sref->sref_sid = sid;
1588 sref->sref_idx = idx;
1589 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001590 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 return OK;
1592}
1593
1594/*
1595 * Generate an ISN_NEWLIST instruction.
1596 */
1597 static int
1598generate_NEWLIST(cctx_T *cctx, int count)
1599{
1600 isn_T *isn;
1601 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 type_T *type;
1603 type_T *member;
1604
Bram Moolenaar080457c2020-03-03 21:53:32 +01001605 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1607 return FAIL;
1608 isn->isn_arg.number = count;
1609
Bram Moolenaar127542b2020-08-09 17:22:04 +02001610 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001611 if (count == 0)
Bram Moolenaar6e48b842021-08-10 22:52:02 +02001612 member = &t_unknown;
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001613 else
1614 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001615 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1616 cctx->ctx_type_list);
1617 type = get_list_type(member, cctx->ctx_type_list);
1618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 // drop the value types
1620 stack->ga_len -= count;
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 // add the list type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001623 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 return FAIL;
1625 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1626 ++stack->ga_len;
1627
1628 return OK;
1629}
1630
1631/*
1632 * Generate an ISN_NEWDICT instruction.
1633 */
1634 static int
1635generate_NEWDICT(cctx_T *cctx, int count)
1636{
1637 isn_T *isn;
1638 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 type_T *type;
1640 type_T *member;
1641
Bram Moolenaar080457c2020-03-03 21:53:32 +01001642 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1644 return FAIL;
1645 isn->isn_arg.number = count;
1646
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001647 if (count == 0)
1648 member = &t_void;
1649 else
1650 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001651 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1652 cctx->ctx_type_list);
1653 type = get_dict_type(member, cctx->ctx_type_list);
1654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 // drop the key and value types
1656 stack->ga_len -= 2 * count;
1657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 // add the dict type to the type stack
Bram Moolenaar35578162021-08-02 19:10:38 +02001659 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 return FAIL;
1661 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1662 ++stack->ga_len;
1663
1664 return OK;
1665}
1666
1667/*
1668 * Generate an ISN_FUNCREF instruction.
1669 */
1670 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001671generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672{
1673 isn_T *isn;
1674 garray_T *stack = &cctx->ctx_type_stack;
1675
Bram Moolenaar080457c2020-03-03 21:53:32 +01001676 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1678 return FAIL;
Bram Moolenaar38453522021-11-28 22:00:12 +00001679 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1680 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1681 else
1682 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001683 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001685 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001686 // the nested context, including this one.
1687 if (ufunc->uf_flags & FC_CLOSURE)
1688 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1689
Bram Moolenaar35578162021-08-02 19:10:38 +02001690 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001692 ((type_T **)stack->ga_data)[stack->ga_len] =
1693 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 ++stack->ga_len;
1695
1696 return OK;
1697}
1698
1699/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001700 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001701 * "lambda_name" and "func_name" must be in allocated memory and will be
1702 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001703 */
1704 static int
1705generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1706{
1707 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001708
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001709 if (cctx->ctx_skip == SKIP_YES)
1710 {
1711 vim_free(lambda_name);
1712 vim_free(func_name);
1713 return OK;
1714 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001715 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001716 {
1717 vim_free(lambda_name);
1718 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001719 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001720 }
1721 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001722 isn->isn_arg.newfunc.nf_global = func_name;
1723
1724 return OK;
1725}
1726
1727/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001728 * Generate an ISN_DEF instruction: list functions
1729 */
1730 static int
1731generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1732{
1733 isn_T *isn;
1734
1735 RETURN_OK_IF_SKIP(cctx);
1736 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1737 return FAIL;
1738 if (len > 0)
1739 {
1740 isn->isn_arg.string = vim_strnsave(name, len);
1741 if (isn->isn_arg.string == NULL)
1742 return FAIL;
1743 }
1744 return OK;
1745}
1746
1747/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 * Generate an ISN_JUMP instruction.
1749 */
1750 static int
1751generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1752{
1753 isn_T *isn;
1754 garray_T *stack = &cctx->ctx_type_stack;
1755
Bram Moolenaar080457c2020-03-03 21:53:32 +01001756 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.jump.jump_when = when;
1760 isn->isn_arg.jump.jump_where = where;
1761
1762 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1763 --stack->ga_len;
1764
1765 return OK;
1766}
1767
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001768/*
1769 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1770 */
1771 static int
1772generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1773{
1774 isn_T *isn;
1775
1776 RETURN_OK_IF_SKIP(cctx);
1777 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1778 return FAIL;
1779 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1780 // jump_where is set later
1781 return OK;
1782}
1783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 static int
1785generate_FOR(cctx_T *cctx, int loop_idx)
1786{
1787 isn_T *isn;
1788 garray_T *stack = &cctx->ctx_type_stack;
1789
Bram Moolenaar080457c2020-03-03 21:53:32 +01001790 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1792 return FAIL;
1793 isn->isn_arg.forloop.for_idx = loop_idx;
1794
Bram Moolenaar35578162021-08-02 19:10:38 +02001795 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 return FAIL;
1797 // type doesn't matter, will be stored next
1798 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1799 ++stack->ga_len;
1800
1801 return OK;
1802}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001803/*
1804 * Generate an ISN_TRYCONT instruction.
1805 */
1806 static int
1807generate_TRYCONT(cctx_T *cctx, int levels, int where)
1808{
1809 isn_T *isn;
1810
1811 RETURN_OK_IF_SKIP(cctx);
1812 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1813 return FAIL;
1814 isn->isn_arg.trycont.tct_levels = levels;
1815 isn->isn_arg.trycont.tct_where = where;
1816
1817 return OK;
1818}
1819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
1821/*
1822 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001823 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 * Return FAIL if the number of arguments is wrong.
1825 */
1826 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001827generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828{
1829 isn_T *isn;
1830 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001831 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001832 type_T **argtypes = NULL;
Bram Moolenaar52312242021-07-11 18:23:19 +02001833 type_T *shuffled_argtypes[MAX_FUNC_ARGS];
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001834 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835
Bram Moolenaar080457c2020-03-03 21:53:32 +01001836 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001837 argoff = check_internal_func(func_idx, argcount);
1838 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 return FAIL;
1840
Bram Moolenaar389df252020-07-09 21:20:47 +02001841 if (method_call && argoff > 1)
1842 {
1843 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1844 return FAIL;
1845 isn->isn_arg.shuffle.shfl_item = argcount;
1846 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1847 }
1848
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001849 if (argcount > 0)
1850 {
1851 // Check the types of the arguments.
1852 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar52312242021-07-11 18:23:19 +02001853 if (method_call && argoff > 1)
1854 {
1855 int i;
1856
1857 for (i = 0; i < argcount; ++i)
1858 shuffled_argtypes[i] = (i < argoff - 1)
1859 ? argtypes[i + 1]
1860 : (i == argoff - 1) ? argtypes[0] : argtypes[i];
1861 argtypes = shuffled_argtypes;
1862 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01001863 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1864 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001865 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001866 if (internal_func_is_map(func_idx))
1867 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001868 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1871 return FAIL;
1872 isn->isn_arg.bfunc.cbf_idx = func_idx;
1873 isn->isn_arg.bfunc.cbf_argcount = argcount;
1874
Bram Moolenaar94738d82020-10-21 14:25:07 +02001875 // Drop the argument types and push the return type.
1876 stack->ga_len -= argcount;
Bram Moolenaar35578162021-08-02 19:10:38 +02001877 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 return FAIL;
1879 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001880 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001881 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001883 if (maptype != NULL && maptype->tt_member != NULL
1884 && maptype->tt_member != &t_any)
1885 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001886 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 return OK;
1889}
1890
1891/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001892 * Generate an ISN_LISTAPPEND instruction. Works like add().
1893 * Argument count is already checked.
1894 */
1895 static int
1896generate_LISTAPPEND(cctx_T *cctx)
1897{
1898 garray_T *stack = &cctx->ctx_type_stack;
1899 type_T *list_type;
1900 type_T *item_type;
1901 type_T *expected;
1902
1903 // Caller already checked that list_type is a list.
1904 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1905 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1906 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001907 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001908 return FAIL;
1909
1910 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1911 return FAIL;
1912
1913 --stack->ga_len; // drop the argument
1914 return OK;
1915}
1916
1917/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001918 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1919 * Argument count is already checked.
1920 */
1921 static int
1922generate_BLOBAPPEND(cctx_T *cctx)
1923{
1924 garray_T *stack = &cctx->ctx_type_stack;
1925 type_T *item_type;
1926
1927 // Caller already checked that blob_type is a blob.
1928 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001929 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001930 return FAIL;
1931
1932 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1933 return FAIL;
1934
1935 --stack->ga_len; // drop the argument
1936 return OK;
1937}
1938
1939/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001940 * Return TRUE if "ufunc" should be compiled, taking into account whether
1941 * "profile" indicates profiling is to be done.
1942 */
1943 int
Bram Moolenaare99d4222021-06-13 14:01:26 +02001944func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001945{
1946 switch (ufunc->uf_def_status)
1947 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001948 case UF_TO_BE_COMPILED:
1949 return TRUE;
1950
Bram Moolenaarb2049902021-01-24 12:53:53 +01001951 case UF_COMPILED:
1952 {
1953 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1954 + ufunc->uf_dfunc_idx;
1955
Bram Moolenaare99d4222021-06-13 14:01:26 +02001956 switch (compile_type)
1957 {
Bram Moolenaard9f31c12021-06-13 14:15:29 +02001958 case CT_PROFILE:
1959#ifdef FEAT_PROFILE
1960 return dfunc->df_instr_prof == NULL;
1961#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02001962 case CT_NONE:
1963 return dfunc->df_instr == NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02001964 case CT_DEBUG:
1965 return dfunc->df_instr_debug == NULL;
1966 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001967 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001968
1969 case UF_NOT_COMPILED:
1970 case UF_COMPILE_ERROR:
1971 case UF_COMPILING:
1972 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001973 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001974 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001975}
1976
1977/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 * Generate an ISN_DCALL or ISN_UCALL instruction.
1979 * Return FAIL if the number of arguments is wrong.
1980 */
1981 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001982generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983{
1984 isn_T *isn;
1985 garray_T *stack = &cctx->ctx_type_stack;
1986 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001987 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988
Bram Moolenaar080457c2020-03-03 21:53:32 +01001989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 if (argcount > regular_args && !has_varargs(ufunc))
1991 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001992 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 return FAIL;
1994 }
1995 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1996 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001997 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return FAIL;
1999 }
2000
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02002001 if (ufunc->uf_def_status != UF_NOT_COMPILED
2002 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002003 {
2004 int i;
2005
2006 for (i = 0; i < argcount; ++i)
2007 {
2008 type_T *expected;
2009 type_T *actual;
2010
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002011 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
2012 if (actual == &t_special
2013 && i >= regular_args - ufunc->uf_def_args.ga_len)
2014 {
2015 // assume v:none used for default argument value
2016 continue;
2017 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002018 if (i < regular_args)
2019 {
2020 if (ufunc->uf_arg_types == NULL)
2021 continue;
2022 expected = ufunc->uf_arg_types[i];
2023 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02002024 else if (ufunc->uf_va_type == NULL
2025 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02002026 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02002027 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002028 else
2029 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01002030 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002031 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002032 {
2033 arg_type_mismatch(expected, actual, i + 1);
2034 return FAIL;
2035 }
2036 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02002037 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01002038 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare99d4222021-06-13 14:01:26 +02002039 COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002040 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002041 }
Bram Moolenaarb55d6182021-06-08 22:01:53 +02002042 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
2043 {
2044 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
2045 ufunc->uf_name);
2046 return FAIL;
2047 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002050 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002051 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02002053 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 {
2055 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
2056 isn->isn_arg.dfunc.cdf_argcount = argcount;
2057 }
2058 else
2059 {
2060 // A user function may be deleted and redefined later, can't use the
2061 // ufunc pointer, need to look it up again at runtime.
2062 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2063 isn->isn_arg.ufunc.cuf_argcount = argcount;
2064 }
2065
2066 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002067 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 return FAIL;
2069 // add return value
2070 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2071 ++stack->ga_len;
2072
2073 return OK;
2074}
2075
2076/*
2077 * Generate an ISN_UCALL instruction when the function isn't defined yet.
2078 */
2079 static int
2080generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2081{
2082 isn_T *isn;
2083 garray_T *stack = &cctx->ctx_type_stack;
2084
Bram Moolenaar080457c2020-03-03 21:53:32 +01002085 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2087 return FAIL;
2088 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2089 isn->isn_arg.ufunc.cuf_argcount = argcount;
2090
2091 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar35578162021-08-02 19:10:38 +02002092 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar26e117e2020-02-04 21:24:15 +01002093 return FAIL;
2094 // add return value
2095 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2096 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097
2098 return OK;
2099}
2100
2101/*
2102 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002103 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 */
2105 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002106generate_PCALL(
2107 cctx_T *cctx,
2108 int argcount,
2109 char_u *name,
2110 type_T *type,
2111 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112{
2113 isn_T *isn;
2114 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002115 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116
Bram Moolenaar080457c2020-03-03 21:53:32 +01002117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002118
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002119 if (type->tt_type == VAR_ANY)
2120 ret_type = &t_any;
2121 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002122 {
2123 if (type->tt_argcount != -1)
2124 {
2125 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2126
2127 if (argcount < type->tt_min_argcount - varargs)
2128 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002129 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002130 return FAIL;
2131 }
2132 if (!varargs && argcount > type->tt_argcount)
2133 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01002134 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002135 return FAIL;
2136 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002137 if (type->tt_args != NULL)
2138 {
2139 int i;
2140
2141 for (i = 0; i < argcount; ++i)
2142 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02002143 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002144 type_T *actual = ((type_T **)stack->ga_data)[
2145 stack->ga_len + offset];
2146 type_T *expected;
2147
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002148 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002149 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002150 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002151 else if (i >= type->tt_min_argcount
2152 && actual == &t_special)
2153 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002154 else
2155 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01002156 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002157 cctx, TRUE, FALSE) == FAIL)
2158 {
2159 arg_type_mismatch(expected, actual, i + 1);
2160 return FAIL;
2161 }
2162 }
2163 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002164 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002165 ret_type = type->tt_member;
Bram Moolenaarf7237012021-07-27 22:21:44 +02002166 if (ret_type == &t_unknown)
2167 // return type not known yet, use a runtime check
2168 ret_type = &t_any;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002169 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002170 else
2171 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002172 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002173 return FAIL;
2174 }
2175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2177 return FAIL;
2178 isn->isn_arg.pfunc.cpf_top = at_top;
2179 isn->isn_arg.pfunc.cpf_argcount = argcount;
2180
2181 stack->ga_len -= argcount; // drop the arguments
2182
2183 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002184 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002186 // If partial is above the arguments it must be cleared and replaced with
2187 // the return value.
2188 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2189 return FAIL;
2190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 return OK;
2192}
2193
2194/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002195 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 */
2197 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002198generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199{
2200 isn_T *isn;
2201 garray_T *stack = &cctx->ctx_type_stack;
2202 type_T *type;
2203
Bram Moolenaar080457c2020-03-03 21:53:32 +01002204 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002205 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002207 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002209 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002211 if (type->tt_type != VAR_DICT && type != &t_any)
2212 {
Bram Moolenaard47c3972021-07-28 20:52:13 +02002213 char *tofree;
2214
2215 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2216 name, type_name(type, &tofree));
2217 vim_free(tofree);
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002218 return FAIL;
2219 }
2220 // change dict type to dict member type
2221 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002222 {
2223 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2224 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226
2227 return OK;
2228}
2229
2230/*
2231 * Generate an ISN_ECHO instruction.
2232 */
2233 static int
2234generate_ECHO(cctx_T *cctx, int with_white, int count)
2235{
2236 isn_T *isn;
2237
Bram Moolenaar080457c2020-03-03 21:53:32 +01002238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2240 return FAIL;
2241 isn->isn_arg.echo.echo_with_white = with_white;
2242 isn->isn_arg.echo.echo_count = count;
2243
2244 return OK;
2245}
2246
Bram Moolenaarad39c092020-02-26 18:23:43 +01002247/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002248 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002249 */
2250 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002251generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002252{
2253 isn_T *isn;
2254
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002255 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002256 return FAIL;
2257 isn->isn_arg.number = count;
2258
2259 return OK;
2260}
2261
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002262/*
2263 * Generate an ISN_PUT instruction.
2264 */
2265 static int
2266generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2267{
2268 isn_T *isn;
2269
2270 RETURN_OK_IF_SKIP(cctx);
2271 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2272 return FAIL;
2273 isn->isn_arg.put.put_regname = regname;
2274 isn->isn_arg.put.put_lnum = lnum;
2275 return OK;
2276}
2277
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002278/*
2279 * Generate an EXEC instruction that takes a string argument.
2280 * A copy is made of "line".
2281 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 static int
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002283generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284{
2285 isn_T *isn;
2286
Bram Moolenaar080457c2020-03-03 21:53:32 +01002287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002288 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 return FAIL;
2290 isn->isn_arg.string = vim_strsave(line);
2291 return OK;
2292}
2293
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002294/*
2295 * Generate an EXEC instruction that takes a string argument.
2296 * "str" must be allocated, it is consumed.
2297 */
2298 static int
2299generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2300{
2301 isn_T *isn;
2302
2303 if (cctx->ctx_skip == SKIP_YES)
2304 {
2305 vim_free(str);
2306 return OK;
2307 }
2308 if ((isn = generate_instr(cctx, isntype)) == NULL)
2309 {
2310 vim_free(str);
2311 return FAIL;
2312 }
2313 isn->isn_arg.string = str;
2314 return OK;
2315}
2316
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002317 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002318generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2319{
2320 isn_T *isn;
2321 garray_T *stack = &cctx->ctx_type_stack;
2322
2323 RETURN_OK_IF_SKIP(cctx);
2324 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2325 return FAIL;
2326 isn->isn_arg.string = vim_strsave(line);
2327
Bram Moolenaar35578162021-08-02 19:10:38 +02002328 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002329 return FAIL;
2330 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2331 ++stack->ga_len;
2332
2333 return OK;
2334}
2335
2336 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002337generate_EXECCONCAT(cctx_T *cctx, int count)
2338{
2339 isn_T *isn;
2340
2341 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2342 return FAIL;
2343 isn->isn_arg.number = count;
2344 return OK;
2345}
2346
Bram Moolenaar08597872020-12-10 19:43:40 +01002347/*
2348 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2349 */
2350 static int
2351generate_RANGE(cctx_T *cctx, char_u *range)
2352{
2353 isn_T *isn;
2354 garray_T *stack = &cctx->ctx_type_stack;
2355
2356 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2357 return FAIL;
2358 isn->isn_arg.string = range;
2359
Bram Moolenaar35578162021-08-02 19:10:38 +02002360 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002361 return FAIL;
2362 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2363 ++stack->ga_len;
2364 return OK;
2365}
2366
Bram Moolenaar792f7862020-11-23 08:31:18 +01002367 static int
2368generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2369{
2370 isn_T *isn;
2371
2372 RETURN_OK_IF_SKIP(cctx);
2373 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2374 return FAIL;
2375 isn->isn_arg.unpack.unp_count = var_count;
2376 isn->isn_arg.unpack.unp_semicolon = semicolon;
2377 return OK;
2378}
2379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002381 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002382 */
2383 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002384generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002385{
2386 isn_T *isn;
2387
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002388 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002389 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002390 cctx->ctx_has_cmdmod = TRUE;
2391
2392 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002393 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002394 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2395 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2396 return FAIL;
2397 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002398 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002399 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002400 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002401
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002402 return OK;
2403}
2404
2405 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002406generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002407{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002408 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2409 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002410 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002411 return OK;
2412}
2413
Bram Moolenaarfa984412021-03-25 22:15:28 +01002414 static int
2415misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002416{
2417 garray_T *instr = &cctx->ctx_instr;
2418
Bram Moolenaara91a7132021-03-25 21:12:15 +01002419 if (cctx->ctx_has_cmdmod
2420 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2421 == ISN_CMDMOD)
2422 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002423 emsg(_(e_misplaced_command_modifier));
2424 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002425 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002426 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002427}
2428
2429/*
2430 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002431 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002432 */
2433 static int
2434current_instr_idx(cctx_T *cctx)
2435{
2436 garray_T *instr = &cctx->ctx_instr;
2437 int idx = instr->ga_len;
2438
Bram Moolenaare99d4222021-06-13 14:01:26 +02002439 while (idx > 0)
2440 {
2441 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002442 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002443 {
2444 --idx;
2445 continue;
2446 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002447#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002448 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2449 {
2450 --idx;
2451 continue;
2452 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002453#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002454 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2455 {
2456 --idx;
2457 continue;
2458 }
2459 break;
2460 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002461 return idx;
2462}
2463
Bram Moolenaarf002a412021-01-24 13:34:18 +01002464#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002465 static void
2466may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2467{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002468 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002469 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002470}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002471#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002472
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002473/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002475 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002477 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002478reserve_local(
2479 cctx_T *cctx,
2480 char_u *name,
2481 size_t len,
2482 int isConst,
2483 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002486 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002488 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002490 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002491 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 }
2493
Bram Moolenaar35578162021-08-02 19:10:38 +02002494 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002495 return NULL;
2496 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002497 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002499 // Every local variable uses the next entry on the stack. We could re-use
2500 // the last ones when leaving a scope, but then variables used in a closure
2501 // might get overwritten. To keep things simple do not re-use stack
2502 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002503 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2504 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002505
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002506 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 lvar->lv_const = isConst;
2508 lvar->lv_type = type;
2509
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002510 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002511 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002512 return NULL;
2513 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2514 vim_strsave(lvar->lv_name);
2515 ++dfunc->df_var_names.ga_len;
2516
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002517 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518}
2519
2520/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002521 * Remove local variables above "new_top".
2522 */
2523 static void
2524unwind_locals(cctx_T *cctx, int new_top)
2525{
2526 if (cctx->ctx_locals.ga_len > new_top)
2527 {
2528 int idx;
2529 lvar_T *lvar;
2530
2531 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2532 {
2533 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2534 vim_free(lvar->lv_name);
2535 }
2536 }
2537 cctx->ctx_locals.ga_len = new_top;
2538}
2539
2540/*
2541 * Free all local variables.
2542 */
2543 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002544free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002545{
2546 unwind_locals(cctx, 0);
2547 ga_clear(&cctx->ctx_locals);
2548}
2549
2550/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002551 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2552 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2553 * error if the variable was defined with :const.
2554 */
2555 static int
2556check_item_writable(svar_T *sv, int check_writable, char_u *name)
2557{
2558 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2559 || (check_writable == ASSIGN_FINAL
2560 && sv->sv_const == ASSIGN_CONST))
2561 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002562 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002563 return FAIL;
2564 }
2565 return OK;
2566}
2567
2568/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002570 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 * Returns the index in "sn_var_vals" if found.
2572 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002573 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 */
2575 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002576get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577{
2578 hashtab_T *ht;
2579 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002580 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002581 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 int idx;
2583
Bram Moolenaare3d46852020-08-29 13:39:17 +02002584 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002586 if (sid == current_sctx.sc_sid)
2587 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002588 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002589
2590 if (sav == NULL)
2591 return -2;
2592 idx = sav->sav_var_vals_idx;
2593 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002594 if (check_item_writable(sv, check_writable, name) == FAIL)
2595 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002596 return idx;
2597 }
2598
2599 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 ht = &SCRIPT_VARS(sid);
2601 di = find_var_in_ht(ht, 0, name, TRUE);
2602 if (di == NULL)
2603 return -2;
2604
2605 // Now find the svar_T index in sn_var_vals.
2606 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2607 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002608 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 if (sv->sv_tv == &di->di_tv)
2610 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002611 if (check_item_writable(sv, check_writable, name) == FAIL)
2612 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 return idx;
2614 }
2615 }
2616 return -1;
2617}
2618
2619/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002620 * Find "name" in imported items of the current script or in "cctx" if not
2621 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 */
2623 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002624find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 int idx;
2627
Bram Moolenaare3d46852020-08-29 13:39:17 +02002628 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002629 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 if (cctx != NULL)
2631 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2632 {
2633 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2634 + idx;
2635
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002636 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2637 : STRLEN(import->imp_name) == len
2638 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 return import;
2640 }
2641
Bram Moolenaarefa94442020-08-08 22:16:00 +02002642 return find_imported_in_script(name, len, current_sctx.sc_sid);
2643}
2644
2645 imported_T *
2646find_imported_in_script(char_u *name, size_t len, int sid)
2647{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002648 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002649 int idx;
2650
Bram Moolenaare3d46852020-08-29 13:39:17 +02002651 if (!SCRIPT_ID_VALID(sid))
2652 return NULL;
2653 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2655 {
2656 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2657
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002658 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2659 : STRLEN(import->imp_name) == len
2660 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 return import;
2662 }
2663 return NULL;
2664}
2665
2666/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002667 * Free all imported variables.
2668 */
2669 static void
2670free_imported(cctx_T *cctx)
2671{
2672 int idx;
2673
2674 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2675 {
2676 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2677
2678 vim_free(import->imp_name);
2679 }
2680 ga_clear(&cctx->ctx_imports);
2681}
2682
2683/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002684 * Return a pointer to the next line that isn't empty or only contains a
2685 * comment. Skips over white space.
2686 * Returns NULL if there is none.
2687 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002688 char_u *
2689peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002690{
2691 int lnum = cctx->ctx_lnum;
2692
2693 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2694 {
2695 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002696 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002697
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002698 // ignore NULLs inserted for continuation lines
2699 if (line != NULL)
2700 {
2701 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002702 if (vim9_bad_comment(p))
2703 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002704 if (*p != NUL && !vim9_comment_start(p))
2705 return p;
2706 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002707 }
2708 return NULL;
2709}
2710
2711/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002712 * Called when checking for a following operator at "arg". When the rest of
2713 * the line is empty or only a comment, peek the next line. If there is a next
2714 * line return a pointer to it and set "nextp".
2715 * Otherwise skip over white space.
2716 */
2717 static char_u *
2718may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2719{
2720 char_u *p = skipwhite(arg);
2721
2722 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002723 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002724 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002725 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002726 if (*nextp != NULL)
2727 return *nextp;
2728 }
2729 return p;
2730}
2731
2732/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002733 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002734 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002735 * Returns NULL when at the end.
2736 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002737 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002738next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002739{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002740 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002741
2742 do
2743 {
2744 ++cctx->ctx_lnum;
2745 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002746 {
2747 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002748 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002749 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002750 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002751 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002752 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002753 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002754 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002755 return line;
2756}
2757
2758/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002759 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002760 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002761 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002762 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2763 */
2764 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002765may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002766{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002767 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002768 if (vim9_bad_comment(*arg))
2769 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002770 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002771 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002772 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002773
2774 if (next == NULL)
2775 return FAIL;
2776 *arg = skipwhite(next);
2777 }
2778 return OK;
2779}
2780
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002781/*
2782 * Idem, and give an error when failed.
2783 */
2784 static int
2785may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2786{
2787 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2788 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002789 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002790 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002791 return FAIL;
2792 }
2793 return OK;
2794}
2795
2796
Bram Moolenaara5565e42020-05-09 15:44:01 +02002797// Structure passed between the compile_expr* functions to keep track of
2798// constants that have been parsed but for which no code was produced yet. If
2799// possible expressions on these constants are applied at compile time. If
2800// that is not possible, the code to push the constants needs to be generated
2801// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002802// Using 50 should be more than enough of 5 levels of ().
2803#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002804typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002805 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002806 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002807 int pp_is_const; // all generated code was constants, used for a
2808 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002809} ppconst_T;
2810
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002811static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002812static int compile_expr0(char_u **arg, cctx_T *cctx);
2813static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2814
Bram Moolenaara5565e42020-05-09 15:44:01 +02002815/*
2816 * Generate a PUSH instruction for "tv".
2817 * "tv" will be consumed or cleared.
2818 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2819 */
2820 static int
2821generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2822{
2823 if (tv != NULL)
2824 {
2825 switch (tv->v_type)
2826 {
2827 case VAR_UNKNOWN:
2828 break;
2829 case VAR_BOOL:
2830 generate_PUSHBOOL(cctx, tv->vval.v_number);
2831 break;
2832 case VAR_SPECIAL:
2833 generate_PUSHSPEC(cctx, tv->vval.v_number);
2834 break;
2835 case VAR_NUMBER:
2836 generate_PUSHNR(cctx, tv->vval.v_number);
2837 break;
2838#ifdef FEAT_FLOAT
2839 case VAR_FLOAT:
2840 generate_PUSHF(cctx, tv->vval.v_float);
2841 break;
2842#endif
2843 case VAR_BLOB:
2844 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2845 tv->vval.v_blob = NULL;
2846 break;
2847 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002848 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002849 tv->vval.v_string = NULL;
2850 break;
2851 default:
2852 iemsg("constant type not supported");
2853 clear_tv(tv);
2854 return FAIL;
2855 }
2856 tv->v_type = VAR_UNKNOWN;
2857 }
2858 return OK;
2859}
2860
2861/*
2862 * Generate code for any ppconst entries.
2863 */
2864 static int
2865generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2866{
2867 int i;
2868 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002869 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002870
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002871 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002872 for (i = 0; i < ppconst->pp_used; ++i)
2873 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2874 ret = FAIL;
2875 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002876 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002877 return ret;
2878}
2879
2880/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002881 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002882 */
2883 static int
2884check_ppconst_bool(ppconst_T *ppconst)
2885{
2886 if (ppconst->pp_used > 0)
2887 {
2888 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002889 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002890
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002891 return check_typval_type(&t_bool, tv, where);
2892 }
2893 return OK;
2894}
2895
2896/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002897 * Clear ppconst constants. Used when failing.
2898 */
2899 static void
2900clear_ppconst(ppconst_T *ppconst)
2901{
2902 int i;
2903
2904 for (i = 0; i < ppconst->pp_used; ++i)
2905 clear_tv(&ppconst->pp_tv[i]);
2906 ppconst->pp_used = 0;
2907}
2908
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002909/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002910 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002911 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002912 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002913 */
2914 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002915compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002916{
2917 type_T **typep;
2918 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002919 vartype_T vartype;
2920 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002921
Bram Moolenaar261417b2021-05-06 21:04:55 +02002922 // We can index a list, dict and blob. If we don't know the type
2923 // we can use the index value type. If we still don't know use an "ANY"
2924 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002925 typep = ((type_T **)stack->ga_data) + stack->ga_len
2926 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002927 vartype = (*typep)->tt_type;
2928 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002929 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002930 if (*typep == &t_any && idxtype == &t_string)
2931 vartype = VAR_DICT;
2932 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002933 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002934 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002935 return FAIL;
2936 if (is_slice)
2937 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002938 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2939 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002940 FALSE, FALSE) == FAIL)
2941 return FAIL;
2942 }
2943 }
2944
Bram Moolenaar261417b2021-05-06 21:04:55 +02002945 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002946 {
2947 if (is_slice)
2948 {
2949 emsg(_(e_cannot_slice_dictionary));
2950 return FAIL;
2951 }
2952 if ((*typep)->tt_type == VAR_DICT)
2953 {
2954 *typep = (*typep)->tt_member;
2955 if (*typep == &t_unknown)
2956 // empty dict was used
2957 *typep = &t_any;
2958 }
2959 else
2960 {
2961 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2962 FALSE, FALSE) == FAIL)
2963 return FAIL;
2964 *typep = &t_any;
2965 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002966 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002967 return FAIL;
2968 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2969 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002970 if (keeping_dict != NULL)
2971 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002972 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002973 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002974 {
2975 *typep = &t_string;
2976 if ((is_slice
2977 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2978 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2979 return FAIL;
2980 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002981 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002982 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002983 if (is_slice)
2984 {
2985 *typep = &t_blob;
2986 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2987 return FAIL;
2988 }
2989 else
2990 {
2991 *typep = &t_number;
2992 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2993 return FAIL;
2994 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002995 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002996 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002997 {
2998 if (is_slice)
2999 {
3000 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003001 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02003002 2) == FAIL)
3003 return FAIL;
3004 }
3005 else
3006 {
3007 if ((*typep)->tt_type == VAR_LIST)
3008 {
3009 *typep = (*typep)->tt_member;
3010 if (*typep == &t_unknown)
3011 // empty list was used
3012 *typep = &t_any;
3013 }
3014 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02003015 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
3016 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02003017 return FAIL;
3018 }
3019 }
3020 else
3021 {
3022 emsg(_(e_string_list_dict_or_blob_required));
3023 return FAIL;
3024 }
3025 return OK;
3026}
3027
3028/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003029 * Generate an instruction to load script-local variable "name", without the
3030 * leading "s:".
3031 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 */
3033 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003034compile_load_scriptvar(
3035 cctx_T *cctx,
3036 char_u *name, // variable NUL terminated
3037 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003038 char_u **end, // end of variable
3039 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003041 scriptitem_T *si;
3042 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 imported_T *import;
3044
Bram Moolenaare3d46852020-08-29 13:39:17 +02003045 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3046 return FAIL;
3047 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003048 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003049 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003051 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003052 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3053 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055 if (idx >= 0)
3056 {
3057 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3058
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003059 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 current_sctx.sc_sid, idx, sv->sv_type);
3061 return OK;
3062 }
3063
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003064 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 if (import != NULL)
3066 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003067 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003068 {
3069 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003070 char_u *exp_name;
3071 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003072 ufunc_T *ufunc;
3073 type_T *type;
3074
3075 // Used "import * as Name", need to lookup the member.
3076 if (*p != '.')
3077 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003078 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003079 return FAIL;
3080 }
3081 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003082 if (VIM_ISWHITE(*p))
3083 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003084 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003085 return FAIL;
3086 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003087
Bram Moolenaar1c991142020-07-04 13:15:31 +02003088 // isolate one name
3089 exp_name = p;
3090 while (eval_isnamec(*p))
3091 ++p;
3092 cc = *p;
3093 *p = NUL;
3094
Bram Moolenaaredba7072021-03-13 21:14:18 +01003095 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3096 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003097 *p = cc;
3098 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003099 *end = p;
3100
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003101 if (idx < 0)
3102 {
3103 if (*p == '(' && ufunc != NULL)
3104 {
3105 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3106 return OK;
3107 }
3108 return FAIL;
3109 }
3110
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003111 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3112 import->imp_sid,
3113 idx,
3114 type);
3115 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003116 else if (import->imp_funcname != NULL)
3117 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003118 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003119 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3120 import->imp_sid,
3121 import->imp_var_vals_idx,
3122 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 return OK;
3124 }
3125
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003126 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003127 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 return FAIL;
3129}
3130
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003131 static int
3132generate_funcref(cctx_T *cctx, char_u *name)
3133{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003134 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003135
3136 if (ufunc == NULL)
3137 return FAIL;
3138
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003139 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003140 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3141 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003142 == FAIL)
3143 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003144 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003145}
3146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147/*
3148 * Compile a variable name into a load instruction.
3149 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003150 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 * When "error" is FALSE do not give an error when not found.
3152 */
3153 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003154compile_load(
3155 char_u **arg,
3156 char_u *end_arg,
3157 cctx_T *cctx,
3158 int is_expr,
3159 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160{
3161 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003162 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003163 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003165 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166
3167 if (*(*arg + 1) == ':')
3168 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003169 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003170 {
3171 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172
Bram Moolenaarfa596382021-04-07 21:58:16 +02003173 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003174 switch (**arg)
3175 {
3176 case 'g': isn_type = ISN_LOADGDICT; break;
3177 case 'w': isn_type = ISN_LOADWDICT; break;
3178 case 't': isn_type = ISN_LOADTDICT; break;
3179 case 'b': isn_type = ISN_LOADBDICT; break;
3180 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003181 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003182 goto theend;
3183 }
3184 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3185 goto theend;
3186 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 else
3189 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003190 isntype_T isn_type = ISN_DROP;
3191
Bram Moolenaarfa596382021-04-07 21:58:16 +02003192 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003193 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3194 if (name == NULL)
3195 return FAIL;
3196
3197 switch (**arg)
3198 {
3199 case 'v': res = generate_LOADV(cctx, name, error);
3200 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003201 case 's': if (is_expr && ASCII_ISUPPER(*name)
3202 && find_func(name, FALSE, cctx) != NULL)
3203 res = generate_funcref(cctx, name);
3204 else
3205 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003206 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003207 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003208 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003209 {
3210 if (is_expr && ASCII_ISUPPER(*name)
3211 && find_func(name, FALSE, cctx) != NULL)
3212 res = generate_funcref(cctx, name);
3213 else
3214 isn_type = ISN_LOADG;
3215 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003216 else
3217 {
3218 isn_type = ISN_LOADAUTO;
3219 vim_free(name);
3220 name = vim_strnsave(*arg, end - *arg);
3221 if (name == NULL)
3222 return FAIL;
3223 }
3224 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003225 case 'w': isn_type = ISN_LOADW; break;
3226 case 't': isn_type = ISN_LOADT; break;
3227 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003228 default: // cannot happen, just in case
3229 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003230 goto theend;
3231 }
3232 if (isn_type != ISN_DROP)
3233 {
3234 // Global, Buffer-local, Window-local and Tabpage-local
3235 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003236 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003237 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 }
3240 }
3241 else
3242 {
3243 size_t len = end - *arg;
3244 int idx;
3245 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003246 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247
3248 name = vim_strnsave(*arg, end - *arg);
3249 if (name == NULL)
3250 return FAIL;
3251
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003252 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3253 {
3254 script_autoload(name, FALSE);
3255 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3256 }
3257 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3258 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003260 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003261 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 }
3263 else
3264 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003265 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003266
Bram Moolenaar709664c2020-12-12 14:33:41 +01003267 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003269 type = lvar.lv_type;
3270 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003271 if (lvar.lv_from_outer != 0)
3272 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003273 else
3274 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 }
3276 else
3277 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003278 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003279 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003280 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003281 || find_imported(name, 0, cctx) != NULL)
3282 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003283
Bram Moolenaar0f769812020-09-12 18:32:34 +02003284 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003285 // uppercase letter it can be a user defined function.
3286 // generate_funcref() will fail if the function can't be found.
3287 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003288 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 }
3290 }
3291 if (gen_load)
3292 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003293 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003294 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003295 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003296 cctx->ctx_outer_used = TRUE;
3297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 }
3299
3300 *arg = end;
3301
3302theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003303 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003304 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 vim_free(name);
3306 return res;
3307}
3308
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 static void
3310clear_instr_ga(garray_T *gap)
3311{
3312 int idx;
3313
3314 for (idx = 0; idx < gap->ga_len; ++idx)
3315 delete_instr(((isn_T *)gap->ga_data) + idx);
3316 ga_clear(gap);
3317}
3318
3319/*
3320 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3321 * Returns FAIL if compilation fails.
3322 */
3323 static int
3324compile_string(isn_T *isn, cctx_T *cctx)
3325{
3326 char_u *s = isn->isn_arg.string;
3327 garray_T save_ga = cctx->ctx_instr;
3328 int expr_res;
3329 int trailing_error;
3330 int instr_count;
3331 isn_T *instr = NULL;
3332
Bram Moolenaarcd268012021-07-22 19:11:08 +02003333 // Remove the string type from the stack.
3334 --cctx->ctx_type_stack.ga_len;
3335
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003336 // Temporarily reset the list of instructions so that the jump labels are
3337 // correct.
3338 cctx->ctx_instr.ga_len = 0;
3339 cctx->ctx_instr.ga_maxlen = 0;
3340 cctx->ctx_instr.ga_data = NULL;
3341 expr_res = compile_expr0(&s, cctx);
3342 s = skipwhite(s);
3343 trailing_error = *s != NUL;
3344
Bram Moolenaarff652882021-05-16 15:24:49 +02003345 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003346 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003347 {
3348 if (trailing_error)
3349 semsg(_(e_trailing_arg), s);
3350 clear_instr_ga(&cctx->ctx_instr);
3351 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003352 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003353 return FAIL;
3354 }
3355
3356 // Move the generated instructions into the ISN_INSTR instruction, then
3357 // restore the list of instructions.
3358 instr_count = cctx->ctx_instr.ga_len;
3359 instr = cctx->ctx_instr.ga_data;
3360 instr[instr_count].isn_type = ISN_FINISH;
3361
3362 cctx->ctx_instr = save_ga;
3363 vim_free(isn->isn_arg.string);
3364 isn->isn_type = ISN_INSTR;
3365 isn->isn_arg.instr = instr;
3366 return OK;
3367}
3368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369/*
3370 * Compile the argument expressions.
3371 * "arg" points to just after the "(" and is advanced to after the ")"
3372 */
3373 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003374compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003376 char_u *p = *arg;
3377 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003378 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003379 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380
Bram Moolenaare6085c52020-04-12 20:19:16 +02003381 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003383 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3384 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003385 if (*p == ')')
3386 {
3387 *arg = p + 1;
3388 return OK;
3389 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003390 if (must_end)
3391 {
3392 semsg(_(e_missing_comma_before_argument_str), p);
3393 return FAIL;
3394 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003395
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003396 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003397 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 return FAIL;
3399 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003400
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003401 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003402 && cctx->ctx_instr.ga_len == instr_count + 1)
3403 {
3404 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3405
3406 // {skip} argument of searchpair() can be compiled if not empty
3407 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3408 compile_string(isn, cctx);
3409 }
3410
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003411 if (*p != ',' && *skipwhite(p) == ',')
3412 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003413 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003414 p = skipwhite(p);
3415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003417 {
3418 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003419 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003420 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003421 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003422 else
3423 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003424 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003425 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003427failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003428 emsg(_(e_missing_close));
3429 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430}
3431
3432/*
3433 * Compile a function call: name(arg1, arg2)
3434 * "arg" points to "name", "arg + varlen" to the "(".
3435 * "argcount_init" is 1 for "value->method()"
3436 * Instructions:
3437 * EVAL arg1
3438 * EVAL arg2
3439 * BCALL / DCALL / UCALL
3440 */
3441 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003442compile_call(
3443 char_u **arg,
3444 size_t varlen,
3445 cctx_T *cctx,
3446 ppconst_T *ppconst,
3447 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448{
3449 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003450 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 int argcount = argcount_init;
3452 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003453 char_u fname_buf[FLEN_FIXED + 1];
3454 char_u *tofree = NULL;
3455 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003456 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003457 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003458 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003459 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003461 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003462 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003463 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003464 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003465 {
3466 char_u *s = skipwhite(*arg + varlen + 1);
3467 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003468 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003469
3470 argvars[0].v_type = VAR_UNKNOWN;
3471 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003472 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003473 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003474 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003475 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003476 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003477 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003478 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003479 {
3480 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3481
3482 *arg = s + 1;
3483 argvars[1].v_type = VAR_UNKNOWN;
3484 tv->v_type = VAR_NUMBER;
3485 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003486 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003487 f_has(argvars, tv);
3488 else
3489 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003490 clear_tv(&argvars[0]);
3491 ++ppconst->pp_used;
3492 return OK;
3493 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003494 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003495 if (!is_has)
3496 {
3497 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3498 return FAIL;
3499 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003500 }
3501
3502 if (generate_ppconst(cctx, ppconst) == FAIL)
3503 return FAIL;
3504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 if (varlen >= sizeof(namebuf))
3506 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003507 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 return FAIL;
3509 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003510 vim_strncpy(namebuf, *arg, varlen);
3511 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003513 // We handle the "skip" argument of searchpair() and searchpairpos()
3514 // differently.
3515 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3516 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3517 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3518 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003521 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523
Bram Moolenaar03290b82020-12-19 16:30:44 +01003524 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003525 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 {
3527 int idx;
3528
3529 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003530 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003532 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003533 if (STRCMP(name, "flatten") == 0)
3534 {
3535 emsg(_(e_cannot_use_flatten_in_vim9_script));
3536 goto theend;
3537 }
3538
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003539 if (STRCMP(name, "add") == 0 && argcount == 2)
3540 {
3541 garray_T *stack = &cctx->ctx_type_stack;
3542 type_T *type = ((type_T **)stack->ga_data)[
3543 stack->ga_len - 2];
3544
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003545 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003546 if (type->tt_type == VAR_LIST)
3547 {
3548 // inline "add(list, item)" so that the type can be checked
3549 res = generate_LISTAPPEND(cctx);
3550 idx = -1;
3551 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003552 else if (type->tt_type == VAR_BLOB)
3553 {
3554 // inline "add(blob, nr)" so that the type can be checked
3555 res = generate_BLOBAPPEND(cctx);
3556 idx = -1;
3557 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003558 }
3559
3560 if (idx >= 0)
3561 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3562 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003563 else
3564 semsg(_(e_unknownfunc), namebuf);
3565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 }
3567
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003568 // An argument or local variable can be a function reference, this
3569 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003570 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003571 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003572 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003573 // If we can find the function by name generate the right call.
3574 // Skip global functions here, a local funcref takes precedence.
3575 ufunc = find_func(name, FALSE, cctx);
3576 if (ufunc != NULL && !func_is_global(ufunc))
3577 {
3578 res = generate_CALL(cctx, ufunc, argcount);
3579 goto theend;
3580 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582
3583 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003584 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003585 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003587 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003588 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003589 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003591 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003592
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003593 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003594 goto theend;
3595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596
Bram Moolenaar0f769812020-09-12 18:32:34 +02003597 // If we can find a global function by name generate the right call.
3598 if (ufunc != NULL)
3599 {
3600 res = generate_CALL(cctx, ufunc, argcount);
3601 goto theend;
3602 }
3603
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003604 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003605 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003606 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003607 res = generate_UCALL(cctx, name, argcount);
3608 else
3609 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003610
3611theend:
3612 vim_free(tofree);
3613 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614}
3615
3616// like NAMESPACE_CHAR but with 'a' and 'l'.
3617#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3618
3619/*
3620 * Find the end of a variable or function name. Unlike find_name_end() this
3621 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003622 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 * Return a pointer to just after the name. Equal to "arg" if there is no
3624 * valid name.
3625 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003626 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003627to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628{
3629 char_u *p;
3630
3631 // Quick check for valid starting character.
3632 if (!eval_isnamec1(*arg))
3633 return arg;
3634
3635 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3636 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3637 // and can be used in slice "[n:]".
3638 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003639 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3641 break;
3642 return p;
3643}
3644
3645/*
3646 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003647 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003648 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 */
3650 char_u *
3651to_name_const_end(char_u *arg)
3652{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003653 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 typval_T rettv;
3655
Bram Moolenaar678b2072021-07-26 21:10:11 +02003656 if (STRNCMP(p, "<SNR>", 5) == 0)
3657 p = skipdigits(p + 5);
3658 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 if (p == arg && *arg == '[')
3660 {
3661
3662 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003663 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 p = arg;
3665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 return p;
3667}
3668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669/*
3670 * parse a list: [expr, expr]
3671 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003672 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 */
3674 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676{
3677 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003678 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003680 int is_const;
3681 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003683 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003685 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003686 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003687 semsg(_(e_list_end), *arg);
3688 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003689 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003690 if (*p == ',')
3691 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003692 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003693 return FAIL;
3694 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003695 if (*p == ']')
3696 {
3697 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003698 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003699 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003700 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003701 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003702 if (!is_const)
3703 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 ++count;
3705 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003706 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003708 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3709 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003710 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003711 return FAIL;
3712 }
3713 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003714 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 p = skipwhite(p);
3716 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003717 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003719 ppconst->pp_is_const = is_all_const;
3720 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721}
3722
3723/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003724 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003725 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003726 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 */
3728 static int
3729compile_lambda(char_u **arg, cctx_T *cctx)
3730{
Bram Moolenaare462f522020-12-27 14:43:30 +01003731 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 typval_T rettv;
3733 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003734 evalarg_T evalarg;
3735
Bram Moolenaar844fb642021-10-23 13:32:30 +01003736 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003737 evalarg.eval_flags = EVAL_EVALUATE;
3738 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739
3740 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003741 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3742 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003743 {
3744 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003745 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003746 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003747
Bram Moolenaar65c44152020-12-24 15:14:01 +01003748 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003750 ++ufunc->uf_refcount;
3751 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752
Bram Moolenaara9931532021-06-12 15:58:16 +02003753 // Compile it here to get the return type. The return type is optional,
3754 // when it's missing use t_unknown. This is recognized in
3755 // compile_return().
3756 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3757 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003758 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759
Bram Moolenaar648594e2021-07-11 17:55:01 +02003760#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003761 // When the outer function is compiled for profiling, the lambda may be
3762 // called without profiling. Compile it here in the right context.
3763 if (cctx->ctx_compile_type == CT_PROFILE)
3764 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003765#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003766
Bram Moolenaar844fb642021-10-23 13:32:30 +01003767 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3768 // "*arg" may point into it. Point into the original line to avoid a
3769 // dangling pointer.
3770 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003771 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003772 garray_T *gap = &evalarg.eval_tofree_ga;
3773 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003774
3775 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3776 + off;
3777 }
3778
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003779 clear_evalarg(&evalarg, NULL);
3780
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003781 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003782 {
3783 // The return type will now be known.
3784 set_function_type(ufunc);
3785
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003786 // The function reference count will be 1. When the ISN_FUNCREF
3787 // instruction is deleted the reference count is decremented and the
3788 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003789 return generate_FUNCREF(cctx, ufunc);
3790 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003791
3792 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 return FAIL;
3794}
3795
3796/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003797 * Get a lambda and compile it. Uses Vim9 syntax.
3798 */
3799 int
3800get_lambda_tv_and_compile(
3801 char_u **arg,
3802 typval_T *rettv,
3803 int types_optional,
3804 evalarg_T *evalarg)
3805{
3806 int r;
3807 ufunc_T *ufunc;
3808 int save_sc_version = current_sctx.sc_version;
3809
3810 // Get the funcref in "rettv".
3811 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3812 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3813 current_sctx.sc_version = save_sc_version;
3814 if (r != OK)
3815 return r;
3816
3817 // "rettv" will now be a partial referencing the function.
3818 ufunc = rettv->vval.v_partial->pt_func;
3819
3820 // Compile it here to get the return type. The return type is optional,
3821 // when it's missing use t_unknown. This is recognized in
3822 // compile_return().
3823 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3824 ufunc->uf_ret_type = &t_unknown;
3825 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3826
3827 if (ufunc->uf_def_status == UF_COMPILED)
3828 {
3829 // The return type will now be known.
3830 set_function_type(ufunc);
3831 return OK;
3832 }
3833 clear_tv(rettv);
3834 return FAIL;
3835}
3836
3837/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003838 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003840 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 */
3842 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003843compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844{
3845 garray_T *instr = &cctx->ctx_instr;
3846 int count = 0;
3847 dict_T *d = dict_alloc();
3848 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003849 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003850 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003851 int is_const;
3852 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853
3854 if (d == NULL)
3855 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003856 if (generate_ppconst(cctx, ppconst) == FAIL)
3857 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003858 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003860 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861
Bram Moolenaar23c55272020-06-21 16:58:13 +02003862 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003863 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003864 *arg = NULL;
3865 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003866 }
3867
3868 if (**arg == '}')
3869 break;
3870
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003871 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003873 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003875 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003876 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003877 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003880 if (isn->isn_type == ISN_PUSHNR)
3881 {
3882 char buf[NUMBUFLEN];
3883
3884 // Convert to string at compile time.
3885 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3886 isn->isn_type = ISN_PUSHS;
3887 isn->isn_arg.string = vim_strsave((char_u *)buf);
3888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 if (isn->isn_type == ISN_PUSHS)
3890 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003891 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003892 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003893 *arg = skipwhite(*arg);
3894 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003895 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003896 emsg(_(e_missing_matching_bracket_after_dict_key));
3897 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003898 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003899 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003901 else
3902 {
3903 // {"name": value},
3904 // {'name': value},
3905 // {name: value} use "name" as a literal key
3906 key = get_literal_key(arg);
3907 if (key == NULL)
3908 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003909 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003910 return FAIL;
3911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912
3913 // Check for duplicate keys, if using string keys.
3914 if (key != NULL)
3915 {
3916 item = dict_find(d, key, -1);
3917 if (item != NULL)
3918 {
3919 semsg(_(e_duplicate_key), key);
3920 goto failret;
3921 }
3922 item = dictitem_alloc(key);
3923 if (item != NULL)
3924 {
3925 item->di_tv.v_type = VAR_UNKNOWN;
3926 item->di_tv.v_lock = 0;
3927 if (dict_add(d, item) == FAIL)
3928 dictitem_free(item);
3929 }
3930 }
3931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 if (**arg != ':')
3933 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003934 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003935 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003936 else
3937 semsg(_(e_missing_dict_colon), *arg);
3938 return FAIL;
3939 }
3940 whitep = *arg + 1;
3941 if (!IS_WHITE_OR_NUL(*whitep))
3942 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003943 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 return FAIL;
3945 }
3946
Bram Moolenaar23c55272020-06-21 16:58:13 +02003947 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003948 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003949 *arg = NULL;
3950 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003951 }
3952
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003953 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003955 if (!is_const)
3956 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 ++count;
3958
Bram Moolenaar2c330432020-04-13 14:41:35 +02003959 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003960 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003961 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003962 *arg = NULL;
3963 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 if (**arg == '}')
3966 break;
3967 if (**arg != ',')
3968 {
3969 semsg(_(e_missing_dict_comma), *arg);
3970 goto failret;
3971 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003972 if (IS_WHITE_OR_NUL(*whitep))
3973 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003974 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003975 return FAIL;
3976 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003977 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003978 if (!IS_WHITE_OR_NUL(*whitep))
3979 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003980 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003981 return FAIL;
3982 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003983 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 }
3985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 *arg = *arg + 1;
3987
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003988 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003989 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003990 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003991 *arg += STRLEN(*arg);
3992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003994 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 return generate_NEWDICT(cctx, count);
3996
3997failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003998 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003999 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02004000 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02004001 *arg = (char_u *)"";
4002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 dict_unref(d);
4004 return FAIL;
4005}
4006
4007/*
4008 * Compile "&option".
4009 */
4010 static int
4011compile_get_option(char_u **arg, cctx_T *cctx)
4012{
4013 typval_T rettv;
4014 char_u *start = *arg;
4015 int ret;
4016
4017 // parse the option and get the current value to get the type.
4018 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004019 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 if (ret == OK)
4021 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004022 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01004023 char_u *name = vim_strnsave(start, *arg - start);
4024 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
4025 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026
4027 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4028 vim_free(name);
4029 }
4030 clear_tv(&rettv);
4031
4032 return ret;
4033}
4034
4035/*
4036 * Compile "$VAR".
4037 */
4038 static int
4039compile_get_env(char_u **arg, cctx_T *cctx)
4040{
4041 char_u *start = *arg;
4042 int len;
4043 int ret;
4044 char_u *name;
4045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 ++*arg;
4047 len = get_env_len(arg);
4048 if (len == 0)
4049 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004050 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return FAIL;
4052 }
4053
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004054 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 name = vim_strnsave(start, len + 1);
4056 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4057 vim_free(name);
4058 return ret;
4059}
4060
4061/*
4062 * Compile "@r".
4063 */
4064 static int
4065compile_get_register(char_u **arg, cctx_T *cctx)
4066{
4067 int ret;
4068
4069 ++*arg;
4070 if (**arg == NUL)
4071 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004072 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 return FAIL;
4074 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004075 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 {
4077 emsg_invreg(**arg);
4078 return FAIL;
4079 }
4080 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4081 ++*arg;
4082 return ret;
4083}
4084
4085/*
4086 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004087 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 */
4089 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004090apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004092 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
4094 // this works from end to start
4095 while (p > start)
4096 {
4097 --p;
4098 if (*p == '-' || *p == '+')
4099 {
4100 // only '-' has an effect, for '+' we only check the type
4101#ifdef FEAT_FLOAT
4102 if (rettv->v_type == VAR_FLOAT)
4103 {
4104 if (*p == '-')
4105 rettv->vval.v_float = -rettv->vval.v_float;
4106 }
4107 else
4108#endif
4109 {
4110 varnumber_T val;
4111 int error = FALSE;
4112
4113 // tv_get_number_chk() accepts a string, but we don't want that
4114 // here
4115 if (check_not_string(rettv) == FAIL)
4116 return FAIL;
4117 val = tv_get_number_chk(rettv, &error);
4118 clear_tv(rettv);
4119 if (error)
4120 return FAIL;
4121 if (*p == '-')
4122 val = -val;
4123 rettv->v_type = VAR_NUMBER;
4124 rettv->vval.v_number = val;
4125 }
4126 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004127 else if (numeric_only)
4128 {
4129 ++p;
4130 break;
4131 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004132 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 {
4134 int v = tv2bool(rettv);
4135
4136 // '!' is permissive in the type.
4137 clear_tv(rettv);
4138 rettv->v_type = VAR_BOOL;
4139 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4140 }
4141 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004142 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 return OK;
4144}
4145
4146/*
4147 * Recognize v: variables that are constants and set "rettv".
4148 */
4149 static void
4150get_vim_constant(char_u **arg, typval_T *rettv)
4151{
4152 if (STRNCMP(*arg, "v:true", 6) == 0)
4153 {
4154 rettv->v_type = VAR_BOOL;
4155 rettv->vval.v_number = VVAL_TRUE;
4156 *arg += 6;
4157 }
4158 else if (STRNCMP(*arg, "v:false", 7) == 0)
4159 {
4160 rettv->v_type = VAR_BOOL;
4161 rettv->vval.v_number = VVAL_FALSE;
4162 *arg += 7;
4163 }
4164 else if (STRNCMP(*arg, "v:null", 6) == 0)
4165 {
4166 rettv->v_type = VAR_SPECIAL;
4167 rettv->vval.v_number = VVAL_NULL;
4168 *arg += 6;
4169 }
4170 else if (STRNCMP(*arg, "v:none", 6) == 0)
4171 {
4172 rettv->v_type = VAR_SPECIAL;
4173 rettv->vval.v_number = VVAL_NONE;
4174 *arg += 6;
4175 }
4176}
4177
Bram Moolenaar657137c2021-01-09 15:45:23 +01004178 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004179get_compare_type(char_u *p, int *len, int *type_is)
4180{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004181 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004182 int i;
4183
4184 switch (p[0])
4185 {
4186 case '=': if (p[1] == '=')
4187 type = EXPR_EQUAL;
4188 else if (p[1] == '~')
4189 type = EXPR_MATCH;
4190 break;
4191 case '!': if (p[1] == '=')
4192 type = EXPR_NEQUAL;
4193 else if (p[1] == '~')
4194 type = EXPR_NOMATCH;
4195 break;
4196 case '>': if (p[1] != '=')
4197 {
4198 type = EXPR_GREATER;
4199 *len = 1;
4200 }
4201 else
4202 type = EXPR_GEQUAL;
4203 break;
4204 case '<': if (p[1] != '=')
4205 {
4206 type = EXPR_SMALLER;
4207 *len = 1;
4208 }
4209 else
4210 type = EXPR_SEQUAL;
4211 break;
4212 case 'i': if (p[1] == 's')
4213 {
4214 // "is" and "isnot"; but not a prefix of a name
4215 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4216 *len = 5;
4217 i = p[*len];
4218 if (!isalnum(i) && i != '_')
4219 {
4220 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4221 *type_is = TRUE;
4222 }
4223 }
4224 break;
4225 }
4226 return type;
4227}
4228
4229/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004230 * Skip over an expression, ignoring most errors.
4231 */
4232 static void
4233skip_expr_cctx(char_u **arg, cctx_T *cctx)
4234{
4235 evalarg_T evalarg;
4236
Bram Moolenaar844fb642021-10-23 13:32:30 +01004237 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004238 evalarg.eval_cctx = cctx;
4239 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004240 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004241}
4242
4243/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004245 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 */
4247 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004248compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004250 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
4252 // this works from end to start
4253 while (p > start)
4254 {
4255 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004256 while (VIM_ISWHITE(*p))
4257 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 if (*p == '-' || *p == '+')
4259 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004260 int negate = *p == '-';
4261 isn_T *isn;
4262 garray_T *stack = &cctx->ctx_type_stack;
4263 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004265 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4266 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4267 return FAIL;
4268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4270 {
4271 --p;
4272 if (*p == '-')
4273 negate = !negate;
4274 }
4275 // only '-' has an effect, for '+' we only check the type
4276 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004277 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004279 if (isn == NULL)
4280 return FAIL;
4281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004283 else if (numeric_only)
4284 {
4285 ++p;
4286 break;
4287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 else
4289 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004290 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004292 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004294 if (p[-1] == '!')
4295 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004298 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 return FAIL;
4300 }
4301 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004302 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 return OK;
4304}
4305
4306/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004307 * Compile "(expression)": recursive!
4308 * Return FAIL/OK.
4309 */
4310 static int
4311compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4312{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004313 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004314 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004315
Bram Moolenaar24156692021-01-14 20:35:49 +01004316 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4317 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004318 if (ppconst->pp_used <= PPSIZE - 10)
4319 {
4320 ret = compile_expr1(arg, cctx, ppconst);
4321 }
4322 else
4323 {
4324 // Not enough space in ppconst, flush constants.
4325 if (generate_ppconst(cctx, ppconst) == FAIL)
4326 return FAIL;
4327 ret = compile_expr0(arg, cctx);
4328 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004329 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4330 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004331 if (**arg == ')')
4332 ++*arg;
4333 else if (ret == OK)
4334 {
4335 emsg(_(e_missing_close));
4336 ret = FAIL;
4337 }
4338 return ret;
4339}
4340
4341/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004343 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 */
4345 static int
4346compile_subscript(
4347 char_u **arg,
4348 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004349 char_u *start_leader,
4350 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004351 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004353 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004354 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 for (;;)
4357 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004358 char_u *p = skipwhite(*arg);
4359
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004360 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004361 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004362 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004363
4364 // If a following line starts with "->{" or "->X" advance to that
4365 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004366 // Also if a following line starts with ".x".
4367 if (next != NULL &&
4368 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004369 && (next[2] == '{'
4370 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004371 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004372 {
4373 next = next_line_from_context(cctx, TRUE);
4374 if (next == NULL)
4375 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004376 *arg = next;
4377 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004378 }
4379 }
4380
Bram Moolenaarcd268012021-07-22 19:11:08 +02004381 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4382 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004383 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004385 garray_T *stack = &cctx->ctx_type_stack;
4386 type_T *type;
4387 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004389 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004390 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004391 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004394 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4395
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004396 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004397 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004399 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004401 if (keeping_dict)
4402 {
4403 keeping_dict = FALSE;
4404 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4405 return FAIL;
4406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004408 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004410 char_u *pstart = p;
4411
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004412 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004413 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004414 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 // something->method()
4417 // Apply the '!', '-' and '+' first:
4418 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004419 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004422 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004423 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004424 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004425 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004426 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004427 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004428 garray_T *stack = &cctx->ctx_type_stack;
4429 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004430 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004431 int expr_isn_start = cctx->ctx_instr.ga_len;
4432 int expr_isn_end;
4433 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004434
4435 // Funcref call: list->(Refs[2])(arg)
4436 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004437 //
4438 // Fist compile the function expression.
4439 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004440 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004441
4442 // Remember the next instruction index, where the instructions
4443 // for arguments are being written.
4444 expr_isn_end = cctx->ctx_instr.ga_len;
4445
4446 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004447 if (**arg != '(')
4448 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004449 if (*skipwhite(*arg) == '(')
4450 emsg(_(e_nowhitespace));
4451 else
4452 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004453 return FAIL;
4454 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004455 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004456 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004457 return FAIL;
4458
Bram Moolenaar2927c072021-04-05 19:41:21 +02004459 // Move the instructions for the arguments to before the
4460 // instructions of the expression and move the type of the
4461 // expression after the argument types. This is what ISN_PCALL
4462 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004463 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004464 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4465 if (arg_isn_count > 0)
4466 {
4467 int expr_isn_count = expr_isn_end - expr_isn_start;
4468 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4469
4470 if (isn == NULL)
4471 return FAIL;
4472 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4473 + expr_isn_start,
4474 sizeof(isn_T) * expr_isn_count);
4475 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4476 + expr_isn_start,
4477 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4478 sizeof(isn_T) * arg_isn_count);
4479 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4480 + expr_isn_start + arg_isn_count,
4481 isn, sizeof(isn_T) * expr_isn_count);
4482 vim_free(isn);
4483
4484 type = ((type_T **)stack->ga_data)[type_idx_start];
4485 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4486 ((type_T **)stack->ga_data) + type_idx_start + 1,
4487 sizeof(type_T *)
4488 * (stack->ga_len - type_idx_start - 1));
4489 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4490 }
4491
Bram Moolenaar7e368202020-12-25 21:56:57 +01004492 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004493 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
4495 }
4496 else
4497 {
4498 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004499 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004500 if (!eval_isnamec1(*p))
4501 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004502 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004503 return FAIL;
4504 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004505 if (ASCII_ISALPHA(*p) && p[1] == ':')
4506 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004507 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 ;
4509 if (*p != '(')
4510 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004511 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 return FAIL;
4513 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004514 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 return FAIL;
4516 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004517 if (keeping_dict)
4518 {
4519 keeping_dict = FALSE;
4520 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4521 return FAIL;
4522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004524 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004526 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004527
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004528 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004529 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004530 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004531 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004532 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004533 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004534 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004536 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004537 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004538 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004539 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004540 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004541 // missing first index is equal to zero
4542 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004543 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004544 else
4545 {
4546 if (compile_expr0(arg, cctx) == FAIL)
4547 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004548 if (**arg == ':')
4549 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004550 semsg(_(e_white_space_required_before_and_after_str_at_str),
4551 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004552 return FAIL;
4553 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004554 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004555 return FAIL;
4556 *arg = skipwhite(*arg);
4557 }
4558 if (**arg == ':')
4559 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004560 is_slice = TRUE;
4561 ++*arg;
4562 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4563 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004564 semsg(_(e_white_space_required_before_and_after_str_at_str),
4565 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004566 return FAIL;
4567 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004568 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004569 return FAIL;
4570 if (**arg == ']')
4571 // missing second index is equal to end of string
4572 generate_PUSHNR(cctx, -1);
4573 else
4574 {
4575 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004576 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004577 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004578 return FAIL;
4579 *arg = skipwhite(*arg);
4580 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582
4583 if (**arg != ']')
4584 {
4585 emsg(_(e_missbrac));
4586 return FAIL;
4587 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004588 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004590 if (keeping_dict)
4591 {
4592 keeping_dict = FALSE;
4593 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4594 return FAIL;
4595 }
4596 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004599 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004601 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004602 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004603 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004604 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004605
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004606 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004607 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004608 {
4609 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004610 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004611 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004612 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004613 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 while (eval_isnamec(*p))
4615 MB_PTR_ADV(p);
4616 if (p == *arg)
4617 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004618 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 return FAIL;
4620 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004621 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4622 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004623 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004625 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 *arg = p;
4627 }
4628 else
4629 break;
4630 }
4631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004633 // This needs to be done at runtime to be able to check the type.
4634 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4635 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636
4637 return OK;
4638}
4639
4640/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004641 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4642 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004644 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004645 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004646 *
4647 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 */
4649
4650/*
4651 * number number constant
4652 * 0zFFFFFFFF Blob constant
4653 * "string" string constant
4654 * 'string' literal string constant
4655 * &option-name option value
4656 * @r register contents
4657 * identifier variable value
4658 * function() function call
4659 * $VAR environment variable
4660 * (expression) nested expression
4661 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004662 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 *
4664 * Also handle:
4665 * ! in front logical NOT
4666 * - in front unary minus
4667 * + in front unary plus (ignored)
4668 * trailing (arg) funcref/partial call
4669 * trailing [] subscript in String or List
4670 * trailing .name entry in Dictionary
4671 * trailing ->name() method call
4672 */
4673 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004674compile_expr7(
4675 char_u **arg,
4676 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004677 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 char_u *start_leader, *end_leader;
4680 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004681 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004682 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004684 ppconst->pp_is_const = FALSE;
4685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 /*
4687 * Skip '!', '-' and '+' characters. They are handled later.
4688 */
4689 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004690 if (eval_leader(arg, TRUE) == FAIL)
4691 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 end_leader = *arg;
4693
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004694 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 switch (**arg)
4696 {
4697 /*
4698 * Number constant.
4699 */
4700 case '0': // also for blob starting with 0z
4701 case '1':
4702 case '2':
4703 case '3':
4704 case '4':
4705 case '5':
4706 case '6':
4707 case '7':
4708 case '8':
4709 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004710 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004712 // Apply "-" and "+" just before the number now, right to
4713 // left. Matters especially when "->" follows. Stops at
4714 // '!'.
4715 if (apply_leader(rettv, TRUE,
4716 start_leader, &end_leader) == FAIL)
4717 {
4718 clear_tv(rettv);
4719 return FAIL;
4720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 break;
4722
4723 /*
4724 * String constant: "string".
4725 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004726 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 return FAIL;
4728 break;
4729
4730 /*
4731 * Literal string constant: 'str''ing'.
4732 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004733 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 return FAIL;
4735 break;
4736
4737 /*
4738 * Constant Vim variable.
4739 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004740 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 ret = NOTDONE;
4742 break;
4743
4744 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004745 * "true" constant
4746 */
4747 case 't': if (STRNCMP(*arg, "true", 4) == 0
4748 && !eval_isnamec((*arg)[4]))
4749 {
4750 *arg += 4;
4751 rettv->v_type = VAR_BOOL;
4752 rettv->vval.v_number = VVAL_TRUE;
4753 }
4754 else
4755 ret = NOTDONE;
4756 break;
4757
4758 /*
4759 * "false" constant
4760 */
4761 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4762 && !eval_isnamec((*arg)[5]))
4763 {
4764 *arg += 5;
4765 rettv->v_type = VAR_BOOL;
4766 rettv->vval.v_number = VVAL_FALSE;
4767 }
4768 else
4769 ret = NOTDONE;
4770 break;
4771
4772 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004773 * "null" constant
4774 */
4775 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004776 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004777 {
4778 *arg += 4;
4779 rettv->v_type = VAR_SPECIAL;
4780 rettv->vval.v_number = VVAL_NULL;
4781 }
4782 else
4783 ret = NOTDONE;
4784 break;
4785
4786 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 * List: [expr, expr]
4788 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004789 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4790 return FAIL;
4791 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 break;
4793
4794 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 * Dictionary: {'key': val, 'key': val}
4796 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004797 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4798 return FAIL;
4799 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 break;
4801
4802 /*
4803 * Option value: &name
4804 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004805 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4806 return FAIL;
4807 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 break;
4809
4810 /*
4811 * Environment variable: $VAR.
4812 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004813 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4814 return FAIL;
4815 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 break;
4817
4818 /*
4819 * Register contents: @r.
4820 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004821 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4822 return FAIL;
4823 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 break;
4825 /*
4826 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004827 * lambda: (arg, arg) => expr
4828 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004830 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4831 ret = compile_lambda(arg, cctx);
4832 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004833 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 break;
4835
4836 default: ret = NOTDONE;
4837 break;
4838 }
4839 if (ret == FAIL)
4840 return FAIL;
4841
Bram Moolenaar1c747212020-05-09 18:28:34 +02004842 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004844 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004845 clear_tv(rettv);
4846 else
4847 // A constant expression can possibly be handled compile time,
4848 // return the value instead of generating code.
4849 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 }
4851 else if (ret == NOTDONE)
4852 {
4853 char_u *p;
4854 int r;
4855
4856 if (!eval_isnamec1(**arg))
4857 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004858 if (!vim9_bad_comment(*arg))
4859 {
4860 if (ends_excmd(*skipwhite(*arg)))
4861 semsg(_(e_empty_expression_str), *arg);
4862 else
4863 semsg(_(e_name_expected_str), *arg);
4864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 return FAIL;
4866 }
4867
4868 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004869 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004870 if (p - *arg == (size_t)1 && **arg == '_')
4871 {
4872 emsg(_(e_cannot_use_underscore_here));
4873 return FAIL;
4874 }
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004877 {
4878 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4879 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004881 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004882 if (cctx->ctx_skip != SKIP_YES
4883 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004884 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004885 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 if (r == FAIL)
4888 return FAIL;
4889 }
4890
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004891 // Handle following "[]", ".member", etc.
4892 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004893 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 ppconst) == FAIL)
4895 return FAIL;
4896 if (ppconst->pp_used > 0)
4897 {
4898 // apply the '!', '-' and '+' before the constant
4899 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004900 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004901 return FAIL;
4902 return OK;
4903 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004904 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004906 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907}
4908
4909/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004910 * Give the "white on both sides" error, taking the operator from "p[len]".
4911 */
4912 void
4913error_white_both(char_u *op, int len)
4914{
4915 char_u buf[10];
4916
4917 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004918 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004919}
4920
4921/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004922 * <type>expr7: runtime type check / conversion
4923 */
4924 static int
4925compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4926{
4927 type_T *want_type = NULL;
4928
4929 // Recognize <type>
4930 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4931 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004932 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004933 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4934 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004935 return FAIL;
4936
4937 if (**arg != '>')
4938 {
4939 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004940 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004941 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004942 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004943 return FAIL;
4944 }
4945 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004946 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004947 return FAIL;
4948 }
4949
4950 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4951 return FAIL;
4952
4953 if (want_type != NULL)
4954 {
4955 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004956 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004957 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004958
Bram Moolenaard1103582020-08-14 22:44:25 +02004959 generate_ppconst(cctx, ppconst);
4960 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004961 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004962 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004963 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004964 return FAIL;
4965 }
4966 }
4967
4968 return OK;
4969}
4970
4971/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 * * number multiplication
4973 * / number division
4974 * % number modulo
4975 */
4976 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004977compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978{
4979 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004980 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004981 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004983 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004984 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 return FAIL;
4986
4987 /*
4988 * Repeat computing, until no "*", "/" or "%" is following.
4989 */
4990 for (;;)
4991 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004992 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 if (*op != '*' && *op != '/' && *op != '%')
4994 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004995 if (next != NULL)
4996 {
4997 *arg = next_line_from_context(cctx, TRUE);
4998 op = skipwhite(*arg);
4999 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005000
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005001 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005003 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005004 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01005006 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005007 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005009 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02005010 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005012
5013 if (ppconst->pp_used == ppconst_used + 2
5014 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5015 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005016 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005017 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5018 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
5019 varnumber_T res = 0;
5020 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005022 // both are numbers: compute the result
5023 switch (*op)
5024 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005025 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005026 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005027 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005028 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005029 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005030 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005031 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005032 break;
5033 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005034 if (failed)
5035 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005036 tv1->vval.v_number = res;
5037 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005038 }
5039 else
5040 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005041 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005042 generate_two_op(cctx, op);
5043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 }
5045
5046 return OK;
5047}
5048
5049/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005050 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 * - number subtraction
5052 * .. string concatenation
5053 */
5054 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005055compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056{
5057 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005058 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005060 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061
5062 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005063 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 return FAIL;
5065
5066 /*
5067 * Repeat computing, until no "+", "-" or ".." is following.
5068 */
5069 for (;;)
5070 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005071 op = may_peek_next_line(cctx, *arg, &next);
5072 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005074 if (op[0] == op[1] && *op != '.' && next)
5075 // Finding "++" or "--" on the next line is a separate command.
5076 // But ".." is concatenation.
5077 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005079 if (next != NULL)
5080 {
5081 *arg = next_line_from_context(cctx, TRUE);
5082 op = skipwhite(*arg);
5083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005085 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005087 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005088 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 }
5090
Bram Moolenaare0de1712020-12-02 17:36:54 +01005091 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005092 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005094 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005095 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 return FAIL;
5097
Bram Moolenaara5565e42020-05-09 15:44:01 +02005098 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005099 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005100 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5101 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5102 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5103 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5106 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005107
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005108 // concat/subtract/add constant numbers
5109 if (*op == '+')
5110 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5111 else if (*op == '-')
5112 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5113 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005114 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005115 // concatenate constant strings
5116 char_u *s1 = tv1->vval.v_string;
5117 char_u *s2 = tv2->vval.v_string;
5118 size_t len1 = STRLEN(s1);
5119
5120 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5121 if (tv1->vval.v_string == NULL)
5122 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005123 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005124 return FAIL;
5125 }
5126 mch_memmove(tv1->vval.v_string, s1, len1);
5127 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005128 vim_free(s1);
5129 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005130 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 }
5133 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005134 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005135 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005136 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005137 if (*op == '.')
5138 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005139 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5140 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005141 return FAIL;
5142 generate_instr_drop(cctx, ISN_CONCAT, 1);
5143 }
5144 else
5145 generate_two_op(cctx, op);
5146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 }
5148
5149 return OK;
5150}
5151
5152/*
5153 * expr5a == expr5b
5154 * expr5a =~ expr5b
5155 * expr5a != expr5b
5156 * expr5a !~ expr5b
5157 * expr5a > expr5b
5158 * expr5a >= expr5b
5159 * expr5a < expr5b
5160 * expr5a <= expr5b
5161 * expr5a is expr5b
5162 * expr5a isnot expr5b
5163 *
5164 * Produces instructions:
5165 * EVAL expr5a Push result of "expr5a"
5166 * EVAL expr5b Push result of "expr5b"
5167 * COMPARE one of the compare instructions
5168 */
5169 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005170compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005172 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005174 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005177 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178
5179 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005180 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 return FAIL;
5182
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005183 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005184 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185
5186 /*
5187 * If there is a comparative operator, use it.
5188 */
5189 if (type != EXPR_UNKNOWN)
5190 {
5191 int ic = FALSE; // Default: do not ignore case
5192
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005193 if (next != NULL)
5194 {
5195 *arg = next_line_from_context(cctx, TRUE);
5196 p = skipwhite(*arg);
5197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 if (type_is && (p[len] == '?' || p[len] == '#'))
5199 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005200 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 return FAIL;
5202 }
5203 // extra question mark appended: ignore case
5204 if (p[len] == '?')
5205 {
5206 ic = TRUE;
5207 ++len;
5208 }
5209 // extra '#' appended: match case (ignored)
5210 else if (p[len] == '#')
5211 ++len;
5212 // nothing appended: match case
5213
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005214 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005216 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
5219
5220 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005221 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005222 return FAIL;
5223
Bram Moolenaara5565e42020-05-09 15:44:01 +02005224 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 return FAIL;
5226
Bram Moolenaara5565e42020-05-09 15:44:01 +02005227 if (ppconst->pp_used == ppconst_used + 2)
5228 {
5229 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5230 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5231 int ret;
5232
5233 // Both sides are a constant, compute the result now.
5234 // First check for a valid combination of types, this is more
5235 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005236 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005237 ret = FAIL;
5238 else
5239 {
5240 ret = typval_compare(tv1, tv2, type, ic);
5241 tv1->v_type = VAR_BOOL;
5242 tv1->vval.v_number = tv1->vval.v_number
5243 ? VVAL_TRUE : VVAL_FALSE;
5244 clear_tv(tv2);
5245 --ppconst->pp_used;
5246 }
5247 return ret;
5248 }
5249
5250 generate_ppconst(cctx, ppconst);
5251 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 }
5253
5254 return OK;
5255}
5256
Bram Moolenaar7f141552020-05-09 17:35:53 +02005257static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259/*
5260 * Compile || or &&.
5261 */
5262 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005263compile_and_or(
5264 char_u **arg,
5265 cctx_T *cctx,
5266 char *op,
5267 ppconst_T *ppconst,
5268 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005270 char_u *next;
5271 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 int opchar = *op;
5273
5274 if (p[0] == opchar && p[1] == opchar)
5275 {
5276 garray_T *instr = &cctx->ctx_instr;
5277 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005278 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279
5280 /*
5281 * Repeat until there is no following "||" or "&&"
5282 */
5283 ga_init2(&end_ga, sizeof(int), 10);
5284 while (p[0] == opchar && p[1] == opchar)
5285 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005286 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005287 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005288 int start_ctx_lnum = cctx->ctx_lnum;
5289 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005290 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005291 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005292 jumpwhen_T jump_when = opchar == '|'
5293 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005294
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005295 if (next != NULL)
5296 {
5297 *arg = next_line_from_context(cctx, TRUE);
5298 p = skipwhite(*arg);
5299 }
5300
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005301 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5302 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005303 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005304 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005305 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005306 return FAIL;
5307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005309 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005310 SOURCING_LNUM = start_lnum;
5311 save_lnum = cctx->ctx_lnum;
5312 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005313
5314 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005315 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005316 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005317 // Use the last ppconst if possible.
5318 if (ppconst->pp_used > 0)
5319 {
5320 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5321 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005322
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005323 if ((is_true && opchar == '|')
5324 || (!is_true && opchar == '&'))
5325 {
5326 // For "false && expr" and "true || expr" the "expr"
5327 // does not need to be evaluated.
5328 cctx->ctx_skip = SKIP_YES;
5329 clear_tv(tv);
5330 tv->v_type = VAR_BOOL;
5331 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5332 }
5333 else
5334 {
5335 // For "true && expr" and "false || expr" only "expr"
5336 // needs to be evaluated.
5337 --ppconst->pp_used;
5338 jump_when = JUMP_NEVER;
5339 }
5340 }
5341 else
5342 {
5343 // Every part must evaluate to a bool.
5344 status = bool_on_stack(cctx);
5345 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005346 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005347 if (status != FAIL)
5348 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005349 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005350 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 {
5352 ga_clear(&end_ga);
5353 return FAIL;
5354 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005355
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005356 if (jump_when != JUMP_NEVER)
5357 {
5358 if (cctx->ctx_skip != SKIP_YES)
5359 {
5360 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5361 ++end_ga.ga_len;
5362 }
5363 generate_JUMP(cctx, jump_when, 0);
5364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365
5366 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005367 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005368 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005369 {
5370 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005371 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005372 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005373
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005374 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005375 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5376 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377 {
5378 ga_clear(&end_ga);
5379 return FAIL;
5380 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005381
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005382 // "0 || 1" results in true, "1 && 0" results in false.
5383 if (ppconst->pp_used == const_used + 1)
5384 {
5385 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5386
5387 if (tv->v_type == VAR_NUMBER
5388 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5389 {
5390 tv->vval.v_number = tv->vval.v_number == 1
5391 ? VVAL_TRUE : VVAL_FALSE;
5392 tv->v_type = VAR_BOOL;
5393 }
5394 }
5395
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005396 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005398
5399 if (check_ppconst_bool(ppconst) == FAIL)
5400 {
5401 ga_clear(&end_ga);
5402 return FAIL;
5403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005405 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5406 // Every part must evaluate to a bool.
5407 if (bool_on_stack(cctx) == FAIL)
5408 {
5409 ga_clear(&end_ga);
5410 return FAIL;
5411 }
5412
5413 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005414 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005415 // Fill in the end label in all jumps.
5416 generate_ppconst(cctx, ppconst);
5417 while (end_ga.ga_len > 0)
5418 {
5419 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005420
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005421 --end_ga.ga_len;
5422 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005424 isn->isn_arg.jump.jump_where = instr->ga_len;
5425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005427 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005428
5429 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 }
5431
5432 return OK;
5433}
5434
5435/*
5436 * expr4a && expr4a && expr4a logical AND
5437 *
5438 * Produces instructions:
5439 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005440 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005441 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005443 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 * EVAL expr4c Push result of "expr4c"
5445 * end:
5446 */
5447 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005448compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005450 int ppconst_used = ppconst->pp_used;
5451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005453 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 return FAIL;
5455
5456 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005457 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458}
5459
5460/*
5461 * expr3a || expr3b || expr3c logical OR
5462 *
5463 * Produces instructions:
5464 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005465 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005466 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005468 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469 * EVAL expr3c Push result of "expr3c"
5470 * end:
5471 */
5472 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005473compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005475 int ppconst_used = ppconst->pp_used;
5476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005478 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479 return FAIL;
5480
5481 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005482 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483}
5484
5485/*
5486 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005488 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 * JUMP_IF_FALSE alt jump if false
5490 * EVAL expr1a
5491 * JUMP_ALWAYS end
5492 * alt: EVAL expr1b
5493 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005494 *
5495 * Toplevel expression: expr2 ?? expr1
5496 * Produces instructions:
5497 * EVAL expr2 Push result of "expr2"
5498 * JUMP_AND_KEEP_IF_TRUE end jump if true
5499 * EVAL expr1
5500 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 */
5502 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005503compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504{
5505 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005506 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005507 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508
Bram Moolenaar3988f642020-08-27 22:43:03 +02005509 // Ignore all kinds of errors when not producing code.
5510 if (cctx->ctx_skip == SKIP_YES)
5511 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005512 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005513 return OK;
5514 }
5515
Bram Moolenaar61a89812020-05-07 16:58:17 +02005516 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005517 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518 return FAIL;
5519
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005520 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005521 if (*p == '?')
5522 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005523 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524 garray_T *instr = &cctx->ctx_instr;
5525 garray_T *stack = &cctx->ctx_type_stack;
5526 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005527 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005529 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005530 int has_const_expr = FALSE;
5531 int const_value = FALSE;
5532 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005534 if (next != NULL)
5535 {
5536 *arg = next_line_from_context(cctx, TRUE);
5537 p = skipwhite(*arg);
5538 }
5539
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005540 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005541 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005542 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005543 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005544 return FAIL;
5545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
Bram Moolenaara5565e42020-05-09 15:44:01 +02005547 if (ppconst->pp_used == ppconst_used + 1)
5548 {
5549 // the condition is a constant, we know whether the ? or the :
5550 // expression is to be evaluated.
5551 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005552 if (op_falsy)
5553 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5554 else
5555 {
5556 int error = FALSE;
5557
5558 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5559 &error);
5560 if (error)
5561 return FAIL;
5562 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005563 cctx->ctx_skip = save_skip == SKIP_YES ||
5564 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5565
5566 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5567 // "left ?? right" and "left" is truthy: produce "left"
5568 generate_ppconst(cctx, ppconst);
5569 else
5570 {
5571 clear_tv(&ppconst->pp_tv[ppconst_used]);
5572 --ppconst->pp_used;
5573 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005574 }
5575 else
5576 {
5577 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005578 if (op_falsy)
5579 end_idx = instr->ga_len;
5580 generate_JUMP(cctx, op_falsy
5581 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5582 if (op_falsy)
5583 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585
5586 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005587 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005588 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005589 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005590 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591
Bram Moolenaara5565e42020-05-09 15:44:01 +02005592 if (!has_const_expr)
5593 {
5594 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005596 if (!op_falsy)
5597 {
5598 // remember the type and drop it
5599 --stack->ga_len;
5600 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005602 end_idx = instr->ga_len;
5603 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005604
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005605 // jump here from JUMP_IF_FALSE
5606 isn = ((isn_T *)instr->ga_data) + alt_idx;
5607 isn->isn_arg.jump.jump_where = instr->ga_len;
5608 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005611 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005613 // Check for the ":".
5614 p = may_peek_next_line(cctx, *arg, &next);
5615 if (*p != ':')
5616 {
5617 emsg(_(e_missing_colon));
5618 return FAIL;
5619 }
5620 if (next != NULL)
5621 {
5622 *arg = next_line_from_context(cctx, TRUE);
5623 p = skipwhite(*arg);
5624 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005625
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005626 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5627 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005628 semsg(_(e_white_space_required_before_and_after_str_at_str),
5629 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005630 return FAIL;
5631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005633 // evaluate the third expression
5634 if (has_const_expr)
5635 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005636 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005637 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005638 return FAIL;
5639 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5640 return FAIL;
5641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642
Bram Moolenaara5565e42020-05-09 15:44:01 +02005643 if (!has_const_expr)
5644 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005645 type_T **typep;
5646
Bram Moolenaara5565e42020-05-09 15:44:01 +02005647 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648
Bram Moolenaara5565e42020-05-09 15:44:01 +02005649 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005650 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5651 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005652
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005653 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005654 isn = ((isn_T *)instr->ga_data) + end_idx;
5655 isn->isn_arg.jump.jump_where = instr->ga_len;
5656 }
5657
5658 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659 }
5660 return OK;
5661}
5662
5663/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005664 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005665 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5666 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005667 */
5668 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005669compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005670{
5671 ppconst_T ppconst;
5672
5673 CLEAR_FIELD(ppconst);
5674 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5675 {
5676 clear_ppconst(&ppconst);
5677 return FAIL;
5678 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005679 if (is_const != NULL)
5680 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005681 if (generate_ppconst(cctx, &ppconst) == FAIL)
5682 return FAIL;
5683 return OK;
5684}
5685
5686/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005687 * Toplevel expression.
5688 */
5689 static int
5690compile_expr0(char_u **arg, cctx_T *cctx)
5691{
5692 return compile_expr0_ext(arg, cctx, NULL);
5693}
5694
5695/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005696 * Compile "return [expr]".
5697 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698 */
5699 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005700compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701{
5702 char_u *p = arg;
5703 garray_T *stack = &cctx->ctx_type_stack;
5704 type_T *stack_type;
5705
5706 if (*p != NUL && *p != '|' && *p != '\n')
5707 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005708 if (legacy)
5709 {
5710 int save_flags = cmdmod.cmod_flags;
5711
5712 generate_LEGACY_EVAL(cctx, p);
5713 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5714 0, cctx, FALSE, FALSE) == FAIL)
5715 return NULL;
5716 cmdmod.cmod_flags |= CMOD_LEGACY;
5717 (void)skip_expr(&p, NULL);
5718 cmdmod.cmod_flags = save_flags;
5719 }
5720 else
5721 {
5722 // compile return argument into instructions
5723 if (compile_expr0(&p, cctx) == FAIL)
5724 return NULL;
5725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005727 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005728 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005729 // "check_return_type" with uf_ret_type set to &t_unknown is used
5730 // for an inline function without a specified return type. Set the
5731 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005732 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005733 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005734 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5735 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005736 || (!check_return_type
5737 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005738 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005739 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005740 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005741 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005742 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005743 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5744 && stack_type->tt_type != VAR_VOID
5745 && stack_type->tt_type != VAR_UNKNOWN)
5746 {
5747 emsg(_(e_returning_value_in_function_without_return_type));
5748 return NULL;
5749 }
5750 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005751 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005752 return NULL;
5753 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 }
5756 else
5757 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005758 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005759 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005760 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5761 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005763 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005764 return NULL;
5765 }
5766
5767 // No argument, return zero.
5768 generate_PUSHNR(cctx, 0);
5769 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005770
5771 // Undo any command modifiers.
5772 generate_undo_cmdmods(cctx);
5773
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005774 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775 return NULL;
5776
5777 // "return val | endif" is possible
5778 return skipwhite(p);
5779}
5780
5781/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005782 * Get a line from the compilation context, compatible with exarg_T getline().
5783 * Return a pointer to the line in allocated memory.
5784 * Return NULL for end-of-file or some error.
5785 */
5786 static char_u *
5787exarg_getline(
5788 int c UNUSED,
5789 void *cookie,
5790 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005791 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005792{
5793 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005794 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005795
Bram Moolenaar66250c92020-08-20 15:02:42 +02005796 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005797 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005798 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005799 return NULL;
5800 ++cctx->ctx_lnum;
5801 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5802 // Comment lines result in NULL pointers, skip them.
5803 if (p != NULL)
5804 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005805 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005806}
5807
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005808 void
5809fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5810{
5811 eap->getline = exarg_getline;
5812 eap->cookie = cctx;
5813}
5814
Bram Moolenaar04b12692020-05-04 23:24:44 +02005815/*
5816 * Compile a nested :def command.
5817 */
5818 static char_u *
5819compile_nested_function(exarg_T *eap, cctx_T *cctx)
5820{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005821 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005822 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005823 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005824 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005825 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005826 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005827 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005828
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005829 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005830 {
5831 emsg(_(e_cannot_use_bang_with_nested_def));
5832 return NULL;
5833 }
5834
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005835 if (*name_start == '/')
5836 {
5837 name_end = skip_regexp(name_start + 1, '/', TRUE);
5838 if (*name_end == '/')
5839 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005840 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005841 }
5842 if (name_end == name_start || *skipwhite(name_end) != '(')
5843 {
5844 if (!ends_excmd2(name_start, name_end))
5845 {
5846 semsg(_(e_invalid_command_str), eap->cmd);
5847 return NULL;
5848 }
5849
5850 // "def" or "def Name": list functions
5851 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5852 return NULL;
5853 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5854 }
5855
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005856 // Only g:Func() can use a namespace.
5857 if (name_start[1] == ':' && !is_global)
5858 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005859 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005860 return NULL;
5861 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005862 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005863 return NULL;
5864
Bram Moolenaar04b12692020-05-04 23:24:44 +02005865 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005866 fill_exarg_from_cctx(eap, cctx);
5867
Bram Moolenaar04b12692020-05-04 23:24:44 +02005868 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005869 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005870 lambda_name = vim_strsave(get_lambda_name());
5871 if (lambda_name == NULL)
5872 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005873 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005874
Bram Moolenaar822ba242020-05-24 23:00:18 +02005875 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005876 {
5877 r = eap->skip ? OK : FAIL;
5878 goto theend;
5879 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005880
5881 // copy over the block scope IDs before compiling
5882 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5883 {
5884 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5885
5886 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5887 if (ufunc->uf_block_ids != NULL)
5888 {
5889 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5890 sizeof(int) * block_depth);
5891 ufunc->uf_block_depth = block_depth;
5892 }
5893 }
5894
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005895 compile_type = COMPILE_TYPE(ufunc);
5896#ifdef FEAT_PROFILE
5897 // If the outer function is profiled, also compile the nested function for
5898 // profiling.
5899 if (cctx->ctx_compile_type == CT_PROFILE)
5900 compile_type = CT_PROFILE;
5901#endif
5902 if (func_needs_compiling(ufunc, compile_type)
5903 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005904 {
5905 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005906 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005907 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005908
Bram Moolenaar648594e2021-07-11 17:55:01 +02005909#ifdef FEAT_PROFILE
5910 // When the outer function is compiled for profiling, the nested function
5911 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005912 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005913 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5914#endif
5915
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005916 if (is_global)
5917 {
5918 char_u *func_name = vim_strnsave(name_start + 2,
5919 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005920
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005921 if (func_name == NULL)
5922 r = FAIL;
5923 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005924 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005925 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005926 lambda_name = NULL;
5927 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005928 }
5929 else
5930 {
5931 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005932 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005933 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005934
Bram Moolenaareef21022020-08-01 22:16:43 +02005935 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005936 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005937 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005938 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005939 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5940 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005941
5942theend:
5943 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005944 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005945}
5946
5947/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 * Return the length of an assignment operator, or zero if there isn't one.
5949 */
5950 int
5951assignment_len(char_u *p, int *heredoc)
5952{
5953 if (*p == '=')
5954 {
5955 if (p[1] == '<' && p[2] == '<')
5956 {
5957 *heredoc = TRUE;
5958 return 3;
5959 }
5960 return 1;
5961 }
5962 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5963 return 2;
5964 if (STRNCMP(p, "..=", 3) == 0)
5965 return 3;
5966 return 0;
5967}
5968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005970 * Generate the load instruction for "name".
5971 */
5972 static void
5973generate_loadvar(
5974 cctx_T *cctx,
5975 assign_dest_T dest,
5976 char_u *name,
5977 lvar_T *lvar,
5978 type_T *type)
5979{
5980 switch (dest)
5981 {
5982 case dest_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005983 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5984 break;
5985 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005986 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5987 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5988 else
5989 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005990 break;
5991 case dest_buffer:
5992 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5993 break;
5994 case dest_window:
5995 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5996 break;
5997 case dest_tab:
5998 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5999 break;
6000 case dest_script:
6001 compile_load_scriptvar(cctx,
6002 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
6003 break;
6004 case dest_env:
6005 // Include $ in the name here
6006 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
6007 break;
6008 case dest_reg:
6009 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
6010 break;
6011 case dest_vimvar:
6012 generate_LOADV(cctx, name + 2, TRUE);
6013 break;
6014 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01006015 if (lvar->lv_from_outer > 0)
6016 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
6017 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006018 else
6019 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
6020 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006021 case dest_expr:
6022 // list or dict value should already be on the stack.
6023 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006024 }
6025}
6026
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006027/*
6028 * Skip over "[expr]" or ".member".
6029 * Does not check for any errors.
6030 */
6031 static char_u *
6032skip_index(char_u *start)
6033{
6034 char_u *p = start;
6035
6036 if (*p == '[')
6037 {
6038 p = skipwhite(p + 1);
6039 (void)skip_expr(&p, NULL);
6040 p = skipwhite(p);
6041 if (*p == ']')
6042 return p + 1;
6043 return p;
6044 }
6045 // if (*p == '.')
6046 return to_name_end(p + 1, TRUE);
6047}
6048
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006049 void
6050vim9_declare_error(char_u *name)
6051{
6052 char *scope = "";
6053
6054 switch (*name)
6055 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006056 case 'g': scope = _("global"); break;
6057 case 'b': scope = _("buffer"); break;
6058 case 'w': scope = _("window"); break;
6059 case 't': scope = _("tab"); break;
6060 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006061 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006062 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006063 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006064 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006065 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006066 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006067 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006068 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006069 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006070}
6071
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006072/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006073 * For one assignment figure out the type of destination. Return it in "dest".
6074 * When not recognized "dest" is not set.
6075 * For an option "opt_flags" is set.
6076 * For a v:var "vimvaridx" is set.
6077 * "type" is set to the destination type if known, unchanted otherwise.
6078 * Return FAIL if an error message was given.
6079 */
6080 static int
6081get_var_dest(
6082 char_u *name,
6083 assign_dest_T *dest,
6084 int cmdidx,
6085 int *opt_flags,
6086 int *vimvaridx,
6087 type_T **type,
6088 cctx_T *cctx)
6089{
6090 char_u *p;
6091
6092 if (*name == '&')
6093 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006094 int cc;
6095 long numval;
6096 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006097
6098 *dest = dest_option;
6099 if (cmdidx == CMD_final || cmdidx == CMD_const)
6100 {
6101 emsg(_(e_const_option));
6102 return FAIL;
6103 }
6104 p = name;
6105 p = find_option_end(&p, opt_flags);
6106 if (p == NULL)
6107 {
6108 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006109 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006110 return FAIL;
6111 }
6112 cc = *p;
6113 *p = NUL;
6114 opt_type = get_option_value(skip_option_env_lead(name),
6115 &numval, NULL, *opt_flags);
6116 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006117 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006118 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006119 case gov_unknown:
6120 semsg(_(e_unknown_option), name);
6121 return FAIL;
6122 case gov_string:
6123 case gov_hidden_string:
6124 *type = &t_string;
6125 break;
6126 case gov_bool:
6127 case gov_hidden_bool:
6128 *type = &t_bool;
6129 break;
6130 case gov_number:
6131 case gov_hidden_number:
6132 *type = &t_number;
6133 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006134 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006135 }
6136 else if (*name == '$')
6137 {
6138 *dest = dest_env;
6139 *type = &t_string;
6140 }
6141 else if (*name == '@')
6142 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006143 if (name[1] != '@'
6144 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006145 {
6146 emsg_invreg(name[1]);
6147 return FAIL;
6148 }
6149 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006150 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006151 }
6152 else if (STRNCMP(name, "g:", 2) == 0)
6153 {
6154 *dest = dest_global;
6155 }
6156 else if (STRNCMP(name, "b:", 2) == 0)
6157 {
6158 *dest = dest_buffer;
6159 }
6160 else if (STRNCMP(name, "w:", 2) == 0)
6161 {
6162 *dest = dest_window;
6163 }
6164 else if (STRNCMP(name, "t:", 2) == 0)
6165 {
6166 *dest = dest_tab;
6167 }
6168 else if (STRNCMP(name, "v:", 2) == 0)
6169 {
6170 typval_T *vtv;
6171 int di_flags;
6172
6173 *vimvaridx = find_vim_var(name + 2, &di_flags);
6174 if (*vimvaridx < 0)
6175 {
6176 semsg(_(e_variable_not_found_str), name);
6177 return FAIL;
6178 }
6179 // We use the current value of "sandbox" here, is that OK?
6180 if (var_check_ro(di_flags, name, FALSE))
6181 return FAIL;
6182 *dest = dest_vimvar;
6183 vtv = get_vim_var_tv(*vimvaridx);
6184 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6185 }
6186 return OK;
6187}
6188
6189/*
6190 * Generate a STORE instruction for "dest", not being "dest_local".
6191 * Return FAIL when out of memory.
6192 */
6193 static int
6194generate_store_var(
6195 cctx_T *cctx,
6196 assign_dest_T dest,
6197 int opt_flags,
6198 int vimvaridx,
6199 int scriptvar_idx,
6200 int scriptvar_sid,
6201 type_T *type,
6202 char_u *name)
6203{
6204 switch (dest)
6205 {
6206 case dest_option:
6207 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6208 opt_flags);
6209 case dest_global:
6210 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006211 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6212 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006213 case dest_buffer:
6214 // include b: with the name, easier to execute that way
6215 return generate_STORE(cctx, ISN_STOREB, 0, name);
6216 case dest_window:
6217 // include w: with the name, easier to execute that way
6218 return generate_STORE(cctx, ISN_STOREW, 0, name);
6219 case dest_tab:
6220 // include t: with the name, easier to execute that way
6221 return generate_STORE(cctx, ISN_STORET, 0, name);
6222 case dest_env:
6223 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6224 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006225 return generate_STORE(cctx, ISN_STOREREG,
6226 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006227 case dest_vimvar:
6228 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6229 case dest_script:
6230 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006231 // "s:" may be included in the name.
6232 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006233 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006234 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6235 scriptvar_sid, scriptvar_idx, type);
6236 case dest_local:
6237 case dest_expr:
6238 // cannot happen
6239 break;
6240 }
6241 return FAIL;
6242}
6243
Bram Moolenaar752fc692021-01-04 21:57:11 +01006244 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006245generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6246{
6247 if (lhs->lhs_dest != dest_local)
6248 return generate_store_var(cctx, lhs->lhs_dest,
6249 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6250 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6251 lhs->lhs_type, lhs->lhs_name);
6252
6253 if (lhs->lhs_lvar != NULL)
6254 {
6255 garray_T *instr = &cctx->ctx_instr;
6256 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6257
6258 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6259 // ISN_STORENR
6260 if (lhs->lhs_lvar->lv_from_outer == 0
6261 && instr->ga_len == instr_count + 1
6262 && isn->isn_type == ISN_PUSHNR)
6263 {
6264 varnumber_T val = isn->isn_arg.number;
6265 garray_T *stack = &cctx->ctx_type_stack;
6266
6267 isn->isn_type = ISN_STORENR;
6268 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6269 isn->isn_arg.storenr.stnr_val = val;
6270 if (stack->ga_len > 0)
6271 --stack->ga_len;
6272 }
6273 else if (lhs->lhs_lvar->lv_from_outer > 0)
6274 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6275 lhs->lhs_lvar->lv_from_outer);
6276 else
6277 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6278 }
6279 return OK;
6280}
6281
6282 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006283is_decl_command(int cmdidx)
6284{
6285 return cmdidx == CMD_let || cmdidx == CMD_var
6286 || cmdidx == CMD_final || cmdidx == CMD_const;
6287}
6288
6289/*
6290 * Figure out the LHS type and other properties for an assignment or one item
6291 * of ":unlet" with an index.
6292 * Returns OK or FAIL.
6293 */
6294 static int
6295compile_lhs(
6296 char_u *var_start,
6297 lhs_T *lhs,
6298 int cmdidx,
6299 int heredoc,
6300 int oplen,
6301 cctx_T *cctx)
6302{
6303 char_u *var_end;
6304 int is_decl = is_decl_command(cmdidx);
6305
6306 CLEAR_POINTER(lhs);
6307 lhs->lhs_dest = dest_local;
6308 lhs->lhs_vimvaridx = -1;
6309 lhs->lhs_scriptvar_idx = -1;
6310
6311 // "dest_end" is the end of the destination, including "[expr]" or
6312 // ".name".
6313 // "var_end" is the end of the variable/option/etc. name.
6314 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6315 if (*var_start == '@')
6316 var_end = var_start + 2;
6317 else
6318 {
6319 // skip over the leading "&", "&l:", "&g:" and "$"
6320 var_end = skip_option_env_lead(var_start);
6321 var_end = to_name_end(var_end, TRUE);
6322 }
6323
6324 // "a: type" is declaring variable "a" with a type, not dict "a:".
6325 if (is_decl && lhs->lhs_dest_end == var_start + 2
6326 && lhs->lhs_dest_end[-1] == ':')
6327 --lhs->lhs_dest_end;
6328 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6329 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006330 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006331
6332 // compute the length of the destination without "[expr]" or ".name"
6333 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006334 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006335 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6336 if (lhs->lhs_name == NULL)
6337 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006338
6339 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6340 // Something follows after the variable: "var[idx]" or "var.key".
6341 lhs->lhs_has_index = TRUE;
6342
Bram Moolenaar752fc692021-01-04 21:57:11 +01006343 if (heredoc)
6344 lhs->lhs_type = &t_list_string;
6345 else
6346 lhs->lhs_type = &t_any;
6347
6348 if (cctx->ctx_skip != SKIP_YES)
6349 {
6350 int declare_error = FALSE;
6351
6352 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6353 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6354 &lhs->lhs_type, cctx) == FAIL)
6355 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006356 if (lhs->lhs_dest != dest_local
6357 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006358 {
6359 // Specific kind of variable recognized.
6360 declare_error = is_decl;
6361 }
6362 else
6363 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006365 if (check_reserved_name(lhs->lhs_name) == FAIL)
6366 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006367
6368 if (lookup_local(var_start, lhs->lhs_varlen,
6369 &lhs->lhs_local_lvar, cctx) == OK)
6370 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6371 else
6372 {
6373 CLEAR_FIELD(lhs->lhs_arg_lvar);
6374 if (arg_exists(var_start, lhs->lhs_varlen,
6375 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6376 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6377 {
6378 if (is_decl)
6379 {
6380 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6381 return FAIL;
6382 }
6383 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6384 }
6385 }
6386 if (lhs->lhs_lvar != NULL)
6387 {
6388 if (is_decl)
6389 {
6390 semsg(_(e_variable_already_declared), lhs->lhs_name);
6391 return FAIL;
6392 }
6393 }
6394 else
6395 {
6396 int script_namespace = lhs->lhs_varlen > 1
6397 && STRNCMP(var_start, "s:", 2) == 0;
6398 int script_var = (script_namespace
6399 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006400 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006401 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006402 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006403 imported_T *import =
6404 find_imported(var_start, lhs->lhs_varlen, cctx);
6405
6406 if (script_namespace || script_var || import != NULL)
6407 {
6408 char_u *rawname = lhs->lhs_name
6409 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6410
6411 if (is_decl)
6412 {
6413 if (script_namespace)
6414 semsg(_(e_cannot_declare_script_variable_in_function),
6415 lhs->lhs_name);
6416 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006417 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 lhs->lhs_name);
6419 return FAIL;
6420 }
6421 else if (cctx->ctx_ufunc->uf_script_ctx_version
6422 == SCRIPT_VERSION_VIM9
6423 && script_namespace
6424 && !script_var && import == NULL)
6425 {
6426 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6427 return FAIL;
6428 }
6429
6430 lhs->lhs_dest = dest_script;
6431
6432 // existing script-local variables should have a type
6433 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6434 if (import != NULL)
6435 lhs->lhs_scriptvar_sid = import->imp_sid;
6436 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6437 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006438 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006439 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006440 lhs->lhs_scriptvar_sid, rawname,
6441 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6442 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443 if (lhs->lhs_scriptvar_idx >= 0)
6444 {
6445 scriptitem_T *si = SCRIPT_ITEM(
6446 lhs->lhs_scriptvar_sid);
6447 svar_T *sv =
6448 ((svar_T *)si->sn_var_vals.ga_data)
6449 + lhs->lhs_scriptvar_idx;
6450 lhs->lhs_type = sv->sv_type;
6451 }
6452 }
6453 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006454 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 == FAIL)
6456 return FAIL;
6457 }
6458 }
6459
6460 if (declare_error)
6461 {
6462 vim9_declare_error(lhs->lhs_name);
6463 return FAIL;
6464 }
6465 }
6466
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006467 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006468 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6469 var_end = lhs->lhs_dest_end;
6470
6471 if (lhs->lhs_dest != dest_option)
6472 {
6473 if (is_decl && *var_end == ':')
6474 {
6475 char_u *p;
6476
6477 // parse optional type: "let var: type = expr"
6478 if (!VIM_ISWHITE(var_end[1]))
6479 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006480 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006481 return FAIL;
6482 }
6483 p = skipwhite(var_end + 1);
6484 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6485 if (lhs->lhs_type == NULL)
6486 return FAIL;
6487 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006488 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006489 }
6490 else if (lhs->lhs_lvar != NULL)
6491 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6492 }
6493
Bram Moolenaare42939a2021-04-05 17:11:17 +02006494 if (oplen == 3 && !heredoc
6495 && lhs->lhs_dest != dest_global
6496 && !lhs->lhs_has_index
6497 && lhs->lhs_type->tt_type != VAR_STRING
6498 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 {
6500 emsg(_(e_can_only_concatenate_to_string));
6501 return FAIL;
6502 }
6503
6504 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6505 && cctx->ctx_skip != SKIP_YES)
6506 {
6507 if (oplen > 1 && !heredoc)
6508 {
6509 // +=, /=, etc. require an existing variable
6510 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6511 return FAIL;
6512 }
6513 if (!is_decl)
6514 {
6515 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6516 return FAIL;
6517 }
6518
Bram Moolenaar3f327882021-03-17 20:56:38 +01006519 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006520 if ((lhs->lhs_type->tt_type == VAR_FUNC
6521 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006522 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006523 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006524
6525 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006526 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6527 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6528 if (lhs->lhs_lvar == NULL)
6529 return FAIL;
6530 lhs->lhs_new_local = TRUE;
6531 }
6532
6533 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006534 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006535 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006536 char_u *after = var_start + lhs->lhs_varlen;
6537 char_u *p;
6538
Bram Moolenaar752fc692021-01-04 21:57:11 +01006539 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 if (is_decl)
6541 {
6542 emsg(_(e_cannot_use_index_when_declaring_variable));
6543 return FAIL;
6544 }
6545
Bram Moolenaarc750d912021-11-29 22:02:12 +00006546 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6547 // Only the last index is used below, if there are others
6548 // before it generate code for the expression. Thus for
6549 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6550 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006551 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006552 p = skip_index(after);
6553 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006554 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006555 lhs->lhs_varlen_total = p - var_start;
6556 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006557 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006558 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006560 if (after > var_start + lhs->lhs_varlen)
6561 {
6562 lhs->lhs_varlen = after - var_start;
6563 lhs->lhs_dest = dest_expr;
6564 // We don't know the type before evaluating the expression,
6565 // use "any" until then.
6566 lhs->lhs_type = &t_any;
6567 }
6568
6569 if (lhs->lhs_type->tt_member == NULL)
6570 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006571 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006572 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006573 }
6574 return OK;
6575}
6576
6577/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006578 * Figure out the LHS and check a few errors.
6579 */
6580 static int
6581compile_assign_lhs(
6582 char_u *var_start,
6583 lhs_T *lhs,
6584 int cmdidx,
6585 int is_decl,
6586 int heredoc,
6587 int oplen,
6588 cctx_T *cctx)
6589{
6590 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6591 return FAIL;
6592
6593 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6594 {
6595 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6596 return FAIL;
6597 }
6598 if (!is_decl && lhs->lhs_lvar != NULL
6599 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6600 {
6601 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6602 return FAIL;
6603 }
6604 return OK;
6605}
6606
6607/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006608 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6609 */
6610 static int
6611has_list_index(char_u *idx_start, cctx_T *cctx)
6612{
6613 char_u *p = idx_start;
6614 int save_skip;
6615
6616 if (*p != '[')
6617 return FALSE;
6618
6619 p = skipwhite(p + 1);
6620 if (*p == ':')
6621 return TRUE;
6622
6623 save_skip = cctx->ctx_skip;
6624 cctx->ctx_skip = SKIP_YES;
6625 (void)compile_expr0(&p, cctx);
6626 cctx->ctx_skip = save_skip;
6627 return *skipwhite(p) == ':';
6628}
6629
6630/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006631 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6632 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006633 */
6634 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006635compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 char_u *var_start,
6637 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006638 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006639 cctx_T *cctx)
6640{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006641 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006642 char_u *p;
6643 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006644 int need_white_before = TRUE;
6645 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006646
Bram Moolenaar752fc692021-01-04 21:57:11 +01006647 p = var_start + varlen;
6648 if (*p == '[')
6649 {
6650 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006651 if (*p == ':')
6652 {
6653 // empty first index, push zero
6654 r = generate_PUSHNR(cctx, 0);
6655 need_white_before = FALSE;
6656 }
6657 else
6658 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006659
6660 if (r == OK && *skipwhite(p) == ':')
6661 {
6662 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006663 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006664 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006665 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006666 empty_second = *skipwhite(p + 1) == ']';
6667 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6668 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006669 {
6670 semsg(_(e_white_space_required_before_and_after_str_at_str),
6671 ":", p);
6672 return FAIL;
6673 }
6674 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006675 if (*p == ']')
6676 // empty second index, push "none"
6677 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6678 else
6679 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006680 }
6681
Bram Moolenaar752fc692021-01-04 21:57:11 +01006682 if (r == OK && *skipwhite(p) != ']')
6683 {
6684 // this should not happen
6685 emsg(_(e_missbrac));
6686 r = FAIL;
6687 }
6688 }
6689 else // if (*p == '.')
6690 {
6691 char_u *key_end = to_name_end(p + 1, TRUE);
6692 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6693
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006694 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006696 return r;
6697}
6698
6699/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006700 * For a LHS with an index, load the variable to be indexed.
6701 */
6702 static int
6703compile_load_lhs(
6704 lhs_T *lhs,
6705 char_u *var_start,
6706 type_T *rhs_type,
6707 cctx_T *cctx)
6708{
6709 if (lhs->lhs_dest == dest_expr)
6710 {
6711 size_t varlen = lhs->lhs_varlen;
6712 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006713 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006714 char_u *p = var_start;
6715 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006716 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006717
Bram Moolenaare97976b2021-08-01 13:17:17 +02006718 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6719 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006720 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006721 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6722 res = compile_expr0(&p, cctx);
6723 var_start[varlen] = c;
6724 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6725 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006726 {
6727 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006728 if (res != FAIL)
6729 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006730 return FAIL;
6731 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006732
6733 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6734 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6735 // now we can properly check the type
6736 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6737 && rhs_type != &t_void
6738 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6739 FALSE, FALSE) == FAIL)
6740 return FAIL;
6741 }
6742 else
6743 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6744 lhs->lhs_lvar, lhs->lhs_type);
6745 return OK;
6746}
6747
6748/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006749 * Produce code for loading "lhs" and also take care of an index.
6750 * Return OK/FAIL.
6751 */
6752 static int
6753compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6754{
6755 compile_load_lhs(lhs, var_start, NULL, cctx);
6756
6757 if (lhs->lhs_has_index)
6758 {
6759 int range = FALSE;
6760
6761 // Get member from list or dict. First compile the
6762 // index value.
6763 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6764 return FAIL;
6765 if (range)
6766 {
6767 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6768 var_start);
6769 return FAIL;
6770 }
6771
6772 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006773 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006774 return FAIL;
6775 }
6776 return OK;
6777}
6778
6779/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006780 * Assignment to a list or dict member, or ":unlet" for the item, using the
6781 * information in "lhs".
6782 * Returns OK or FAIL.
6783 */
6784 static int
6785compile_assign_unlet(
6786 char_u *var_start,
6787 lhs_T *lhs,
6788 int is_assign,
6789 type_T *rhs_type,
6790 cctx_T *cctx)
6791{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006792 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006793 garray_T *stack = &cctx->ctx_type_stack;
6794 int range = FALSE;
6795
Bram Moolenaar68452172021-04-12 21:21:02 +02006796 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006797 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006798 if (is_assign && range
6799 && lhs->lhs_type->tt_type != VAR_LIST
6800 && lhs->lhs_type != &t_blob
6801 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006802 {
6803 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6804 return FAIL;
6805 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006806
6807 if (lhs->lhs_type == &t_any)
6808 {
6809 // Index on variable of unknown type: check at runtime.
6810 dest_type = VAR_ANY;
6811 }
6812 else
6813 {
6814 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006815 if (dest_type == VAR_DICT && range)
6816 {
6817 emsg(e_cannot_use_range_with_dictionary);
6818 return FAIL;
6819 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006820 if (dest_type == VAR_DICT
6821 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006822 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006823 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006824 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006825 type_T *type;
6826
6827 if (range)
6828 {
6829 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6830 if (need_type(type, &t_number,
6831 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006832 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006833 }
6834 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006835 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006836 && need_type(type, &t_number,
6837 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006838 return FAIL;
6839 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006840 }
6841
6842 // Load the dict or list. On the stack we then have:
6843 // - value (for assignment, not for :unlet)
6844 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006845 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006847 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6848 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006849
Bram Moolenaar68452172021-04-12 21:21:02 +02006850 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6851 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006852 {
6853 if (is_assign)
6854 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006855 if (range)
6856 {
6857 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6858 return FAIL;
6859 }
6860 else
6861 {
6862 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006863
Bram Moolenaar68452172021-04-12 21:21:02 +02006864 if (isn == NULL)
6865 return FAIL;
6866 isn->isn_arg.vartype = dest_type;
6867 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006868 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006869 else if (range)
6870 {
6871 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6872 return FAIL;
6873 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006874 else
6875 {
6876 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6877 return FAIL;
6878 }
6879 }
6880 else
6881 {
6882 emsg(_(e_indexable_type_required));
6883 return FAIL;
6884 }
6885
6886 return OK;
6887}
6888
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006889/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006890 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006891 * "let name"
6892 * "var name = expr"
6893 * "final name = expr"
6894 * "const name = expr"
6895 * "name = expr"
6896 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006897 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006898 * Return NULL for an error.
6899 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 */
6901 static char_u *
6902compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6903{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006904 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006906 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 char_u *ret = NULL;
6908 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006909 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006911 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006913 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 int oplen = 0;
6916 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006917 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006918 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006920 int is_decl = is_decl_command(cmdidx);
6921 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006922 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006924 // Skip over the "var" or "[var, var]" to get to any "=".
6925 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6926 if (p == NULL)
6927 return *arg == '[' ? arg : NULL;
6928
Bram Moolenaar752fc692021-01-04 21:57:11 +01006929 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 sp = p;
6932 p = skipwhite(p);
6933 op = p;
6934 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935
6936 if (var_count > 0 && oplen == 0)
6937 // can be something like "[1, 2]->func()"
6938 return arg;
6939
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006940 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006942 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006943 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006944 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006945 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6946 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006947 if (VIM_ISWHITE(eap->cmd[2]))
6948 {
6949 semsg(_(e_no_white_space_allowed_after_str_str),
6950 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6951 return NULL;
6952 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006953 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6954 oplen = 2;
6955 incdec = TRUE;
6956 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 if (heredoc)
6959 {
6960 list_T *l;
6961 listitem_T *li;
6962
6963 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006964 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006966 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006967 if (l == NULL)
6968 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969
Bram Moolenaar078269b2020-09-21 20:35:55 +02006970 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006972 // Push each line and the create the list.
6973 FOR_ALL_LIST_ITEMS(l, li)
6974 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006975 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006976 li->li_tv.vval.v_string = NULL;
6977 }
6978 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 list_free(l);
6981 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006982 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006984 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006986 char_u *wp;
6987
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006988 // for "[var, var] = expr" evaluate the expression here, loop over the
6989 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006990 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006991
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006992 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006993 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006994 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006995 if (compile_expr0(&p, cctx) == FAIL)
6996 return NULL;
6997 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006999 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007001 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007002 int needed_list_len;
7003 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007004
Bram Moolenaarec5929d2020-04-07 20:53:39 +02007005 stacktype = stack->ga_len == 0 ? &t_void
7006 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007007 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007008 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007009 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007010 goto theend;
7011 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01007012 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007013 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007014 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00007015 // If a constant list was used we can check the length right here.
7016 needed_list_len = semicolon ? var_count - 1 : var_count;
7017 if (instr->ga_len > 0)
7018 {
7019 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
7020
7021 if (isn->isn_type == ISN_NEWLIST)
7022 {
7023 did_check = TRUE;
7024 if (semicolon ? isn->isn_arg.number < needed_list_len
7025 : isn->isn_arg.number != needed_list_len)
7026 {
7027 semsg(_(e_expected_nr_items_but_got_nr),
7028 needed_list_len, isn->isn_arg.number);
7029 goto theend;
7030 }
7031 }
7032 }
7033 if (!did_check)
7034 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007035 if (stacktype->tt_member != NULL)
7036 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007037 }
7038 }
7039
7040 /*
7041 * Loop over variables in "[var, var] = expr".
7042 * For "var = expr" and "let var: type" this is done only once.
7043 */
7044 if (var_count > 0)
7045 var_start = skipwhite(arg + 1); // skip over the "["
7046 else
7047 var_start = arg;
7048 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7049 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007050 int instr_count = -1;
7051 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007052
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007053 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7054 {
7055 // Ignore underscore in "[a, _, b] = list".
7056 if (var_count > 0)
7057 {
7058 var_start = skipwhite(var_start + 2);
7059 continue;
7060 }
7061 emsg(_(e_cannot_use_underscore_here));
7062 goto theend;
7063 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007064 vim_free(lhs.lhs_name);
7065
7066 /*
7067 * Figure out the LHS type and other properties.
7068 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007069 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7070 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007071 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007072 if (heredoc)
7073 {
7074 SOURCING_LNUM = start_lnum;
7075 if (lhs.lhs_has_type
7076 && need_type(&t_list_string, lhs.lhs_type,
7077 -1, 0, cctx, FALSE, FALSE) == FAIL)
7078 goto theend;
7079 }
7080 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007081 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007082 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007083 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007084 if (oplen > 0 && var_count == 0)
7085 {
7086 // skip over the "=" and the expression
7087 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007088 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007089 }
7090 }
7091 else if (oplen > 0)
7092 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007093 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007094 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007095
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007096 // for "+=", "*=", "..=" etc. first load the current value
7097 if (*op != '='
7098 && compile_load_lhs_with_index(&lhs, var_start,
7099 cctx) == FAIL)
7100 goto theend;
7101
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007102 // For "var = expr" evaluate the expression.
7103 if (var_count == 0)
7104 {
7105 int r;
7106
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007107 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007108 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007109 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007110 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007111 r = generate_PUSHNR(cctx, 1);
7112 }
7113 else
7114 {
7115 // Temporarily hide the new local variable here, it is
7116 // not available to this expression.
7117 if (lhs.lhs_new_local)
7118 --cctx->ctx_locals.ga_len;
7119 wp = op + oplen;
7120 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7121 {
7122 if (lhs.lhs_new_local)
7123 ++cctx->ctx_locals.ga_len;
7124 goto theend;
7125 }
7126 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007127 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007128 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007129 if (r == FAIL)
7130 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007131 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007132 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007133 else if (semicolon && var_idx == var_count - 1)
7134 {
7135 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007136 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007137 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7138 goto theend;
7139 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007140 else
7141 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007142 // For "[var, var] = expr" get the "var_idx" item from the
7143 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007144 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007145 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007146 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007147
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007148 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007149 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007150 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007151 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007152 if ((rhs_type->tt_type == VAR_FUNC
7153 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007154 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007155 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007156 goto theend;
7157
Bram Moolenaar752fc692021-01-04 21:57:11 +01007158 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007159 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007160 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007161 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007162 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007163 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007164 }
7165 else
7166 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007167 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007168 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007169 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007170 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007171 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007172 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007173 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007174 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007175 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007176 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007177 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007178 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007179 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007180 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007181 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007182 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007183
Bram Moolenaar77709b12021-04-03 21:01:01 +02007184 // Without operator check type here, otherwise below.
7185 // Use the line number of the assignment.
7186 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007187 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7188 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007189 // If assigning to a list or dict member, use the
7190 // member type. Not for "list[:] =".
7191 if (lhs.lhs_has_index
7192 && !has_list_index(var_start + lhs.lhs_varlen,
7193 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007194 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007195 if (need_type_where(rhs_type, use_type, -1, where,
7196 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007197 goto theend;
7198 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007199 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007200 else
7201 {
7202 type_T *lhs_type = lhs.lhs_member_type;
7203
7204 // Special case: assigning to @# can use a number or a
7205 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007206 // Also: can assign a number to a float.
7207 if ((lhs_type == &t_number_or_string
7208 || lhs_type == &t_float)
7209 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007210 lhs_type = &t_number;
7211 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007212 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007213 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007216 else if (cmdidx == CMD_final)
7217 {
7218 emsg(_(e_final_requires_a_value));
7219 goto theend;
7220 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007221 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007223 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007224 goto theend;
7225 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007226 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007227 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007228 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007229 goto theend;
7230 }
7231 else
7232 {
7233 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007234 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007235 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007236 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007237 {
7238 case VAR_BOOL:
7239 generate_PUSHBOOL(cctx, VVAL_FALSE);
7240 break;
7241 case VAR_FLOAT:
7242#ifdef FEAT_FLOAT
7243 generate_PUSHF(cctx, 0.0);
7244#endif
7245 break;
7246 case VAR_STRING:
7247 generate_PUSHS(cctx, NULL);
7248 break;
7249 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007250 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007251 break;
7252 case VAR_FUNC:
7253 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7254 break;
7255 case VAR_LIST:
7256 generate_NEWLIST(cctx, 0);
7257 break;
7258 case VAR_DICT:
7259 generate_NEWDICT(cctx, 0);
7260 break;
7261 case VAR_JOB:
7262 generate_PUSHJOB(cctx, NULL);
7263 break;
7264 case VAR_CHANNEL:
7265 generate_PUSHCHANNEL(cctx, NULL);
7266 break;
7267 case VAR_NUMBER:
7268 case VAR_UNKNOWN:
7269 case VAR_ANY:
7270 case VAR_PARTIAL:
7271 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007272 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007273 case VAR_SPECIAL: // cannot happen
7274 generate_PUSHNR(cctx, 0);
7275 break;
7276 }
7277 }
7278 if (var_count == 0)
7279 end = p;
7280 }
7281
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007282 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007283 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007284 break;
7285
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007286 if (oplen > 0 && *op != '=')
7287 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007288 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007289 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007290
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007291 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007292 {
7293 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7294 goto theend;
7295 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007296 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007297 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007298 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007299 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7300 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007301#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007302 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007303 !(expected == &t_float && (stacktype == &t_number
7304 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007305#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007306 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007307 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007308 goto theend;
7309 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007310
7311 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007312 {
7313 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7314 goto theend;
7315 }
7316 else if (*op == '+')
7317 {
7318 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007319 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007320 lhs.lhs_member_type, stacktype,
7321 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007322 goto theend;
7323 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007324 else if (generate_two_op(cctx, op) == FAIL)
7325 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007328 // Use the line number of the assignment for store instruction.
7329 save_lnum = cctx->ctx_lnum;
7330 cctx->ctx_lnum = start_lnum - 1;
7331
Bram Moolenaar752fc692021-01-04 21:57:11 +01007332 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007333 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007334 // Use the info in "lhs" to store the value at the index in the
7335 // list or dict.
7336 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7337 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007338 {
7339 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007340 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007341 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007342 }
7343 else
7344 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007345 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007346 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007347 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007348 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007349 generate_LOCKCONST(cctx);
7350
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007351 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007352 && (lhs.lhs_type->tt_type == VAR_DICT
7353 || lhs.lhs_type->tt_type == VAR_LIST)
7354 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007355 && !(lhs.lhs_type->tt_member == &t_any
7356 && oplen > 0
7357 && rhs_type != NULL
7358 && rhs_type->tt_type == lhs.lhs_type->tt_type
7359 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007360 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007361 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007362 // also in legacy script. Not for "list<any> = val", then the
7363 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007364 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007365
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007366 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007367 {
7368 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007369 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007370 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007371 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007372 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007373
7374 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007375 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007377
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007378 // For "[var, var] = expr" drop the "expr" value.
7379 // Also for "[var, var; _] = expr".
7380 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007381 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007382 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007383 goto theend;
7384 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007385
Bram Moolenaarb2097502020-07-19 17:17:02 +02007386 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387
7388theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007389 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 return ret;
7391}
7392
7393/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007394 * Check for an assignment at "eap->cmd", compile it if found.
7395 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7396 */
7397 static int
7398may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7399{
7400 char_u *pskip;
7401 char_u *p;
7402
7403 // Assuming the command starts with a variable or function name,
7404 // find what follows.
7405 // Skip over "var.member", "var[idx]" and the like.
7406 // Also "&opt = val", "$ENV = val" and "@r = val".
7407 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7408 ? eap->cmd + 1 : eap->cmd;
7409 p = to_name_end(pskip, TRUE);
7410 if (p > eap->cmd && *p != NUL)
7411 {
7412 char_u *var_end;
7413 int oplen;
7414 int heredoc;
7415
7416 if (eap->cmd[0] == '@')
7417 var_end = eap->cmd + 2;
7418 else
7419 var_end = find_name_end(pskip, NULL, NULL,
7420 FNE_CHECK_START | FNE_INCL_BR);
7421 oplen = assignment_len(skipwhite(var_end), &heredoc);
7422 if (oplen > 0)
7423 {
7424 size_t len = p - eap->cmd;
7425
7426 // Recognize an assignment if we recognize the variable
7427 // name:
7428 // "g:var = expr"
7429 // "local = expr" where "local" is a local var.
7430 // "script = expr" where "script" is a script-local var.
7431 // "import = expr" where "import" is an imported var
7432 // "&opt = expr"
7433 // "$ENV = expr"
7434 // "@r = expr"
7435 if (*eap->cmd == '&'
7436 || *eap->cmd == '$'
7437 || *eap->cmd == '@'
7438 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007439 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007440 {
7441 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7442 if (*line == NULL || *line == eap->cmd)
7443 return FAIL;
7444 return OK;
7445 }
7446 }
7447 }
7448
7449 if (*eap->cmd == '[')
7450 {
7451 // [var, var] = expr
7452 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7453 if (*line == NULL)
7454 return FAIL;
7455 if (*line != eap->cmd)
7456 return OK;
7457 }
7458 return NOTDONE;
7459}
7460
7461/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007462 * Check if "name" can be "unlet".
7463 */
7464 int
7465check_vim9_unlet(char_u *name)
7466{
7467 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7468 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007469 // "unlet s:var" is allowed in legacy script.
7470 if (*name == 's' && !script_is_vim9())
7471 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007472 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007473 return FAIL;
7474 }
7475 return OK;
7476}
7477
7478/*
7479 * Callback passed to ex_unletlock().
7480 */
7481 static int
7482compile_unlet(
7483 lval_T *lvp,
7484 char_u *name_end,
7485 exarg_T *eap,
7486 int deep UNUSED,
7487 void *coookie)
7488{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007489 cctx_T *cctx = coookie;
7490 char_u *p = lvp->ll_name;
7491 int cc = *name_end;
7492 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007493
Bram Moolenaar752fc692021-01-04 21:57:11 +01007494 if (cctx->ctx_skip == SKIP_YES)
7495 return OK;
7496
7497 *name_end = NUL;
7498 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007499 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007500 // :unlet $ENV_VAR
7501 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7502 }
7503 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7504 {
7505 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007506
Bram Moolenaar752fc692021-01-04 21:57:11 +01007507 // This is similar to assigning: lookup the list/dict, compile the
7508 // idx/key. Then instead of storing the value unlet the item.
7509 // unlet {list}[idx]
7510 // unlet {dict}[key] dict.key
7511 //
7512 // Figure out the LHS type and other properties.
7513 //
7514 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7515
7516 // : unlet an indexed item
7517 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007518 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007519 iemsg("called compile_lhs() without an index");
7520 ret = FAIL;
7521 }
7522 else
7523 {
7524 // Use the info in "lhs" to unlet the item at the index in the
7525 // list or dict.
7526 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007527 }
7528
Bram Moolenaar752fc692021-01-04 21:57:11 +01007529 vim_free(lhs.lhs_name);
7530 }
7531 else if (check_vim9_unlet(p) == FAIL)
7532 {
7533 ret = FAIL;
7534 }
7535 else
7536 {
7537 // Normal name. Only supports g:, w:, t: and b: namespaces.
7538 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007539 }
7540
Bram Moolenaar752fc692021-01-04 21:57:11 +01007541 *name_end = cc;
7542 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007543}
7544
7545/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007546 * Callback passed to ex_unletlock().
7547 */
7548 static int
7549compile_lock_unlock(
7550 lval_T *lvp,
7551 char_u *name_end,
7552 exarg_T *eap,
7553 int deep UNUSED,
7554 void *coookie)
7555{
7556 cctx_T *cctx = coookie;
7557 int cc = *name_end;
7558 char_u *p = lvp->ll_name;
7559 int ret = OK;
7560 size_t len;
7561 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007562 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007563
7564 if (cctx->ctx_skip == SKIP_YES)
7565 return OK;
7566
7567 // Cannot use :lockvar and :unlockvar on local variables.
7568 if (p[1] != ':')
7569 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007570 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007571
7572 if (lookup_local(p, end - p, NULL, cctx) == OK)
7573 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007574 char_u *s = p;
7575
7576 if (*end != '.' && *end != '[')
7577 {
7578 emsg(_(e_cannot_lock_unlock_local_variable));
7579 return FAIL;
7580 }
7581
7582 // For "d.member" put the local variable on the stack, it will be
7583 // passed to ex_lockvar() indirectly.
7584 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7585 return FAIL;
7586 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007587 }
7588 }
7589
7590 // Checking is done at runtime.
7591 *name_end = NUL;
7592 len = name_end - p + 20;
7593 buf = alloc(len);
7594 if (buf == NULL)
7595 ret = FAIL;
7596 else
7597 {
7598 vim_snprintf((char *)buf, len, "%s %s",
7599 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7600 p);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007601 ret = generate_EXEC_copy(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007602
7603 vim_free(buf);
7604 *name_end = cc;
7605 }
7606 return ret;
7607}
7608
7609/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007610 * compile "unlet var", "lock var" and "unlock var"
7611 * "arg" points to "var".
7612 */
7613 static char_u *
7614compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7615{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007616 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7617 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7618 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007619 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7620}
7621
7622/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7624 */
7625 static int
7626compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7627{
7628 garray_T *instr = &cctx->ctx_instr;
7629 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7630
7631 if (endlabel == NULL)
7632 return FAIL;
7633 endlabel->el_next = *el;
7634 *el = endlabel;
7635 endlabel->el_end_label = instr->ga_len;
7636
7637 generate_JUMP(cctx, when, 0);
7638 return OK;
7639}
7640
7641 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007642compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643{
7644 garray_T *instr = &cctx->ctx_instr;
7645
7646 while (*el != NULL)
7647 {
7648 endlabel_T *cur = (*el);
7649 isn_T *isn;
7650
7651 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007652 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 *el = cur->el_next;
7654 vim_free(cur);
7655 }
7656}
7657
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007658 static void
7659compile_free_jump_to_end(endlabel_T **el)
7660{
7661 while (*el != NULL)
7662 {
7663 endlabel_T *cur = (*el);
7664
7665 *el = cur->el_next;
7666 vim_free(cur);
7667 }
7668}
7669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670/*
7671 * Create a new scope and set up the generic items.
7672 */
7673 static scope_T *
7674new_scope(cctx_T *cctx, scopetype_T type)
7675{
7676 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7677
7678 if (scope == NULL)
7679 return NULL;
7680 scope->se_outer = cctx->ctx_scope;
7681 cctx->ctx_scope = scope;
7682 scope->se_type = type;
7683 scope->se_local_count = cctx->ctx_locals.ga_len;
7684 return scope;
7685}
7686
7687/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007688 * Free the current scope and go back to the outer scope.
7689 */
7690 static void
7691drop_scope(cctx_T *cctx)
7692{
7693 scope_T *scope = cctx->ctx_scope;
7694
7695 if (scope == NULL)
7696 {
7697 iemsg("calling drop_scope() without a scope");
7698 return;
7699 }
7700 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007701 switch (scope->se_type)
7702 {
7703 case IF_SCOPE:
7704 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7705 case FOR_SCOPE:
7706 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7707 case WHILE_SCOPE:
7708 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7709 case TRY_SCOPE:
7710 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7711 case NO_SCOPE:
7712 case BLOCK_SCOPE:
7713 break;
7714 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007715 vim_free(scope);
7716}
7717
7718/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 * compile "if expr"
7720 *
7721 * "if expr" Produces instructions:
7722 * EVAL expr Push result of "expr"
7723 * JUMP_IF_FALSE end
7724 * ... body ...
7725 * end:
7726 *
7727 * "if expr | else" Produces instructions:
7728 * EVAL expr Push result of "expr"
7729 * JUMP_IF_FALSE else
7730 * ... body ...
7731 * JUMP_ALWAYS end
7732 * else:
7733 * ... body ...
7734 * end:
7735 *
7736 * "if expr1 | elseif expr2 | else" Produces instructions:
7737 * EVAL expr Push result of "expr"
7738 * JUMP_IF_FALSE elseif
7739 * ... body ...
7740 * JUMP_ALWAYS end
7741 * elseif:
7742 * EVAL expr Push result of "expr"
7743 * JUMP_IF_FALSE else
7744 * ... body ...
7745 * JUMP_ALWAYS end
7746 * else:
7747 * ... body ...
7748 * end:
7749 */
7750 static char_u *
7751compile_if(char_u *arg, cctx_T *cctx)
7752{
7753 char_u *p = arg;
7754 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007755 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007757 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007758 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759
Bram Moolenaara5565e42020-05-09 15:44:01 +02007760 CLEAR_FIELD(ppconst);
7761 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007762 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007763 clear_ppconst(&ppconst);
7764 return NULL;
7765 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007766 if (!ends_excmd2(arg, skipwhite(p)))
7767 {
7768 semsg(_(e_trailing_arg), p);
7769 return NULL;
7770 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007771 if (cctx->ctx_skip == SKIP_YES)
7772 clear_ppconst(&ppconst);
7773 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007774 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007775 int error = FALSE;
7776 int v;
7777
Bram Moolenaara5565e42020-05-09 15:44:01 +02007778 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007779 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007780 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007781 if (error)
7782 return NULL;
7783 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007784 }
7785 else
7786 {
7787 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007788 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007789 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007790 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007791 if (bool_on_stack(cctx) == FAIL)
7792 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794
Bram Moolenaara91a7132021-03-25 21:12:15 +01007795 // CMDMOD_REV must come before the jump
7796 generate_undo_cmdmods(cctx);
7797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 scope = new_scope(cctx, IF_SCOPE);
7799 if (scope == NULL)
7800 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007801 scope->se_skip_save = skip_save;
7802 // "is_had_return" will be reset if any block does not end in :return
7803 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007804
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007805 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007806 {
7807 // "where" is set when ":elseif", "else" or ":endif" is found
7808 scope->se_u.se_if.is_if_label = instr->ga_len;
7809 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7810 }
7811 else
7812 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813
Bram Moolenaarced68a02021-01-24 17:53:47 +01007814#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007815 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007816 && skip_save != SKIP_YES)
7817 {
7818 // generated a profile start, need to generate a profile end, since it
7819 // won't be done after returning
7820 cctx->ctx_skip = SKIP_NOT;
7821 generate_instr(cctx, ISN_PROF_END);
7822 cctx->ctx_skip = SKIP_YES;
7823 }
7824#endif
7825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 return p;
7827}
7828
7829 static char_u *
7830compile_elseif(char_u *arg, cctx_T *cctx)
7831{
7832 char_u *p = arg;
7833 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007834 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 isn_T *isn;
7836 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007837 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007838 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839
7840 if (scope == NULL || scope->se_type != IF_SCOPE)
7841 {
7842 emsg(_(e_elseif_without_if));
7843 return NULL;
7844 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007845 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007846 if (!cctx->ctx_had_return)
7847 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848
Bram Moolenaarced68a02021-01-24 17:53:47 +01007849 if (cctx->ctx_skip == SKIP_NOT)
7850 {
7851 // previous block was executed, this one and following will not
7852 cctx->ctx_skip = SKIP_YES;
7853 scope->se_u.se_if.is_seen_skip_not = TRUE;
7854 }
7855 if (scope->se_u.se_if.is_seen_skip_not)
7856 {
7857 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007858 // Do not count the "elseif" for profiling and cmdmod
7859 instr->ga_len = current_instr_idx(cctx);
7860
Bram Moolenaarced68a02021-01-24 17:53:47 +01007861 skip_expr_cctx(&p, cctx);
7862 return p;
7863 }
7864
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007865 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007866 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007867 int moved_cmdmod = FALSE;
7868 int saved_debug = FALSE;
7869 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007870
7871 // Move any CMDMOD instruction to after the jump
7872 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7873 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007874 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007875 return NULL;
7876 ((isn_T *)instr->ga_data)[instr->ga_len] =
7877 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7878 --instr->ga_len;
7879 moved_cmdmod = TRUE;
7880 }
7881
Bram Moolenaar093165c2021-08-22 13:35:31 +02007882 // Remove the already generated ISN_DEBUG, it is written below the
7883 // ISN_FOR instruction.
7884 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7885 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7886 .isn_type == ISN_DEBUG)
7887 {
7888 --instr->ga_len;
7889 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7890 saved_debug = TRUE;
7891 }
7892
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007893 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007895 return NULL;
7896 // previous "if" or "elseif" jumps here
7897 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7898 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007899
Bram Moolenaara91a7132021-03-25 21:12:15 +01007900 if (moved_cmdmod)
7901 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007902
7903 if (saved_debug)
7904 {
7905 // move the debug instruction here
7906 if (GA_GROW_FAILS(instr, 1))
7907 return NULL;
7908 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7909 ++instr->ga_len;
7910 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007913 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007914 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007915 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007916 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007917 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007918#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007919 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007920 // the previous block was skipped, need to profile this line
7921 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007922#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007923 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007924 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007925 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007926 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007927
7928 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007929 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007930 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007931 clear_ppconst(&ppconst);
7932 return NULL;
7933 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007934 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007935 if (!ends_excmd2(arg, skipwhite(p)))
7936 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007937 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007938 semsg(_(e_trailing_arg), p);
7939 return NULL;
7940 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007941 if (scope->se_skip_save == SKIP_YES)
7942 clear_ppconst(&ppconst);
7943 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007944 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007945 int error = FALSE;
7946 int v;
7947
Bram Moolenaarfad27422021-11-30 21:58:19 +00007948 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007949 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7950 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007951 {
7952 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007953 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007954 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007955 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007956 clear_ppconst(&ppconst);
7957 scope->se_u.se_if.is_if_label = -1;
7958 }
7959 else
7960 {
7961 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007962 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007963 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007964 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007965 if (bool_on_stack(cctx) == FAIL)
7966 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967
Bram Moolenaara91a7132021-03-25 21:12:15 +01007968 // CMDMOD_REV must come before the jump
7969 generate_undo_cmdmods(cctx);
7970
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007971 // "where" is set when ":elseif", "else" or ":endif" is found
7972 scope->se_u.se_if.is_if_label = instr->ga_len;
7973 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007975
7976 return p;
7977}
7978
7979 static char_u *
7980compile_else(char_u *arg, cctx_T *cctx)
7981{
7982 char_u *p = arg;
7983 garray_T *instr = &cctx->ctx_instr;
7984 isn_T *isn;
7985 scope_T *scope = cctx->ctx_scope;
7986
7987 if (scope == NULL || scope->se_type != IF_SCOPE)
7988 {
7989 emsg(_(e_else_without_if));
7990 return NULL;
7991 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007992 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007993 if (!cctx->ctx_had_return)
7994 scope->se_u.se_if.is_had_return = FALSE;
7995 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007996
Bram Moolenaarced68a02021-01-24 17:53:47 +01007997#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007998 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007999 {
8000 if (cctx->ctx_skip == SKIP_NOT
8001 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8002 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02008003 // the previous block was executed, do not count "else" for
8004 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01008005 --instr->ga_len;
8006 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
8007 {
8008 // the previous block was not executed, this one will, do count the
8009 // "else" for profiling
8010 cctx->ctx_skip = SKIP_NOT;
8011 generate_instr(cctx, ISN_PROF_END);
8012 generate_instr(cctx, ISN_PROF_START);
8013 cctx->ctx_skip = SKIP_YES;
8014 }
8015 }
8016#endif
8017
8018 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008019 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008020 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008021 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008022 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02008023 if (!cctx->ctx_had_return
8024 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
8025 JUMP_ALWAYS, cctx) == FAIL)
8026 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008027 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008028
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008029 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008030 {
8031 if (scope->se_u.se_if.is_if_label >= 0)
8032 {
8033 // previous "if" or "elseif" jumps here
8034 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8035 isn->isn_arg.jump.jump_where = instr->ga_len;
8036 scope->se_u.se_if.is_if_label = -1;
8037 }
8038 }
8039
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02008040 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02008041 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
8042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043
8044 return p;
8045}
8046
8047 static char_u *
8048compile_endif(char_u *arg, cctx_T *cctx)
8049{
8050 scope_T *scope = cctx->ctx_scope;
8051 ifscope_T *ifscope;
8052 garray_T *instr = &cctx->ctx_instr;
8053 isn_T *isn;
8054
Bram Moolenaarfa984412021-03-25 22:15:28 +01008055 if (misplaced_cmdmod(cctx))
8056 return NULL;
8057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 if (scope == NULL || scope->se_type != IF_SCOPE)
8059 {
8060 emsg(_(e_endif_without_if));
8061 return NULL;
8062 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008063 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008064 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008065 if (!cctx->ctx_had_return)
8066 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008067
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008068 if (scope->se_u.se_if.is_if_label >= 0)
8069 {
8070 // previous "if" or "elseif" jumps here
8071 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8072 isn->isn_arg.jump.jump_where = instr->ga_len;
8073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008075 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008076
8077#ifdef FEAT_PROFILE
8078 // even when skipping we count the endif as executed, unless the block it's
8079 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008080 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008081 && scope->se_skip_save != SKIP_YES)
8082 {
8083 cctx->ctx_skip = SKIP_NOT;
8084 generate_instr(cctx, ISN_PROF_START);
8085 }
8086#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008087 cctx->ctx_skip = scope->se_skip_save;
8088
8089 // If all the blocks end in :return and there is an :else then the
8090 // had_return flag is set.
8091 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008093 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008094 return arg;
8095}
8096
8097/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008098 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 *
8100 * Produces instructions:
8101 * PUSHNR -1
8102 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008103 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8105 * - if beyond end, jump to "end"
8106 * - otherwise get item from list and push it
8107 * STORE var Store item in "var"
8108 * ... body ...
8109 * JUMP top Jump back to repeat
8110 * end: DROP Drop the result of "expr"
8111 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008112 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8113 * UNPACK 2 Split item in 2
8114 * STORE var1 Store item in "var1"
8115 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116 */
8117 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008118compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008120 char_u *arg;
8121 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008122 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008124 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008125 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008126 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008127 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008128 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008130 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008131 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008132 lvar_T *loop_lvar; // loop iteration variable
8133 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008135 type_T *item_type = &t_any;
8136 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008137 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138
Bram Moolenaar792f7862020-11-23 08:31:18 +01008139 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008140 if (p == NULL)
8141 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008142 if (var_count == 0)
8143 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008144 else
8145 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146
8147 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008148 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008149 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8150 return NULL;
8151 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008153 if (*p == ':' && wp != p)
8154 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8155 else
8156 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 return NULL;
8158 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008159 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008160 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8161 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008162
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008163 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8164 // instruction.
8165 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8166 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8167 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008168 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008169 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008170 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8171 .isn_arg.debug.dbg_break_lnum;
8172 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174 scope = new_scope(cctx, FOR_SCOPE);
8175 if (scope == NULL)
8176 return NULL;
8177
Bram Moolenaar792f7862020-11-23 08:31:18 +01008178 // Reserve a variable to store the loop iteration counter and initialize it
8179 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008180 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8181 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008182 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008183 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008184 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008186 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008187 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188
8189 // compile "expr", it remains on the stack until "endfor"
8190 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008191 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008192 {
8193 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008195 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008196 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008197
rbtnnbebf0692021-08-21 17:26:50 +02008198 if (cctx->ctx_skip != SKIP_YES)
8199 {
8200 // If we know the type of "var" and it is a not a supported type we can
8201 // give an error now.
8202 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8203 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008204 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008205 {
rbtnnbebf0692021-08-21 17:26:50 +02008206 semsg(_(e_for_loop_on_str_not_supported),
8207 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008208 drop_scope(cctx);
8209 return NULL;
8210 }
rbtnnbebf0692021-08-21 17:26:50 +02008211
8212 if (vartype->tt_type == VAR_STRING)
8213 item_type = &t_string;
8214 else if (vartype->tt_type == VAR_BLOB)
8215 item_type = &t_number;
8216 else if (vartype->tt_type == VAR_LIST
8217 && vartype->tt_member->tt_type != VAR_ANY)
8218 {
8219 if (!var_list)
8220 item_type = vartype->tt_member;
8221 else if (vartype->tt_member->tt_type == VAR_LIST
8222 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008223 item_type = vartype->tt_member->tt_member;
8224 }
8225
8226 // CMDMOD_REV must come before the FOR instruction.
8227 generate_undo_cmdmods(cctx);
8228
8229 // "for_end" is set when ":endfor" is found
8230 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8231
8232 generate_FOR(cctx, loop_lvar->lv_idx);
8233
8234 arg = arg_start;
8235 if (var_list)
8236 {
8237 generate_UNPACK(cctx, var_count, semicolon);
8238 arg = skipwhite(arg + 1); // skip white after '['
8239
8240 // the list item is replaced by a number of items
8241 if (GA_GROW_FAILS(stack, var_count - 1))
8242 {
8243 drop_scope(cctx);
8244 return NULL;
8245 }
8246 --stack->ga_len;
8247 for (idx = 0; idx < var_count; ++idx)
8248 {
8249 ((type_T **)stack->ga_data)[stack->ga_len] =
8250 (semicolon && idx == 0) ? vartype : item_type;
8251 ++stack->ga_len;
8252 }
8253 }
8254
Bram Moolenaar792f7862020-11-23 08:31:18 +01008255 for (idx = 0; idx < var_count; ++idx)
8256 {
rbtnnbebf0692021-08-21 17:26:50 +02008257 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008258 int opt_flags = 0;
8259 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008260 type_T *type = &t_any;
8261 type_T *lhs_type = &t_any;
8262 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008263
rbtnnbebf0692021-08-21 17:26:50 +02008264 p = skip_var_one(arg, FALSE);
8265 varlen = p - arg;
8266 name = vim_strnsave(arg, varlen);
8267 if (name == NULL)
8268 goto failed;
8269 if (*p == ':')
8270 {
8271 p = skipwhite(p + 1);
8272 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8273 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008274
Bram Moolenaarfad27422021-11-30 21:58:19 +00008275 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008276 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008277 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008278 goto failed;
8279 if (dest != dest_local)
8280 {
8281 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008282 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008283 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008284 }
rbtnnbebf0692021-08-21 17:26:50 +02008285 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008286 {
rbtnnbebf0692021-08-21 17:26:50 +02008287 // Assigning to "_": drop the value.
8288 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8289 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008290 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008291 else
rbtnnbebf0692021-08-21 17:26:50 +02008292 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008293 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008294 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008295 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8296 {
8297 semsg(_(e_variable_already_declared), arg);
8298 goto failed;
8299 }
8300
8301 if (STRNCMP(name, "s:", 2) == 0)
8302 {
8303 semsg(_(e_cannot_declare_script_variable_in_function), name);
8304 goto failed;
8305 }
8306
8307 // Reserve a variable to store "var".
8308 where.wt_index = var_list ? idx + 1 : 0;
8309 where.wt_variable = TRUE;
8310 if (lhs_type == &t_any)
8311 lhs_type = item_type;
8312 else if (item_type != &t_unknown
8313 && (item_type == &t_any
8314 ? need_type(item_type, lhs_type,
8315 -1, 0, cctx, FALSE, FALSE)
8316 : check_type(lhs_type, item_type, TRUE, where))
8317 == FAIL)
8318 goto failed;
8319 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8320 if (var_lvar == NULL)
8321 // out of memory or used as an argument
8322 goto failed;
8323
8324 if (semicolon && idx == var_count - 1)
8325 var_lvar->lv_type = vartype;
8326 else
8327 var_lvar->lv_type = item_type;
8328 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8329 }
8330
8331 if (*p == ',' || *p == ';')
8332 ++p;
8333 arg = skipwhite(p);
8334 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008335 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008336
rbtnnbebf0692021-08-21 17:26:50 +02008337 if (cctx->ctx_compile_type == CT_DEBUG)
8338 {
8339 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008340
rbtnnbebf0692021-08-21 17:26:50 +02008341 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8342 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8343 cctx->ctx_prev_lnum = prev_lnum;
8344 generate_instr_debug(cctx);
8345 cctx->ctx_prev_lnum = save_prev_lnum;
8346 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008347 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008348
Bram Moolenaar792f7862020-11-23 08:31:18 +01008349 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008350
8351failed:
8352 vim_free(name);
8353 drop_scope(cctx);
8354 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355}
8356
8357/*
8358 * compile "endfor"
8359 */
8360 static char_u *
8361compile_endfor(char_u *arg, cctx_T *cctx)
8362{
8363 garray_T *instr = &cctx->ctx_instr;
8364 scope_T *scope = cctx->ctx_scope;
8365 forscope_T *forscope;
8366 isn_T *isn;
8367
Bram Moolenaarfa984412021-03-25 22:15:28 +01008368 if (misplaced_cmdmod(cctx))
8369 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008371 if (scope == NULL || scope->se_type != FOR_SCOPE)
8372 {
8373 emsg(_(e_for));
8374 return NULL;
8375 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008376 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008377 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008378 if (cctx->ctx_skip != SKIP_YES)
8379 {
8380 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381
rbtnnbebf0692021-08-21 17:26:50 +02008382 // At end of ":for" scope jump back to the FOR instruction.
8383 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384
rbtnnbebf0692021-08-21 17:26:50 +02008385 // Fill in the "end" label in the FOR statement so it can jump here.
8386 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8387 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008388
rbtnnbebf0692021-08-21 17:26:50 +02008389 // Fill in the "end" label any BREAK statements
8390 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391
rbtnnbebf0692021-08-21 17:26:50 +02008392 // Below the ":for" scope drop the "expr" list from the stack.
8393 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8394 return NULL;
8395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396
8397 vim_free(scope);
8398
8399 return arg;
8400}
8401
8402/*
8403 * compile "while expr"
8404 *
8405 * Produces instructions:
8406 * top: EVAL expr Push result of "expr"
8407 * JUMP_IF_FALSE end jump if false
8408 * ... body ...
8409 * JUMP top Jump back to repeat
8410 * end:
8411 *
8412 */
8413 static char_u *
8414compile_while(char_u *arg, cctx_T *cctx)
8415{
8416 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008417 scope_T *scope;
8418
8419 scope = new_scope(cctx, WHILE_SCOPE);
8420 if (scope == NULL)
8421 return NULL;
8422
Bram Moolenaara91a7132021-03-25 21:12:15 +01008423 // "endwhile" jumps back here, one before when profiling or using cmdmods
8424 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425
8426 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008427 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008429
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008430 if (!ends_excmd2(arg, skipwhite(p)))
8431 {
8432 semsg(_(e_trailing_arg), p);
8433 return NULL;
8434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008435
rbtnnd895b1d2021-08-20 20:54:25 +02008436 if (cctx->ctx_skip != SKIP_YES)
8437 {
8438 if (bool_on_stack(cctx) == FAIL)
8439 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008440
rbtnnd895b1d2021-08-20 20:54:25 +02008441 // CMDMOD_REV must come before the jump
8442 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008443
rbtnnd895b1d2021-08-20 20:54:25 +02008444 // "while_end" is set when ":endwhile" is found
8445 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008447 return FAIL;
8448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449
8450 return p;
8451}
8452
8453/*
8454 * compile "endwhile"
8455 */
8456 static char_u *
8457compile_endwhile(char_u *arg, cctx_T *cctx)
8458{
8459 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008460 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008461
Bram Moolenaarfa984412021-03-25 22:15:28 +01008462 if (misplaced_cmdmod(cctx))
8463 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008464 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8465 {
8466 emsg(_(e_while));
8467 return NULL;
8468 }
8469 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008470 if (cctx->ctx_skip != SKIP_YES)
8471 {
8472 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473
Bram Moolenaarf002a412021-01-24 13:34:18 +01008474#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008475 // count the endwhile before jumping
8476 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008477#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008478
rbtnnd895b1d2021-08-20 20:54:25 +02008479 // At end of ":for" scope jump back to the FOR instruction.
8480 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008481
rbtnnd895b1d2021-08-20 20:54:25 +02008482 // Fill in the "end" label in the WHILE statement so it can jump here.
8483 // And in any jumps for ":break"
8484 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008485 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008486 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487
8488 vim_free(scope);
8489
8490 return arg;
8491}
8492
8493/*
8494 * compile "continue"
8495 */
8496 static char_u *
8497compile_continue(char_u *arg, cctx_T *cctx)
8498{
8499 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008500 int try_scopes = 0;
8501 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502
8503 for (;;)
8504 {
8505 if (scope == NULL)
8506 {
8507 emsg(_(e_continue));
8508 return NULL;
8509 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008510 if (scope->se_type == FOR_SCOPE)
8511 {
8512 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008513 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008514 }
8515 if (scope->se_type == WHILE_SCOPE)
8516 {
8517 loop_label = scope->se_u.se_while.ws_top_label;
8518 break;
8519 }
8520 if (scope->se_type == TRY_SCOPE)
8521 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008522 scope = scope->se_outer;
8523 }
8524
Bram Moolenaarc150c092021-02-13 15:02:46 +01008525 if (try_scopes > 0)
8526 // Inside one or more try/catch blocks we first need to jump to the
8527 // "finally" or "endtry" to cleanup.
8528 generate_TRYCONT(cctx, try_scopes, loop_label);
8529 else
8530 // Jump back to the FOR or WHILE instruction.
8531 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533 return arg;
8534}
8535
8536/*
8537 * compile "break"
8538 */
8539 static char_u *
8540compile_break(char_u *arg, cctx_T *cctx)
8541{
8542 scope_T *scope = cctx->ctx_scope;
8543 endlabel_T **el;
8544
8545 for (;;)
8546 {
8547 if (scope == NULL)
8548 {
8549 emsg(_(e_break));
8550 return NULL;
8551 }
8552 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8553 break;
8554 scope = scope->se_outer;
8555 }
8556
8557 // Jump to the end of the FOR or WHILE loop.
8558 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008559 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008561 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008562 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8563 return FAIL;
8564
8565 return arg;
8566}
8567
8568/*
8569 * compile "{" start of block
8570 */
8571 static char_u *
8572compile_block(char_u *arg, cctx_T *cctx)
8573{
8574 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8575 return NULL;
8576 return skipwhite(arg + 1);
8577}
8578
8579/*
8580 * compile end of block: drop one scope
8581 */
8582 static void
8583compile_endblock(cctx_T *cctx)
8584{
8585 scope_T *scope = cctx->ctx_scope;
8586
8587 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008588 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008589 vim_free(scope);
8590}
8591
8592/*
8593 * compile "try"
8594 * Creates a new scope for the try-endtry, pointing to the first catch and
8595 * finally.
8596 * Creates another scope for the "try" block itself.
8597 * TRY instruction sets up exception handling at runtime.
8598 *
8599 * "try"
8600 * TRY -> catch1, -> finally push trystack entry
8601 * ... try block
8602 * "throw {exception}"
8603 * EVAL {exception}
8604 * THROW create exception
8605 * ... try block
8606 * " catch {expr}"
8607 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008608 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609 * EVAL {expr}
8610 * MATCH
8611 * JUMP nomatch -> catch2
8612 * CATCH remove exception
8613 * ... catch block
8614 * " catch"
8615 * JUMP -> finally
8616 * catch2: CATCH remove exception
8617 * ... catch block
8618 * " finally"
8619 * finally:
8620 * ... finally block
8621 * " endtry"
8622 * ENDTRY pop trystack entry, may rethrow
8623 */
8624 static char_u *
8625compile_try(char_u *arg, cctx_T *cctx)
8626{
8627 garray_T *instr = &cctx->ctx_instr;
8628 scope_T *try_scope;
8629 scope_T *scope;
8630
Bram Moolenaarfa984412021-03-25 22:15:28 +01008631 if (misplaced_cmdmod(cctx))
8632 return NULL;
8633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634 // scope that holds the jumps that go to catch/finally/endtry
8635 try_scope = new_scope(cctx, TRY_SCOPE);
8636 if (try_scope == NULL)
8637 return NULL;
8638
Bram Moolenaar69f70502021-01-01 16:10:46 +01008639 if (cctx->ctx_skip != SKIP_YES)
8640 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008641 isn_T *isn;
8642
8643 // "try_catch" is set when the first ":catch" is found or when no catch
8644 // is found and ":finally" is found.
8645 // "try_finally" is set when ":finally" is found
8646 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008647 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008648 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8649 return NULL;
8650 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8651 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008652 return NULL;
8653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008654
8655 // scope for the try block itself
8656 scope = new_scope(cctx, BLOCK_SCOPE);
8657 if (scope == NULL)
8658 return NULL;
8659
8660 return arg;
8661}
8662
8663/*
8664 * compile "catch {expr}"
8665 */
8666 static char_u *
8667compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8668{
8669 scope_T *scope = cctx->ctx_scope;
8670 garray_T *instr = &cctx->ctx_instr;
8671 char_u *p;
8672 isn_T *isn;
8673
Bram Moolenaarfa984412021-03-25 22:15:28 +01008674 if (misplaced_cmdmod(cctx))
8675 return NULL;
8676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008677 // end block scope from :try or :catch
8678 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8679 compile_endblock(cctx);
8680 scope = cctx->ctx_scope;
8681
8682 // Error if not in a :try scope
8683 if (scope == NULL || scope->se_type != TRY_SCOPE)
8684 {
8685 emsg(_(e_catch));
8686 return NULL;
8687 }
8688
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008689 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008691 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692 return NULL;
8693 }
8694
Bram Moolenaar69f70502021-01-01 16:10:46 +01008695 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008696 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008697#ifdef FEAT_PROFILE
8698 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008699 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008700 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008701 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008702 .isn_type == ISN_PROF_START)
8703 --instr->ga_len;
8704#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008705 // Jump from end of previous block to :finally or :endtry
8706 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8707 JUMP_ALWAYS, cctx) == FAIL)
8708 return NULL;
8709
8710 // End :try or :catch scope: set value in ISN_TRY instruction
8711 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008712 if (isn->isn_arg.try.try_ref->try_catch == 0)
8713 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008714 if (scope->se_u.se_try.ts_catch_label != 0)
8715 {
8716 // Previous catch without match jumps here
8717 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8718 isn->isn_arg.jump.jump_where = instr->ga_len;
8719 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008720#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008721 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008722 {
8723 // a "throw" that jumps here needs to be counted
8724 generate_instr(cctx, ISN_PROF_END);
8725 // the "catch" is also counted
8726 generate_instr(cctx, ISN_PROF_START);
8727 }
8728#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008729 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008730 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008731 }
8732
8733 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008734 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008736 scope->se_u.se_try.ts_caught_all = TRUE;
8737 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738 }
8739 else
8740 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008741 char_u *end;
8742 char_u *pat;
8743 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008744 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008745 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 // Push v:exception, push {expr} and MATCH
8748 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8749
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008750 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008751 if (*end != *p)
8752 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008753 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008754 vim_free(tofree);
8755 return FAIL;
8756 }
8757 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008758 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008759 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008760 len = (int)(end - tofree);
8761 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008762 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008763 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008764 if (pat == NULL)
8765 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008766 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008767 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008769 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8770 return NULL;
8771
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008772 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008773 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8774 return NULL;
8775 }
8776
Bram Moolenaar69f70502021-01-01 16:10:46 +01008777 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 return NULL;
8779
8780 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8781 return NULL;
8782 return p;
8783}
8784
8785 static char_u *
8786compile_finally(char_u *arg, cctx_T *cctx)
8787{
8788 scope_T *scope = cctx->ctx_scope;
8789 garray_T *instr = &cctx->ctx_instr;
8790 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008791 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008792
Bram Moolenaarfa984412021-03-25 22:15:28 +01008793 if (misplaced_cmdmod(cctx))
8794 return NULL;
8795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008796 // end block scope from :try or :catch
8797 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8798 compile_endblock(cctx);
8799 scope = cctx->ctx_scope;
8800
8801 // Error if not in a :try scope
8802 if (scope == NULL || scope->se_type != TRY_SCOPE)
8803 {
8804 emsg(_(e_finally));
8805 return NULL;
8806 }
8807
rbtnn84934992021-08-07 13:26:53 +02008808 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809 {
rbtnn84934992021-08-07 13:26:53 +02008810 // End :catch or :finally scope: set value in ISN_TRY instruction
8811 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8812 if (isn->isn_arg.try.try_ref->try_finally != 0)
8813 {
8814 emsg(_(e_finally_dup));
8815 return NULL;
8816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008817
rbtnn84934992021-08-07 13:26:53 +02008818 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008819#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008820 if (cctx->ctx_compile_type == CT_PROFILE
8821 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008822 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008823 {
8824 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008825 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008826
8827 // jump to the profile end above it
8828 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008829 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008830 --this_instr;
8831 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008832#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008833
rbtnn84934992021-08-07 13:26:53 +02008834 // Fill in the "end" label in jumps at the end of the blocks.
8835 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008836 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008837
rbtnn84934992021-08-07 13:26:53 +02008838 // If there is no :catch then an exception jumps to :finally.
8839 if (isn->isn_arg.try.try_ref->try_catch == 0)
8840 isn->isn_arg.try.try_ref->try_catch = this_instr;
8841 isn->isn_arg.try.try_ref->try_finally = this_instr;
8842 if (scope->se_u.se_try.ts_catch_label != 0)
8843 {
8844 // Previous catch without match jumps here
8845 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8846 isn->isn_arg.jump.jump_where = this_instr;
8847 scope->se_u.se_try.ts_catch_label = 0;
8848 }
8849 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8850 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008852
8853 return arg;
8854}
8855
8856 static char_u *
8857compile_endtry(char_u *arg, cctx_T *cctx)
8858{
8859 scope_T *scope = cctx->ctx_scope;
8860 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008861 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008862
Bram Moolenaarfa984412021-03-25 22:15:28 +01008863 if (misplaced_cmdmod(cctx))
8864 return NULL;
8865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008866 // end block scope from :catch or :finally
8867 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8868 compile_endblock(cctx);
8869 scope = cctx->ctx_scope;
8870
8871 // Error if not in a :try scope
8872 if (scope == NULL || scope->se_type != TRY_SCOPE)
8873 {
8874 if (scope == NULL)
8875 emsg(_(e_no_endtry));
8876 else if (scope->se_type == WHILE_SCOPE)
8877 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008878 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008879 emsg(_(e_endfor));
8880 else
8881 emsg(_(e_endif));
8882 return NULL;
8883 }
8884
Bram Moolenaarc150c092021-02-13 15:02:46 +01008885 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008886 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008887 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008888 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8889 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008890 {
8891 emsg(_(e_missing_catch_or_finally));
8892 return NULL;
8893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008894
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008895#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008896 if (cctx->ctx_compile_type == CT_PROFILE
8897 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008898 .isn_type == ISN_PROF_START)
8899 // move the profile start after "endtry" so that it's not counted when
8900 // the exception is rethrown.
8901 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008902#endif
8903
Bram Moolenaar69f70502021-01-01 16:10:46 +01008904 // Fill in the "end" label in jumps at the end of the blocks, if not
8905 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008906 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8907 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008908
Bram Moolenaar69f70502021-01-01 16:10:46 +01008909 if (scope->se_u.se_try.ts_catch_label != 0)
8910 {
8911 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008912 isn_T *isn = ((isn_T *)instr->ga_data)
8913 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008914 isn->isn_arg.jump.jump_where = instr->ga_len;
8915 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008916 }
8917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008918 compile_endblock(cctx);
8919
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008920 if (cctx->ctx_skip != SKIP_YES)
8921 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008922 // End :catch or :finally scope: set instruction index in ISN_TRY
8923 // instruction
8924 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008925 if (cctx->ctx_skip != SKIP_YES
8926 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8927 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008928#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008929 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008930 generate_instr(cctx, ISN_PROF_START);
8931#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008933 return arg;
8934}
8935
8936/*
8937 * compile "throw {expr}"
8938 */
8939 static char_u *
8940compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8941{
8942 char_u *p = skipwhite(arg);
8943
Bram Moolenaara5565e42020-05-09 15:44:01 +02008944 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008945 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008946 if (cctx->ctx_skip == SKIP_YES)
8947 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008948 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008949 return NULL;
8950 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8951 return NULL;
8952
8953 return p;
8954}
8955
Bram Moolenaarc3235272021-07-10 19:42:03 +02008956 static char_u *
8957compile_eval(char_u *arg, cctx_T *cctx)
8958{
8959 char_u *p = arg;
8960 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008961 long lnum = SOURCING_LNUM;
8962
8963 // find_ex_command() will consider a variable name an expression, assuming
8964 // that something follows on the next line. Check that something actually
8965 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008966 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008967
Bram Moolenaarc3235272021-07-10 19:42:03 +02008968 if (compile_expr0(&p, cctx) == FAIL)
8969 return NULL;
8970
8971 if (name_only && lnum == SOURCING_LNUM)
8972 {
8973 semsg(_(e_expression_without_effect_str), arg);
8974 return NULL;
8975 }
8976
8977 // drop the result
8978 generate_instr_drop(cctx, ISN_DROP, 1);
8979
8980 return skipwhite(p);
8981}
8982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008983/*
8984 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008985 * compile "echomsg expr"
8986 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008987 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008988 * compile "execute expr"
8989 */
8990 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008991compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008992{
8993 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008994 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008995 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008996 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008997 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008998 garray_T *stack = &cctx->ctx_type_stack;
8999 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009000
9001 for (;;)
9002 {
Bram Moolenaare4984292020-12-13 14:19:25 +01009003 if (ends_excmd2(prev, p))
9004 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009005 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009006 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01009007 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02009008
9009 if (cctx->ctx_skip != SKIP_YES)
9010 {
9011 // check for non-void type
9012 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9013 if (type->tt_type == VAR_VOID)
9014 {
9015 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
9016 return NULL;
9017 }
9018 }
9019
Bram Moolenaarad39c092020-02-26 18:23:43 +01009020 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02009021 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01009022 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009023 }
9024
Bram Moolenaare4984292020-12-13 14:19:25 +01009025 if (count > 0)
9026 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009027 long save_lnum = cctx->ctx_lnum;
9028
9029 // Use the line number where the command started.
9030 cctx->ctx_lnum = start_ctx_lnum;
9031
Bram Moolenaare4984292020-12-13 14:19:25 +01009032 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
9033 generate_ECHO(cctx, cmdidx == CMD_echo, count);
9034 else if (cmdidx == CMD_execute)
9035 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
9036 else if (cmdidx == CMD_echomsg)
9037 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02009038 else if (cmdidx == CMD_echoconsole)
9039 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01009040 else
9041 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02009042
9043 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01009044 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009045 return p;
9046}
9047
9048/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009049 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009050 * instruction to compute it and return OK.
9051 * Otherwise return FAIL, the caller must deal with any range.
9052 */
9053 static int
9054compile_variable_range(exarg_T *eap, cctx_T *cctx)
9055{
9056 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9057 char_u *p = skipdigits(eap->cmd);
9058
9059 if (p == range_end)
9060 return FAIL;
9061 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9062}
9063
9064/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009065 * :put r
9066 * :put ={expr}
9067 */
9068 static char_u *
9069compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9070{
9071 char_u *line = arg;
9072 linenr_T lnum;
9073 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009074 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009075
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009076 eap->regname = *line;
9077
9078 if (eap->regname == '=')
9079 {
9080 char_u *p = line + 1;
9081
9082 if (compile_expr0(&p, cctx) == FAIL)
9083 return NULL;
9084 line = p;
9085 }
9086 else if (eap->regname != NUL)
9087 ++line;
9088
Bram Moolenaar08597872020-12-10 19:43:40 +01009089 if (compile_variable_range(eap, cctx) == OK)
9090 {
9091 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9092 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009093 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009094 {
9095 // Either no range or a number.
9096 // "errormsg" will not be set because the range is ADDR_LINES.
9097 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009098 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009099 return NULL;
9100 if (eap->addr_count == 0)
9101 lnum = -1;
9102 else
9103 lnum = eap->line2;
9104 if (above)
9105 --lnum;
9106 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009107
9108 generate_PUT(cctx, eap->regname, lnum);
9109 return line;
9110}
9111
9112/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009113 * A command that is not compiled, execute with legacy code.
9114 */
9115 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009116compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009117{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009118 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009119 char_u *p;
9120 int has_expr = FALSE;
9121 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009122 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009123 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009124
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009125 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009126 goto theend;
9127
Bram Moolenaare729ce22021-06-06 21:38:09 +02009128 // If there was a prececing command modifier, drop it and include it in the
9129 // EXEC command.
9130 if (cctx->ctx_has_cmdmod)
9131 {
9132 garray_T *instr = &cctx->ctx_instr;
9133 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9134
9135 if (isn->isn_type == ISN_CMDMOD)
9136 {
9137 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9138 ->cmod_filter_regmatch.regprog);
9139 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9140 --instr->ga_len;
9141 cctx->ctx_has_cmdmod = FALSE;
9142 }
9143 }
9144
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009145 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009146 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009147 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009148 int usefilter = FALSE;
9149
9150 has_expr = argt & (EX_XFILE | EX_EXPAND);
9151
9152 // If the command can be followed by a bar, find the bar and truncate
9153 // it, so that the following command can be compiled.
9154 // The '|' is overwritten with a NUL, it is put back below.
9155 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9156 && *eap->arg == '!')
9157 // :w !filter or :r !filter or :r! filter
9158 usefilter = TRUE;
9159 if ((argt & EX_TRLBAR) && !usefilter)
9160 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009161 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009162 separate_nextcmd(eap);
9163 if (eap->nextcmd != NULL)
9164 nextcmd = eap->nextcmd;
9165 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009166 else if (eap->cmdidx == CMD_wincmd)
9167 {
9168 p = eap->arg;
9169 if (*p != NUL)
9170 ++p;
9171 if (*p == 'g' || *p == Ctrl_G)
9172 ++p;
9173 p = skipwhite(p);
9174 if (*p == '|')
9175 {
9176 *p = NUL;
9177 nextcmd = p + 1;
9178 }
9179 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009180 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9181 {
9182 // If there is a trailing '{' read lines until the '}'
9183 p = eap->arg + STRLEN(eap->arg) - 1;
9184 while (p > eap->arg && VIM_ISWHITE(*p))
9185 --p;
9186 if (*p == '{')
9187 {
9188 exarg_T ea;
9189 int flags; // unused
9190 int start_lnum = SOURCING_LNUM;
9191
9192 CLEAR_FIELD(ea);
9193 ea.arg = eap->arg;
9194 fill_exarg_from_cctx(&ea, cctx);
9195 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9196 if (tofree != NULL)
9197 {
9198 *p = NUL;
9199 line = concat_str(line, tofree);
9200 if (line == NULL)
9201 goto theend;
9202 vim_free(tofree);
9203 tofree = line;
9204 SOURCING_LNUM = start_lnum;
9205 }
9206 }
9207 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009208 }
9209
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009210 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9211 {
9212 // expand filename in "syntax include [@group] filename"
9213 has_expr = TRUE;
9214 eap->arg = skipwhite(eap->arg + 7);
9215 if (*eap->arg == '@')
9216 eap->arg = skiptowhite(eap->arg);
9217 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009218
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009219 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9220 && STRLEN(eap->arg) > 4)
9221 {
9222 int delim = *eap->arg;
9223
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009224 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009225 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009226 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009227 }
9228
Bram Moolenaarecac5912021-01-05 19:23:28 +01009229 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009230 cmd_arg = eap->arg;
9231
9232 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009233 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009234 exarg_T nea;
9235
9236 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009237 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009238 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009239 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009240 {
9241 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9242 if (has_expr)
9243 eap->arg = skiptowhite(eap->arg);
9244 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009245 }
9246
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009247 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009248 {
9249 int count = 0;
9250 char_u *start = skipwhite(line);
9251
9252 // :cmd xxx`=expr1`yyy`=expr2`zzz
9253 // PUSHS ":cmd xxx"
9254 // eval expr1
9255 // PUSHS "yyy"
9256 // eval expr2
9257 // PUSHS "zzz"
9258 // EXECCONCAT 5
9259 for (;;)
9260 {
9261 if (p > start)
9262 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009263 char_u *val = vim_strnsave(start, p - start);
9264
9265 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009266 ++count;
9267 }
9268 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009269 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009270 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009271 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009272 ++count;
9273 p = skipwhite(p);
9274 if (*p != '`')
9275 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009276 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009277 return NULL;
9278 }
9279 start = p + 1;
9280
9281 p = (char_u *)strstr((char *)start, "`=");
9282 if (p == NULL)
9283 {
9284 if (*skipwhite(start) != NUL)
9285 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009286 char_u *val = vim_strsave(start);
9287
9288 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009289 ++count;
9290 }
9291 break;
9292 }
9293 }
9294 generate_EXECCONCAT(cctx, count);
9295 }
9296 else
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009297 generate_EXEC_copy(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009298
9299theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009300 if (*nextcmd != NUL)
9301 {
9302 // the parser expects a pointer to the bar, put it back
9303 --nextcmd;
9304 *nextcmd = '|';
9305 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009306 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009307
9308 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009309}
9310
Bram Moolenaar20677332021-06-06 17:02:53 +02009311/*
9312 * A script command with heredoc, e.g.
9313 * ruby << EOF
9314 * command
9315 * EOF
9316 * Has been turned into one long line with NL characters by
9317 * get_function_body():
9318 * ruby << EOF<NL> command<NL>EOF
9319 */
9320 static char_u *
9321compile_script(char_u *line, cctx_T *cctx)
9322{
9323 if (cctx->ctx_skip != SKIP_YES)
9324 {
9325 isn_T *isn;
9326
9327 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9328 return NULL;
9329 isn->isn_arg.string = vim_strsave(line);
9330 }
9331 return (char_u *)"";
9332}
9333
Bram Moolenaar8238f082021-04-20 21:10:48 +02009334
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009335/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009336 * :s/pat/repl/
9337 */
9338 static char_u *
9339compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9340{
9341 char_u *cmd = eap->arg;
9342 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9343
9344 if (expr != NULL)
9345 {
9346 int delimiter = *cmd++;
9347
9348 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009349 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009350 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9351 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009352 garray_T save_ga = cctx->ctx_instr;
9353 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009354 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009355 int trailing_error;
9356 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009357 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009358 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009359
9360 cmd += 3;
9361 end = skip_substitute(cmd, delimiter);
9362
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009363 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009364 // labels are correct.
9365 cctx->ctx_instr.ga_len = 0;
9366 cctx->ctx_instr.ga_maxlen = 0;
9367 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009368 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009369 if (end[-1] == NUL)
9370 end[-1] = delimiter;
9371 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009372 trailing_error = *cmd != delimiter && *cmd != NUL;
9373
Bram Moolenaar169502f2021-04-21 12:19:35 +02009374 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009375 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009376 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009377 if (trailing_error)
9378 semsg(_(e_trailing_arg), cmd);
9379 clear_instr_ga(&cctx->ctx_instr);
9380 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009381 return NULL;
9382 }
9383
Bram Moolenaar8238f082021-04-20 21:10:48 +02009384 // Move the generated instructions into the ISN_SUBSTITUTE
9385 // instructions, then restore the list of instructions before
9386 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009387 instr_count = cctx->ctx_instr.ga_len;
9388 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009389 instr[instr_count].isn_type = ISN_FINISH;
9390
9391 cctx->ctx_instr = save_ga;
9392 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9393 {
9394 int idx;
9395
9396 for (idx = 0; idx < instr_count; ++idx)
9397 delete_instr(instr + idx);
9398 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009399 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009400 }
9401 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9402 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009403
9404 // skip over flags
9405 if (*end == '&')
9406 ++end;
9407 while (ASCII_ISALPHA(*end) || *end == '#')
9408 ++end;
9409 return end;
9410 }
9411 }
9412
9413 return compile_exec(arg, eap, cctx);
9414}
9415
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009416 static char_u *
9417compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9418{
9419 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009420 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009421
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009422 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009423 {
9424 if (STRNCMP(arg, "END", 3) == 0)
9425 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009426 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009427 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009428 // First load the current variable value.
9429 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9430 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009431 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009432 }
9433
9434 // Gets the redirected text and put it on the stack, then store it
9435 // in the variable.
9436 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9437
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009438 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009439 generate_instr_drop(cctx, ISN_CONCAT, 1);
9440
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009441 if (lhs->lhs_has_index)
9442 {
9443 // Use the info in "lhs" to store the value at the index in the
9444 // list or dict.
9445 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9446 &t_string, cctx) == FAIL)
9447 return NULL;
9448 }
9449 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009450 return NULL;
9451
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009452 VIM_CLEAR(lhs->lhs_name);
9453 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009454 return arg + 3;
9455 }
9456 emsg(_(e_cannot_nest_redir));
9457 return NULL;
9458 }
9459
9460 if (arg[0] == '=' && arg[1] == '>')
9461 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009462 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009463
9464 // redirect to a variable is compiled
9465 arg += 2;
9466 if (*arg == '>')
9467 {
9468 ++arg;
9469 append = TRUE;
9470 }
9471 arg = skipwhite(arg);
9472
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009473 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009474 FALSE, FALSE, 1, cctx) == FAIL)
9475 return NULL;
9476 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009477 lhs->lhs_append = append;
9478 if (lhs->lhs_has_index)
9479 {
9480 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9481 if (lhs->lhs_whole == NULL)
9482 return NULL;
9483 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009484
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009485 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009486 }
9487
9488 // other redirects are handled like at script level
9489 return compile_exec(line, eap, cctx);
9490}
9491
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009492#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009493 static char_u *
9494compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9495{
9496 isn_T *isn;
9497 char_u *p;
9498
9499 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9500 if (isn == NULL)
9501 return NULL;
9502 isn->isn_arg.number = eap->cmdidx;
9503
9504 p = eap->arg;
9505 if (compile_expr0(&p, cctx) == FAIL)
9506 return NULL;
9507
9508 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9509 if (isn == NULL)
9510 return NULL;
9511 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9512 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9513 return NULL;
9514 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9515 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9516 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9517
9518 return p;
9519}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009520#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009521
Bram Moolenaar4c137212021-04-19 16:48:48 +02009522/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009523 * Check if the separator for a :global or :substitute command is OK.
9524 */
9525 int
9526check_global_and_subst(char_u *cmd, char_u *arg)
9527{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009528 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009529 {
9530 semsg(_(e_separator_not_supported_str), arg);
9531 return FAIL;
9532 }
9533 if (VIM_ISWHITE(cmd[1]))
9534 {
9535 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9536 return FAIL;
9537 }
9538 return OK;
9539}
9540
9541
9542/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009543 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009544 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009545 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009546 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009547add_def_function(ufunc_T *ufunc)
9548{
9549 dfunc_T *dfunc;
9550
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009551 if (def_functions.ga_len == 0)
9552 {
9553 // The first position is not used, so that a zero uf_dfunc_idx means it
9554 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009555 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009556 return FAIL;
9557 ++def_functions.ga_len;
9558 }
9559
Bram Moolenaar09689a02020-05-09 22:50:08 +02009560 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009561 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009562 return FAIL;
9563 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9564 CLEAR_POINTER(dfunc);
9565 dfunc->df_idx = def_functions.ga_len;
9566 ufunc->uf_dfunc_idx = dfunc->df_idx;
9567 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009568 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009569 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009570 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009571 ++def_functions.ga_len;
9572 return OK;
9573}
9574
9575/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009576 * After ex_function() has collected all the function lines: parse and compile
9577 * the lines into instructions.
9578 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009579 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9580 * the return statement (used for lambda). When uf_ret_type is already set
9581 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009582 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009583 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009584 * This can be used recursively through compile_lambda(), which may reallocate
9585 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009586 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009587 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009588 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009589compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009590 ufunc_T *ufunc,
9591 int check_return_type,
9592 compiletype_T compile_type,
9593 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009594{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009595 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009596 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009597 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009598 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009599 cctx_T cctx;
9600 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009601 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009602 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009603 int ret = FAIL;
9604 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009605 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009606 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009607 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009608 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009609#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009610 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009611#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009612 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009613
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009614 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009615 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009616 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009617 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009618 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9619 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009620 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009621
9622 switch (compile_type)
9623 {
9624 case CT_PROFILE:
9625#ifdef FEAT_PROFILE
9626 instr_dest = dfunc->df_instr_prof; break;
9627#endif
9628 case CT_NONE: instr_dest = dfunc->df_instr; break;
9629 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9630 }
9631 if (instr_dest != NULL)
9632 // Was compiled in this mode before: Free old instructions.
9633 delete_def_function_contents(dfunc, FALSE);
9634 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009635 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009636 else
9637 {
9638 if (add_def_function(ufunc) == FAIL)
9639 return FAIL;
9640 new_def_function = TRUE;
9641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009642
Bram Moolenaar985116a2020-07-12 17:31:09 +02009643 ufunc->uf_def_status = UF_COMPILING;
9644
Bram Moolenaara80faa82020-04-12 19:37:17 +02009645 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009646
Bram Moolenaare99d4222021-06-13 14:01:26 +02009647 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009648 cctx.ctx_ufunc = ufunc;
9649 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009650 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009651 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9652 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9653 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9654 cctx.ctx_type_list = &ufunc->uf_type_list;
9655 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9656 instr = &cctx.ctx_instr;
9657
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009658 // Set the context to the function, it may be compiled when called from
9659 // another script. Set the script version to the most modern one.
9660 // The line number will be set in next_line_from_context().
9661 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009662 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9663
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009664 // Don't use the flag from ":legacy" here.
9665 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9666
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009667 // Make sure error messages are OK.
9668 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9669 if (do_estack_push)
9670 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009671 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009672
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009673 if (ufunc->uf_def_args.ga_len > 0)
9674 {
9675 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009676 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009677 int i;
9678 char_u *arg;
9679 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009680 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009681
9682 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009683 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009684 for (i = 0; i < count; ++i)
9685 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009686 garray_T *stack = &cctx.ctx_type_stack;
9687 type_T *val_type;
9688 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009689 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009690 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009691 int jump_instr_idx = instr->ga_len;
9692 isn_T *isn;
9693
9694 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9695 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9696 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009697
9698 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009699 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009700
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009701 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009702 r = compile_expr0(&arg, &cctx);
9703
Bram Moolenaar12bce952021-03-11 20:04:04 +01009704 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009705 goto erret;
9706
9707 // If no type specified use the type of the default value.
9708 // Otherwise check that the default value type matches the
9709 // specified type.
9710 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009711 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009712 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009713 {
9714 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009715 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009716 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009717 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009718 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009719 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009720
9721 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009722 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009723
9724 // set instruction index in JUMP_IF_ARG_SET to here
9725 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9726 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009727 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009728
9729 if (did_set_arg_type)
9730 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009731 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009732 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009733
9734 /*
9735 * Loop over all the lines of the function and generate instructions.
9736 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009737 for (;;)
9738 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009739 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009740 int starts_with_colon = FALSE;
9741 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009742 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009743
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009744 // Bail out on the first error to avoid a flood of errors and report
9745 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009746 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009747 goto erret;
9748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009749 if (line != NULL && *line == '|')
9750 // the line continues after a '|'
9751 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009752 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009753 && !(*line == '#' && (line == cctx.ctx_line_start
9754 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009755 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009756 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009757 goto erret;
9758 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009759 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9760 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009761 else
9762 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009763 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009764 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009765 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009766 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009767#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009768 if (cctx.ctx_skip != SKIP_YES)
9769 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009770#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009771 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009772 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009773 // Make a copy, splitting off nextcmd and removing trailing spaces
9774 // may change it.
9775 if (line != NULL)
9776 {
9777 line = vim_strsave(line);
9778 vim_free(line_to_free);
9779 line_to_free = line;
9780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009781 }
9782
Bram Moolenaara80faa82020-04-12 19:37:17 +02009783 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009784 ea.cmdlinep = &line;
9785 ea.cmd = skipwhite(line);
9786
Bram Moolenaarb2049902021-01-24 12:53:53 +01009787 if (*ea.cmd == '#')
9788 {
9789 // "#" starts a comment
9790 line = (char_u *)"";
9791 continue;
9792 }
9793
Bram Moolenaarf002a412021-01-24 13:34:18 +01009794#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009795 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9796 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009797 {
9798 may_generate_prof_end(&cctx, prof_lnum);
9799
9800 prof_lnum = cctx.ctx_lnum;
9801 generate_instr(&cctx, ISN_PROF_START);
9802 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009803#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009804 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9805 && cctx.ctx_skip != SKIP_YES)
9806 {
9807 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009808 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009809 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009810 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009811
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009812 // Some things can be recognized by the first character.
9813 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009814 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009815 case '}':
9816 {
9817 // "}" ends a block scope
9818 scopetype_T stype = cctx.ctx_scope == NULL
9819 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009820
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009821 if (stype == BLOCK_SCOPE)
9822 {
9823 compile_endblock(&cctx);
9824 line = ea.cmd;
9825 }
9826 else
9827 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009828 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009829 goto erret;
9830 }
9831 if (line != NULL)
9832 line = skipwhite(ea.cmd + 1);
9833 continue;
9834 }
9835
9836 case '{':
9837 // "{" starts a block scope
9838 // "{'a': 1}->func() is something else
9839 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9840 {
9841 line = compile_block(ea.cmd, &cctx);
9842 continue;
9843 }
9844 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009845 }
9846
9847 /*
9848 * COMMAND MODIFIERS
9849 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009850 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009851 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9852 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009853 {
9854 if (errormsg != NULL)
9855 goto erret;
9856 // empty line or comment
9857 line = (char_u *)"";
9858 continue;
9859 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009860 generate_cmdmods(&cctx, &local_cmdmod);
9861 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009862
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009863 // Check if there was a colon after the last command modifier or before
9864 // the current position.
9865 for (p = ea.cmd; p >= line; --p)
9866 {
9867 if (*p == ':')
9868 starts_with_colon = TRUE;
9869 if (p < ea.cmd && !VIM_ISWHITE(*p))
9870 break;
9871 }
9872
Bram Moolenaarce024c32021-06-26 13:00:49 +02009873 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009874 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009875 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009876 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009877 if (checkforcmd(&ea.cmd, "call", 3))
9878 {
9879 if (*ea.cmd == '(')
9880 // not for "call()"
9881 ea.cmd = p;
9882 else
9883 ea.cmd = skipwhite(ea.cmd);
9884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009885
Bram Moolenaarce024c32021-06-26 13:00:49 +02009886 if (!starts_with_colon)
9887 {
9888 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009889
Bram Moolenaarce024c32021-06-26 13:00:49 +02009890 // Check for assignment after command modifiers.
9891 assign = may_compile_assignment(&ea, &line, &cctx);
9892 if (assign == OK)
9893 goto nextline;
9894 if (assign == FAIL)
9895 goto erret;
9896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009897 }
9898
9899 /*
9900 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009901 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009902 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009903 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009904 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009905 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009906 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009907 && (starts_with_colon || !(*cmd == '\''
9908 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009909 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009910 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009911 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009912 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009913 if (!starts_with_colon
9914 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009915 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009916 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009917 goto erret;
9918 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009919 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009920 if (ends_excmd2(line, ea.cmd))
9921 {
9922 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009923 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00009924 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00009925 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009926 goto nextline;
9927 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009928 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009929 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009930 p = find_ex_command(&ea, NULL,
9931 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009932 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009933
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009934 if (p == NULL)
9935 {
9936 if (cctx.ctx_skip != SKIP_YES)
9937 emsg(_(e_ambiguous_use_of_user_defined_command));
9938 goto erret;
9939 }
9940
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009941 // When using ":legacy cmd" always use compile_exec().
9942 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009943 {
9944 char_u *start = ea.cmd;
9945
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009946 switch (ea.cmdidx)
9947 {
9948 case CMD_if:
9949 case CMD_elseif:
9950 case CMD_else:
9951 case CMD_endif:
9952 case CMD_for:
9953 case CMD_endfor:
9954 case CMD_continue:
9955 case CMD_break:
9956 case CMD_while:
9957 case CMD_endwhile:
9958 case CMD_try:
9959 case CMD_catch:
9960 case CMD_finally:
9961 case CMD_endtry:
9962 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9963 goto erret;
9964 default: break;
9965 }
9966
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009967 // ":legacy return expr" needs to be handled differently.
9968 if (checkforcmd(&start, "return", 4))
9969 ea.cmdidx = CMD_return;
9970 else
9971 ea.cmdidx = CMD_legacy;
9972 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009974 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9975 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009976 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009977 {
9978 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009979 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009980 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009981 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009982 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009983 // CMD_var cannot happen, compile_assignment() above would be
9984 // used. Most likely an assignment to a non-existing variable.
9985 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009986 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009988 }
9989
Bram Moolenaar3988f642020-08-27 22:43:03 +02009990 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009991 && ea.cmdidx != CMD_elseif
9992 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009993 && ea.cmdidx != CMD_endif
9994 && ea.cmdidx != CMD_endfor
9995 && ea.cmdidx != CMD_endwhile
9996 && ea.cmdidx != CMD_catch
9997 && ea.cmdidx != CMD_finally
9998 && ea.cmdidx != CMD_endtry)
9999 {
Bram Moolenaar3988f642020-08-27 22:43:03 +020010000 emsg(_(e_unreachable_code_after_return));
10001 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +020010002 }
10003
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010004 p = skipwhite(p);
10005 if (ea.cmdidx != CMD_SIZE
10006 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
10007 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +020010008 if (ea.cmdidx >= 0)
10009 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010010 if ((ea.argt & EX_BANG) && *p == '!')
10011 {
10012 ea.forceit = TRUE;
10013 p = skipwhite(p + 1);
10014 }
10015 }
10016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010017 switch (ea.cmdidx)
10018 {
10019 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +000010020 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +020010021 ea.arg = p;
10022 line = compile_nested_function(&ea, &cctx);
10023 break;
10024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010025 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010026 line = compile_return(p, check_return_type,
10027 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010028 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010029 break;
10030
10031 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +020010032 emsg(_(e_cannot_use_let_in_vim9_script));
10033 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010034 case CMD_var:
10035 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010036 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +020010037 case CMD_increment:
10038 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010039 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +020010040 if (line == p)
10041 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010042 break;
10043
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010044 case CMD_unlet:
10045 case CMD_unlockvar:
10046 case CMD_lockvar:
10047 line = compile_unletlock(p, &ea, &cctx);
10048 break;
10049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010050 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010051 emsg(_(e_import_can_only_be_used_in_script));
10052 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010053 break;
10054
10055 case CMD_if:
10056 line = compile_if(p, &cctx);
10057 break;
10058 case CMD_elseif:
10059 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010060 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010061 break;
10062 case CMD_else:
10063 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010064 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010065 break;
10066 case CMD_endif:
10067 line = compile_endif(p, &cctx);
10068 break;
10069
10070 case CMD_while:
10071 line = compile_while(p, &cctx);
10072 break;
10073 case CMD_endwhile:
10074 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010075 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010076 break;
10077
10078 case CMD_for:
10079 line = compile_for(p, &cctx);
10080 break;
10081 case CMD_endfor:
10082 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010083 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010084 break;
10085 case CMD_continue:
10086 line = compile_continue(p, &cctx);
10087 break;
10088 case CMD_break:
10089 line = compile_break(p, &cctx);
10090 break;
10091
10092 case CMD_try:
10093 line = compile_try(p, &cctx);
10094 break;
10095 case CMD_catch:
10096 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010097 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010098 break;
10099 case CMD_finally:
10100 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010101 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010102 break;
10103 case CMD_endtry:
10104 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010105 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010106 break;
10107 case CMD_throw:
10108 line = compile_throw(p, &cctx);
10109 break;
10110
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010111 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010112 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010113 break;
10114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010115 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010116 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010117 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010118 case CMD_echomsg:
10119 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010120 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010121 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010122 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010123
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010124 case CMD_put:
10125 ea.cmd = cmd;
10126 line = compile_put(p, &ea, &cctx);
10127 break;
10128
Bram Moolenaar4c137212021-04-19 16:48:48 +020010129 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010130 if (check_global_and_subst(ea.cmd, p) == FAIL)
10131 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010132 if (cctx.ctx_skip == SKIP_YES)
10133 line = (char_u *)"";
10134 else
10135 {
10136 ea.arg = p;
10137 line = compile_substitute(line, &ea, &cctx);
10138 }
10139 break;
10140
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010141 case CMD_redir:
10142 ea.arg = p;
10143 line = compile_redir(line, &ea, &cctx);
10144 break;
10145
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010146 case CMD_cexpr:
10147 case CMD_lexpr:
10148 case CMD_caddexpr:
10149 case CMD_laddexpr:
10150 case CMD_cgetexpr:
10151 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010152#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010153 ea.arg = p;
10154 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010155#else
10156 ex_ni(&ea);
10157 line = NULL;
10158#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010159 break;
10160
Bram Moolenaarae616492020-07-28 20:07:27 +020010161 case CMD_append:
10162 case CMD_change:
10163 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010164 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010165 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010166 case CMD_xit:
10167 not_in_vim9(&ea);
10168 goto erret;
10169
Bram Moolenaar002262f2020-07-08 17:47:57 +020010170 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010171 if (cctx.ctx_skip != SKIP_YES)
10172 {
10173 semsg(_(e_invalid_command_str), ea.cmd);
10174 goto erret;
10175 }
10176 // We don't check for a next command here.
10177 line = (char_u *)"";
10178 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010179
Bram Moolenaar20677332021-06-06 17:02:53 +020010180 case CMD_lua:
10181 case CMD_mzscheme:
10182 case CMD_perl:
10183 case CMD_py3:
10184 case CMD_python3:
10185 case CMD_python:
10186 case CMD_pythonx:
10187 case CMD_ruby:
10188 case CMD_tcl:
10189 ea.arg = p;
10190 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010191 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010192 else
10193 // heredoc lines have been concatenated with NL
10194 // characters in get_function_body()
10195 line = compile_script(line, &cctx);
10196 break;
10197
Bram Moolenaar7b829262021-10-13 15:04:34 +010010198 case CMD_global:
10199 if (check_global_and_subst(ea.cmd, p) == FAIL)
10200 goto erret;
10201 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010202 default:
10203 // Not recognized, execute with do_cmdline_cmd().
10204 ea.arg = p;
10205 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010206 break;
10207 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010208nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010209 if (line == NULL)
10210 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010211 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010212
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010213 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010214 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010216 if (cctx.ctx_type_stack.ga_len < 0)
10217 {
10218 iemsg("Type stack underflow");
10219 goto erret;
10220 }
10221 }
10222
10223 if (cctx.ctx_scope != NULL)
10224 {
10225 if (cctx.ctx_scope->se_type == IF_SCOPE)
10226 emsg(_(e_endif));
10227 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10228 emsg(_(e_endwhile));
10229 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10230 emsg(_(e_endfor));
10231 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010232 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010233 goto erret;
10234 }
10235
Bram Moolenaarefd88552020-06-18 20:50:10 +020010236 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010237 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010238 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10239 ufunc->uf_ret_type = &t_void;
10240 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010241 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010242 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010243 goto erret;
10244 }
10245
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010246 // Return void if there is no return at the end.
10247 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010248 }
10249
Bram Moolenaar599410c2021-04-10 14:03:43 +020010250 // When compiled with ":silent!" and there was an error don't consider the
10251 // function compiled.
10252 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010253 {
10254 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10255 + ufunc->uf_dfunc_idx;
10256 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010257 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010258#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010259 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010260 {
10261 dfunc->df_instr_prof = instr->ga_data;
10262 dfunc->df_instr_prof_count = instr->ga_len;
10263 }
10264 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010265#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010266 if (cctx.ctx_compile_type == CT_DEBUG)
10267 {
10268 dfunc->df_instr_debug = instr->ga_data;
10269 dfunc->df_instr_debug_count = instr->ga_len;
10270 }
10271 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010272 {
10273 dfunc->df_instr = instr->ga_data;
10274 dfunc->df_instr_count = instr->ga_len;
10275 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010276 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010277 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010278 if (cctx.ctx_outer_used)
10279 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010280 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010282
10283 ret = OK;
10284
10285erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010286 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010287 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010288 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10289 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010290
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010291 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010292 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010293 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010294 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010295
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010296 // If using the last entry in the table and it was added above, we
10297 // might as well remove it.
10298 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010299 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010300 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010301 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010302 ufunc->uf_dfunc_idx = 0;
10303 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010304 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010305
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010306 while (cctx.ctx_scope != NULL)
10307 drop_scope(&cctx);
10308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010309 if (errormsg != NULL)
10310 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010311 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010312 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010313 }
10314
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010315 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10316 {
10317 if (ret == OK)
10318 {
10319 emsg(_(e_missing_redir_end));
10320 ret = FAIL;
10321 }
10322 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010323 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010324 }
10325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010326 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010327 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010328 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010329 if (do_estack_push)
10330 estack_pop();
10331
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010332 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010333 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010334 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010335 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010336 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010337}
10338
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010339 void
10340set_function_type(ufunc_T *ufunc)
10341{
10342 int varargs = ufunc->uf_va_name != NULL;
10343 int argcount = ufunc->uf_args.ga_len;
10344
10345 // Create a type for the function, with the return type and any
10346 // argument types.
10347 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10348 // The type is included in "tt_args".
10349 if (argcount > 0 || varargs)
10350 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010351 if (ufunc->uf_type_list.ga_itemsize == 0)
10352 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010353 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10354 argcount, &ufunc->uf_type_list);
10355 // Add argument types to the function type.
10356 if (func_type_add_arg_types(ufunc->uf_func_type,
10357 argcount + varargs,
10358 &ufunc->uf_type_list) == FAIL)
10359 return;
10360 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10361 ufunc->uf_func_type->tt_min_argcount =
10362 argcount - ufunc->uf_def_args.ga_len;
10363 if (ufunc->uf_arg_types == NULL)
10364 {
10365 int i;
10366
10367 // lambda does not have argument types.
10368 for (i = 0; i < argcount; ++i)
10369 ufunc->uf_func_type->tt_args[i] = &t_any;
10370 }
10371 else
10372 mch_memmove(ufunc->uf_func_type->tt_args,
10373 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10374 if (varargs)
10375 {
10376 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010377 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010378 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10379 }
10380 }
10381 else
10382 // No arguments, can use a predefined type.
10383 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10384 argcount, &ufunc->uf_type_list);
10385}
10386
10387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010388/*
10389 * Delete an instruction, free what it contains.
10390 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010391 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010392delete_instr(isn_T *isn)
10393{
10394 switch (isn->isn_type)
10395 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010396 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010397 case ISN_EXEC:
Bram Moolenaare4eed8c2021-12-01 15:22:56 +000010398 case ISN_EXECRANGE:
Bram Moolenaar20677332021-06-06 17:02:53 +020010399 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010400 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010401 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010402 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010403 case ISN_LOADENV:
10404 case ISN_LOADG:
10405 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010406 case ISN_LOADT:
10407 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010408 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010409 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010410 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010411 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010412 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010413 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010414 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010415 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010416 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010417 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010418 case ISN_STOREW:
10419 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010420 vim_free(isn->isn_arg.string);
10421 break;
10422
Bram Moolenaar4c137212021-04-19 16:48:48 +020010423 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010424 {
10425 int idx;
10426 isn_T *list = isn->isn_arg.subs.subs_instr;
10427
10428 vim_free(isn->isn_arg.subs.subs_cmd);
10429 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10430 delete_instr(list + idx);
10431 vim_free(list);
10432 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010433 break;
10434
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010435 case ISN_INSTR:
10436 {
10437 int idx;
10438 isn_T *list = isn->isn_arg.instr;
10439
10440 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10441 delete_instr(list + idx);
10442 vim_free(list);
10443 }
10444 break;
10445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010446 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010447 case ISN_STORES:
10448 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010449 break;
10450
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010451 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010452 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010453 vim_free(isn->isn_arg.unlet.ul_name);
10454 break;
10455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010456 case ISN_STOREOPT:
10457 vim_free(isn->isn_arg.storeopt.so_name);
10458 break;
10459
10460 case ISN_PUSHBLOB: // push blob isn_arg.blob
10461 blob_unref(isn->isn_arg.blob);
10462 break;
10463
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010464 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010465#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010466 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010467#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010468 break;
10469
10470 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010471#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010472 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010473#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010474 break;
10475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010476 case ISN_UCALL:
10477 vim_free(isn->isn_arg.ufunc.cuf_name);
10478 break;
10479
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010480 case ISN_FUNCREF:
10481 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010482 if (isn->isn_arg.funcref.fr_func_name == NULL)
10483 {
10484 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10485 + isn->isn_arg.funcref.fr_dfunc_idx;
10486 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010487
Bram Moolenaar38453522021-11-28 22:00:12 +000010488 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10489 func_ptr_unref(ufunc);
10490 }
10491 else
10492 {
10493 char_u *name = isn->isn_arg.funcref.fr_func_name;
10494
10495 if (name != NULL)
10496 func_unref(name);
10497 vim_free(isn->isn_arg.funcref.fr_func_name);
10498 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010499 }
10500 break;
10501
10502 case ISN_DCALL:
10503 {
10504 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10505 + isn->isn_arg.dfunc.cdf_idx;
10506
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010507 if (dfunc->df_ufunc != NULL
10508 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010509 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010510 }
10511 break;
10512
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010513 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010514 {
10515 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10516 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10517
10518 if (ufunc != NULL)
10519 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010520 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010521 func_ptr_unref(ufunc);
10522 }
10523
10524 vim_free(lambda);
10525 vim_free(isn->isn_arg.newfunc.nf_global);
10526 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010527 break;
10528
Bram Moolenaar5e654232020-09-16 15:22:00 +020010529 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010530 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010531 free_type(isn->isn_arg.type.ct_type);
10532 break;
10533
Bram Moolenaar02194d22020-10-24 23:08:38 +020010534 case ISN_CMDMOD:
10535 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10536 ->cmod_filter_regmatch.regprog);
10537 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10538 break;
10539
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010540 case ISN_LOADSCRIPT:
10541 case ISN_STORESCRIPT:
10542 vim_free(isn->isn_arg.script.scriptref);
10543 break;
10544
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010545 case ISN_TRY:
10546 vim_free(isn->isn_arg.try.try_ref);
10547 break;
10548
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010549 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010550 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010551 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10552 break;
10553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010554 case ISN_2BOOL:
10555 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010556 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010557 case ISN_ADDBLOB:
10558 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010559 case ISN_ANYINDEX:
10560 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010561 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010562 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010563 case ISN_BLOBINDEX:
10564 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010565 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010566 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010567 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010568 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010569 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010570 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010571 case ISN_COMPAREANY:
10572 case ISN_COMPAREBLOB:
10573 case ISN_COMPAREBOOL:
10574 case ISN_COMPAREDICT:
10575 case ISN_COMPAREFLOAT:
10576 case ISN_COMPAREFUNC:
10577 case ISN_COMPARELIST:
10578 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010579 case ISN_COMPARESPECIAL:
10580 case ISN_COMPARESTRING:
10581 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010582 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010583 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010584 case ISN_DROP:
10585 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010586 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010587 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010588 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010589 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010590 case ISN_EXECCONCAT:
10591 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010592 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010593 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010594 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010595 case ISN_GETITEM:
10596 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010597 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010598 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010599 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010600 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010601 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010602 case ISN_LOADBDICT:
10603 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010604 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010605 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010606 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010607 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010608 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010609 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010610 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010611 case ISN_NEGATENR:
10612 case ISN_NEWDICT:
10613 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010614 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010615 case ISN_OPFLOAT:
10616 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010617 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010618 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010619 case ISN_PROF_END:
10620 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010621 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010622 case ISN_PUSHF:
10623 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010624 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010625 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010626 case ISN_REDIREND:
10627 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010628 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010629 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010630 case ISN_SHUFFLE:
10631 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010632 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010633 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010634 case ISN_STORENR:
10635 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010636 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010637 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010638 case ISN_STOREV:
10639 case ISN_STRINDEX:
10640 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010641 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010642 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010643 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010644 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010645 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010646 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010647 // nothing allocated
10648 break;
10649 }
10650}
10651
10652/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010653 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010654 */
10655 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010656delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010657{
10658 int idx;
10659
10660 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010661 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010662
10663 if (dfunc->df_instr != NULL)
10664 {
10665 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10666 delete_instr(dfunc->df_instr + idx);
10667 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010668 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010669 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010670 if (dfunc->df_instr_debug != NULL)
10671 {
10672 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10673 delete_instr(dfunc->df_instr_debug + idx);
10674 VIM_CLEAR(dfunc->df_instr_debug);
10675 dfunc->df_instr_debug = NULL;
10676 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010677#ifdef FEAT_PROFILE
10678 if (dfunc->df_instr_prof != NULL)
10679 {
10680 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10681 delete_instr(dfunc->df_instr_prof + idx);
10682 VIM_CLEAR(dfunc->df_instr_prof);
10683 dfunc->df_instr_prof = NULL;
10684 }
10685#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010686
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010687 if (mark_deleted)
10688 dfunc->df_deleted = TRUE;
10689 if (dfunc->df_ufunc != NULL)
10690 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010691}
10692
10693/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010694 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010695 * function, unless another user function still uses it.
10696 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010697 */
10698 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010699unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010700{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010701 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010702 {
10703 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10704 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010705
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010706 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010707 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010708 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010709 ufunc->uf_dfunc_idx = 0;
10710 if (dfunc->df_ufunc == ufunc)
10711 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010712 }
10713}
10714
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010715/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010716 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010717 */
10718 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010719link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010720{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010721 if (ufunc->uf_dfunc_idx > 0)
10722 {
10723 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10724 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010725
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010726 ++dfunc->df_refcount;
10727 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010728}
10729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010730#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010731/*
10732 * Free all functions defined with ":def".
10733 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010734 void
10735free_def_functions(void)
10736{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010737 int idx;
10738
10739 for (idx = 0; idx < def_functions.ga_len; ++idx)
10740 {
10741 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10742
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010743 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010744 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010745 }
10746
10747 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010748}
10749#endif
10750
10751
10752#endif // FEAT_EVAL