blob: 2ed1f0e58e19229281cee1703200ea5826b62c64 [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 Moolenaar8a7d6542020-01-26 15:56:19 +01002278 static int
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002279generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *line)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280{
2281 isn_T *isn;
2282
Bram Moolenaar080457c2020-03-03 21:53:32 +01002283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002284 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 return FAIL;
2286 isn->isn_arg.string = vim_strsave(line);
2287 return OK;
2288}
2289
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002290 static int
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002291generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2292{
2293 isn_T *isn;
2294 garray_T *stack = &cctx->ctx_type_stack;
2295
2296 RETURN_OK_IF_SKIP(cctx);
2297 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2298 return FAIL;
2299 isn->isn_arg.string = vim_strsave(line);
2300
Bram Moolenaar35578162021-08-02 19:10:38 +02002301 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002302 return FAIL;
2303 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2304 ++stack->ga_len;
2305
2306 return OK;
2307}
2308
2309 static int
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002310generate_EXECCONCAT(cctx_T *cctx, int count)
2311{
2312 isn_T *isn;
2313
2314 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2315 return FAIL;
2316 isn->isn_arg.number = count;
2317 return OK;
2318}
2319
Bram Moolenaar08597872020-12-10 19:43:40 +01002320/*
2321 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2322 */
2323 static int
2324generate_RANGE(cctx_T *cctx, char_u *range)
2325{
2326 isn_T *isn;
2327 garray_T *stack = &cctx->ctx_type_stack;
2328
2329 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2330 return FAIL;
2331 isn->isn_arg.string = range;
2332
Bram Moolenaar35578162021-08-02 19:10:38 +02002333 if (GA_GROW_FAILS(stack, 1))
Bram Moolenaar08597872020-12-10 19:43:40 +01002334 return FAIL;
2335 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2336 ++stack->ga_len;
2337 return OK;
2338}
2339
Bram Moolenaar792f7862020-11-23 08:31:18 +01002340 static int
2341generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2342{
2343 isn_T *isn;
2344
2345 RETURN_OK_IF_SKIP(cctx);
2346 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2347 return FAIL;
2348 isn->isn_arg.unpack.unp_count = var_count;
2349 isn->isn_arg.unpack.unp_semicolon = semicolon;
2350 return OK;
2351}
2352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002354 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002355 */
2356 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002357generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002358{
2359 isn_T *isn;
2360
Bram Moolenaar917c46a2021-08-10 19:53:01 +02002361 if (has_cmdmod(cmod, FALSE))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002362 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002363 cctx->ctx_has_cmdmod = TRUE;
2364
2365 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002366 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002367 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2368 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2369 return FAIL;
2370 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002371 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002372 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002373 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002374
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002375 return OK;
2376}
2377
2378 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002379generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002380{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002381 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2382 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002383 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002384 return OK;
2385}
2386
Bram Moolenaarfa984412021-03-25 22:15:28 +01002387 static int
2388misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002389{
2390 garray_T *instr = &cctx->ctx_instr;
2391
Bram Moolenaara91a7132021-03-25 21:12:15 +01002392 if (cctx->ctx_has_cmdmod
2393 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2394 == ISN_CMDMOD)
2395 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002396 emsg(_(e_misplaced_command_modifier));
2397 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002398 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002399 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002400}
2401
2402/*
2403 * Get the index of the current instruction.
Bram Moolenaar093165c2021-08-22 13:35:31 +02002404 * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
Bram Moolenaara91a7132021-03-25 21:12:15 +01002405 */
2406 static int
2407current_instr_idx(cctx_T *cctx)
2408{
2409 garray_T *instr = &cctx->ctx_instr;
2410 int idx = instr->ga_len;
2411
Bram Moolenaare99d4222021-06-13 14:01:26 +02002412 while (idx > 0)
2413 {
2414 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
Bram Moolenaara91a7132021-03-25 21:12:15 +01002415 .isn_type == ISN_CMDMOD)
Bram Moolenaare99d4222021-06-13 14:01:26 +02002416 {
2417 --idx;
2418 continue;
2419 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002420#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002421 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2422 {
2423 --idx;
2424 continue;
2425 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002426#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002427 if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2428 {
2429 --idx;
2430 continue;
2431 }
2432 break;
2433 }
Bram Moolenaara91a7132021-03-25 21:12:15 +01002434 return idx;
2435}
2436
Bram Moolenaarf002a412021-01-24 13:34:18 +01002437#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002438 static void
2439may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2440{
Bram Moolenaare99d4222021-06-13 14:01:26 +02002441 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002442 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002443}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002444#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002445
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002446/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002448 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002450 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002451reserve_local(
2452 cctx_T *cctx,
2453 char_u *name,
2454 size_t len,
2455 int isConst,
2456 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002459 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002461 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002463 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002464 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 }
2466
Bram Moolenaar35578162021-08-02 19:10:38 +02002467 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002468 return NULL;
2469 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002470 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002472 // Every local variable uses the next entry on the stack. We could re-use
2473 // the last ones when leaving a scope, but then variables used in a closure
2474 // might get overwritten. To keep things simple do not re-use stack
2475 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002476 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2477 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002478
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002479 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 lvar->lv_const = isConst;
2481 lvar->lv_type = type;
2482
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002483 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +02002484 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002485 return NULL;
2486 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2487 vim_strsave(lvar->lv_name);
2488 ++dfunc->df_var_names.ga_len;
2489
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002490 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491}
2492
2493/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002494 * Remove local variables above "new_top".
2495 */
2496 static void
2497unwind_locals(cctx_T *cctx, int new_top)
2498{
2499 if (cctx->ctx_locals.ga_len > new_top)
2500 {
2501 int idx;
2502 lvar_T *lvar;
2503
2504 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2505 {
2506 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2507 vim_free(lvar->lv_name);
2508 }
2509 }
2510 cctx->ctx_locals.ga_len = new_top;
2511}
2512
2513/*
2514 * Free all local variables.
2515 */
2516 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002517free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002518{
2519 unwind_locals(cctx, 0);
2520 ga_clear(&cctx->ctx_locals);
2521}
2522
2523/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002524 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2525 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2526 * error if the variable was defined with :const.
2527 */
2528 static int
2529check_item_writable(svar_T *sv, int check_writable, char_u *name)
2530{
2531 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2532 || (check_writable == ASSIGN_FINAL
2533 && sv->sv_const == ASSIGN_CONST))
2534 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002535 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +01002536 return FAIL;
2537 }
2538 return OK;
2539}
2540
2541/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002543 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 * Returns the index in "sn_var_vals" if found.
2545 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002546 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 */
2548 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002549get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550{
2551 hashtab_T *ht;
2552 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002553 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002554 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 int idx;
2556
Bram Moolenaare3d46852020-08-29 13:39:17 +02002557 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002559 if (sid == current_sctx.sc_sid)
2560 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002561 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002562
2563 if (sav == NULL)
2564 return -2;
2565 idx = sav->sav_var_vals_idx;
2566 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002567 if (check_item_writable(sv, check_writable, name) == FAIL)
2568 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002569 return idx;
2570 }
2571
2572 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 ht = &SCRIPT_VARS(sid);
2574 di = find_var_in_ht(ht, 0, name, TRUE);
2575 if (di == NULL)
2576 return -2;
2577
2578 // Now find the svar_T index in sn_var_vals.
2579 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2580 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002581 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 if (sv->sv_tv == &di->di_tv)
2583 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002584 if (check_item_writable(sv, check_writable, name) == FAIL)
2585 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 return idx;
2587 }
2588 }
2589 return -1;
2590}
2591
2592/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002593 * Find "name" in imported items of the current script or in "cctx" if not
2594 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 */
2596 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002597find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 int idx;
2600
Bram Moolenaare3d46852020-08-29 13:39:17 +02002601 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002602 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 if (cctx != NULL)
2604 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2605 {
2606 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2607 + idx;
2608
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002609 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2610 : STRLEN(import->imp_name) == len
2611 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 return import;
2613 }
2614
Bram Moolenaarefa94442020-08-08 22:16:00 +02002615 return find_imported_in_script(name, len, current_sctx.sc_sid);
2616}
2617
2618 imported_T *
2619find_imported_in_script(char_u *name, size_t len, int sid)
2620{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002621 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002622 int idx;
2623
Bram Moolenaare3d46852020-08-29 13:39:17 +02002624 if (!SCRIPT_ID_VALID(sid))
2625 return NULL;
2626 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2628 {
2629 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2630
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002631 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2632 : STRLEN(import->imp_name) == len
2633 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 return import;
2635 }
2636 return NULL;
2637}
2638
2639/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002640 * Free all imported variables.
2641 */
2642 static void
2643free_imported(cctx_T *cctx)
2644{
2645 int idx;
2646
2647 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2648 {
2649 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2650
2651 vim_free(import->imp_name);
2652 }
2653 ga_clear(&cctx->ctx_imports);
2654}
2655
2656/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002657 * Return a pointer to the next line that isn't empty or only contains a
2658 * comment. Skips over white space.
2659 * Returns NULL if there is none.
2660 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002661 char_u *
2662peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002663{
2664 int lnum = cctx->ctx_lnum;
2665
2666 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2667 {
2668 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002669 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002670
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002671 // ignore NULLs inserted for continuation lines
2672 if (line != NULL)
2673 {
2674 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002675 if (vim9_bad_comment(p))
2676 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002677 if (*p != NUL && !vim9_comment_start(p))
2678 return p;
2679 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002680 }
2681 return NULL;
2682}
2683
2684/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002685 * Called when checking for a following operator at "arg". When the rest of
2686 * the line is empty or only a comment, peek the next line. If there is a next
2687 * line return a pointer to it and set "nextp".
2688 * Otherwise skip over white space.
2689 */
2690 static char_u *
2691may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2692{
2693 char_u *p = skipwhite(arg);
2694
2695 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002696 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002697 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002698 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002699 if (*nextp != NULL)
2700 return *nextp;
2701 }
2702 return p;
2703}
2704
2705/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002706 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002707 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002708 * Returns NULL when at the end.
2709 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002710 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002711next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002712{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002713 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002714
2715 do
2716 {
2717 ++cctx->ctx_lnum;
2718 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002719 {
2720 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002721 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002722 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002723 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002724 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002725 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002726 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002727 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002728 return line;
2729}
2730
2731/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002732 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002733 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002734 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002735 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2736 */
2737 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002738may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002739{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002740 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002741 if (vim9_bad_comment(*arg))
2742 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002743 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002744 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002745 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002746
2747 if (next == NULL)
2748 return FAIL;
2749 *arg = skipwhite(next);
2750 }
2751 return OK;
2752}
2753
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002754/*
2755 * Idem, and give an error when failed.
2756 */
2757 static int
2758may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2759{
2760 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2761 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002762 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002763 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002764 return FAIL;
2765 }
2766 return OK;
2767}
2768
2769
Bram Moolenaara5565e42020-05-09 15:44:01 +02002770// Structure passed between the compile_expr* functions to keep track of
2771// constants that have been parsed but for which no code was produced yet. If
2772// possible expressions on these constants are applied at compile time. If
2773// that is not possible, the code to push the constants needs to be generated
2774// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002775// Using 50 should be more than enough of 5 levels of ().
2776#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002777typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002778 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002779 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002780 int pp_is_const; // all generated code was constants, used for a
2781 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002782} ppconst_T;
2783
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002784static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002785static int compile_expr0(char_u **arg, cctx_T *cctx);
2786static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2787
Bram Moolenaara5565e42020-05-09 15:44:01 +02002788/*
2789 * Generate a PUSH instruction for "tv".
2790 * "tv" will be consumed or cleared.
2791 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2792 */
2793 static int
2794generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2795{
2796 if (tv != NULL)
2797 {
2798 switch (tv->v_type)
2799 {
2800 case VAR_UNKNOWN:
2801 break;
2802 case VAR_BOOL:
2803 generate_PUSHBOOL(cctx, tv->vval.v_number);
2804 break;
2805 case VAR_SPECIAL:
2806 generate_PUSHSPEC(cctx, tv->vval.v_number);
2807 break;
2808 case VAR_NUMBER:
2809 generate_PUSHNR(cctx, tv->vval.v_number);
2810 break;
2811#ifdef FEAT_FLOAT
2812 case VAR_FLOAT:
2813 generate_PUSHF(cctx, tv->vval.v_float);
2814 break;
2815#endif
2816 case VAR_BLOB:
2817 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2818 tv->vval.v_blob = NULL;
2819 break;
2820 case VAR_STRING:
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002821 generate_PUSHS(cctx, &tv->vval.v_string);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002822 tv->vval.v_string = NULL;
2823 break;
2824 default:
2825 iemsg("constant type not supported");
2826 clear_tv(tv);
2827 return FAIL;
2828 }
2829 tv->v_type = VAR_UNKNOWN;
2830 }
2831 return OK;
2832}
2833
2834/*
2835 * Generate code for any ppconst entries.
2836 */
2837 static int
2838generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2839{
2840 int i;
2841 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002842 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002843
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002844 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002845 for (i = 0; i < ppconst->pp_used; ++i)
2846 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2847 ret = FAIL;
2848 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002849 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002850 return ret;
2851}
2852
2853/*
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02002854 * Check that the last item of "ppconst" is a bool, if there is an item.
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002855 */
2856 static int
2857check_ppconst_bool(ppconst_T *ppconst)
2858{
2859 if (ppconst->pp_used > 0)
2860 {
2861 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002862 where_T where = WHERE_INIT;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002863
Bram Moolenaar05bd9782021-07-21 21:37:28 +02002864 return check_typval_type(&t_bool, tv, where);
2865 }
2866 return OK;
2867}
2868
2869/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02002870 * Clear ppconst constants. Used when failing.
2871 */
2872 static void
2873clear_ppconst(ppconst_T *ppconst)
2874{
2875 int i;
2876
2877 for (i = 0; i < ppconst->pp_used; ++i)
2878 clear_tv(&ppconst->pp_tv[i]);
2879 ppconst->pp_used = 0;
2880}
2881
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002882/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002883 * Compile getting a member from a list/dict/string/blob. Stack has the
Bram Moolenaar261417b2021-05-06 21:04:55 +02002884 * indexable value and the index or the two indexes of a slice.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002885 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002886 */
2887 static int
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002888compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002889{
2890 type_T **typep;
2891 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar261417b2021-05-06 21:04:55 +02002892 vartype_T vartype;
2893 type_T *idxtype;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002894
Bram Moolenaar261417b2021-05-06 21:04:55 +02002895 // We can index a list, dict and blob. If we don't know the type
2896 // we can use the index value type. If we still don't know use an "ANY"
2897 // instruction.
Bram Moolenaare42939a2021-04-05 17:11:17 +02002898 typep = ((type_T **)stack->ga_data) + stack->ga_len
2899 - (is_slice ? 3 : 2);
Bram Moolenaar261417b2021-05-06 21:04:55 +02002900 vartype = (*typep)->tt_type;
2901 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaare42939a2021-04-05 17:11:17 +02002902 // If the index is a string, the variable must be a Dict.
Bram Moolenaar261417b2021-05-06 21:04:55 +02002903 if (*typep == &t_any && idxtype == &t_string)
2904 vartype = VAR_DICT;
2905 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002906 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002907 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002908 return FAIL;
2909 if (is_slice)
2910 {
Bram Moolenaar261417b2021-05-06 21:04:55 +02002911 idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2912 if (need_type(idxtype, &t_number, -2, 0, cctx,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002913 FALSE, FALSE) == FAIL)
2914 return FAIL;
2915 }
2916 }
2917
Bram Moolenaar261417b2021-05-06 21:04:55 +02002918 if (vartype == VAR_DICT)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002919 {
2920 if (is_slice)
2921 {
2922 emsg(_(e_cannot_slice_dictionary));
2923 return FAIL;
2924 }
2925 if ((*typep)->tt_type == VAR_DICT)
2926 {
2927 *typep = (*typep)->tt_member;
2928 if (*typep == &t_unknown)
2929 // empty dict was used
2930 *typep = &t_any;
2931 }
2932 else
2933 {
2934 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2935 FALSE, FALSE) == FAIL)
2936 return FAIL;
2937 *typep = &t_any;
2938 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002939 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002940 return FAIL;
2941 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2942 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002943 if (keeping_dict != NULL)
2944 *keeping_dict = TRUE;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002945 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002946 else if (vartype == VAR_STRING)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002947 {
2948 *typep = &t_string;
2949 if ((is_slice
2950 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2951 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2952 return FAIL;
2953 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002954 else if (vartype == VAR_BLOB)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002955 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002956 if (is_slice)
2957 {
2958 *typep = &t_blob;
2959 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2960 return FAIL;
2961 }
2962 else
2963 {
2964 *typep = &t_number;
2965 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2966 return FAIL;
2967 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002968 }
Bram Moolenaar261417b2021-05-06 21:04:55 +02002969 else if (vartype == VAR_LIST || *typep == &t_any)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002970 {
2971 if (is_slice)
2972 {
2973 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002974 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002975 2) == FAIL)
2976 return FAIL;
2977 }
2978 else
2979 {
2980 if ((*typep)->tt_type == VAR_LIST)
2981 {
2982 *typep = (*typep)->tt_member;
2983 if (*typep == &t_unknown)
2984 // empty list was used
2985 *typep = &t_any;
2986 }
2987 if (generate_instr_drop(cctx,
Bram Moolenaar261417b2021-05-06 21:04:55 +02002988 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
2989 == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002990 return FAIL;
2991 }
2992 }
2993 else
2994 {
2995 emsg(_(e_string_list_dict_or_blob_required));
2996 return FAIL;
2997 }
2998 return OK;
2999}
3000
3001/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003002 * Generate an instruction to load script-local variable "name", without the
3003 * leading "s:".
3004 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 */
3006 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003007compile_load_scriptvar(
3008 cctx_T *cctx,
3009 char_u *name, // variable NUL terminated
3010 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003011 char_u **end, // end of variable
3012 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013{
Bram Moolenaare3d46852020-08-29 13:39:17 +02003014 scriptitem_T *si;
3015 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 imported_T *import;
3017
Bram Moolenaare3d46852020-08-29 13:39:17 +02003018 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3019 return FAIL;
3020 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01003021 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003022 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01003024 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003025 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
3026 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028 if (idx >= 0)
3029 {
3030 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
3031
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003032 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 current_sctx.sc_sid, idx, sv->sv_type);
3034 return OK;
3035 }
3036
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003037 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 if (import != NULL)
3039 {
Bram Moolenaara6294952020-12-27 13:39:50 +01003040 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003041 {
3042 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003043 char_u *exp_name;
3044 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003045 ufunc_T *ufunc;
3046 type_T *type;
3047
3048 // Used "import * as Name", need to lookup the member.
3049 if (*p != '.')
3050 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003051 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003052 return FAIL;
3053 }
3054 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003055 if (VIM_ISWHITE(*p))
3056 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003057 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003058 return FAIL;
3059 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003060
Bram Moolenaar1c991142020-07-04 13:15:31 +02003061 // isolate one name
3062 exp_name = p;
3063 while (eval_isnamec(*p))
3064 ++p;
3065 cc = *p;
3066 *p = NUL;
3067
Bram Moolenaaredba7072021-03-13 21:14:18 +01003068 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
3069 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02003070 *p = cc;
3071 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003072 *end = p;
3073
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02003074 if (idx < 0)
3075 {
3076 if (*p == '(' && ufunc != NULL)
3077 {
3078 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
3079 return OK;
3080 }
3081 return FAIL;
3082 }
3083
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003084 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3085 import->imp_sid,
3086 idx,
3087 type);
3088 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003089 else if (import->imp_funcname != NULL)
3090 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003091 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003092 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3093 import->imp_sid,
3094 import->imp_var_vals_idx,
3095 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 return OK;
3097 }
3098
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003099 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003100 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 return FAIL;
3102}
3103
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003104 static int
3105generate_funcref(cctx_T *cctx, char_u *name)
3106{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003107 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003108
3109 if (ufunc == NULL)
3110 return FAIL;
3111
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003112 // Need to compile any default values to get the argument types.
Bram Moolenaare99d4222021-06-13 14:01:26 +02003113 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3114 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003115 == FAIL)
3116 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02003117 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003118}
3119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120/*
3121 * Compile a variable name into a load instruction.
3122 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02003123 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 * When "error" is FALSE do not give an error when not found.
3125 */
3126 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02003127compile_load(
3128 char_u **arg,
3129 char_u *end_arg,
3130 cctx_T *cctx,
3131 int is_expr,
3132 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133{
3134 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003135 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01003136 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003138 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139
3140 if (*(*arg + 1) == ':')
3141 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003142 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003143 {
3144 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
Bram Moolenaarfa596382021-04-07 21:58:16 +02003146 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003147 switch (**arg)
3148 {
3149 case 'g': isn_type = ISN_LOADGDICT; break;
3150 case 'w': isn_type = ISN_LOADWDICT; break;
3151 case 't': isn_type = ISN_LOADTDICT; break;
3152 case 'b': isn_type = ISN_LOADBDICT; break;
3153 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003154 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003155 goto theend;
3156 }
3157 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3158 goto theend;
3159 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 else
3162 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003163 isntype_T isn_type = ISN_DROP;
3164
Bram Moolenaarfa596382021-04-07 21:58:16 +02003165 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003166 name = vim_strnsave(*arg + 2, end - (*arg + 2));
3167 if (name == NULL)
3168 return FAIL;
3169
3170 switch (**arg)
3171 {
3172 case 'v': res = generate_LOADV(cctx, name, error);
3173 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02003174 case 's': if (is_expr && ASCII_ISUPPER(*name)
3175 && find_func(name, FALSE, cctx) != NULL)
3176 res = generate_funcref(cctx, name);
3177 else
3178 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02003179 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003180 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003181 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02003182 {
3183 if (is_expr && ASCII_ISUPPER(*name)
3184 && find_func(name, FALSE, cctx) != NULL)
3185 res = generate_funcref(cctx, name);
3186 else
3187 isn_type = ISN_LOADG;
3188 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01003189 else
3190 {
3191 isn_type = ISN_LOADAUTO;
3192 vim_free(name);
3193 name = vim_strnsave(*arg, end - *arg);
3194 if (name == NULL)
3195 return FAIL;
3196 }
3197 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003198 case 'w': isn_type = ISN_LOADW; break;
3199 case 't': isn_type = ISN_LOADT; break;
3200 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01003201 default: // cannot happen, just in case
3202 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003203 goto theend;
3204 }
3205 if (isn_type != ISN_DROP)
3206 {
3207 // Global, Buffer-local, Window-local and Tabpage-local
3208 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003209 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003210 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213 }
3214 else
3215 {
3216 size_t len = end - *arg;
3217 int idx;
3218 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003219 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220
3221 name = vim_strnsave(*arg, end - *arg);
3222 if (name == NULL)
3223 return FAIL;
3224
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003225 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3226 {
3227 script_autoload(name, FALSE);
3228 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3229 }
3230 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3231 == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003233 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003234 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 }
3236 else
3237 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003238 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003239
Bram Moolenaar709664c2020-12-12 14:33:41 +01003240 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003242 type = lvar.lv_type;
3243 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003244 if (lvar.lv_from_outer != 0)
3245 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003246 else
3247 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 }
3249 else
3250 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003251 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003252 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003253 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003254 || find_imported(name, 0, cctx) != NULL)
3255 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003256
Bram Moolenaar0f769812020-09-12 18:32:34 +02003257 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003258 // uppercase letter it can be a user defined function.
3259 // generate_funcref() will fail if the function can't be found.
3260 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003261 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 }
3263 }
3264 if (gen_load)
3265 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003266 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003267 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003268 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003269 cctx->ctx_outer_used = TRUE;
3270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 }
3272
3273 *arg = end;
3274
3275theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003276 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003277 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 vim_free(name);
3279 return res;
3280}
3281
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003282 static void
3283clear_instr_ga(garray_T *gap)
3284{
3285 int idx;
3286
3287 for (idx = 0; idx < gap->ga_len; ++idx)
3288 delete_instr(((isn_T *)gap->ga_data) + idx);
3289 ga_clear(gap);
3290}
3291
3292/*
3293 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3294 * Returns FAIL if compilation fails.
3295 */
3296 static int
3297compile_string(isn_T *isn, cctx_T *cctx)
3298{
3299 char_u *s = isn->isn_arg.string;
3300 garray_T save_ga = cctx->ctx_instr;
3301 int expr_res;
3302 int trailing_error;
3303 int instr_count;
3304 isn_T *instr = NULL;
3305
Bram Moolenaarcd268012021-07-22 19:11:08 +02003306 // Remove the string type from the stack.
3307 --cctx->ctx_type_stack.ga_len;
3308
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003309 // Temporarily reset the list of instructions so that the jump labels are
3310 // correct.
3311 cctx->ctx_instr.ga_len = 0;
3312 cctx->ctx_instr.ga_maxlen = 0;
3313 cctx->ctx_instr.ga_data = NULL;
3314 expr_res = compile_expr0(&s, cctx);
3315 s = skipwhite(s);
3316 trailing_error = *s != NUL;
3317
Bram Moolenaarff652882021-05-16 15:24:49 +02003318 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02003319 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003320 {
3321 if (trailing_error)
3322 semsg(_(e_trailing_arg), s);
3323 clear_instr_ga(&cctx->ctx_instr);
3324 cctx->ctx_instr = save_ga;
Bram Moolenaar5a234eb2021-07-24 13:18:48 +02003325 ++cctx->ctx_type_stack.ga_len;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003326 return FAIL;
3327 }
3328
3329 // Move the generated instructions into the ISN_INSTR instruction, then
3330 // restore the list of instructions.
3331 instr_count = cctx->ctx_instr.ga_len;
3332 instr = cctx->ctx_instr.ga_data;
3333 instr[instr_count].isn_type = ISN_FINISH;
3334
3335 cctx->ctx_instr = save_ga;
3336 vim_free(isn->isn_arg.string);
3337 isn->isn_type = ISN_INSTR;
3338 isn->isn_arg.instr = instr;
3339 return OK;
3340}
3341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342/*
3343 * Compile the argument expressions.
3344 * "arg" points to just after the "(" and is advanced to after the ")"
3345 */
3346 static int
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003347compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003349 char_u *p = *arg;
3350 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003351 int must_end = FALSE;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003352 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353
Bram Moolenaare6085c52020-04-12 20:19:16 +02003354 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003356 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3357 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003358 if (*p == ')')
3359 {
3360 *arg = p + 1;
3361 return OK;
3362 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003363 if (must_end)
3364 {
3365 semsg(_(e_missing_comma_before_argument_str), p);
3366 return FAIL;
3367 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003368
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003369 instr_count = cctx->ctx_instr.ga_len;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003370 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 return FAIL;
3372 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003373
Bram Moolenaardd0b2872021-07-24 15:44:30 +02003374 if (is_searchpair && *argcount == 5
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003375 && cctx->ctx_instr.ga_len == instr_count + 1)
3376 {
3377 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3378
3379 // {skip} argument of searchpair() can be compiled if not empty
3380 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3381 compile_string(isn, cctx);
3382 }
3383
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003384 if (*p != ',' && *skipwhite(p) == ',')
3385 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003386 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003387 p = skipwhite(p);
3388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003390 {
3391 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003392 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003393 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003394 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003395 else
3396 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003397 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003398 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003400failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003401 emsg(_(e_missing_close));
3402 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403}
3404
3405/*
3406 * Compile a function call: name(arg1, arg2)
3407 * "arg" points to "name", "arg + varlen" to the "(".
3408 * "argcount_init" is 1 for "value->method()"
3409 * Instructions:
3410 * EVAL arg1
3411 * EVAL arg2
3412 * BCALL / DCALL / UCALL
3413 */
3414 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003415compile_call(
3416 char_u **arg,
3417 size_t varlen,
3418 cctx_T *cctx,
3419 ppconst_T *ppconst,
3420 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421{
3422 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003423 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 int argcount = argcount_init;
3425 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003426 char_u fname_buf[FLEN_FIXED + 1];
3427 char_u *tofree = NULL;
3428 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003429 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003430 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003431 int is_autoload;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003432 int is_searchpair;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003434 // We can evaluate "has('name')" at compile time.
Bram Moolenaar26735992021-08-08 14:43:22 +02003435 // We always evaluate "exists_compiled()" at compile time.
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003436 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
Bram Moolenaar26735992021-08-08 14:43:22 +02003437 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003438 {
3439 char_u *s = skipwhite(*arg + varlen + 1);
3440 typval_T argvars[2];
Bram Moolenaarc3160722021-08-02 21:12:05 +02003441 int is_has = **arg == 'h';
Bram Moolenaara5565e42020-05-09 15:44:01 +02003442
3443 argvars[0].v_type = VAR_UNKNOWN;
3444 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003445 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003446 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003447 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003448 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003449 if (*s == ')' && argvars[0].v_type == VAR_STRING
Bram Moolenaarc3160722021-08-02 21:12:05 +02003450 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaar26735992021-08-08 14:43:22 +02003451 || !is_has))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003452 {
3453 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3454
3455 *arg = s + 1;
3456 argvars[1].v_type = VAR_UNKNOWN;
3457 tv->v_type = VAR_NUMBER;
3458 tv->vval.v_number = 0;
Bram Moolenaarc3160722021-08-02 21:12:05 +02003459 if (is_has)
Bram Moolenaarbb7ee7a2021-08-02 20:06:50 +02003460 f_has(argvars, tv);
3461 else
3462 f_exists(argvars, tv);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003463 clear_tv(&argvars[0]);
3464 ++ppconst->pp_used;
3465 return OK;
3466 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003467 clear_tv(&argvars[0]);
Bram Moolenaar26735992021-08-08 14:43:22 +02003468 if (!is_has)
3469 {
3470 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
3471 return FAIL;
3472 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003473 }
3474
3475 if (generate_ppconst(cctx, ppconst) == FAIL)
3476 return FAIL;
3477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 if (varlen >= sizeof(namebuf))
3479 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003480 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 return FAIL;
3482 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003483 vim_strncpy(namebuf, *arg, varlen);
3484 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02003486 // We handle the "skip" argument of searchpair() and searchpairpos()
3487 // differently.
3488 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3489 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3490 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3491 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 *arg = skipwhite(*arg + varlen + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003494 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003495 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496
Bram Moolenaar03290b82020-12-19 16:30:44 +01003497 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003498 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 {
3500 int idx;
3501
3502 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003503 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003505 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003506 if (STRCMP(name, "flatten") == 0)
3507 {
3508 emsg(_(e_cannot_use_flatten_in_vim9_script));
3509 goto theend;
3510 }
3511
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003512 if (STRCMP(name, "add") == 0 && argcount == 2)
3513 {
3514 garray_T *stack = &cctx->ctx_type_stack;
3515 type_T *type = ((type_T **)stack->ga_data)[
3516 stack->ga_len - 2];
3517
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003518 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003519 if (type->tt_type == VAR_LIST)
3520 {
3521 // inline "add(list, item)" so that the type can be checked
3522 res = generate_LISTAPPEND(cctx);
3523 idx = -1;
3524 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003525 else if (type->tt_type == VAR_BLOB)
3526 {
3527 // inline "add(blob, nr)" so that the type can be checked
3528 res = generate_BLOBAPPEND(cctx);
3529 idx = -1;
3530 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003531 }
3532
3533 if (idx >= 0)
3534 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3535 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003536 else
3537 semsg(_(e_unknownfunc), namebuf);
3538 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 }
3540
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003541 // An argument or local variable can be a function reference, this
3542 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003543 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003544 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003545 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003546 // If we can find the function by name generate the right call.
3547 // Skip global functions here, a local funcref takes precedence.
3548 ufunc = find_func(name, FALSE, cctx);
3549 if (ufunc != NULL && !func_is_global(ufunc))
3550 {
3551 res = generate_CALL(cctx, ufunc, argcount);
3552 goto theend;
3553 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555
3556 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003557 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003558 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003560 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003561 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003562 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003563 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003564 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003565
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003566 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003567 goto theend;
3568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569
Bram Moolenaar0f769812020-09-12 18:32:34 +02003570 // If we can find a global function by name generate the right call.
3571 if (ufunc != NULL)
3572 {
3573 res = generate_CALL(cctx, ufunc, argcount);
3574 goto theend;
3575 }
3576
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003577 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003578 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003579 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003580 res = generate_UCALL(cctx, name, argcount);
3581 else
3582 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003583
3584theend:
3585 vim_free(tofree);
3586 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587}
3588
3589// like NAMESPACE_CHAR but with 'a' and 'l'.
3590#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3591
3592/*
3593 * Find the end of a variable or function name. Unlike find_name_end() this
3594 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003595 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 * Return a pointer to just after the name. Equal to "arg" if there is no
3597 * valid name.
3598 */
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003599 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003600to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601{
3602 char_u *p;
3603
3604 // Quick check for valid starting character.
3605 if (!eval_isnamec1(*arg))
3606 return arg;
3607
3608 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3609 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3610 // and can be used in slice "[n:]".
3611 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003612 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3614 break;
3615 return p;
3616}
3617
3618/*
3619 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003620 * Also accept "<SNR>123_Func".
Bram Moolenaar1c991142020-07-04 13:15:31 +02003621 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 */
3623 char_u *
3624to_name_const_end(char_u *arg)
3625{
Bram Moolenaar678b2072021-07-26 21:10:11 +02003626 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 typval_T rettv;
3628
Bram Moolenaar678b2072021-07-26 21:10:11 +02003629 if (STRNCMP(p, "<SNR>", 5) == 0)
3630 p = skipdigits(p + 5);
3631 p = to_name_end(p, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 if (p == arg && *arg == '[')
3633 {
3634
3635 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003636 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 p = arg;
3638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 return p;
3640}
3641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642/*
3643 * parse a list: [expr, expr]
3644 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003645 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 */
3647 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003648compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649{
3650 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003651 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003653 int is_const;
3654 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003656 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003658 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003659 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003660 semsg(_(e_list_end), *arg);
3661 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003662 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003663 if (*p == ',')
3664 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003665 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003666 return FAIL;
3667 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003668 if (*p == ']')
3669 {
3670 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003671 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003672 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003673 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003674 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675 if (!is_const)
3676 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 ++count;
3678 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003679 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003681 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3682 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003683 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003684 return FAIL;
3685 }
3686 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003687 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 p = skipwhite(p);
3689 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003690 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003692 ppconst->pp_is_const = is_all_const;
3693 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694}
3695
3696/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003697 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003698 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003699 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 */
3701 static int
3702compile_lambda(char_u **arg, cctx_T *cctx)
3703{
Bram Moolenaare462f522020-12-27 14:43:30 +01003704 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 typval_T rettv;
3706 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003707 evalarg_T evalarg;
3708
Bram Moolenaar844fb642021-10-23 13:32:30 +01003709 init_evalarg(&evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003710 evalarg.eval_flags = EVAL_EVALUATE;
3711 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712
3713 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003714 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3715 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003716 {
3717 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003718 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003719 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003720
Bram Moolenaar65c44152020-12-24 15:14:01 +01003721 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003723 ++ufunc->uf_refcount;
3724 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725
Bram Moolenaara9931532021-06-12 15:58:16 +02003726 // Compile it here to get the return type. The return type is optional,
3727 // when it's missing use t_unknown. This is recognized in
3728 // compile_return().
3729 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3730 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar17d868b2021-06-27 16:29:53 +02003731 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732
Bram Moolenaar648594e2021-07-11 17:55:01 +02003733#ifdef FEAT_PROFILE
Bram Moolenaard9162552021-07-11 15:26:13 +02003734 // When the outer function is compiled for profiling, the lambda may be
3735 // called without profiling. Compile it here in the right context.
3736 if (cctx->ctx_compile_type == CT_PROFILE)
3737 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
Bram Moolenaar648594e2021-07-11 17:55:01 +02003738#endif
Bram Moolenaard9162552021-07-11 15:26:13 +02003739
Bram Moolenaar844fb642021-10-23 13:32:30 +01003740 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
3741 // "*arg" may point into it. Point into the original line to avoid a
3742 // dangling pointer.
3743 if (evalarg.eval_using_cmdline)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003744 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003745 garray_T *gap = &evalarg.eval_tofree_ga;
3746 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003747
3748 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3749 + off;
3750 }
3751
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003752 clear_evalarg(&evalarg, NULL);
3753
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003754 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003755 {
3756 // The return type will now be known.
3757 set_function_type(ufunc);
3758
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003759 // The function reference count will be 1. When the ISN_FUNCREF
3760 // instruction is deleted the reference count is decremented and the
3761 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003762 return generate_FUNCREF(cctx, ufunc);
3763 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003764
3765 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
3767}
3768
3769/*
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +02003770 * Get a lambda and compile it. Uses Vim9 syntax.
3771 */
3772 int
3773get_lambda_tv_and_compile(
3774 char_u **arg,
3775 typval_T *rettv,
3776 int types_optional,
3777 evalarg_T *evalarg)
3778{
3779 int r;
3780 ufunc_T *ufunc;
3781 int save_sc_version = current_sctx.sc_version;
3782
3783 // Get the funcref in "rettv".
3784 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3785 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
3786 current_sctx.sc_version = save_sc_version;
3787 if (r != OK)
3788 return r;
3789
3790 // "rettv" will now be a partial referencing the function.
3791 ufunc = rettv->vval.v_partial->pt_func;
3792
3793 // Compile it here to get the return type. The return type is optional,
3794 // when it's missing use t_unknown. This is recognized in
3795 // compile_return().
3796 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
3797 ufunc->uf_ret_type = &t_unknown;
3798 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
3799
3800 if (ufunc->uf_def_status == UF_COMPILED)
3801 {
3802 // The return type will now be known.
3803 set_function_type(ufunc);
3804 return OK;
3805 }
3806 clear_tv(rettv);
3807 return FAIL;
3808}
3809
3810/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003811 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003813 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 */
3815 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003816compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817{
3818 garray_T *instr = &cctx->ctx_instr;
3819 int count = 0;
3820 dict_T *d = dict_alloc();
3821 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003822 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003823 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003824 int is_const;
3825 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826
3827 if (d == NULL)
3828 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003829 if (generate_ppconst(cctx, ppconst) == FAIL)
3830 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003831 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003833 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834
Bram Moolenaar23c55272020-06-21 16:58:13 +02003835 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003836 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003837 *arg = NULL;
3838 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003839 }
3840
3841 if (**arg == '}')
3842 break;
3843
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003844 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003846 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003848 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003849 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003850 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003853 if (isn->isn_type == ISN_PUSHNR)
3854 {
3855 char buf[NUMBUFLEN];
3856
3857 // Convert to string at compile time.
3858 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3859 isn->isn_type = ISN_PUSHS;
3860 isn->isn_arg.string = vim_strsave((char_u *)buf);
3861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 if (isn->isn_type == ISN_PUSHS)
3863 key = isn->isn_arg.string;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003864 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003865 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003866 *arg = skipwhite(*arg);
3867 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003868 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003869 emsg(_(e_missing_matching_bracket_after_dict_key));
3870 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003871 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003872 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003874 else
3875 {
3876 // {"name": value},
3877 // {'name': value},
3878 // {name: value} use "name" as a literal key
3879 key = get_literal_key(arg);
3880 if (key == NULL)
3881 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02003882 if (generate_PUSHS(cctx, &key) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003883 return FAIL;
3884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885
3886 // Check for duplicate keys, if using string keys.
3887 if (key != NULL)
3888 {
3889 item = dict_find(d, key, -1);
3890 if (item != NULL)
3891 {
3892 semsg(_(e_duplicate_key), key);
3893 goto failret;
3894 }
3895 item = dictitem_alloc(key);
3896 if (item != NULL)
3897 {
3898 item->di_tv.v_type = VAR_UNKNOWN;
3899 item->di_tv.v_lock = 0;
3900 if (dict_add(d, item) == FAIL)
3901 dictitem_free(item);
3902 }
3903 }
3904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 if (**arg != ':')
3906 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003907 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003908 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003909 else
3910 semsg(_(e_missing_dict_colon), *arg);
3911 return FAIL;
3912 }
3913 whitep = *arg + 1;
3914 if (!IS_WHITE_OR_NUL(*whitep))
3915 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003916 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 return FAIL;
3918 }
3919
Bram Moolenaar23c55272020-06-21 16:58:13 +02003920 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003921 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003922 *arg = NULL;
3923 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003924 }
3925
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003926 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003928 if (!is_const)
3929 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 ++count;
3931
Bram Moolenaar2c330432020-04-13 14:41:35 +02003932 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003933 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003934 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003935 *arg = NULL;
3936 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 if (**arg == '}')
3939 break;
3940 if (**arg != ',')
3941 {
3942 semsg(_(e_missing_dict_comma), *arg);
3943 goto failret;
3944 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003945 if (IS_WHITE_OR_NUL(*whitep))
3946 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003947 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003948 return FAIL;
3949 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003950 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003951 if (!IS_WHITE_OR_NUL(*whitep))
3952 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003953 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003954 return FAIL;
3955 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003956 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 }
3958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 *arg = *arg + 1;
3960
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003961 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003962 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003963 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003964 *arg += STRLEN(*arg);
3965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003967 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 return generate_NEWDICT(cctx, count);
3969
3970failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003971 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003972 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003973 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003974 *arg = (char_u *)"";
3975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 dict_unref(d);
3977 return FAIL;
3978}
3979
3980/*
3981 * Compile "&option".
3982 */
3983 static int
3984compile_get_option(char_u **arg, cctx_T *cctx)
3985{
3986 typval_T rettv;
3987 char_u *start = *arg;
3988 int ret;
3989
3990 // parse the option and get the current value to get the type.
3991 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003992 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 if (ret == OK)
3994 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003995 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003996 char_u *name = vim_strnsave(start, *arg - start);
3997 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3998 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
4000 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4001 vim_free(name);
4002 }
4003 clear_tv(&rettv);
4004
4005 return ret;
4006}
4007
4008/*
4009 * Compile "$VAR".
4010 */
4011 static int
4012compile_get_env(char_u **arg, cctx_T *cctx)
4013{
4014 char_u *start = *arg;
4015 int len;
4016 int ret;
4017 char_u *name;
4018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 ++*arg;
4020 len = get_env_len(arg);
4021 if (len == 0)
4022 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004023 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 return FAIL;
4025 }
4026
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004027 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 name = vim_strnsave(start, len + 1);
4029 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
4030 vim_free(name);
4031 return ret;
4032}
4033
4034/*
4035 * Compile "@r".
4036 */
4037 static int
4038compile_get_register(char_u **arg, cctx_T *cctx)
4039{
4040 int ret;
4041
4042 ++*arg;
4043 if (**arg == NUL)
4044 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004045 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 return FAIL;
4047 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02004048 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 {
4050 emsg_invreg(**arg);
4051 return FAIL;
4052 }
4053 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
4054 ++*arg;
4055 return ret;
4056}
4057
4058/*
4059 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004060 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 */
4062 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004063apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004065 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066
4067 // this works from end to start
4068 while (p > start)
4069 {
4070 --p;
4071 if (*p == '-' || *p == '+')
4072 {
4073 // only '-' has an effect, for '+' we only check the type
4074#ifdef FEAT_FLOAT
4075 if (rettv->v_type == VAR_FLOAT)
4076 {
4077 if (*p == '-')
4078 rettv->vval.v_float = -rettv->vval.v_float;
4079 }
4080 else
4081#endif
4082 {
4083 varnumber_T val;
4084 int error = FALSE;
4085
4086 // tv_get_number_chk() accepts a string, but we don't want that
4087 // here
4088 if (check_not_string(rettv) == FAIL)
4089 return FAIL;
4090 val = tv_get_number_chk(rettv, &error);
4091 clear_tv(rettv);
4092 if (error)
4093 return FAIL;
4094 if (*p == '-')
4095 val = -val;
4096 rettv->v_type = VAR_NUMBER;
4097 rettv->vval.v_number = val;
4098 }
4099 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004100 else if (numeric_only)
4101 {
4102 ++p;
4103 break;
4104 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004105 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 {
4107 int v = tv2bool(rettv);
4108
4109 // '!' is permissive in the type.
4110 clear_tv(rettv);
4111 rettv->v_type = VAR_BOOL;
4112 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
4113 }
4114 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004115 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 return OK;
4117}
4118
4119/*
4120 * Recognize v: variables that are constants and set "rettv".
4121 */
4122 static void
4123get_vim_constant(char_u **arg, typval_T *rettv)
4124{
4125 if (STRNCMP(*arg, "v:true", 6) == 0)
4126 {
4127 rettv->v_type = VAR_BOOL;
4128 rettv->vval.v_number = VVAL_TRUE;
4129 *arg += 6;
4130 }
4131 else if (STRNCMP(*arg, "v:false", 7) == 0)
4132 {
4133 rettv->v_type = VAR_BOOL;
4134 rettv->vval.v_number = VVAL_FALSE;
4135 *arg += 7;
4136 }
4137 else if (STRNCMP(*arg, "v:null", 6) == 0)
4138 {
4139 rettv->v_type = VAR_SPECIAL;
4140 rettv->vval.v_number = VVAL_NULL;
4141 *arg += 6;
4142 }
4143 else if (STRNCMP(*arg, "v:none", 6) == 0)
4144 {
4145 rettv->v_type = VAR_SPECIAL;
4146 rettv->vval.v_number = VVAL_NONE;
4147 *arg += 6;
4148 }
4149}
4150
Bram Moolenaar657137c2021-01-09 15:45:23 +01004151 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02004152get_compare_type(char_u *p, int *len, int *type_is)
4153{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004154 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004155 int i;
4156
4157 switch (p[0])
4158 {
4159 case '=': if (p[1] == '=')
4160 type = EXPR_EQUAL;
4161 else if (p[1] == '~')
4162 type = EXPR_MATCH;
4163 break;
4164 case '!': if (p[1] == '=')
4165 type = EXPR_NEQUAL;
4166 else if (p[1] == '~')
4167 type = EXPR_NOMATCH;
4168 break;
4169 case '>': if (p[1] != '=')
4170 {
4171 type = EXPR_GREATER;
4172 *len = 1;
4173 }
4174 else
4175 type = EXPR_GEQUAL;
4176 break;
4177 case '<': if (p[1] != '=')
4178 {
4179 type = EXPR_SMALLER;
4180 *len = 1;
4181 }
4182 else
4183 type = EXPR_SEQUAL;
4184 break;
4185 case 'i': if (p[1] == 's')
4186 {
4187 // "is" and "isnot"; but not a prefix of a name
4188 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4189 *len = 5;
4190 i = p[*len];
4191 if (!isalnum(i) && i != '_')
4192 {
4193 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4194 *type_is = TRUE;
4195 }
4196 }
4197 break;
4198 }
4199 return type;
4200}
4201
4202/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004203 * Skip over an expression, ignoring most errors.
4204 */
4205 static void
4206skip_expr_cctx(char_u **arg, cctx_T *cctx)
4207{
4208 evalarg_T evalarg;
4209
Bram Moolenaar844fb642021-10-23 13:32:30 +01004210 init_evalarg(&evalarg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004211 evalarg.eval_cctx = cctx;
4212 skip_expr(arg, &evalarg);
Bram Moolenaar844fb642021-10-23 13:32:30 +01004213 clear_evalarg(&evalarg, NULL);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004214}
4215
4216/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004218 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 */
4220 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004221compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004223 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
4225 // this works from end to start
4226 while (p > start)
4227 {
4228 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01004229 while (VIM_ISWHITE(*p))
4230 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 if (*p == '-' || *p == '+')
4232 {
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004233 int negate = *p == '-';
4234 isn_T *isn;
4235 garray_T *stack = &cctx->ctx_type_stack;
4236 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004238 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4239 if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4240 return FAIL;
4241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 while (p > start && (p[-1] == '-' || p[-1] == '+'))
4243 {
4244 --p;
4245 if (*p == '-')
4246 negate = !negate;
4247 }
4248 // only '-' has an effect, for '+' we only check the type
4249 if (negate)
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004250 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 isn = generate_instr(cctx, ISN_NEGATENR);
Bram Moolenaarcd6b4f32021-08-15 20:36:28 +02004252 if (isn == NULL)
4253 return FAIL;
4254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004256 else if (numeric_only)
4257 {
4258 ++p;
4259 break;
4260 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 else
4262 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004263 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004265 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004267 if (p[-1] == '!')
4268 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004271 if (generate_2BOOL(cctx, invert, -1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 return FAIL;
4273 }
4274 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004275 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 return OK;
4277}
4278
4279/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01004280 * Compile "(expression)": recursive!
4281 * Return FAIL/OK.
4282 */
4283 static int
4284compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4285{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004286 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01004287 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004288
Bram Moolenaar24156692021-01-14 20:35:49 +01004289 if (may_get_next_line_error(p, arg, cctx) == FAIL)
4290 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004291 if (ppconst->pp_used <= PPSIZE - 10)
4292 {
4293 ret = compile_expr1(arg, cctx, ppconst);
4294 }
4295 else
4296 {
4297 // Not enough space in ppconst, flush constants.
4298 if (generate_ppconst(cctx, ppconst) == FAIL)
4299 return FAIL;
4300 ret = compile_expr0(arg, cctx);
4301 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004302 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4303 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004304 if (**arg == ')')
4305 ++*arg;
4306 else if (ret == OK)
4307 {
4308 emsg(_(e_missing_close));
4309 ret = FAIL;
4310 }
4311 return ret;
4312}
4313
4314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004316 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 */
4318 static int
4319compile_subscript(
4320 char_u **arg,
4321 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004322 char_u *start_leader,
4323 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004324 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004326 char_u *name_start = *end_leader;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004327 int keeping_dict = FALSE;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 for (;;)
4330 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02004331 char_u *p = skipwhite(*arg);
4332
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004333 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004334 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004335 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004336
4337 // If a following line starts with "->{" or "->X" advance to that
4338 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004339 // Also if a following line starts with ".x".
4340 if (next != NULL &&
4341 ((next[0] == '-' && next[1] == '>'
Bram Moolenaara7330422021-06-08 20:46:45 +02004342 && (next[2] == '{'
4343 || ASCII_ISALPHA(*skipwhite(next + 2))))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004344 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02004345 {
4346 next = next_line_from_context(cctx, TRUE);
4347 if (next == NULL)
4348 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004349 *arg = next;
4350 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02004351 }
4352 }
4353
Bram Moolenaarcd268012021-07-22 19:11:08 +02004354 // Do not skip over white space to find the "(", "execute 'x' (expr)"
4355 // is not a function call.
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02004356 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004358 garray_T *stack = &cctx->ctx_type_stack;
4359 type_T *type;
4360 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004362 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004363 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004364 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004367 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4368
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004369 *arg = skipwhite(p + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004370 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004372 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004374 if (keeping_dict)
4375 {
4376 keeping_dict = FALSE;
4377 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4378 return FAIL;
4379 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004381 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004383 char_u *pstart = p;
4384
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004385 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004386 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004387 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 // something->method()
4390 // Apply the '!', '-' and '+' first:
4391 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004392 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004395 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004396 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004397 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004398 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004399 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004400 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004401 garray_T *stack = &cctx->ctx_type_stack;
4402 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004403 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004404 int expr_isn_start = cctx->ctx_instr.ga_len;
4405 int expr_isn_end;
4406 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004407
4408 // Funcref call: list->(Refs[2])(arg)
4409 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004410 //
4411 // Fist compile the function expression.
4412 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004413 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004414
4415 // Remember the next instruction index, where the instructions
4416 // for arguments are being written.
4417 expr_isn_end = cctx->ctx_instr.ga_len;
4418
4419 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004420 if (**arg != '(')
4421 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004422 if (*skipwhite(*arg) == '(')
4423 emsg(_(e_nowhitespace));
4424 else
4425 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004426 return FAIL;
4427 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004428 *arg = skipwhite(*arg + 1);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004429 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004430 return FAIL;
4431
Bram Moolenaar2927c072021-04-05 19:41:21 +02004432 // Move the instructions for the arguments to before the
4433 // instructions of the expression and move the type of the
4434 // expression after the argument types. This is what ISN_PCALL
4435 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004436 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004437 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4438 if (arg_isn_count > 0)
4439 {
4440 int expr_isn_count = expr_isn_end - expr_isn_start;
4441 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4442
4443 if (isn == NULL)
4444 return FAIL;
4445 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4446 + expr_isn_start,
4447 sizeof(isn_T) * expr_isn_count);
4448 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4449 + expr_isn_start,
4450 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4451 sizeof(isn_T) * arg_isn_count);
4452 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4453 + expr_isn_start + arg_isn_count,
4454 isn, sizeof(isn_T) * expr_isn_count);
4455 vim_free(isn);
4456
4457 type = ((type_T **)stack->ga_data)[type_idx_start];
4458 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4459 ((type_T **)stack->ga_data) + type_idx_start + 1,
4460 sizeof(type_T *)
4461 * (stack->ga_len - type_idx_start - 1));
4462 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4463 }
4464
Bram Moolenaar7e368202020-12-25 21:56:57 +01004465 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarc4c56422021-07-21 20:38:46 +02004466 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 return FAIL;
4468 }
4469 else
4470 {
4471 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004472 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004473 if (!eval_isnamec1(*p))
4474 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004475 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004476 return FAIL;
4477 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004478 if (ASCII_ISALPHA(*p) && p[1] == ':')
4479 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004480 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 ;
4482 if (*p != '(')
4483 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004484 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 return FAIL;
4486 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004487 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 return FAIL;
4489 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004490 if (keeping_dict)
4491 {
4492 keeping_dict = FALSE;
4493 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4494 return FAIL;
4495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004497 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004499 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004500
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004501 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004502 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004503 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004504 // blob index: blob[123]
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004505 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004506 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004507 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004508
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004509 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004510 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004511 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004512 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004513 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004514 // missing first index is equal to zero
4515 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004516 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004517 else
4518 {
4519 if (compile_expr0(arg, cctx) == FAIL)
4520 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004521 if (**arg == ':')
4522 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004523 semsg(_(e_white_space_required_before_and_after_str_at_str),
4524 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004525 return FAIL;
4526 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004527 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004528 return FAIL;
4529 *arg = skipwhite(*arg);
4530 }
4531 if (**arg == ':')
4532 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004533 is_slice = TRUE;
4534 ++*arg;
4535 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4536 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004537 semsg(_(e_white_space_required_before_and_after_str_at_str),
4538 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004539 return FAIL;
4540 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004541 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004542 return FAIL;
4543 if (**arg == ']')
4544 // missing second index is equal to end of string
4545 generate_PUSHNR(cctx, -1);
4546 else
4547 {
4548 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004549 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004550 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004551 return FAIL;
4552 *arg = skipwhite(*arg);
4553 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555
4556 if (**arg != ']')
4557 {
4558 emsg(_(e_missbrac));
4559 return FAIL;
4560 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004561 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004563 if (keeping_dict)
4564 {
4565 keeping_dict = FALSE;
4566 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
4567 return FAIL;
4568 }
4569 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004570 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004572 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004574 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004575 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004576 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004577 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004578
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004579 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004580 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004581 {
4582 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004583 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004584 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004585 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004586 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 while (eval_isnamec(*p))
4588 MB_PTR_ADV(p);
4589 if (p == *arg)
4590 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004591 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 return FAIL;
4593 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004594 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
4595 return FAIL;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004596 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 return FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004598 keeping_dict = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 *arg = p;
4600 }
4601 else
4602 break;
4603 }
4604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 // Turn "dict.Func" into a partial for "Func" bound to "dict".
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004606 // This needs to be done at runtime to be able to check the type.
4607 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
4608 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609
4610 return OK;
4611}
4612
4613/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004614 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4615 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004617 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004618 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004619 *
4620 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 */
4622
4623/*
4624 * number number constant
4625 * 0zFFFFFFFF Blob constant
4626 * "string" string constant
4627 * 'string' literal string constant
4628 * &option-name option value
4629 * @r register contents
4630 * identifier variable value
4631 * function() function call
4632 * $VAR environment variable
4633 * (expression) nested expression
4634 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004635 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 *
4637 * Also handle:
4638 * ! in front logical NOT
4639 * - in front unary minus
4640 * + in front unary plus (ignored)
4641 * trailing (arg) funcref/partial call
4642 * trailing [] subscript in String or List
4643 * trailing .name entry in Dictionary
4644 * trailing ->name() method call
4645 */
4646 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004647compile_expr7(
4648 char_u **arg,
4649 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004650 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 char_u *start_leader, *end_leader;
4653 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004654 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004655 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004657 ppconst->pp_is_const = FALSE;
4658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 /*
4660 * Skip '!', '-' and '+' characters. They are handled later.
4661 */
4662 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004663 if (eval_leader(arg, TRUE) == FAIL)
4664 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 end_leader = *arg;
4666
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004667 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 switch (**arg)
4669 {
4670 /*
4671 * Number constant.
4672 */
4673 case '0': // also for blob starting with 0z
4674 case '1':
4675 case '2':
4676 case '3':
4677 case '4':
4678 case '5':
4679 case '6':
4680 case '7':
4681 case '8':
4682 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004683 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004685 // Apply "-" and "+" just before the number now, right to
4686 // left. Matters especially when "->" follows. Stops at
4687 // '!'.
4688 if (apply_leader(rettv, TRUE,
4689 start_leader, &end_leader) == FAIL)
4690 {
4691 clear_tv(rettv);
4692 return FAIL;
4693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 break;
4695
4696 /*
4697 * String constant: "string".
4698 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004699 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 return FAIL;
4701 break;
4702
4703 /*
4704 * Literal string constant: 'str''ing'.
4705 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004706 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 return FAIL;
4708 break;
4709
4710 /*
4711 * Constant Vim variable.
4712 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004713 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 ret = NOTDONE;
4715 break;
4716
4717 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 * "true" constant
4719 */
4720 case 't': if (STRNCMP(*arg, "true", 4) == 0
4721 && !eval_isnamec((*arg)[4]))
4722 {
4723 *arg += 4;
4724 rettv->v_type = VAR_BOOL;
4725 rettv->vval.v_number = VVAL_TRUE;
4726 }
4727 else
4728 ret = NOTDONE;
4729 break;
4730
4731 /*
4732 * "false" constant
4733 */
4734 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4735 && !eval_isnamec((*arg)[5]))
4736 {
4737 *arg += 5;
4738 rettv->v_type = VAR_BOOL;
4739 rettv->vval.v_number = VVAL_FALSE;
4740 }
4741 else
4742 ret = NOTDONE;
4743 break;
4744
4745 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004746 * "null" constant
4747 */
4748 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004749 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004750 {
4751 *arg += 4;
4752 rettv->v_type = VAR_SPECIAL;
4753 rettv->vval.v_number = VVAL_NULL;
4754 }
4755 else
4756 ret = NOTDONE;
4757 break;
4758
4759 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 * List: [expr, expr]
4761 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004762 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4763 return FAIL;
4764 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 break;
4766
4767 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 * Dictionary: {'key': val, 'key': val}
4769 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004770 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4771 return FAIL;
4772 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 break;
4774
4775 /*
4776 * Option value: &name
4777 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004778 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4779 return FAIL;
4780 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 break;
4782
4783 /*
4784 * Environment variable: $VAR.
4785 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004786 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4787 return FAIL;
4788 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 break;
4790
4791 /*
4792 * Register contents: @r.
4793 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004794 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4795 return FAIL;
4796 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 break;
4798 /*
4799 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004800 * lambda: (arg, arg) => expr
4801 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004803 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4804 ret = compile_lambda(arg, cctx);
4805 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004806 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 break;
4808
4809 default: ret = NOTDONE;
4810 break;
4811 }
4812 if (ret == FAIL)
4813 return FAIL;
4814
Bram Moolenaar1c747212020-05-09 18:28:34 +02004815 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004817 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004818 clear_tv(rettv);
4819 else
4820 // A constant expression can possibly be handled compile time,
4821 // return the value instead of generating code.
4822 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 }
4824 else if (ret == NOTDONE)
4825 {
4826 char_u *p;
4827 int r;
4828
4829 if (!eval_isnamec1(**arg))
4830 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004831 if (!vim9_bad_comment(*arg))
4832 {
4833 if (ends_excmd(*skipwhite(*arg)))
4834 semsg(_(e_empty_expression_str), *arg);
4835 else
4836 semsg(_(e_name_expected_str), *arg);
4837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 return FAIL;
4839 }
4840
4841 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004842 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004843 if (p - *arg == (size_t)1 && **arg == '_')
4844 {
4845 emsg(_(e_cannot_use_underscore_here));
4846 return FAIL;
4847 }
4848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004850 {
4851 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02004855 if (cctx->ctx_skip != SKIP_YES
4856 && generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004857 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004858 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 if (r == FAIL)
4861 return FAIL;
4862 }
4863
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004864 // Handle following "[]", ".member", etc.
4865 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004866 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004867 ppconst) == FAIL)
4868 return FAIL;
4869 if (ppconst->pp_used > 0)
4870 {
4871 // apply the '!', '-' and '+' before the constant
4872 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004873 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004874 return FAIL;
4875 return OK;
4876 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004877 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004879 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880}
4881
4882/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004883 * Give the "white on both sides" error, taking the operator from "p[len]".
4884 */
4885 void
4886error_white_both(char_u *op, int len)
4887{
4888 char_u buf[10];
4889
4890 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004891 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004892}
4893
4894/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004895 * <type>expr7: runtime type check / conversion
4896 */
4897 static int
4898compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4899{
4900 type_T *want_type = NULL;
4901
4902 // Recognize <type>
4903 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4904 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004905 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004906 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4907 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004908 return FAIL;
4909
4910 if (**arg != '>')
4911 {
4912 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004913 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004914 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004915 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004916 return FAIL;
4917 }
4918 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004919 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004920 return FAIL;
4921 }
4922
4923 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4924 return FAIL;
4925
4926 if (want_type != NULL)
4927 {
4928 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004929 type_T *actual;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004930 where_T where = WHERE_INIT;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004931
Bram Moolenaard1103582020-08-14 22:44:25 +02004932 generate_ppconst(cctx, ppconst);
4933 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004934 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004935 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004936 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004937 return FAIL;
4938 }
4939 }
4940
4941 return OK;
4942}
4943
4944/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 * * number multiplication
4946 * / number division
4947 * % number modulo
4948 */
4949 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004950compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951{
4952 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004953 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004954 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004956 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004957 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 return FAIL;
4959
4960 /*
4961 * Repeat computing, until no "*", "/" or "%" is following.
4962 */
4963 for (;;)
4964 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004965 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 if (*op != '*' && *op != '/' && *op != '%')
4967 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004968 if (next != NULL)
4969 {
4970 *arg = next_line_from_context(cctx, TRUE);
4971 op = skipwhite(*arg);
4972 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004973
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004974 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004976 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004977 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004979 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004980 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004982 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004983 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004985
4986 if (ppconst->pp_used == ppconst_used + 2
4987 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4988 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004989 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004990 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4991 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4992 varnumber_T res = 0;
4993 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004995 // both are numbers: compute the result
4996 switch (*op)
4997 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004998 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004999 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005000 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005001 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005002 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01005003 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005004 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005005 break;
5006 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01005007 if (failed)
5008 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005009 tv1->vval.v_number = res;
5010 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005011 }
5012 else
5013 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005014 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005015 generate_two_op(cctx, op);
5016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 }
5018
5019 return OK;
5020}
5021
5022/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01005023 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 * - number subtraction
5025 * .. string concatenation
5026 */
5027 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005028compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029{
5030 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005031 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005033 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034
5035 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005036 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037 return FAIL;
5038
5039 /*
5040 * Repeat computing, until no "+", "-" or ".." is following.
5041 */
5042 for (;;)
5043 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005044 op = may_peek_next_line(cctx, *arg, &next);
5045 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02005047 if (op[0] == op[1] && *op != '.' && next)
5048 // Finding "++" or "--" on the next line is a separate command.
5049 // But ".." is concatenation.
5050 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005052 if (next != NULL)
5053 {
5054 *arg = next_line_from_context(cctx, TRUE);
5055 op = skipwhite(*arg);
5056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005058 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005060 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005061 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 }
5063
Bram Moolenaare0de1712020-12-02 17:36:54 +01005064 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005065 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005067 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 return FAIL;
5070
Bram Moolenaara5565e42020-05-09 15:44:01 +02005071 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005072 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02005073 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
5074 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
5075 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
5076 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005078 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
5079 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005080
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005081 // concat/subtract/add constant numbers
5082 if (*op == '+')
5083 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
5084 else if (*op == '-')
5085 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
5086 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005087 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005088 // concatenate constant strings
5089 char_u *s1 = tv1->vval.v_string;
5090 char_u *s2 = tv2->vval.v_string;
5091 size_t len1 = STRLEN(s1);
5092
5093 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
5094 if (tv1->vval.v_string == NULL)
5095 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005096 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02005097 return FAIL;
5098 }
5099 mch_memmove(tv1->vval.v_string, s1, len1);
5100 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005101 vim_free(s1);
5102 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005103 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005104 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 }
5106 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005107 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005108 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01005109 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005110 if (*op == '.')
5111 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005112 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
5113 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02005114 return FAIL;
5115 generate_instr_drop(cctx, ISN_CONCAT, 1);
5116 }
5117 else
5118 generate_two_op(cctx, op);
5119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 }
5121
5122 return OK;
5123}
5124
5125/*
5126 * expr5a == expr5b
5127 * expr5a =~ expr5b
5128 * expr5a != expr5b
5129 * expr5a !~ expr5b
5130 * expr5a > expr5b
5131 * expr5a >= expr5b
5132 * expr5a < expr5b
5133 * expr5a <= expr5b
5134 * expr5a is expr5b
5135 * expr5a isnot expr5b
5136 *
5137 * Produces instructions:
5138 * EVAL expr5a Push result of "expr5a"
5139 * EVAL expr5b Push result of "expr5b"
5140 * COMPARE one of the compare instructions
5141 */
5142 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144{
Bram Moolenaar657137c2021-01-09 15:45:23 +01005145 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005147 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005150 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151
5152 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005153 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154 return FAIL;
5155
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005156 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005157 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158
5159 /*
5160 * If there is a comparative operator, use it.
5161 */
5162 if (type != EXPR_UNKNOWN)
5163 {
5164 int ic = FALSE; // Default: do not ignore case
5165
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005166 if (next != NULL)
5167 {
5168 *arg = next_line_from_context(cctx, TRUE);
5169 p = skipwhite(*arg);
5170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 if (type_is && (p[len] == '?' || p[len] == '#'))
5172 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005173 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174 return FAIL;
5175 }
5176 // extra question mark appended: ignore case
5177 if (p[len] == '?')
5178 {
5179 ic = TRUE;
5180 ++len;
5181 }
5182 // extra '#' appended: match case (ignored)
5183 else if (p[len] == '#')
5184 ++len;
5185 // nothing appended: match case
5186
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005187 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005189 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005190 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 }
5192
5193 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01005194 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005195 return FAIL;
5196
Bram Moolenaara5565e42020-05-09 15:44:01 +02005197 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 return FAIL;
5199
Bram Moolenaara5565e42020-05-09 15:44:01 +02005200 if (ppconst->pp_used == ppconst_used + 2)
5201 {
5202 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5203 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5204 int ret;
5205
5206 // Both sides are a constant, compute the result now.
5207 // First check for a valid combination of types, this is more
5208 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02005209 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005210 ret = FAIL;
5211 else
5212 {
5213 ret = typval_compare(tv1, tv2, type, ic);
5214 tv1->v_type = VAR_BOOL;
5215 tv1->vval.v_number = tv1->vval.v_number
5216 ? VVAL_TRUE : VVAL_FALSE;
5217 clear_tv(tv2);
5218 --ppconst->pp_used;
5219 }
5220 return ret;
5221 }
5222
5223 generate_ppconst(cctx, ppconst);
5224 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 }
5226
5227 return OK;
5228}
5229
Bram Moolenaar7f141552020-05-09 17:35:53 +02005230static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
5231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232/*
5233 * Compile || or &&.
5234 */
5235 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005236compile_and_or(
5237 char_u **arg,
5238 cctx_T *cctx,
5239 char *op,
5240 ppconst_T *ppconst,
5241 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005243 char_u *next;
5244 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 int opchar = *op;
5246
5247 if (p[0] == opchar && p[1] == opchar)
5248 {
5249 garray_T *instr = &cctx->ctx_instr;
5250 garray_T end_ga;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005251 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252
5253 /*
5254 * Repeat until there is no following "||" or "&&"
5255 */
5256 ga_init2(&end_ga, sizeof(int), 10);
5257 while (p[0] == opchar && p[1] == opchar)
5258 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02005259 long start_lnum = SOURCING_LNUM;
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005260 long save_sourcing_lnum;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005261 int start_ctx_lnum = cctx->ctx_lnum;
5262 int save_lnum;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005263 int const_used;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005264 int status;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005265 jumpwhen_T jump_when = opchar == '|'
5266 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005267
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005268 if (next != NULL)
5269 {
5270 *arg = next_line_from_context(cctx, TRUE);
5271 p = skipwhite(*arg);
5272 }
5273
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005274 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5275 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005276 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02005277 op, p);
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005278 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005279 return FAIL;
5280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005282 save_sourcing_lnum = SOURCING_LNUM;
Bram Moolenaara7511c02021-04-03 21:47:07 +02005283 SOURCING_LNUM = start_lnum;
5284 save_lnum = cctx->ctx_lnum;
5285 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005286
5287 status = check_ppconst_bool(ppconst);
Bram Moolenaar1b862c42021-07-23 19:30:19 +02005288 if (status != FAIL)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005289 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005290 // Use the last ppconst if possible.
5291 if (ppconst->pp_used > 0)
5292 {
5293 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5294 int is_true = tv2bool(tv);
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005295
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005296 if ((is_true && opchar == '|')
5297 || (!is_true && opchar == '&'))
5298 {
5299 // For "false && expr" and "true || expr" the "expr"
5300 // does not need to be evaluated.
5301 cctx->ctx_skip = SKIP_YES;
5302 clear_tv(tv);
5303 tv->v_type = VAR_BOOL;
5304 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
5305 }
5306 else
5307 {
5308 // For "true && expr" and "false || expr" only "expr"
5309 // needs to be evaluated.
5310 --ppconst->pp_used;
5311 jump_when = JUMP_NEVER;
5312 }
5313 }
5314 else
5315 {
5316 // Every part must evaluate to a bool.
5317 status = bool_on_stack(cctx);
5318 }
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005319 }
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005320 if (status != FAIL)
5321 status = ga_grow(&end_ga, 1);
Bram Moolenaara7511c02021-04-03 21:47:07 +02005322 cctx->ctx_lnum = save_lnum;
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005323 if (status == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 {
5325 ga_clear(&end_ga);
5326 return FAIL;
5327 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005328
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005329 if (jump_when != JUMP_NEVER)
5330 {
5331 if (cctx->ctx_skip != SKIP_YES)
5332 {
5333 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5334 ++end_ga.ga_len;
5335 }
5336 generate_JUMP(cctx, jump_when, 0);
5337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338
5339 // eval the next expression
Bram Moolenaar9e60e892021-07-15 15:40:58 +02005340 SOURCING_LNUM = save_sourcing_lnum;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005341 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005342 {
5343 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005344 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01005345 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005346
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005347 const_used = ppconst->pp_used;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005348 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5349 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 {
5351 ga_clear(&end_ga);
5352 return FAIL;
5353 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005354
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005355 // "0 || 1" results in true, "1 && 0" results in false.
5356 if (ppconst->pp_used == const_used + 1)
5357 {
5358 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
5359
5360 if (tv->v_type == VAR_NUMBER
5361 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
5362 {
5363 tv->vval.v_number = tv->vval.v_number == 1
5364 ? VVAL_TRUE : VVAL_FALSE;
5365 tv->v_type = VAR_BOOL;
5366 }
5367 }
5368
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005369 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 }
Bram Moolenaar05bd9782021-07-21 21:37:28 +02005371
5372 if (check_ppconst_bool(ppconst) == FAIL)
5373 {
5374 ga_clear(&end_ga);
5375 return FAIL;
5376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005378 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
5379 // Every part must evaluate to a bool.
5380 if (bool_on_stack(cctx) == FAIL)
5381 {
5382 ga_clear(&end_ga);
5383 return FAIL;
5384 }
5385
5386 if (end_ga.ga_len > 0)
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005387 {
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005388 // Fill in the end label in all jumps.
5389 generate_ppconst(cctx, ppconst);
5390 while (end_ga.ga_len > 0)
5391 {
5392 isn_T *isn;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005393
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005394 --end_ga.ga_len;
5395 isn = ((isn_T *)instr->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005397 isn->isn_arg.jump.jump_where = instr->ga_len;
5398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 }
Bram Moolenaar3dfe2e02021-09-16 20:14:51 +02005400 ga_clear(&end_ga);
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005401
5402 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 }
5404
5405 return OK;
5406}
5407
5408/*
5409 * expr4a && expr4a && expr4a logical AND
5410 *
5411 * Produces instructions:
5412 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005413 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005414 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005416 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417 * EVAL expr4c Push result of "expr4c"
5418 * end:
5419 */
5420 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005421compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005423 int ppconst_used = ppconst->pp_used;
5424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02005426 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 return FAIL;
5428
5429 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005430 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431}
5432
5433/*
5434 * expr3a || expr3b || expr3c logical OR
5435 *
5436 * Produces instructions:
5437 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005438 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005439 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005441 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 * EVAL expr3c Push result of "expr3c"
5443 * end:
5444 */
5445 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02005446compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447{
Bram Moolenaara5565e42020-05-09 15:44:01 +02005448 int ppconst_used = ppconst->pp_used;
5449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02005451 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452 return FAIL;
5453
5454 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005455 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456}
5457
5458/*
5459 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005461 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 * JUMP_IF_FALSE alt jump if false
5463 * EVAL expr1a
5464 * JUMP_ALWAYS end
5465 * alt: EVAL expr1b
5466 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005467 *
5468 * Toplevel expression: expr2 ?? expr1
5469 * Produces instructions:
5470 * EVAL expr2 Push result of "expr2"
5471 * JUMP_AND_KEEP_IF_TRUE end jump if true
5472 * EVAL expr1
5473 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 */
5475 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005476compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477{
5478 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005479 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005480 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481
Bram Moolenaar3988f642020-08-27 22:43:03 +02005482 // Ignore all kinds of errors when not producing code.
5483 if (cctx->ctx_skip == SKIP_YES)
5484 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005485 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005486 return OK;
5487 }
5488
Bram Moolenaar61a89812020-05-07 16:58:17 +02005489 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005490 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 return FAIL;
5492
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005493 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 if (*p == '?')
5495 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005496 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 garray_T *instr = &cctx->ctx_instr;
5498 garray_T *stack = &cctx->ctx_type_stack;
5499 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005500 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005502 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005503 int has_const_expr = FALSE;
5504 int const_value = FALSE;
5505 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005507 if (next != NULL)
5508 {
5509 *arg = next_line_from_context(cctx, TRUE);
5510 p = skipwhite(*arg);
5511 }
5512
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005513 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005514 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005515 semsg(_(e_white_space_required_before_and_after_str_at_str),
mityu4ac198c2021-05-28 17:52:40 +02005516 op_falsy ? "??" : "?", p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005517 return FAIL;
5518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519
Bram Moolenaara5565e42020-05-09 15:44:01 +02005520 if (ppconst->pp_used == ppconst_used + 1)
5521 {
5522 // the condition is a constant, we know whether the ? or the :
5523 // expression is to be evaluated.
5524 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005525 if (op_falsy)
5526 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5527 else
5528 {
5529 int error = FALSE;
5530
5531 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5532 &error);
5533 if (error)
5534 return FAIL;
5535 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005536 cctx->ctx_skip = save_skip == SKIP_YES ||
5537 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5538
5539 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5540 // "left ?? right" and "left" is truthy: produce "left"
5541 generate_ppconst(cctx, ppconst);
5542 else
5543 {
5544 clear_tv(&ppconst->pp_tv[ppconst_used]);
5545 --ppconst->pp_used;
5546 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005547 }
5548 else
5549 {
5550 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005551 if (op_falsy)
5552 end_idx = instr->ga_len;
5553 generate_JUMP(cctx, op_falsy
5554 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5555 if (op_falsy)
5556 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558
5559 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005560 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005561 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005562 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005563 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564
Bram Moolenaara5565e42020-05-09 15:44:01 +02005565 if (!has_const_expr)
5566 {
5567 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005569 if (!op_falsy)
5570 {
5571 // remember the type and drop it
5572 --stack->ga_len;
5573 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005574
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005575 end_idx = instr->ga_len;
5576 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005577
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005578 // jump here from JUMP_IF_FALSE
5579 isn = ((isn_T *)instr->ga_data) + alt_idx;
5580 isn->isn_arg.jump.jump_where = instr->ga_len;
5581 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005584 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005586 // Check for the ":".
5587 p = may_peek_next_line(cctx, *arg, &next);
5588 if (*p != ':')
5589 {
5590 emsg(_(e_missing_colon));
5591 return FAIL;
5592 }
5593 if (next != NULL)
5594 {
5595 *arg = next_line_from_context(cctx, TRUE);
5596 p = skipwhite(*arg);
5597 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005598
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005599 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5600 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005601 semsg(_(e_white_space_required_before_and_after_str_at_str),
5602 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005603 return FAIL;
5604 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005606 // evaluate the third expression
5607 if (has_const_expr)
5608 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005609 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005610 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005611 return FAIL;
5612 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5613 return FAIL;
5614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615
Bram Moolenaara5565e42020-05-09 15:44:01 +02005616 if (!has_const_expr)
5617 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005618 type_T **typep;
5619
Bram Moolenaara5565e42020-05-09 15:44:01 +02005620 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621
Bram Moolenaara5565e42020-05-09 15:44:01 +02005622 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005623 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5624 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005625
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005626 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005627 isn = ((isn_T *)instr->ga_data) + end_idx;
5628 isn->isn_arg.jump.jump_where = instr->ga_len;
5629 }
5630
5631 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632 }
5633 return OK;
5634}
5635
5636/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005637 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005638 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5639 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005640 */
5641 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005642compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005643{
5644 ppconst_T ppconst;
5645
5646 CLEAR_FIELD(ppconst);
5647 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5648 {
5649 clear_ppconst(&ppconst);
5650 return FAIL;
5651 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005652 if (is_const != NULL)
5653 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005654 if (generate_ppconst(cctx, &ppconst) == FAIL)
5655 return FAIL;
5656 return OK;
5657}
5658
5659/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005660 * Toplevel expression.
5661 */
5662 static int
5663compile_expr0(char_u **arg, cctx_T *cctx)
5664{
5665 return compile_expr0_ext(arg, cctx, NULL);
5666}
5667
5668/*
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005669 * Compile "return [expr]".
5670 * When "legacy" is TRUE evaluate [expr] with legacy syntax
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671 */
5672 static char_u *
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005673compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674{
5675 char_u *p = arg;
5676 garray_T *stack = &cctx->ctx_type_stack;
5677 type_T *stack_type;
5678
5679 if (*p != NUL && *p != '|' && *p != '\n')
5680 {
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005681 if (legacy)
5682 {
5683 int save_flags = cmdmod.cmod_flags;
5684
5685 generate_LEGACY_EVAL(cctx, p);
5686 if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5687 0, cctx, FALSE, FALSE) == FAIL)
5688 return NULL;
5689 cmdmod.cmod_flags |= CMOD_LEGACY;
5690 (void)skip_expr(&p, NULL);
5691 cmdmod.cmod_flags = save_flags;
5692 }
5693 else
5694 {
5695 // compile return argument into instructions
5696 if (compile_expr0(&p, cctx) == FAIL)
5697 return NULL;
5698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005700 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005701 {
Bram Moolenaara9931532021-06-12 15:58:16 +02005702 // "check_return_type" with uf_ret_type set to &t_unknown is used
5703 // for an inline function without a specified return type. Set the
5704 // return type here.
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005705 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara9931532021-06-12 15:58:16 +02005706 if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005707 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5708 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaara9931532021-06-12 15:58:16 +02005709 || (!check_return_type
5710 && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005711 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005712 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005713 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005714 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005715 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005716 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5717 && stack_type->tt_type != VAR_VOID
5718 && stack_type->tt_type != VAR_UNKNOWN)
5719 {
5720 emsg(_(e_returning_value_in_function_without_return_type));
5721 return NULL;
5722 }
5723 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005724 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005725 return NULL;
5726 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728 }
5729 else
5730 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005731 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005732 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005733 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5734 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005736 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 return NULL;
5738 }
5739
5740 // No argument, return zero.
5741 generate_PUSHNR(cctx, 0);
5742 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005743
5744 // Undo any command modifiers.
5745 generate_undo_cmdmods(cctx);
5746
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005747 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748 return NULL;
5749
5750 // "return val | endif" is possible
5751 return skipwhite(p);
5752}
5753
5754/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005755 * Get a line from the compilation context, compatible with exarg_T getline().
5756 * Return a pointer to the line in allocated memory.
5757 * Return NULL for end-of-file or some error.
5758 */
5759 static char_u *
5760exarg_getline(
5761 int c UNUSED,
5762 void *cookie,
5763 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005764 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005765{
5766 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005767 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005768
Bram Moolenaar66250c92020-08-20 15:02:42 +02005769 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005770 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005771 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005772 return NULL;
5773 ++cctx->ctx_lnum;
5774 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5775 // Comment lines result in NULL pointers, skip them.
5776 if (p != NULL)
5777 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005778 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005779}
5780
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005781 void
5782fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5783{
5784 eap->getline = exarg_getline;
5785 eap->cookie = cctx;
5786}
5787
Bram Moolenaar04b12692020-05-04 23:24:44 +02005788/*
5789 * Compile a nested :def command.
5790 */
5791 static char_u *
5792compile_nested_function(exarg_T *eap, cctx_T *cctx)
5793{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005794 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005795 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005796 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005797 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005798 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005799 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005800 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005801
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005802 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005803 {
5804 emsg(_(e_cannot_use_bang_with_nested_def));
5805 return NULL;
5806 }
5807
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005808 if (*name_start == '/')
5809 {
5810 name_end = skip_regexp(name_start + 1, '/', TRUE);
5811 if (*name_end == '/')
5812 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02005813 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005814 }
5815 if (name_end == name_start || *skipwhite(name_end) != '(')
5816 {
5817 if (!ends_excmd2(name_start, name_end))
5818 {
5819 semsg(_(e_invalid_command_str), eap->cmd);
5820 return NULL;
5821 }
5822
5823 // "def" or "def Name": list functions
5824 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5825 return NULL;
5826 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5827 }
5828
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005829 // Only g:Func() can use a namespace.
5830 if (name_start[1] == ':' && !is_global)
5831 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005832 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005833 return NULL;
5834 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005835 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005836 return NULL;
5837
Bram Moolenaar04b12692020-05-04 23:24:44 +02005838 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005839 fill_exarg_from_cctx(eap, cctx);
5840
Bram Moolenaar04b12692020-05-04 23:24:44 +02005841 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00005842 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005843 lambda_name = vim_strsave(get_lambda_name());
5844 if (lambda_name == NULL)
5845 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005846 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005847
Bram Moolenaar822ba242020-05-24 23:00:18 +02005848 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005849 {
5850 r = eap->skip ? OK : FAIL;
5851 goto theend;
5852 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005853
5854 // copy over the block scope IDs before compiling
5855 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5856 {
5857 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5858
5859 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5860 if (ufunc->uf_block_ids != NULL)
5861 {
5862 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5863 sizeof(int) * block_depth);
5864 ufunc->uf_block_depth = block_depth;
5865 }
5866 }
5867
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005868 compile_type = COMPILE_TYPE(ufunc);
5869#ifdef FEAT_PROFILE
5870 // If the outer function is profiled, also compile the nested function for
5871 // profiling.
5872 if (cctx->ctx_compile_type == CT_PROFILE)
5873 compile_type = CT_PROFILE;
5874#endif
5875 if (func_needs_compiling(ufunc, compile_type)
5876 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005877 {
5878 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005879 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005880 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005881
Bram Moolenaar648594e2021-07-11 17:55:01 +02005882#ifdef FEAT_PROFILE
5883 // When the outer function is compiled for profiling, the nested function
5884 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02005885 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02005886 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
5887#endif
5888
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005889 if (is_global)
5890 {
5891 char_u *func_name = vim_strnsave(name_start + 2,
5892 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005893
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005894 if (func_name == NULL)
5895 r = FAIL;
5896 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005897 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005898 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005899 lambda_name = NULL;
5900 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005901 }
5902 else
5903 {
5904 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005905 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005906 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005907
Bram Moolenaareef21022020-08-01 22:16:43 +02005908 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005909 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005910 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005911 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005912 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5913 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005914
5915theend:
5916 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005917 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005918}
5919
5920/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921 * Return the length of an assignment operator, or zero if there isn't one.
5922 */
5923 int
5924assignment_len(char_u *p, int *heredoc)
5925{
5926 if (*p == '=')
5927 {
5928 if (p[1] == '<' && p[2] == '<')
5929 {
5930 *heredoc = TRUE;
5931 return 3;
5932 }
5933 return 1;
5934 }
5935 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5936 return 2;
5937 if (STRNCMP(p, "..=", 3) == 0)
5938 return 3;
5939 return 0;
5940}
5941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005943 * Generate the load instruction for "name".
5944 */
5945 static void
5946generate_loadvar(
5947 cctx_T *cctx,
5948 assign_dest_T dest,
5949 char_u *name,
5950 lvar_T *lvar,
5951 type_T *type)
5952{
5953 switch (dest)
5954 {
5955 case dest_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005956 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5957 break;
5958 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005959 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5960 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5961 else
5962 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005963 break;
5964 case dest_buffer:
5965 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5966 break;
5967 case dest_window:
5968 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5969 break;
5970 case dest_tab:
5971 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5972 break;
5973 case dest_script:
5974 compile_load_scriptvar(cctx,
5975 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5976 break;
5977 case dest_env:
5978 // Include $ in the name here
5979 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5980 break;
5981 case dest_reg:
5982 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5983 break;
5984 case dest_vimvar:
5985 generate_LOADV(cctx, name + 2, TRUE);
5986 break;
5987 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005988 if (lvar->lv_from_outer > 0)
5989 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5990 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005991 else
5992 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5993 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005994 case dest_expr:
5995 // list or dict value should already be on the stack.
5996 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005997 }
5998}
5999
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006000/*
6001 * Skip over "[expr]" or ".member".
6002 * Does not check for any errors.
6003 */
6004 static char_u *
6005skip_index(char_u *start)
6006{
6007 char_u *p = start;
6008
6009 if (*p == '[')
6010 {
6011 p = skipwhite(p + 1);
6012 (void)skip_expr(&p, NULL);
6013 p = skipwhite(p);
6014 if (*p == ']')
6015 return p + 1;
6016 return p;
6017 }
6018 // if (*p == '.')
6019 return to_name_end(p + 1, TRUE);
6020}
6021
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006022 void
6023vim9_declare_error(char_u *name)
6024{
6025 char *scope = "";
6026
6027 switch (*name)
6028 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006029 case 'g': scope = _("global"); break;
6030 case 'b': scope = _("buffer"); break;
6031 case 'w': scope = _("window"); break;
6032 case 't': scope = _("tab"); break;
6033 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006034 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006035 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006036 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006037 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006038 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02006039 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02006040 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006041 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006042 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02006043}
6044
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006045/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006046 * For one assignment figure out the type of destination. Return it in "dest".
6047 * When not recognized "dest" is not set.
6048 * For an option "opt_flags" is set.
6049 * For a v:var "vimvaridx" is set.
6050 * "type" is set to the destination type if known, unchanted otherwise.
6051 * Return FAIL if an error message was given.
6052 */
6053 static int
6054get_var_dest(
6055 char_u *name,
6056 assign_dest_T *dest,
6057 int cmdidx,
6058 int *opt_flags,
6059 int *vimvaridx,
6060 type_T **type,
6061 cctx_T *cctx)
6062{
6063 char_u *p;
6064
6065 if (*name == '&')
6066 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006067 int cc;
6068 long numval;
6069 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006070
6071 *dest = dest_option;
6072 if (cmdidx == CMD_final || cmdidx == CMD_const)
6073 {
6074 emsg(_(e_const_option));
6075 return FAIL;
6076 }
6077 p = name;
6078 p = find_option_end(&p, opt_flags);
6079 if (p == NULL)
6080 {
6081 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02006082 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006083 return FAIL;
6084 }
6085 cc = *p;
6086 *p = NUL;
6087 opt_type = get_option_value(skip_option_env_lead(name),
6088 &numval, NULL, *opt_flags);
6089 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006090 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006091 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01006092 case gov_unknown:
6093 semsg(_(e_unknown_option), name);
6094 return FAIL;
6095 case gov_string:
6096 case gov_hidden_string:
6097 *type = &t_string;
6098 break;
6099 case gov_bool:
6100 case gov_hidden_bool:
6101 *type = &t_bool;
6102 break;
6103 case gov_number:
6104 case gov_hidden_number:
6105 *type = &t_number;
6106 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006107 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006108 }
6109 else if (*name == '$')
6110 {
6111 *dest = dest_env;
6112 *type = &t_string;
6113 }
6114 else if (*name == '@')
6115 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02006116 if (name[1] != '@'
6117 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006118 {
6119 emsg_invreg(name[1]);
6120 return FAIL;
6121 }
6122 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006123 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006124 }
6125 else if (STRNCMP(name, "g:", 2) == 0)
6126 {
6127 *dest = dest_global;
6128 }
6129 else if (STRNCMP(name, "b:", 2) == 0)
6130 {
6131 *dest = dest_buffer;
6132 }
6133 else if (STRNCMP(name, "w:", 2) == 0)
6134 {
6135 *dest = dest_window;
6136 }
6137 else if (STRNCMP(name, "t:", 2) == 0)
6138 {
6139 *dest = dest_tab;
6140 }
6141 else if (STRNCMP(name, "v:", 2) == 0)
6142 {
6143 typval_T *vtv;
6144 int di_flags;
6145
6146 *vimvaridx = find_vim_var(name + 2, &di_flags);
6147 if (*vimvaridx < 0)
6148 {
6149 semsg(_(e_variable_not_found_str), name);
6150 return FAIL;
6151 }
6152 // We use the current value of "sandbox" here, is that OK?
6153 if (var_check_ro(di_flags, name, FALSE))
6154 return FAIL;
6155 *dest = dest_vimvar;
6156 vtv = get_vim_var_tv(*vimvaridx);
6157 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
6158 }
6159 return OK;
6160}
6161
6162/*
6163 * Generate a STORE instruction for "dest", not being "dest_local".
6164 * Return FAIL when out of memory.
6165 */
6166 static int
6167generate_store_var(
6168 cctx_T *cctx,
6169 assign_dest_T dest,
6170 int opt_flags,
6171 int vimvaridx,
6172 int scriptvar_idx,
6173 int scriptvar_sid,
6174 type_T *type,
6175 char_u *name)
6176{
6177 switch (dest)
6178 {
6179 case dest_option:
6180 return generate_STOREOPT(cctx, skip_option_env_lead(name),
6181 opt_flags);
6182 case dest_global:
6183 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01006184 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
6185 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006186 case dest_buffer:
6187 // include b: with the name, easier to execute that way
6188 return generate_STORE(cctx, ISN_STOREB, 0, name);
6189 case dest_window:
6190 // include w: with the name, easier to execute that way
6191 return generate_STORE(cctx, ISN_STOREW, 0, name);
6192 case dest_tab:
6193 // include t: with the name, easier to execute that way
6194 return generate_STORE(cctx, ISN_STORET, 0, name);
6195 case dest_env:
6196 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
6197 case dest_reg:
Bram Moolenaar74f4a962021-06-17 21:03:07 +02006198 return generate_STORE(cctx, ISN_STOREREG,
6199 name[1] == '@' ? '"' : name[1], NULL);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006200 case dest_vimvar:
6201 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
6202 case dest_script:
6203 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02006204 // "s:" may be included in the name.
6205 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006206 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006207 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
6208 scriptvar_sid, scriptvar_idx, type);
6209 case dest_local:
6210 case dest_expr:
6211 // cannot happen
6212 break;
6213 }
6214 return FAIL;
6215}
6216
Bram Moolenaar752fc692021-01-04 21:57:11 +01006217 static int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006218generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
6219{
6220 if (lhs->lhs_dest != dest_local)
6221 return generate_store_var(cctx, lhs->lhs_dest,
6222 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
6223 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
6224 lhs->lhs_type, lhs->lhs_name);
6225
6226 if (lhs->lhs_lvar != NULL)
6227 {
6228 garray_T *instr = &cctx->ctx_instr;
6229 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
6230
6231 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
6232 // ISN_STORENR
6233 if (lhs->lhs_lvar->lv_from_outer == 0
6234 && instr->ga_len == instr_count + 1
6235 && isn->isn_type == ISN_PUSHNR)
6236 {
6237 varnumber_T val = isn->isn_arg.number;
6238 garray_T *stack = &cctx->ctx_type_stack;
6239
6240 isn->isn_type = ISN_STORENR;
6241 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
6242 isn->isn_arg.storenr.stnr_val = val;
6243 if (stack->ga_len > 0)
6244 --stack->ga_len;
6245 }
6246 else if (lhs->lhs_lvar->lv_from_outer > 0)
6247 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
6248 lhs->lhs_lvar->lv_from_outer);
6249 else
6250 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
6251 }
6252 return OK;
6253}
6254
6255 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01006256is_decl_command(int cmdidx)
6257{
6258 return cmdidx == CMD_let || cmdidx == CMD_var
6259 || cmdidx == CMD_final || cmdidx == CMD_const;
6260}
6261
6262/*
6263 * Figure out the LHS type and other properties for an assignment or one item
6264 * of ":unlet" with an index.
6265 * Returns OK or FAIL.
6266 */
6267 static int
6268compile_lhs(
6269 char_u *var_start,
6270 lhs_T *lhs,
6271 int cmdidx,
6272 int heredoc,
6273 int oplen,
6274 cctx_T *cctx)
6275{
6276 char_u *var_end;
6277 int is_decl = is_decl_command(cmdidx);
6278
6279 CLEAR_POINTER(lhs);
6280 lhs->lhs_dest = dest_local;
6281 lhs->lhs_vimvaridx = -1;
6282 lhs->lhs_scriptvar_idx = -1;
6283
6284 // "dest_end" is the end of the destination, including "[expr]" or
6285 // ".name".
6286 // "var_end" is the end of the variable/option/etc. name.
6287 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6288 if (*var_start == '@')
6289 var_end = var_start + 2;
6290 else
6291 {
6292 // skip over the leading "&", "&l:", "&g:" and "$"
6293 var_end = skip_option_env_lead(var_start);
6294 var_end = to_name_end(var_end, TRUE);
6295 }
6296
6297 // "a: type" is declaring variable "a" with a type, not dict "a:".
6298 if (is_decl && lhs->lhs_dest_end == var_start + 2
6299 && lhs->lhs_dest_end[-1] == ':')
6300 --lhs->lhs_dest_end;
6301 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6302 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006303 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006304
6305 // compute the length of the destination without "[expr]" or ".name"
6306 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02006307 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6309 if (lhs->lhs_name == NULL)
6310 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01006311
6312 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6313 // Something follows after the variable: "var[idx]" or "var.key".
6314 lhs->lhs_has_index = TRUE;
6315
Bram Moolenaar752fc692021-01-04 21:57:11 +01006316 if (heredoc)
6317 lhs->lhs_type = &t_list_string;
6318 else
6319 lhs->lhs_type = &t_any;
6320
6321 if (cctx->ctx_skip != SKIP_YES)
6322 {
6323 int declare_error = FALSE;
6324
6325 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6326 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6327 &lhs->lhs_type, cctx) == FAIL)
6328 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02006329 if (lhs->lhs_dest != dest_local
6330 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006331 {
6332 // Specific kind of variable recognized.
6333 declare_error = is_decl;
6334 }
6335 else
6336 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006337 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02006338 if (check_reserved_name(lhs->lhs_name) == FAIL)
6339 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006340
6341 if (lookup_local(var_start, lhs->lhs_varlen,
6342 &lhs->lhs_local_lvar, cctx) == OK)
6343 lhs->lhs_lvar = &lhs->lhs_local_lvar;
6344 else
6345 {
6346 CLEAR_FIELD(lhs->lhs_arg_lvar);
6347 if (arg_exists(var_start, lhs->lhs_varlen,
6348 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6349 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6350 {
6351 if (is_decl)
6352 {
6353 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6354 return FAIL;
6355 }
6356 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6357 }
6358 }
6359 if (lhs->lhs_lvar != NULL)
6360 {
6361 if (is_decl)
6362 {
6363 semsg(_(e_variable_already_declared), lhs->lhs_name);
6364 return FAIL;
6365 }
6366 }
6367 else
6368 {
6369 int script_namespace = lhs->lhs_varlen > 1
6370 && STRNCMP(var_start, "s:", 2) == 0;
6371 int script_var = (script_namespace
6372 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006373 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006374 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02006375 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006376 imported_T *import =
6377 find_imported(var_start, lhs->lhs_varlen, cctx);
6378
6379 if (script_namespace || script_var || import != NULL)
6380 {
6381 char_u *rawname = lhs->lhs_name
6382 + (lhs->lhs_name[1] == ':' ? 2 : 0);
6383
6384 if (is_decl)
6385 {
6386 if (script_namespace)
6387 semsg(_(e_cannot_declare_script_variable_in_function),
6388 lhs->lhs_name);
6389 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006390 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01006391 lhs->lhs_name);
6392 return FAIL;
6393 }
6394 else if (cctx->ctx_ufunc->uf_script_ctx_version
6395 == SCRIPT_VERSION_VIM9
6396 && script_namespace
6397 && !script_var && import == NULL)
6398 {
6399 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6400 return FAIL;
6401 }
6402
6403 lhs->lhs_dest = dest_script;
6404
6405 // existing script-local variables should have a type
6406 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6407 if (import != NULL)
6408 lhs->lhs_scriptvar_sid = import->imp_sid;
6409 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6410 {
Bram Moolenaar08251752021-01-11 21:20:18 +01006411 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006412 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01006413 lhs->lhs_scriptvar_sid, rawname,
6414 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6415 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006416 if (lhs->lhs_scriptvar_idx >= 0)
6417 {
6418 scriptitem_T *si = SCRIPT_ITEM(
6419 lhs->lhs_scriptvar_sid);
6420 svar_T *sv =
6421 ((svar_T *)si->sn_var_vals.ga_data)
6422 + lhs->lhs_scriptvar_idx;
6423 lhs->lhs_type = sv->sv_type;
6424 }
6425 }
6426 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01006427 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006428 == FAIL)
6429 return FAIL;
6430 }
6431 }
6432
6433 if (declare_error)
6434 {
6435 vim9_declare_error(lhs->lhs_name);
6436 return FAIL;
6437 }
6438 }
6439
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006440 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6442 var_end = lhs->lhs_dest_end;
6443
6444 if (lhs->lhs_dest != dest_option)
6445 {
6446 if (is_decl && *var_end == ':')
6447 {
6448 char_u *p;
6449
6450 // parse optional type: "let var: type = expr"
6451 if (!VIM_ISWHITE(var_end[1]))
6452 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01006453 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006454 return FAIL;
6455 }
6456 p = skipwhite(var_end + 1);
6457 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6458 if (lhs->lhs_type == NULL)
6459 return FAIL;
6460 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00006461 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 }
6463 else if (lhs->lhs_lvar != NULL)
6464 lhs->lhs_type = lhs->lhs_lvar->lv_type;
6465 }
6466
Bram Moolenaare42939a2021-04-05 17:11:17 +02006467 if (oplen == 3 && !heredoc
6468 && lhs->lhs_dest != dest_global
6469 && !lhs->lhs_has_index
6470 && lhs->lhs_type->tt_type != VAR_STRING
6471 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006472 {
6473 emsg(_(e_can_only_concatenate_to_string));
6474 return FAIL;
6475 }
6476
6477 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6478 && cctx->ctx_skip != SKIP_YES)
6479 {
6480 if (oplen > 1 && !heredoc)
6481 {
6482 // +=, /=, etc. require an existing variable
6483 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6484 return FAIL;
6485 }
6486 if (!is_decl)
6487 {
6488 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6489 return FAIL;
6490 }
6491
Bram Moolenaar3f327882021-03-17 20:56:38 +01006492 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 if ((lhs->lhs_type->tt_type == VAR_FUNC
6494 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006495 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006496 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006497
6498 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6500 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6501 if (lhs->lhs_lvar == NULL)
6502 return FAIL;
6503 lhs->lhs_new_local = TRUE;
6504 }
6505
6506 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006507 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006508 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006509 char_u *after = var_start + lhs->lhs_varlen;
6510 char_u *p;
6511
Bram Moolenaar752fc692021-01-04 21:57:11 +01006512 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006513 if (is_decl)
6514 {
6515 emsg(_(e_cannot_use_index_when_declaring_variable));
6516 return FAIL;
6517 }
6518
Bram Moolenaarc750d912021-11-29 22:02:12 +00006519 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
6520 // Only the last index is used below, if there are others
6521 // before it generate code for the expression. Thus for
6522 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6523 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006524 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006525 p = skip_index(after);
6526 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01006527 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00006528 lhs->lhs_varlen_total = p - var_start;
6529 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006530 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006531 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006532 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00006533 if (after > var_start + lhs->lhs_varlen)
6534 {
6535 lhs->lhs_varlen = after - var_start;
6536 lhs->lhs_dest = dest_expr;
6537 // We don't know the type before evaluating the expression,
6538 // use "any" until then.
6539 lhs->lhs_type = &t_any;
6540 }
6541
6542 if (lhs->lhs_type->tt_member == NULL)
6543 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006544 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00006545 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006546 }
6547 return OK;
6548}
6549
6550/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006551 * Figure out the LHS and check a few errors.
6552 */
6553 static int
6554compile_assign_lhs(
6555 char_u *var_start,
6556 lhs_T *lhs,
6557 int cmdidx,
6558 int is_decl,
6559 int heredoc,
6560 int oplen,
6561 cctx_T *cctx)
6562{
6563 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6564 return FAIL;
6565
6566 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6567 {
6568 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6569 return FAIL;
6570 }
6571 if (!is_decl && lhs->lhs_lvar != NULL
6572 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6573 {
6574 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6575 return FAIL;
6576 }
6577 return OK;
6578}
6579
6580/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006581 * Return TRUE if "lhs" has a range index: "[expr : expr]".
6582 */
6583 static int
6584has_list_index(char_u *idx_start, cctx_T *cctx)
6585{
6586 char_u *p = idx_start;
6587 int save_skip;
6588
6589 if (*p != '[')
6590 return FALSE;
6591
6592 p = skipwhite(p + 1);
6593 if (*p == ':')
6594 return TRUE;
6595
6596 save_skip = cctx->ctx_skip;
6597 cctx->ctx_skip = SKIP_YES;
6598 (void)compile_expr0(&p, cctx);
6599 cctx->ctx_skip = save_skip;
6600 return *skipwhite(p) == ':';
6601}
6602
6603/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006604 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6605 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006606 */
6607 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006608compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006609 char_u *var_start,
6610 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006611 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006612 cctx_T *cctx)
6613{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006614 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006615 char_u *p;
6616 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006617 int need_white_before = TRUE;
6618 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006619
Bram Moolenaar752fc692021-01-04 21:57:11 +01006620 p = var_start + varlen;
6621 if (*p == '[')
6622 {
6623 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006624 if (*p == ':')
6625 {
6626 // empty first index, push zero
6627 r = generate_PUSHNR(cctx, 0);
6628 need_white_before = FALSE;
6629 }
6630 else
6631 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006632
6633 if (r == OK && *skipwhite(p) == ':')
6634 {
6635 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006636 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006637 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006638 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006639 empty_second = *skipwhite(p + 1) == ']';
6640 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6641 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006642 {
6643 semsg(_(e_white_space_required_before_and_after_str_at_str),
6644 ":", p);
6645 return FAIL;
6646 }
6647 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006648 if (*p == ']')
6649 // empty second index, push "none"
6650 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6651 else
6652 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006653 }
6654
Bram Moolenaar752fc692021-01-04 21:57:11 +01006655 if (r == OK && *skipwhite(p) != ']')
6656 {
6657 // this should not happen
6658 emsg(_(e_missbrac));
6659 r = FAIL;
6660 }
6661 }
6662 else // if (*p == '.')
6663 {
6664 char_u *key_end = to_name_end(p + 1, TRUE);
6665 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6666
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006667 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006668 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006669 return r;
6670}
6671
6672/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006673 * For a LHS with an index, load the variable to be indexed.
6674 */
6675 static int
6676compile_load_lhs(
6677 lhs_T *lhs,
6678 char_u *var_start,
6679 type_T *rhs_type,
6680 cctx_T *cctx)
6681{
6682 if (lhs->lhs_dest == dest_expr)
6683 {
6684 size_t varlen = lhs->lhs_varlen;
6685 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02006686 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006687 char_u *p = var_start;
6688 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006689 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006690
Bram Moolenaare97976b2021-08-01 13:17:17 +02006691 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
6692 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006693 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02006694 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
6695 res = compile_expr0(&p, cctx);
6696 var_start[varlen] = c;
6697 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
6698 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006699 {
6700 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02006701 if (res != FAIL)
6702 emsg(_(e_missbrac));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006703 return FAIL;
6704 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006705
6706 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6707 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6708 // now we can properly check the type
6709 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6710 && rhs_type != &t_void
6711 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6712 FALSE, FALSE) == FAIL)
6713 return FAIL;
6714 }
6715 else
6716 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6717 lhs->lhs_lvar, lhs->lhs_type);
6718 return OK;
6719}
6720
6721/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006722 * Produce code for loading "lhs" and also take care of an index.
6723 * Return OK/FAIL.
6724 */
6725 static int
6726compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6727{
6728 compile_load_lhs(lhs, var_start, NULL, cctx);
6729
6730 if (lhs->lhs_has_index)
6731 {
6732 int range = FALSE;
6733
6734 // Get member from list or dict. First compile the
6735 // index value.
6736 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6737 return FAIL;
6738 if (range)
6739 {
6740 semsg(_(e_cannot_use_range_with_assignment_operator_str),
6741 var_start);
6742 return FAIL;
6743 }
6744
6745 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006746 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02006747 return FAIL;
6748 }
6749 return OK;
6750}
6751
6752/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006753 * Assignment to a list or dict member, or ":unlet" for the item, using the
6754 * information in "lhs".
6755 * Returns OK or FAIL.
6756 */
6757 static int
6758compile_assign_unlet(
6759 char_u *var_start,
6760 lhs_T *lhs,
6761 int is_assign,
6762 type_T *rhs_type,
6763 cctx_T *cctx)
6764{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006765 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006766 garray_T *stack = &cctx->ctx_type_stack;
6767 int range = FALSE;
6768
Bram Moolenaar68452172021-04-12 21:21:02 +02006769 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006770 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006771 if (is_assign && range
6772 && lhs->lhs_type->tt_type != VAR_LIST
6773 && lhs->lhs_type != &t_blob
6774 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02006775 {
6776 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6777 return FAIL;
6778 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006779
6780 if (lhs->lhs_type == &t_any)
6781 {
6782 // Index on variable of unknown type: check at runtime.
6783 dest_type = VAR_ANY;
6784 }
6785 else
6786 {
6787 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006788 if (dest_type == VAR_DICT && range)
6789 {
6790 emsg(e_cannot_use_range_with_dictionary);
6791 return FAIL;
6792 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006793 if (dest_type == VAR_DICT
6794 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006795 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006796 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006797 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006798 type_T *type;
6799
6800 if (range)
6801 {
6802 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6803 if (need_type(type, &t_number,
6804 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006805 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006806 }
6807 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar63cb6562021-07-20 22:21:59 +02006808 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02006809 && need_type(type, &t_number,
6810 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006811 return FAIL;
6812 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006813 }
6814
6815 // Load the dict or list. On the stack we then have:
6816 // - value (for assignment, not for :unlet)
6817 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006818 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006819 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006820 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6821 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006822
Bram Moolenaar68452172021-04-12 21:21:02 +02006823 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6824 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006825 {
6826 if (is_assign)
6827 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006828 if (range)
6829 {
6830 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6831 return FAIL;
6832 }
6833 else
6834 {
6835 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006836
Bram Moolenaar68452172021-04-12 21:21:02 +02006837 if (isn == NULL)
6838 return FAIL;
6839 isn->isn_arg.vartype = dest_type;
6840 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006841 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006842 else if (range)
6843 {
6844 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6845 return FAIL;
6846 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006847 else
6848 {
6849 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6850 return FAIL;
6851 }
6852 }
6853 else
6854 {
6855 emsg(_(e_indexable_type_required));
6856 return FAIL;
6857 }
6858
6859 return OK;
6860}
6861
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006862/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006863 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006864 * "let name"
6865 * "var name = expr"
6866 * "final name = expr"
6867 * "const name = expr"
6868 * "name = expr"
6869 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006870 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006871 * Return NULL for an error.
6872 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006873 */
6874 static char_u *
6875compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6876{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006877 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006879 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 char_u *ret = NULL;
6881 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006882 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02006884 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006886 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888 int oplen = 0;
6889 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006890 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006891 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006893 int is_decl = is_decl_command(cmdidx);
6894 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006895 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006897 // Skip over the "var" or "[var, var]" to get to any "=".
6898 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6899 if (p == NULL)
6900 return *arg == '[' ? arg : NULL;
6901
Bram Moolenaar752fc692021-01-04 21:57:11 +01006902 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 sp = p;
6905 p = skipwhite(p);
6906 op = p;
6907 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006908
6909 if (var_count > 0 && oplen == 0)
6910 // can be something like "[1, 2]->func()"
6911 return arg;
6912
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006913 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006915 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006916 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006917 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006918 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6919 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02006920 if (VIM_ISWHITE(eap->cmd[2]))
6921 {
6922 semsg(_(e_no_white_space_allowed_after_str_str),
6923 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6924 return NULL;
6925 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02006926 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6927 oplen = 2;
6928 incdec = TRUE;
6929 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 if (heredoc)
6932 {
6933 list_T *l;
6934 listitem_T *li;
6935
6936 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006937 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006939 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006940 if (l == NULL)
6941 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
Bram Moolenaar078269b2020-09-21 20:35:55 +02006943 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006945 // Push each line and the create the list.
6946 FOR_ALL_LIST_ITEMS(l, li)
6947 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02006948 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02006949 li->li_tv.vval.v_string = NULL;
6950 }
6951 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 list_free(l);
6954 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006955 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006957 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006959 char_u *wp;
6960
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006961 // for "[var, var] = expr" evaluate the expression here, loop over the
6962 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006963 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006964
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006965 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006966 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006967 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006968 if (compile_expr0(&p, cctx) == FAIL)
6969 return NULL;
6970 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006972 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006974 type_T *stacktype;
6975
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006976 stacktype = stack->ga_len == 0 ? &t_void
6977 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006978 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006980 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006981 goto theend;
6982 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006983 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006984 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006985 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006986 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006987 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6988 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006989 if (stacktype->tt_member != NULL)
6990 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006991 }
6992 }
6993
6994 /*
6995 * Loop over variables in "[var, var] = expr".
6996 * For "var = expr" and "let var: type" this is done only once.
6997 */
6998 if (var_count > 0)
6999 var_start = skipwhite(arg + 1); // skip over the "["
7000 else
7001 var_start = arg;
7002 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
7003 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007004 int instr_count = -1;
7005 int save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007006
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02007007 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
7008 {
7009 // Ignore underscore in "[a, _, b] = list".
7010 if (var_count > 0)
7011 {
7012 var_start = skipwhite(var_start + 2);
7013 continue;
7014 }
7015 emsg(_(e_cannot_use_underscore_here));
7016 goto theend;
7017 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007018 vim_free(lhs.lhs_name);
7019
7020 /*
7021 * Figure out the LHS type and other properties.
7022 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007023 if (compile_assign_lhs(var_start, &lhs, cmdidx,
7024 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007025 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02007026 if (heredoc)
7027 {
7028 SOURCING_LNUM = start_lnum;
7029 if (lhs.lhs_has_type
7030 && need_type(&t_list_string, lhs.lhs_type,
7031 -1, 0, cctx, FALSE, FALSE) == FAIL)
7032 goto theend;
7033 }
7034 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007035 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007036 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007037 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007038 if (oplen > 0 && var_count == 0)
7039 {
7040 // skip over the "=" and the expression
7041 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02007042 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007043 }
7044 }
7045 else if (oplen > 0)
7046 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007047 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01007048 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007049
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007050 // for "+=", "*=", "..=" etc. first load the current value
7051 if (*op != '='
7052 && compile_load_lhs_with_index(&lhs, var_start,
7053 cctx) == FAIL)
7054 goto theend;
7055
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007056 // For "var = expr" evaluate the expression.
7057 if (var_count == 0)
7058 {
7059 int r;
7060
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007061 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007062 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007063 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007064 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007065 r = generate_PUSHNR(cctx, 1);
7066 }
7067 else
7068 {
7069 // Temporarily hide the new local variable here, it is
7070 // not available to this expression.
7071 if (lhs.lhs_new_local)
7072 --cctx->ctx_locals.ga_len;
7073 wp = op + oplen;
7074 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7075 {
7076 if (lhs.lhs_new_local)
7077 ++cctx->ctx_locals.ga_len;
7078 goto theend;
7079 }
7080 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01007081 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01007082 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02007083 if (r == FAIL)
7084 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01007085 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007086 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02007087 else if (semicolon && var_idx == var_count - 1)
7088 {
7089 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007090 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02007091 if (generate_SLICE(cctx, var_count - 1) == FAIL)
7092 goto theend;
7093 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007094 else
7095 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007096 // For "[var, var] = expr" get the "var_idx" item from the
7097 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007098 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01007099 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007100 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007101
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007102 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02007103 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01007104 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007105 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007106 if ((rhs_type->tt_type == VAR_FUNC
7107 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01007108 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01007109 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02007110 goto theend;
7111
Bram Moolenaar752fc692021-01-04 21:57:11 +01007112 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007113 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007114 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007116 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007117 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007118 }
7119 else
7120 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02007121 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007122 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007123 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007124 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007125 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007126 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01007127 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007128 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007129 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01007130 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007131 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007132 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007133 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007134 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007135 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007136 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007137
Bram Moolenaar77709b12021-04-03 21:01:01 +02007138 // Without operator check type here, otherwise below.
7139 // Use the line number of the assignment.
7140 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007141 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
7142 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007143 // If assigning to a list or dict member, use the
7144 // member type. Not for "list[:] =".
7145 if (lhs.lhs_has_index
7146 && !has_list_index(var_start + lhs.lhs_varlen,
7147 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01007148 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02007149 if (need_type_where(rhs_type, use_type, -1, where,
7150 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007151 goto theend;
7152 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007153 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007154 else
7155 {
7156 type_T *lhs_type = lhs.lhs_member_type;
7157
7158 // Special case: assigning to @# can use a number or a
7159 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02007160 // Also: can assign a number to a float.
7161 if ((lhs_type == &t_number_or_string
7162 || lhs_type == &t_float)
7163 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007164 lhs_type = &t_number;
7165 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01007166 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007167 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02007168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007170 else if (cmdidx == CMD_final)
7171 {
7172 emsg(_(e_final_requires_a_value));
7173 goto theend;
7174 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007175 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007177 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007178 goto theend;
7179 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01007180 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007181 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02007182 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007183 goto theend;
7184 }
7185 else
7186 {
7187 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02007188 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007189 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01007190 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007191 {
7192 case VAR_BOOL:
7193 generate_PUSHBOOL(cctx, VVAL_FALSE);
7194 break;
7195 case VAR_FLOAT:
7196#ifdef FEAT_FLOAT
7197 generate_PUSHF(cctx, 0.0);
7198#endif
7199 break;
7200 case VAR_STRING:
7201 generate_PUSHS(cctx, NULL);
7202 break;
7203 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007204 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007205 break;
7206 case VAR_FUNC:
7207 generate_PUSHFUNC(cctx, NULL, &t_func_void);
7208 break;
7209 case VAR_LIST:
7210 generate_NEWLIST(cctx, 0);
7211 break;
7212 case VAR_DICT:
7213 generate_NEWDICT(cctx, 0);
7214 break;
7215 case VAR_JOB:
7216 generate_PUSHJOB(cctx, NULL);
7217 break;
7218 case VAR_CHANNEL:
7219 generate_PUSHCHANNEL(cctx, NULL);
7220 break;
7221 case VAR_NUMBER:
7222 case VAR_UNKNOWN:
7223 case VAR_ANY:
7224 case VAR_PARTIAL:
7225 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007226 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007227 case VAR_SPECIAL: // cannot happen
7228 generate_PUSHNR(cctx, 0);
7229 break;
7230 }
7231 }
7232 if (var_count == 0)
7233 end = p;
7234 }
7235
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007236 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007237 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02007238 break;
7239
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007240 if (oplen > 0 && *op != '=')
7241 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007242 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02007243 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007244
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007245 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007246 {
7247 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
7248 goto theend;
7249 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007250 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007251 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007252 expected = lhs.lhs_member_type;
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007253 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7254 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007255#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007256 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02007257 !(expected == &t_float && (stacktype == &t_number
7258 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007259#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01007260 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02007261 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02007262 goto theend;
7263 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007264
7265 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007266 {
7267 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
7268 goto theend;
7269 }
7270 else if (*op == '+')
7271 {
7272 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01007273 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02007274 lhs.lhs_member_type, stacktype,
7275 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02007276 goto theend;
7277 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02007278 else if (generate_two_op(cctx, op) == FAIL)
7279 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007282 // Use the line number of the assignment for store instruction.
7283 save_lnum = cctx->ctx_lnum;
7284 cctx->ctx_lnum = start_lnum - 1;
7285
Bram Moolenaar752fc692021-01-04 21:57:11 +01007286 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007287 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007288 // Use the info in "lhs" to store the value at the index in the
7289 // list or dict.
7290 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
7291 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007292 {
7293 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007294 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007295 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007296 }
7297 else
7298 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007299 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02007300 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01007301 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007302 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007303 generate_LOCKCONST(cctx);
7304
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007305 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01007306 && (lhs.lhs_type->tt_type == VAR_DICT
7307 || lhs.lhs_type->tt_type == VAR_LIST)
7308 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007309 && !(lhs.lhs_type->tt_member == &t_any
7310 && oplen > 0
7311 && rhs_type != NULL
7312 && rhs_type->tt_type == lhs.lhs_type->tt_type
7313 && rhs_type->tt_member != &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01007314 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007315 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar6e48b842021-08-10 22:52:02 +02007316 // also in legacy script. Not for "list<any> = val", then the
7317 // type of "val" is used.
Bram Moolenaar752fc692021-01-04 21:57:11 +01007318 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01007319
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007320 if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007321 {
7322 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007323 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007324 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007325 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02007326 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007327
7328 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00007329 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007331
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02007332 // For "[var, var] = expr" drop the "expr" value.
7333 // Also for "[var, var; _] = expr".
7334 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02007335 {
Bram Moolenaarec792292020-12-13 21:26:56 +01007336 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02007337 goto theend;
7338 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007339
Bram Moolenaarb2097502020-07-19 17:17:02 +02007340 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341
7342theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01007343 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 return ret;
7345}
7346
7347/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01007348 * Check for an assignment at "eap->cmd", compile it if found.
7349 * Return NOTDONE if there is none, FAIL for failure, OK if done.
7350 */
7351 static int
7352may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7353{
7354 char_u *pskip;
7355 char_u *p;
7356
7357 // Assuming the command starts with a variable or function name,
7358 // find what follows.
7359 // Skip over "var.member", "var[idx]" and the like.
7360 // Also "&opt = val", "$ENV = val" and "@r = val".
7361 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7362 ? eap->cmd + 1 : eap->cmd;
7363 p = to_name_end(pskip, TRUE);
7364 if (p > eap->cmd && *p != NUL)
7365 {
7366 char_u *var_end;
7367 int oplen;
7368 int heredoc;
7369
7370 if (eap->cmd[0] == '@')
7371 var_end = eap->cmd + 2;
7372 else
7373 var_end = find_name_end(pskip, NULL, NULL,
7374 FNE_CHECK_START | FNE_INCL_BR);
7375 oplen = assignment_len(skipwhite(var_end), &heredoc);
7376 if (oplen > 0)
7377 {
7378 size_t len = p - eap->cmd;
7379
7380 // Recognize an assignment if we recognize the variable
7381 // name:
7382 // "g:var = expr"
7383 // "local = expr" where "local" is a local var.
7384 // "script = expr" where "script" is a script-local var.
7385 // "import = expr" where "import" is an imported var
7386 // "&opt = expr"
7387 // "$ENV = expr"
7388 // "@r = expr"
7389 if (*eap->cmd == '&'
7390 || *eap->cmd == '$'
7391 || *eap->cmd == '@'
7392 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01007393 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01007394 {
7395 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7396 if (*line == NULL || *line == eap->cmd)
7397 return FAIL;
7398 return OK;
7399 }
7400 }
7401 }
7402
7403 if (*eap->cmd == '[')
7404 {
7405 // [var, var] = expr
7406 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7407 if (*line == NULL)
7408 return FAIL;
7409 if (*line != eap->cmd)
7410 return OK;
7411 }
7412 return NOTDONE;
7413}
7414
7415/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007416 * Check if "name" can be "unlet".
7417 */
7418 int
7419check_vim9_unlet(char_u *name)
7420{
7421 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7422 {
Bram Moolenaar84367732020-08-23 15:21:55 +02007423 // "unlet s:var" is allowed in legacy script.
7424 if (*name == 's' && !script_is_vim9())
7425 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007426 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007427 return FAIL;
7428 }
7429 return OK;
7430}
7431
7432/*
7433 * Callback passed to ex_unletlock().
7434 */
7435 static int
7436compile_unlet(
7437 lval_T *lvp,
7438 char_u *name_end,
7439 exarg_T *eap,
7440 int deep UNUSED,
7441 void *coookie)
7442{
Bram Moolenaar752fc692021-01-04 21:57:11 +01007443 cctx_T *cctx = coookie;
7444 char_u *p = lvp->ll_name;
7445 int cc = *name_end;
7446 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007447
Bram Moolenaar752fc692021-01-04 21:57:11 +01007448 if (cctx->ctx_skip == SKIP_YES)
7449 return OK;
7450
7451 *name_end = NUL;
7452 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007453 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007454 // :unlet $ENV_VAR
7455 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7456 }
7457 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7458 {
7459 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007460
Bram Moolenaar752fc692021-01-04 21:57:11 +01007461 // This is similar to assigning: lookup the list/dict, compile the
7462 // idx/key. Then instead of storing the value unlet the item.
7463 // unlet {list}[idx]
7464 // unlet {dict}[key] dict.key
7465 //
7466 // Figure out the LHS type and other properties.
7467 //
7468 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7469
7470 // : unlet an indexed item
7471 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007472 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01007473 iemsg("called compile_lhs() without an index");
7474 ret = FAIL;
7475 }
7476 else
7477 {
7478 // Use the info in "lhs" to unlet the item at the index in the
7479 // list or dict.
7480 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01007481 }
7482
Bram Moolenaar752fc692021-01-04 21:57:11 +01007483 vim_free(lhs.lhs_name);
7484 }
7485 else if (check_vim9_unlet(p) == FAIL)
7486 {
7487 ret = FAIL;
7488 }
7489 else
7490 {
7491 // Normal name. Only supports g:, w:, t: and b: namespaces.
7492 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007493 }
7494
Bram Moolenaar752fc692021-01-04 21:57:11 +01007495 *name_end = cc;
7496 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007497}
7498
7499/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007500 * Callback passed to ex_unletlock().
7501 */
7502 static int
7503compile_lock_unlock(
7504 lval_T *lvp,
7505 char_u *name_end,
7506 exarg_T *eap,
7507 int deep UNUSED,
7508 void *coookie)
7509{
7510 cctx_T *cctx = coookie;
7511 int cc = *name_end;
7512 char_u *p = lvp->ll_name;
7513 int ret = OK;
7514 size_t len;
7515 char_u *buf;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007516 isntype_T isn = ISN_EXEC;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007517
7518 if (cctx->ctx_skip == SKIP_YES)
7519 return OK;
7520
7521 // Cannot use :lockvar and :unlockvar on local variables.
7522 if (p[1] != ':')
7523 {
Bram Moolenaarbd77aa92021-08-12 17:06:05 +02007524 char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007525
7526 if (lookup_local(p, end - p, NULL, cctx) == OK)
7527 {
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007528 char_u *s = p;
7529
7530 if (*end != '.' && *end != '[')
7531 {
7532 emsg(_(e_cannot_lock_unlock_local_variable));
7533 return FAIL;
7534 }
7535
7536 // For "d.member" put the local variable on the stack, it will be
7537 // passed to ex_lockvar() indirectly.
7538 if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
7539 return FAIL;
7540 isn = ISN_LOCKUNLOCK;
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007541 }
7542 }
7543
7544 // Checking is done at runtime.
7545 *name_end = NUL;
7546 len = name_end - p + 20;
7547 buf = alloc(len);
7548 if (buf == NULL)
7549 ret = FAIL;
7550 else
7551 {
7552 vim_snprintf((char *)buf, len, "%s %s",
7553 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7554 p);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007555 ret = generate_EXEC(cctx, isn, buf);
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007556
7557 vim_free(buf);
7558 *name_end = cc;
7559 }
7560 return ret;
7561}
7562
7563/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007564 * compile "unlet var", "lock var" and "unlock var"
7565 * "arg" points to "var".
7566 */
7567 static char_u *
7568compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7569{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02007570 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7571 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7572 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007573 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7574}
7575
7576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7578 */
7579 static int
7580compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7581{
7582 garray_T *instr = &cctx->ctx_instr;
7583 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7584
7585 if (endlabel == NULL)
7586 return FAIL;
7587 endlabel->el_next = *el;
7588 *el = endlabel;
7589 endlabel->el_end_label = instr->ga_len;
7590
7591 generate_JUMP(cctx, when, 0);
7592 return OK;
7593}
7594
7595 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007596compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597{
7598 garray_T *instr = &cctx->ctx_instr;
7599
7600 while (*el != NULL)
7601 {
7602 endlabel_T *cur = (*el);
7603 isn_T *isn;
7604
7605 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007606 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 *el = cur->el_next;
7608 vim_free(cur);
7609 }
7610}
7611
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007612 static void
7613compile_free_jump_to_end(endlabel_T **el)
7614{
7615 while (*el != NULL)
7616 {
7617 endlabel_T *cur = (*el);
7618
7619 *el = cur->el_next;
7620 vim_free(cur);
7621 }
7622}
7623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624/*
7625 * Create a new scope and set up the generic items.
7626 */
7627 static scope_T *
7628new_scope(cctx_T *cctx, scopetype_T type)
7629{
7630 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7631
7632 if (scope == NULL)
7633 return NULL;
7634 scope->se_outer = cctx->ctx_scope;
7635 cctx->ctx_scope = scope;
7636 scope->se_type = type;
7637 scope->se_local_count = cctx->ctx_locals.ga_len;
7638 return scope;
7639}
7640
7641/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007642 * Free the current scope and go back to the outer scope.
7643 */
7644 static void
7645drop_scope(cctx_T *cctx)
7646{
7647 scope_T *scope = cctx->ctx_scope;
7648
7649 if (scope == NULL)
7650 {
7651 iemsg("calling drop_scope() without a scope");
7652 return;
7653 }
7654 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007655 switch (scope->se_type)
7656 {
7657 case IF_SCOPE:
7658 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7659 case FOR_SCOPE:
7660 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7661 case WHILE_SCOPE:
7662 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7663 case TRY_SCOPE:
7664 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7665 case NO_SCOPE:
7666 case BLOCK_SCOPE:
7667 break;
7668 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007669 vim_free(scope);
7670}
7671
7672/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 * compile "if expr"
7674 *
7675 * "if expr" Produces instructions:
7676 * EVAL expr Push result of "expr"
7677 * JUMP_IF_FALSE end
7678 * ... body ...
7679 * end:
7680 *
7681 * "if expr | else" Produces instructions:
7682 * EVAL expr Push result of "expr"
7683 * JUMP_IF_FALSE else
7684 * ... body ...
7685 * JUMP_ALWAYS end
7686 * else:
7687 * ... body ...
7688 * end:
7689 *
7690 * "if expr1 | elseif expr2 | else" Produces instructions:
7691 * EVAL expr Push result of "expr"
7692 * JUMP_IF_FALSE elseif
7693 * ... body ...
7694 * JUMP_ALWAYS end
7695 * elseif:
7696 * EVAL expr Push result of "expr"
7697 * JUMP_IF_FALSE else
7698 * ... body ...
7699 * JUMP_ALWAYS end
7700 * else:
7701 * ... body ...
7702 * end:
7703 */
7704 static char_u *
7705compile_if(char_u *arg, cctx_T *cctx)
7706{
7707 char_u *p = arg;
7708 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007709 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007711 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007712 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713
Bram Moolenaara5565e42020-05-09 15:44:01 +02007714 CLEAR_FIELD(ppconst);
7715 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007716 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007717 clear_ppconst(&ppconst);
7718 return NULL;
7719 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007720 if (!ends_excmd2(arg, skipwhite(p)))
7721 {
7722 semsg(_(e_trailing_arg), p);
7723 return NULL;
7724 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007725 if (cctx->ctx_skip == SKIP_YES)
7726 clear_ppconst(&ppconst);
7727 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007728 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007729 int error = FALSE;
7730 int v;
7731
Bram Moolenaara5565e42020-05-09 15:44:01 +02007732 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007733 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007734 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007735 if (error)
7736 return NULL;
7737 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007738 }
7739 else
7740 {
7741 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007742 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007743 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007744 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007745 if (bool_on_stack(cctx) == FAIL)
7746 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748
Bram Moolenaara91a7132021-03-25 21:12:15 +01007749 // CMDMOD_REV must come before the jump
7750 generate_undo_cmdmods(cctx);
7751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 scope = new_scope(cctx, IF_SCOPE);
7753 if (scope == NULL)
7754 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007755 scope->se_skip_save = skip_save;
7756 // "is_had_return" will be reset if any block does not end in :return
7757 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007759 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007760 {
7761 // "where" is set when ":elseif", "else" or ":endif" is found
7762 scope->se_u.se_if.is_if_label = instr->ga_len;
7763 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7764 }
7765 else
7766 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767
Bram Moolenaarced68a02021-01-24 17:53:47 +01007768#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007769 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01007770 && skip_save != SKIP_YES)
7771 {
7772 // generated a profile start, need to generate a profile end, since it
7773 // won't be done after returning
7774 cctx->ctx_skip = SKIP_NOT;
7775 generate_instr(cctx, ISN_PROF_END);
7776 cctx->ctx_skip = SKIP_YES;
7777 }
7778#endif
7779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780 return p;
7781}
7782
7783 static char_u *
7784compile_elseif(char_u *arg, cctx_T *cctx)
7785{
7786 char_u *p = arg;
7787 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar90770b72021-11-30 20:57:38 +00007788 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789 isn_T *isn;
7790 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007791 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007792 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793
7794 if (scope == NULL || scope->se_type != IF_SCOPE)
7795 {
7796 emsg(_(e_elseif_without_if));
7797 return NULL;
7798 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007799 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007800 if (!cctx->ctx_had_return)
7801 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802
Bram Moolenaarced68a02021-01-24 17:53:47 +01007803 if (cctx->ctx_skip == SKIP_NOT)
7804 {
7805 // previous block was executed, this one and following will not
7806 cctx->ctx_skip = SKIP_YES;
7807 scope->se_u.se_if.is_seen_skip_not = TRUE;
7808 }
7809 if (scope->se_u.se_if.is_seen_skip_not)
7810 {
7811 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007812 // Do not count the "elseif" for profiling and cmdmod
7813 instr->ga_len = current_instr_idx(cctx);
7814
Bram Moolenaarced68a02021-01-24 17:53:47 +01007815 skip_expr_cctx(&p, cctx);
7816 return p;
7817 }
7818
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007819 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007820 {
Bram Moolenaar093165c2021-08-22 13:35:31 +02007821 int moved_cmdmod = FALSE;
7822 int saved_debug = FALSE;
7823 isn_T debug_isn;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007824
7825 // Move any CMDMOD instruction to after the jump
7826 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7827 {
Bram Moolenaar35578162021-08-02 19:10:38 +02007828 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaara91a7132021-03-25 21:12:15 +01007829 return NULL;
7830 ((isn_T *)instr->ga_data)[instr->ga_len] =
7831 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7832 --instr->ga_len;
7833 moved_cmdmod = TRUE;
7834 }
7835
Bram Moolenaar093165c2021-08-22 13:35:31 +02007836 // Remove the already generated ISN_DEBUG, it is written below the
7837 // ISN_FOR instruction.
7838 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7839 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7840 .isn_type == ISN_DEBUG)
7841 {
7842 --instr->ga_len;
7843 debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
7844 saved_debug = TRUE;
7845 }
7846
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007847 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007849 return NULL;
7850 // previous "if" or "elseif" jumps here
7851 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7852 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007853
Bram Moolenaara91a7132021-03-25 21:12:15 +01007854 if (moved_cmdmod)
7855 ++instr->ga_len;
Bram Moolenaar093165c2021-08-22 13:35:31 +02007856
7857 if (saved_debug)
7858 {
7859 // move the debug instruction here
7860 if (GA_GROW_FAILS(instr, 1))
7861 return NULL;
7862 ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
7863 ++instr->ga_len;
7864 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007867 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007868 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007869 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007870 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007871 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007872#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007873 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007874 // the previous block was skipped, need to profile this line
7875 generate_instr(cctx, ISN_PROF_START);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007876#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007877 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007878 // the previous block was skipped, may want to debug this line
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02007879 generate_instr_debug(cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007880 }
Bram Moolenaar90770b72021-11-30 20:57:38 +00007881
7882 instr_count = instr->ga_len;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007883 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007884 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007885 clear_ppconst(&ppconst);
7886 return NULL;
7887 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007888 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007889 if (!ends_excmd2(arg, skipwhite(p)))
7890 {
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007891 clear_ppconst(&ppconst);
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007892 semsg(_(e_trailing_arg), p);
7893 return NULL;
7894 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007895 if (scope->se_skip_save == SKIP_YES)
7896 clear_ppconst(&ppconst);
7897 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007898 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007899 int error = FALSE;
7900 int v;
7901
Bram Moolenaarfad27422021-11-30 21:58:19 +00007902 // The expression result is a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007903 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7904 if (error)
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007905 {
7906 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007907 return NULL;
Bram Moolenaar56a8ffd2021-12-01 10:10:22 +00007908 }
Bram Moolenaar13106602020-10-04 16:06:05 +02007909 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007910 clear_ppconst(&ppconst);
7911 scope->se_u.se_if.is_if_label = -1;
7912 }
7913 else
7914 {
7915 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007916 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007917 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007918 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007919 if (bool_on_stack(cctx) == FAIL)
7920 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007921
Bram Moolenaara91a7132021-03-25 21:12:15 +01007922 // CMDMOD_REV must come before the jump
7923 generate_undo_cmdmods(cctx);
7924
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007925 // "where" is set when ":elseif", "else" or ":endif" is found
7926 scope->se_u.se_if.is_if_label = instr->ga_len;
7927 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929
7930 return p;
7931}
7932
7933 static char_u *
7934compile_else(char_u *arg, cctx_T *cctx)
7935{
7936 char_u *p = arg;
7937 garray_T *instr = &cctx->ctx_instr;
7938 isn_T *isn;
7939 scope_T *scope = cctx->ctx_scope;
7940
7941 if (scope == NULL || scope->se_type != IF_SCOPE)
7942 {
7943 emsg(_(e_else_without_if));
7944 return NULL;
7945 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007946 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007947 if (!cctx->ctx_had_return)
7948 scope->se_u.se_if.is_had_return = FALSE;
7949 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950
Bram Moolenaarced68a02021-01-24 17:53:47 +01007951#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007952 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007953 {
7954 if (cctx->ctx_skip == SKIP_NOT
7955 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7956 .isn_type == ISN_PROF_START)
Bram Moolenaare99d4222021-06-13 14:01:26 +02007957 // the previous block was executed, do not count "else" for
7958 // profiling
Bram Moolenaarced68a02021-01-24 17:53:47 +01007959 --instr->ga_len;
7960 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7961 {
7962 // the previous block was not executed, this one will, do count the
7963 // "else" for profiling
7964 cctx->ctx_skip = SKIP_NOT;
7965 generate_instr(cctx, ISN_PROF_END);
7966 generate_instr(cctx, ISN_PROF_START);
7967 cctx->ctx_skip = SKIP_YES;
7968 }
7969 }
7970#endif
7971
7972 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007973 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007974 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007975 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007976 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007977 if (!cctx->ctx_had_return
7978 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7979 JUMP_ALWAYS, cctx) == FAIL)
7980 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007981 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007982
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007983 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007984 {
7985 if (scope->se_u.se_if.is_if_label >= 0)
7986 {
7987 // previous "if" or "elseif" jumps here
7988 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7989 isn->isn_arg.jump.jump_where = instr->ga_len;
7990 scope->se_u.se_if.is_if_label = -1;
7991 }
7992 }
7993
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007994 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007995 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007997
7998 return p;
7999}
8000
8001 static char_u *
8002compile_endif(char_u *arg, cctx_T *cctx)
8003{
8004 scope_T *scope = cctx->ctx_scope;
8005 ifscope_T *ifscope;
8006 garray_T *instr = &cctx->ctx_instr;
8007 isn_T *isn;
8008
Bram Moolenaarfa984412021-03-25 22:15:28 +01008009 if (misplaced_cmdmod(cctx))
8010 return NULL;
8011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 if (scope == NULL || scope->se_type != IF_SCOPE)
8013 {
8014 emsg(_(e_endif_without_if));
8015 return NULL;
8016 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008017 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008018 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008019 if (!cctx->ctx_had_return)
8020 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008021
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008022 if (scope->se_u.se_if.is_if_label >= 0)
8023 {
8024 // previous "if" or "elseif" jumps here
8025 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
8026 isn->isn_arg.jump.jump_where = instr->ga_len;
8027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008029 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01008030
8031#ifdef FEAT_PROFILE
8032 // even when skipping we count the endif as executed, unless the block it's
8033 // in is skipped
Bram Moolenaare99d4222021-06-13 14:01:26 +02008034 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
Bram Moolenaarced68a02021-01-24 17:53:47 +01008035 && scope->se_skip_save != SKIP_YES)
8036 {
8037 cctx->ctx_skip = SKIP_NOT;
8038 generate_instr(cctx, ISN_PROF_START);
8039 }
8040#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02008041 cctx->ctx_skip = scope->se_skip_save;
8042
8043 // If all the blocks end in :return and there is an :else then the
8044 // had_return flag is set.
8045 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008047 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008048 return arg;
8049}
8050
8051/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01008052 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008053 *
8054 * Produces instructions:
8055 * PUSHNR -1
8056 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01008057 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 * top: FOR loop-idx, end Increment index, use list on bottom of stack
8059 * - if beyond end, jump to "end"
8060 * - otherwise get item from list and push it
8061 * STORE var Store item in "var"
8062 * ... body ...
8063 * JUMP top Jump back to repeat
8064 * end: DROP Drop the result of "expr"
8065 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008066 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
8067 * UNPACK 2 Split item in 2
8068 * STORE var1 Store item in "var1"
8069 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008070 */
8071 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01008072compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073{
Bram Moolenaar792f7862020-11-23 08:31:18 +01008074 char_u *arg;
8075 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008076 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008077 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008078 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008079 int var_count = 0;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008080 int var_list = FALSE;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008081 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008084 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008086 lvar_T *loop_lvar; // loop iteration variable
8087 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008089 type_T *item_type = &t_any;
8090 int idx;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008091 int prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092
Bram Moolenaar792f7862020-11-23 08:31:18 +01008093 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01008094 if (p == NULL)
8095 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008096 if (var_count == 0)
8097 var_count = 1;
Bram Moolenaar444d8782021-06-26 12:40:56 +02008098 else
8099 var_list = TRUE; // can also be a list of one variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100
8101 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008102 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008103 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8104 return NULL;
8105 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02008107 if (*p == ':' && wp != p)
8108 semsg(_(e_no_white_space_allowed_before_colon_str), p);
8109 else
8110 emsg(_(e_missing_in));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008111 return NULL;
8112 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008113 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01008114 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
8115 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008117 // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
8118 // instruction.
8119 if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
8120 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8121 .isn_type == ISN_DEBUG)
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008122 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008123 --instr->ga_len;
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008124 prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
8125 .isn_arg.debug.dbg_break_lnum;
8126 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008128 scope = new_scope(cctx, FOR_SCOPE);
8129 if (scope == NULL)
8130 return NULL;
8131
Bram Moolenaar792f7862020-11-23 08:31:18 +01008132 // Reserve a variable to store the loop iteration counter and initialize it
8133 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008134 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
8135 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008136 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008137 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008138 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008140 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008141 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142
8143 // compile "expr", it remains on the stack until "endfor"
8144 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008145 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008146 {
8147 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008148 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02008149 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008150 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151
rbtnnbebf0692021-08-21 17:26:50 +02008152 if (cctx->ctx_skip != SKIP_YES)
8153 {
8154 // If we know the type of "var" and it is a not a supported type we can
8155 // give an error now.
8156 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8157 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02008158 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01008159 {
rbtnnbebf0692021-08-21 17:26:50 +02008160 semsg(_(e_for_loop_on_str_not_supported),
8161 vartype_name(vartype->tt_type));
Bram Moolenaar792f7862020-11-23 08:31:18 +01008162 drop_scope(cctx);
8163 return NULL;
8164 }
rbtnnbebf0692021-08-21 17:26:50 +02008165
8166 if (vartype->tt_type == VAR_STRING)
8167 item_type = &t_string;
8168 else if (vartype->tt_type == VAR_BLOB)
8169 item_type = &t_number;
8170 else if (vartype->tt_type == VAR_LIST
8171 && vartype->tt_member->tt_type != VAR_ANY)
8172 {
8173 if (!var_list)
8174 item_type = vartype->tt_member;
8175 else if (vartype->tt_member->tt_type == VAR_LIST
8176 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
rbtnnbebf0692021-08-21 17:26:50 +02008177 item_type = vartype->tt_member->tt_member;
8178 }
8179
8180 // CMDMOD_REV must come before the FOR instruction.
8181 generate_undo_cmdmods(cctx);
8182
8183 // "for_end" is set when ":endfor" is found
8184 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
8185
8186 generate_FOR(cctx, loop_lvar->lv_idx);
8187
8188 arg = arg_start;
8189 if (var_list)
8190 {
8191 generate_UNPACK(cctx, var_count, semicolon);
8192 arg = skipwhite(arg + 1); // skip white after '['
8193
8194 // the list item is replaced by a number of items
8195 if (GA_GROW_FAILS(stack, var_count - 1))
8196 {
8197 drop_scope(cctx);
8198 return NULL;
8199 }
8200 --stack->ga_len;
8201 for (idx = 0; idx < var_count; ++idx)
8202 {
8203 ((type_T **)stack->ga_data)[stack->ga_len] =
8204 (semicolon && idx == 0) ? vartype : item_type;
8205 ++stack->ga_len;
8206 }
8207 }
8208
Bram Moolenaar792f7862020-11-23 08:31:18 +01008209 for (idx = 0; idx < var_count; ++idx)
8210 {
rbtnnbebf0692021-08-21 17:26:50 +02008211 assign_dest_T dest = dest_local;
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008212 int opt_flags = 0;
8213 int vimvaridx = -1;
rbtnnbebf0692021-08-21 17:26:50 +02008214 type_T *type = &t_any;
8215 type_T *lhs_type = &t_any;
8216 where_T where = WHERE_INIT;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008217
rbtnnbebf0692021-08-21 17:26:50 +02008218 p = skip_var_one(arg, FALSE);
8219 varlen = p - arg;
8220 name = vim_strnsave(arg, varlen);
8221 if (name == NULL)
8222 goto failed;
8223 if (*p == ':')
8224 {
8225 p = skipwhite(p + 1);
8226 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
8227 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008228
Bram Moolenaarfad27422021-11-30 21:58:19 +00008229 // Script var is not supported.
rbtnnbebf0692021-08-21 17:26:50 +02008230 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008231 &vimvaridx, &type, cctx) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008232 goto failed;
8233 if (dest != dest_local)
8234 {
8235 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008236 0, 0, type, name) == FAIL)
rbtnnbebf0692021-08-21 17:26:50 +02008237 goto failed;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008238 }
rbtnnbebf0692021-08-21 17:26:50 +02008239 else if (varlen == 1 && *arg == '_')
Bram Moolenaarea870692020-12-02 14:24:30 +01008240 {
rbtnnbebf0692021-08-21 17:26:50 +02008241 // Assigning to "_": drop the value.
8242 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8243 goto failed;
Bram Moolenaarea870692020-12-02 14:24:30 +01008244 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008245 else
rbtnnbebf0692021-08-21 17:26:50 +02008246 {
Mike Williamscc9d7252021-11-23 12:35:57 +00008247 if (!valid_varname(arg, (int)varlen, FALSE))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00008248 goto failed;
rbtnnbebf0692021-08-21 17:26:50 +02008249 if (lookup_local(arg, varlen, NULL, cctx) == OK)
8250 {
8251 semsg(_(e_variable_already_declared), arg);
8252 goto failed;
8253 }
8254
8255 if (STRNCMP(name, "s:", 2) == 0)
8256 {
8257 semsg(_(e_cannot_declare_script_variable_in_function), name);
8258 goto failed;
8259 }
8260
8261 // Reserve a variable to store "var".
8262 where.wt_index = var_list ? idx + 1 : 0;
8263 where.wt_variable = TRUE;
8264 if (lhs_type == &t_any)
8265 lhs_type = item_type;
8266 else if (item_type != &t_unknown
8267 && (item_type == &t_any
8268 ? need_type(item_type, lhs_type,
8269 -1, 0, cctx, FALSE, FALSE)
8270 : check_type(lhs_type, item_type, TRUE, where))
8271 == FAIL)
8272 goto failed;
8273 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
8274 if (var_lvar == NULL)
8275 // out of memory or used as an argument
8276 goto failed;
8277
8278 if (semicolon && idx == var_count - 1)
8279 var_lvar->lv_type = vartype;
8280 else
8281 var_lvar->lv_type = item_type;
8282 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
8283 }
8284
8285 if (*p == ',' || *p == ';')
8286 ++p;
8287 arg = skipwhite(p);
8288 vim_free(name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008289 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01008290
rbtnnbebf0692021-08-21 17:26:50 +02008291 if (cctx->ctx_compile_type == CT_DEBUG)
8292 {
8293 int save_prev_lnum = cctx->ctx_prev_lnum;
Bram Moolenaar792f7862020-11-23 08:31:18 +01008294
rbtnnbebf0692021-08-21 17:26:50 +02008295 // Add ISN_DEBUG here, so that the loop variables can be inspected.
8296 // Use the prev_lnum from the ISN_DEBUG instruction removed above.
8297 cctx->ctx_prev_lnum = prev_lnum;
8298 generate_instr_debug(cctx);
8299 cctx->ctx_prev_lnum = save_prev_lnum;
8300 }
Bram Moolenaar6fc01612021-07-03 13:36:31 +02008301 }
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008302
Bram Moolenaar792f7862020-11-23 08:31:18 +01008303 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01008304
8305failed:
8306 vim_free(name);
8307 drop_scope(cctx);
8308 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008309}
8310
8311/*
8312 * compile "endfor"
8313 */
8314 static char_u *
8315compile_endfor(char_u *arg, cctx_T *cctx)
8316{
8317 garray_T *instr = &cctx->ctx_instr;
8318 scope_T *scope = cctx->ctx_scope;
8319 forscope_T *forscope;
8320 isn_T *isn;
8321
Bram Moolenaarfa984412021-03-25 22:15:28 +01008322 if (misplaced_cmdmod(cctx))
8323 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01008324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008325 if (scope == NULL || scope->se_type != FOR_SCOPE)
8326 {
8327 emsg(_(e_for));
8328 return NULL;
8329 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008330 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331 cctx->ctx_scope = scope->se_outer;
rbtnnbebf0692021-08-21 17:26:50 +02008332 if (cctx->ctx_skip != SKIP_YES)
8333 {
8334 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008335
rbtnnbebf0692021-08-21 17:26:50 +02008336 // At end of ":for" scope jump back to the FOR instruction.
8337 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008338
rbtnnbebf0692021-08-21 17:26:50 +02008339 // Fill in the "end" label in the FOR statement so it can jump here.
8340 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
8341 isn->isn_arg.forloop.for_end = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008342
rbtnnbebf0692021-08-21 17:26:50 +02008343 // Fill in the "end" label any BREAK statements
8344 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345
rbtnnbebf0692021-08-21 17:26:50 +02008346 // Below the ":for" scope drop the "expr" list from the stack.
8347 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
8348 return NULL;
8349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008350
8351 vim_free(scope);
8352
8353 return arg;
8354}
8355
8356/*
8357 * compile "while expr"
8358 *
8359 * Produces instructions:
8360 * top: EVAL expr Push result of "expr"
8361 * JUMP_IF_FALSE end jump if false
8362 * ... body ...
8363 * JUMP top Jump back to repeat
8364 * end:
8365 *
8366 */
8367 static char_u *
8368compile_while(char_u *arg, cctx_T *cctx)
8369{
8370 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008371 scope_T *scope;
8372
8373 scope = new_scope(cctx, WHILE_SCOPE);
8374 if (scope == NULL)
8375 return NULL;
8376
Bram Moolenaara91a7132021-03-25 21:12:15 +01008377 // "endwhile" jumps back here, one before when profiling or using cmdmods
8378 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379
8380 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02008381 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 return NULL;
rbtnnd895b1d2021-08-20 20:54:25 +02008383
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01008384 if (!ends_excmd2(arg, skipwhite(p)))
8385 {
8386 semsg(_(e_trailing_arg), p);
8387 return NULL;
8388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008389
rbtnnd895b1d2021-08-20 20:54:25 +02008390 if (cctx->ctx_skip != SKIP_YES)
8391 {
8392 if (bool_on_stack(cctx) == FAIL)
8393 return FAIL;
Bram Moolenaar13106602020-10-04 16:06:05 +02008394
rbtnnd895b1d2021-08-20 20:54:25 +02008395 // CMDMOD_REV must come before the jump
8396 generate_undo_cmdmods(cctx);
Bram Moolenaara91a7132021-03-25 21:12:15 +01008397
rbtnnd895b1d2021-08-20 20:54:25 +02008398 // "while_end" is set when ":endwhile" is found
8399 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400 JUMP_IF_FALSE, cctx) == FAIL)
rbtnnd895b1d2021-08-20 20:54:25 +02008401 return FAIL;
8402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008403
8404 return p;
8405}
8406
8407/*
8408 * compile "endwhile"
8409 */
8410 static char_u *
8411compile_endwhile(char_u *arg, cctx_T *cctx)
8412{
8413 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008414 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415
Bram Moolenaarfa984412021-03-25 22:15:28 +01008416 if (misplaced_cmdmod(cctx))
8417 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418 if (scope == NULL || scope->se_type != WHILE_SCOPE)
8419 {
8420 emsg(_(e_while));
8421 return NULL;
8422 }
8423 cctx->ctx_scope = scope->se_outer;
rbtnnd895b1d2021-08-20 20:54:25 +02008424 if (cctx->ctx_skip != SKIP_YES)
8425 {
8426 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008427
Bram Moolenaarf002a412021-01-24 13:34:18 +01008428#ifdef FEAT_PROFILE
rbtnnd895b1d2021-08-20 20:54:25 +02008429 // count the endwhile before jumping
8430 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008431#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008432
rbtnnd895b1d2021-08-20 20:54:25 +02008433 // At end of ":for" scope jump back to the FOR instruction.
8434 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008435
rbtnnd895b1d2021-08-20 20:54:25 +02008436 // Fill in the "end" label in the WHILE statement so it can jump here.
8437 // And in any jumps for ":break"
8438 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008439 instr->ga_len, cctx);
rbtnnd895b1d2021-08-20 20:54:25 +02008440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441
8442 vim_free(scope);
8443
8444 return arg;
8445}
8446
8447/*
8448 * compile "continue"
8449 */
8450 static char_u *
8451compile_continue(char_u *arg, cctx_T *cctx)
8452{
8453 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008454 int try_scopes = 0;
8455 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456
8457 for (;;)
8458 {
8459 if (scope == NULL)
8460 {
8461 emsg(_(e_continue));
8462 return NULL;
8463 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01008464 if (scope->se_type == FOR_SCOPE)
8465 {
8466 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008468 }
8469 if (scope->se_type == WHILE_SCOPE)
8470 {
8471 loop_label = scope->se_u.se_while.ws_top_label;
8472 break;
8473 }
8474 if (scope->se_type == TRY_SCOPE)
8475 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008476 scope = scope->se_outer;
8477 }
8478
Bram Moolenaarc150c092021-02-13 15:02:46 +01008479 if (try_scopes > 0)
8480 // Inside one or more try/catch blocks we first need to jump to the
8481 // "finally" or "endtry" to cleanup.
8482 generate_TRYCONT(cctx, try_scopes, loop_label);
8483 else
8484 // Jump back to the FOR or WHILE instruction.
8485 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 return arg;
8488}
8489
8490/*
8491 * compile "break"
8492 */
8493 static char_u *
8494compile_break(char_u *arg, cctx_T *cctx)
8495{
8496 scope_T *scope = cctx->ctx_scope;
8497 endlabel_T **el;
8498
8499 for (;;)
8500 {
8501 if (scope == NULL)
8502 {
8503 emsg(_(e_break));
8504 return NULL;
8505 }
8506 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8507 break;
8508 scope = scope->se_outer;
8509 }
8510
8511 // Jump to the end of the FOR or WHILE loop.
8512 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008513 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008514 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008515 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008516 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8517 return FAIL;
8518
8519 return arg;
8520}
8521
8522/*
8523 * compile "{" start of block
8524 */
8525 static char_u *
8526compile_block(char_u *arg, cctx_T *cctx)
8527{
8528 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8529 return NULL;
8530 return skipwhite(arg + 1);
8531}
8532
8533/*
8534 * compile end of block: drop one scope
8535 */
8536 static void
8537compile_endblock(cctx_T *cctx)
8538{
8539 scope_T *scope = cctx->ctx_scope;
8540
8541 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008542 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 vim_free(scope);
8544}
8545
8546/*
8547 * compile "try"
8548 * Creates a new scope for the try-endtry, pointing to the first catch and
8549 * finally.
8550 * Creates another scope for the "try" block itself.
8551 * TRY instruction sets up exception handling at runtime.
8552 *
8553 * "try"
8554 * TRY -> catch1, -> finally push trystack entry
8555 * ... try block
8556 * "throw {exception}"
8557 * EVAL {exception}
8558 * THROW create exception
8559 * ... try block
8560 * " catch {expr}"
8561 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008562 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 * EVAL {expr}
8564 * MATCH
8565 * JUMP nomatch -> catch2
8566 * CATCH remove exception
8567 * ... catch block
8568 * " catch"
8569 * JUMP -> finally
8570 * catch2: CATCH remove exception
8571 * ... catch block
8572 * " finally"
8573 * finally:
8574 * ... finally block
8575 * " endtry"
8576 * ENDTRY pop trystack entry, may rethrow
8577 */
8578 static char_u *
8579compile_try(char_u *arg, cctx_T *cctx)
8580{
8581 garray_T *instr = &cctx->ctx_instr;
8582 scope_T *try_scope;
8583 scope_T *scope;
8584
Bram Moolenaarfa984412021-03-25 22:15:28 +01008585 if (misplaced_cmdmod(cctx))
8586 return NULL;
8587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008588 // scope that holds the jumps that go to catch/finally/endtry
8589 try_scope = new_scope(cctx, TRY_SCOPE);
8590 if (try_scope == NULL)
8591 return NULL;
8592
Bram Moolenaar69f70502021-01-01 16:10:46 +01008593 if (cctx->ctx_skip != SKIP_YES)
8594 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008595 isn_T *isn;
8596
8597 // "try_catch" is set when the first ":catch" is found or when no catch
8598 // is found and ":finally" is found.
8599 // "try_finally" is set when ":finally" is found
8600 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01008601 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008602 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8603 return NULL;
8604 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8605 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008606 return NULL;
8607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008608
8609 // scope for the try block itself
8610 scope = new_scope(cctx, BLOCK_SCOPE);
8611 if (scope == NULL)
8612 return NULL;
8613
8614 return arg;
8615}
8616
8617/*
8618 * compile "catch {expr}"
8619 */
8620 static char_u *
8621compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8622{
8623 scope_T *scope = cctx->ctx_scope;
8624 garray_T *instr = &cctx->ctx_instr;
8625 char_u *p;
8626 isn_T *isn;
8627
Bram Moolenaarfa984412021-03-25 22:15:28 +01008628 if (misplaced_cmdmod(cctx))
8629 return NULL;
8630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008631 // end block scope from :try or :catch
8632 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8633 compile_endblock(cctx);
8634 scope = cctx->ctx_scope;
8635
8636 // Error if not in a :try scope
8637 if (scope == NULL || scope->se_type != TRY_SCOPE)
8638 {
8639 emsg(_(e_catch));
8640 return NULL;
8641 }
8642
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008643 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008644 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008645 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008646 return NULL;
8647 }
8648
Bram Moolenaar69f70502021-01-01 16:10:46 +01008649 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008651#ifdef FEAT_PROFILE
8652 // the profile-start should be after the jump
Bram Moolenaare99d4222021-06-13 14:01:26 +02008653 if (cctx->ctx_compile_type == CT_PROFILE
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02008654 && instr->ga_len > 0
Bram Moolenaare99d4222021-06-13 14:01:26 +02008655 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008656 .isn_type == ISN_PROF_START)
8657 --instr->ga_len;
8658#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008659 // Jump from end of previous block to :finally or :endtry
8660 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8661 JUMP_ALWAYS, cctx) == FAIL)
8662 return NULL;
8663
8664 // End :try or :catch scope: set value in ISN_TRY instruction
8665 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008666 if (isn->isn_arg.try.try_ref->try_catch == 0)
8667 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008668 if (scope->se_u.se_try.ts_catch_label != 0)
8669 {
8670 // Previous catch without match jumps here
8671 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8672 isn->isn_arg.jump.jump_where = instr->ga_len;
8673 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008674#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008675 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008676 {
8677 // a "throw" that jumps here needs to be counted
8678 generate_instr(cctx, ISN_PROF_END);
8679 // the "catch" is also counted
8680 generate_instr(cctx, ISN_PROF_START);
8681 }
8682#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008683 if (cctx->ctx_compile_type == CT_DEBUG)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02008684 generate_instr_debug(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008685 }
8686
8687 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008688 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008689 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008690 scope->se_u.se_try.ts_caught_all = TRUE;
8691 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692 }
8693 else
8694 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008695 char_u *end;
8696 char_u *pat;
8697 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008698 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008699 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008701 // Push v:exception, push {expr} and MATCH
8702 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8703
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008704 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008705 if (*end != *p)
8706 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008707 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008708 vim_free(tofree);
8709 return FAIL;
8710 }
8711 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008712 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008713 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008714 len = (int)(end - tofree);
8715 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008716 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008717 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008718 if (pat == NULL)
8719 return FAIL;
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02008720 if (generate_PUSHS(cctx, &pat) == FAIL)
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008721 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008723 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8724 return NULL;
8725
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008726 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008727 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8728 return NULL;
8729 }
8730
Bram Moolenaar69f70502021-01-01 16:10:46 +01008731 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008732 return NULL;
8733
8734 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8735 return NULL;
8736 return p;
8737}
8738
8739 static char_u *
8740compile_finally(char_u *arg, cctx_T *cctx)
8741{
8742 scope_T *scope = cctx->ctx_scope;
8743 garray_T *instr = &cctx->ctx_instr;
8744 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008745 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008746
Bram Moolenaarfa984412021-03-25 22:15:28 +01008747 if (misplaced_cmdmod(cctx))
8748 return NULL;
8749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750 // end block scope from :try or :catch
8751 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8752 compile_endblock(cctx);
8753 scope = cctx->ctx_scope;
8754
8755 // Error if not in a :try scope
8756 if (scope == NULL || scope->se_type != TRY_SCOPE)
8757 {
8758 emsg(_(e_finally));
8759 return NULL;
8760 }
8761
rbtnn84934992021-08-07 13:26:53 +02008762 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008763 {
rbtnn84934992021-08-07 13:26:53 +02008764 // End :catch or :finally scope: set value in ISN_TRY instruction
8765 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8766 if (isn->isn_arg.try.try_ref->try_finally != 0)
8767 {
8768 emsg(_(e_finally_dup));
8769 return NULL;
8770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008771
rbtnn84934992021-08-07 13:26:53 +02008772 this_instr = instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008773#ifdef FEAT_PROFILE
rbtnn84934992021-08-07 13:26:53 +02008774 if (cctx->ctx_compile_type == CT_PROFILE
8775 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008776 .isn_type == ISN_PROF_START)
rbtnn84934992021-08-07 13:26:53 +02008777 {
8778 // jump to the profile start of the "finally"
Bram Moolenaar834193a2021-06-30 20:39:15 +02008779 --this_instr;
rbtnn84934992021-08-07 13:26:53 +02008780
8781 // jump to the profile end above it
8782 if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
Bram Moolenaarfad27422021-11-30 21:58:19 +00008783 .isn_type == ISN_PROF_END)
rbtnn84934992021-08-07 13:26:53 +02008784 --this_instr;
8785 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008786#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787
rbtnn84934992021-08-07 13:26:53 +02008788 // Fill in the "end" label in jumps at the end of the blocks.
8789 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaarfad27422021-11-30 21:58:19 +00008790 this_instr, cctx);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008791
rbtnn84934992021-08-07 13:26:53 +02008792 // If there is no :catch then an exception jumps to :finally.
8793 if (isn->isn_arg.try.try_ref->try_catch == 0)
8794 isn->isn_arg.try.try_ref->try_catch = this_instr;
8795 isn->isn_arg.try.try_ref->try_finally = this_instr;
8796 if (scope->se_u.se_try.ts_catch_label != 0)
8797 {
8798 // Previous catch without match jumps here
8799 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8800 isn->isn_arg.jump.jump_where = this_instr;
8801 scope->se_u.se_try.ts_catch_label = 0;
8802 }
8803 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8804 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008806
8807 return arg;
8808}
8809
8810 static char_u *
8811compile_endtry(char_u *arg, cctx_T *cctx)
8812{
8813 scope_T *scope = cctx->ctx_scope;
8814 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008815 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008816
Bram Moolenaarfa984412021-03-25 22:15:28 +01008817 if (misplaced_cmdmod(cctx))
8818 return NULL;
8819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008820 // end block scope from :catch or :finally
8821 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8822 compile_endblock(cctx);
8823 scope = cctx->ctx_scope;
8824
8825 // Error if not in a :try scope
8826 if (scope == NULL || scope->se_type != TRY_SCOPE)
8827 {
8828 if (scope == NULL)
8829 emsg(_(e_no_endtry));
8830 else if (scope->se_type == WHILE_SCOPE)
8831 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008832 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008833 emsg(_(e_endfor));
8834 else
8835 emsg(_(e_endif));
8836 return NULL;
8837 }
8838
Bram Moolenaarc150c092021-02-13 15:02:46 +01008839 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008840 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008841 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008842 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8843 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008844 {
8845 emsg(_(e_missing_catch_or_finally));
8846 return NULL;
8847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008848
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008849#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008850 if (cctx->ctx_compile_type == CT_PROFILE
8851 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
Dominique Pelle4781d6f2021-05-18 21:46:31 +02008852 .isn_type == ISN_PROF_START)
8853 // move the profile start after "endtry" so that it's not counted when
8854 // the exception is rethrown.
8855 --instr->ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008856#endif
8857
Bram Moolenaar69f70502021-01-01 16:10:46 +01008858 // Fill in the "end" label in jumps at the end of the blocks, if not
8859 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008860 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8861 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008862
Bram Moolenaar69f70502021-01-01 16:10:46 +01008863 if (scope->se_u.se_try.ts_catch_label != 0)
8864 {
8865 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008866 isn_T *isn = ((isn_T *)instr->ga_data)
8867 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008868 isn->isn_arg.jump.jump_where = instr->ga_len;
8869 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008870 }
8871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008872 compile_endblock(cctx);
8873
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008874 if (cctx->ctx_skip != SKIP_YES)
8875 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008876 // End :catch or :finally scope: set instruction index in ISN_TRY
8877 // instruction
8878 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008879 if (cctx->ctx_skip != SKIP_YES
8880 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8881 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008882#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008883 if (cctx->ctx_compile_type == CT_PROFILE)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008884 generate_instr(cctx, ISN_PROF_START);
8885#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008887 return arg;
8888}
8889
8890/*
8891 * compile "throw {expr}"
8892 */
8893 static char_u *
8894compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8895{
8896 char_u *p = skipwhite(arg);
8897
Bram Moolenaara5565e42020-05-09 15:44:01 +02008898 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008900 if (cctx->ctx_skip == SKIP_YES)
8901 return p;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02008902 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008903 return NULL;
8904 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8905 return NULL;
8906
8907 return p;
8908}
8909
Bram Moolenaarc3235272021-07-10 19:42:03 +02008910 static char_u *
8911compile_eval(char_u *arg, cctx_T *cctx)
8912{
8913 char_u *p = arg;
8914 int name_only;
Bram Moolenaarc3235272021-07-10 19:42:03 +02008915 long lnum = SOURCING_LNUM;
8916
8917 // find_ex_command() will consider a variable name an expression, assuming
8918 // that something follows on the next line. Check that something actually
8919 // follows, otherwise it's probably a misplaced command.
Bram Moolenaar4799cef2021-08-25 22:37:36 +02008920 name_only = cmd_is_name_only(arg);
Bram Moolenaarc3235272021-07-10 19:42:03 +02008921
Bram Moolenaarc3235272021-07-10 19:42:03 +02008922 if (compile_expr0(&p, cctx) == FAIL)
8923 return NULL;
8924
8925 if (name_only && lnum == SOURCING_LNUM)
8926 {
8927 semsg(_(e_expression_without_effect_str), arg);
8928 return NULL;
8929 }
8930
8931 // drop the result
8932 generate_instr_drop(cctx, ISN_DROP, 1);
8933
8934 return skipwhite(p);
8935}
8936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008937/*
8938 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008939 * compile "echomsg expr"
8940 * compile "echoerr expr"
Bram Moolenaar7de62622021-08-07 15:05:47 +02008941 * compile "echoconsole expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008942 * compile "execute expr"
8943 */
8944 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008945compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008946{
8947 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008948 char_u *prev = arg;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008949 char_u *expr_start;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008950 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008951 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008952 garray_T *stack = &cctx->ctx_type_stack;
8953 type_T *type;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008954
8955 for (;;)
8956 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008957 if (ends_excmd2(prev, p))
8958 break;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008959 expr_start = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008960 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008961 return NULL;
Bram Moolenaar68db9962021-05-09 23:19:22 +02008962
8963 if (cctx->ctx_skip != SKIP_YES)
8964 {
8965 // check for non-void type
8966 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8967 if (type->tt_type == VAR_VOID)
8968 {
8969 semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8970 return NULL;
8971 }
8972 }
8973
Bram Moolenaarad39c092020-02-26 18:23:43 +01008974 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008975 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008976 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008977 }
8978
Bram Moolenaare4984292020-12-13 14:19:25 +01008979 if (count > 0)
8980 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008981 long save_lnum = cctx->ctx_lnum;
8982
8983 // Use the line number where the command started.
8984 cctx->ctx_lnum = start_ctx_lnum;
8985
Bram Moolenaare4984292020-12-13 14:19:25 +01008986 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8987 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8988 else if (cmdidx == CMD_execute)
8989 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8990 else if (cmdidx == CMD_echomsg)
8991 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
Bram Moolenaar7de62622021-08-07 15:05:47 +02008992 else if (cmdidx == CMD_echoconsole)
8993 generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
Bram Moolenaare4984292020-12-13 14:19:25 +01008994 else
8995 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008996
8997 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008999 return p;
9000}
9001
9002/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01009003 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01009004 * instruction to compute it and return OK.
9005 * Otherwise return FAIL, the caller must deal with any range.
9006 */
9007 static int
9008compile_variable_range(exarg_T *eap, cctx_T *cctx)
9009{
9010 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
9011 char_u *p = skipdigits(eap->cmd);
9012
9013 if (p == range_end)
9014 return FAIL;
9015 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
9016}
9017
9018/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009019 * :put r
9020 * :put ={expr}
9021 */
9022 static char_u *
9023compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
9024{
9025 char_u *line = arg;
9026 linenr_T lnum;
9027 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009028 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009029
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009030 eap->regname = *line;
9031
9032 if (eap->regname == '=')
9033 {
9034 char_u *p = line + 1;
9035
9036 if (compile_expr0(&p, cctx) == FAIL)
9037 return NULL;
9038 line = p;
9039 }
9040 else if (eap->regname != NUL)
9041 ++line;
9042
Bram Moolenaar08597872020-12-10 19:43:40 +01009043 if (compile_variable_range(eap, cctx) == OK)
9044 {
9045 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
9046 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009047 else
Bram Moolenaar08597872020-12-10 19:43:40 +01009048 {
9049 // Either no range or a number.
9050 // "errormsg" will not be set because the range is ADDR_LINES.
9051 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01009052 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01009053 return NULL;
9054 if (eap->addr_count == 0)
9055 lnum = -1;
9056 else
9057 lnum = eap->line2;
9058 if (above)
9059 --lnum;
9060 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009061
9062 generate_PUT(cctx, eap->regname, lnum);
9063 return line;
9064}
9065
9066/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009067 * A command that is not compiled, execute with legacy code.
9068 */
9069 static char_u *
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009070compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009071{
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009072 char_u *line = line_arg;
Bram Moolenaare729ce22021-06-06 21:38:09 +02009073 char_u *p;
9074 int has_expr = FALSE;
9075 char_u *nextcmd = (char_u *)"";
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009076 char_u *tofree = NULL;
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009077 char_u *cmd_arg = NULL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009078
Bram Moolenaar9b68c822020-06-18 19:31:08 +02009079 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009080 goto theend;
9081
Bram Moolenaare729ce22021-06-06 21:38:09 +02009082 // If there was a prececing command modifier, drop it and include it in the
9083 // EXEC command.
9084 if (cctx->ctx_has_cmdmod)
9085 {
9086 garray_T *instr = &cctx->ctx_instr;
9087 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
9088
9089 if (isn->isn_type == ISN_CMDMOD)
9090 {
9091 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9092 ->cmod_filter_regmatch.regprog);
9093 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9094 --instr->ga_len;
9095 cctx->ctx_has_cmdmod = FALSE;
9096 }
9097 }
9098
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02009099 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009100 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009101 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009102 int usefilter = FALSE;
9103
9104 has_expr = argt & (EX_XFILE | EX_EXPAND);
9105
9106 // If the command can be followed by a bar, find the bar and truncate
9107 // it, so that the following command can be compiled.
9108 // The '|' is overwritten with a NUL, it is put back below.
9109 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
9110 && *eap->arg == '!')
9111 // :w !filter or :r !filter or :r! filter
9112 usefilter = TRUE;
9113 if ((argt & EX_TRLBAR) && !usefilter)
9114 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02009115 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009116 separate_nextcmd(eap);
9117 if (eap->nextcmd != NULL)
9118 nextcmd = eap->nextcmd;
9119 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01009120 else if (eap->cmdidx == CMD_wincmd)
9121 {
9122 p = eap->arg;
9123 if (*p != NUL)
9124 ++p;
9125 if (*p == 'g' || *p == Ctrl_G)
9126 ++p;
9127 p = skipwhite(p);
9128 if (*p == '|')
9129 {
9130 *p = NUL;
9131 nextcmd = p + 1;
9132 }
9133 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009134 else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
9135 {
9136 // If there is a trailing '{' read lines until the '}'
9137 p = eap->arg + STRLEN(eap->arg) - 1;
9138 while (p > eap->arg && VIM_ISWHITE(*p))
9139 --p;
9140 if (*p == '{')
9141 {
9142 exarg_T ea;
9143 int flags; // unused
9144 int start_lnum = SOURCING_LNUM;
9145
9146 CLEAR_FIELD(ea);
9147 ea.arg = eap->arg;
9148 fill_exarg_from_cctx(&ea, cctx);
9149 (void)may_get_cmd_block(&ea, p, &tofree, &flags);
9150 if (tofree != NULL)
9151 {
9152 *p = NUL;
9153 line = concat_str(line, tofree);
9154 if (line == NULL)
9155 goto theend;
9156 vim_free(tofree);
9157 tofree = line;
9158 SOURCING_LNUM = start_lnum;
9159 }
9160 }
9161 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009162 }
9163
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009164 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
9165 {
9166 // expand filename in "syntax include [@group] filename"
9167 has_expr = TRUE;
9168 eap->arg = skipwhite(eap->arg + 7);
9169 if (*eap->arg == '@')
9170 eap->arg = skiptowhite(eap->arg);
9171 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009172
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009173 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
9174 && STRLEN(eap->arg) > 4)
9175 {
9176 int delim = *eap->arg;
9177
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01009178 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009179 if (*p == delim)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009180 cmd_arg = p + 1;
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01009181 }
9182
Bram Moolenaarecac5912021-01-05 19:23:28 +01009183 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009184 cmd_arg = eap->arg;
9185
9186 if (cmd_arg != NULL)
Bram Moolenaarecac5912021-01-05 19:23:28 +01009187 {
Bram Moolenaarfad27422021-11-30 21:58:19 +00009188 exarg_T nea;
9189
9190 CLEAR_FIELD(nea);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009191 nea.cmd = cmd_arg;
Bram Moolenaarfad27422021-11-30 21:58:19 +00009192 p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar3d2e0312021-12-01 09:27:20 +00009193 if (nea.cmdidx < CMD_SIZE)
Bram Moolenaarfad27422021-11-30 21:58:19 +00009194 {
9195 has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
9196 if (has_expr)
9197 eap->arg = skiptowhite(eap->arg);
9198 }
Bram Moolenaarecac5912021-01-05 19:23:28 +01009199 }
9200
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02009201 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009202 {
9203 int count = 0;
9204 char_u *start = skipwhite(line);
9205
9206 // :cmd xxx`=expr1`yyy`=expr2`zzz
9207 // PUSHS ":cmd xxx"
9208 // eval expr1
9209 // PUSHS "yyy"
9210 // eval expr2
9211 // PUSHS "zzz"
9212 // EXECCONCAT 5
9213 for (;;)
9214 {
9215 if (p > start)
9216 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009217 char_u *val = vim_strnsave(start, p - start);
9218
9219 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009220 ++count;
9221 }
9222 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02009223 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009224 return NULL;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02009225 may_generate_2STRING(-1, TRUE, cctx);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009226 ++count;
9227 p = skipwhite(p);
9228 if (*p != '`')
9229 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009230 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009231 return NULL;
9232 }
9233 start = p + 1;
9234
9235 p = (char_u *)strstr((char *)start, "`=");
9236 if (p == NULL)
9237 {
9238 if (*skipwhite(start) != NUL)
9239 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02009240 char_u *val = vim_strsave(start);
9241
9242 generate_PUSHS(cctx, &val);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009243 ++count;
9244 }
9245 break;
9246 }
9247 }
9248 generate_EXECCONCAT(cctx, count);
9249 }
9250 else
Bram Moolenaaraacc9662021-08-13 19:40:51 +02009251 generate_EXEC(cctx, ISN_EXEC, line);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009252
9253theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009254 if (*nextcmd != NUL)
9255 {
9256 // the parser expects a pointer to the bar, put it back
9257 --nextcmd;
9258 *nextcmd = '|';
9259 }
Bram Moolenaare4db17f2021-08-01 21:19:43 +02009260 vim_free(tofree);
Bram Moolenaare9f262b2020-07-05 14:57:51 +02009261
9262 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009263}
9264
Bram Moolenaar20677332021-06-06 17:02:53 +02009265/*
9266 * A script command with heredoc, e.g.
9267 * ruby << EOF
9268 * command
9269 * EOF
9270 * Has been turned into one long line with NL characters by
9271 * get_function_body():
9272 * ruby << EOF<NL> command<NL>EOF
9273 */
9274 static char_u *
9275compile_script(char_u *line, cctx_T *cctx)
9276{
9277 if (cctx->ctx_skip != SKIP_YES)
9278 {
9279 isn_T *isn;
9280
9281 if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
9282 return NULL;
9283 isn->isn_arg.string = vim_strsave(line);
9284 }
9285 return (char_u *)"";
9286}
9287
Bram Moolenaar8238f082021-04-20 21:10:48 +02009288
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009289/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02009290 * :s/pat/repl/
9291 */
9292 static char_u *
9293compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
9294{
9295 char_u *cmd = eap->arg;
9296 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
9297
9298 if (expr != NULL)
9299 {
9300 int delimiter = *cmd++;
9301
9302 // There is a \=expr, find it in the substitute part.
Bram Moolenaar8238f082021-04-20 21:10:48 +02009303 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009304 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
9305 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009306 garray_T save_ga = cctx->ctx_instr;
9307 char_u *end;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009308 int expr_res;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009309 int trailing_error;
9310 int instr_count;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009311 isn_T *instr;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009312 isn_T *isn;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009313
9314 cmd += 3;
9315 end = skip_substitute(cmd, delimiter);
9316
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009317 // Temporarily reset the list of instructions so that the jump
Bram Moolenaar8238f082021-04-20 21:10:48 +02009318 // labels are correct.
9319 cctx->ctx_instr.ga_len = 0;
9320 cctx->ctx_instr.ga_maxlen = 0;
9321 cctx->ctx_instr.ga_data = NULL;
Bram Moolenaar169502f2021-04-21 12:19:35 +02009322 expr_res = compile_expr0(&cmd, cctx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009323 if (end[-1] == NUL)
9324 end[-1] = delimiter;
9325 cmd = skipwhite(cmd);
Bram Moolenaar8238f082021-04-20 21:10:48 +02009326 trailing_error = *cmd != delimiter && *cmd != NUL;
9327
Bram Moolenaar169502f2021-04-21 12:19:35 +02009328 if (expr_res == FAIL || trailing_error
Bram Moolenaar35578162021-08-02 19:10:38 +02009329 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
Bram Moolenaar4c137212021-04-19 16:48:48 +02009330 {
Bram Moolenaar8238f082021-04-20 21:10:48 +02009331 if (trailing_error)
9332 semsg(_(e_trailing_arg), cmd);
9333 clear_instr_ga(&cctx->ctx_instr);
9334 cctx->ctx_instr = save_ga;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009335 return NULL;
9336 }
9337
Bram Moolenaar8238f082021-04-20 21:10:48 +02009338 // Move the generated instructions into the ISN_SUBSTITUTE
9339 // instructions, then restore the list of instructions before
9340 // adding the ISN_SUBSTITUTE instruction.
Bram Moolenaar5c787fb2021-04-20 21:49:35 +02009341 instr_count = cctx->ctx_instr.ga_len;
9342 instr = cctx->ctx_instr.ga_data;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009343 instr[instr_count].isn_type = ISN_FINISH;
9344
9345 cctx->ctx_instr = save_ga;
9346 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
9347 {
9348 int idx;
9349
9350 for (idx = 0; idx < instr_count; ++idx)
9351 delete_instr(instr + idx);
9352 vim_free(instr);
Bram Moolenaar4c137212021-04-19 16:48:48 +02009353 return NULL;
Bram Moolenaar8238f082021-04-20 21:10:48 +02009354 }
9355 isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
9356 isn->isn_arg.subs.subs_instr = instr;
Bram Moolenaar4c137212021-04-19 16:48:48 +02009357
9358 // skip over flags
9359 if (*end == '&')
9360 ++end;
9361 while (ASCII_ISALPHA(*end) || *end == '#')
9362 ++end;
9363 return end;
9364 }
9365 }
9366
9367 return compile_exec(arg, eap, cctx);
9368}
9369
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009370 static char_u *
9371compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
9372{
9373 char_u *arg = eap->arg;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009374 lhs_T *lhs = &cctx->ctx_redir_lhs;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009375
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009376 if (lhs->lhs_name != NULL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009377 {
9378 if (STRNCMP(arg, "END", 3) == 0)
9379 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009380 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009381 {
Bram Moolenaara369c3d2021-04-21 16:00:10 +02009382 // First load the current variable value.
9383 if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
9384 cctx) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009385 return NULL;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009386 }
9387
9388 // Gets the redirected text and put it on the stack, then store it
9389 // in the variable.
9390 generate_instr_type(cctx, ISN_REDIREND, &t_string);
9391
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009392 if (lhs->lhs_append)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009393 generate_instr_drop(cctx, ISN_CONCAT, 1);
9394
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009395 if (lhs->lhs_has_index)
9396 {
9397 // Use the info in "lhs" to store the value at the index in the
9398 // list or dict.
9399 if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
9400 &t_string, cctx) == FAIL)
9401 return NULL;
9402 }
9403 else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009404 return NULL;
9405
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009406 VIM_CLEAR(lhs->lhs_name);
9407 VIM_CLEAR(lhs->lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009408 return arg + 3;
9409 }
9410 emsg(_(e_cannot_nest_redir));
9411 return NULL;
9412 }
9413
9414 if (arg[0] == '=' && arg[1] == '>')
9415 {
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009416 int append = FALSE;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009417
9418 // redirect to a variable is compiled
9419 arg += 2;
9420 if (*arg == '>')
9421 {
9422 ++arg;
9423 append = TRUE;
9424 }
9425 arg = skipwhite(arg);
9426
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009427 if (compile_assign_lhs(arg, lhs, CMD_redir,
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009428 FALSE, FALSE, 1, cctx) == FAIL)
9429 return NULL;
9430 generate_instr(cctx, ISN_REDIRSTART);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009431 lhs->lhs_append = append;
9432 if (lhs->lhs_has_index)
9433 {
9434 lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
9435 if (lhs->lhs_whole == NULL)
9436 return NULL;
9437 }
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009438
Bram Moolenaar753bcf82021-04-21 14:24:24 +02009439 return arg + lhs->lhs_varlen_total;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02009440 }
9441
9442 // other redirects are handled like at script level
9443 return compile_exec(line, eap, cctx);
9444}
9445
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009446#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009447 static char_u *
9448compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
9449{
9450 isn_T *isn;
9451 char_u *p;
9452
9453 isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
9454 if (isn == NULL)
9455 return NULL;
9456 isn->isn_arg.number = eap->cmdidx;
9457
9458 p = eap->arg;
9459 if (compile_expr0(&p, cctx) == FAIL)
9460 return NULL;
9461
9462 isn = generate_instr(cctx, ISN_CEXPR_CORE);
9463 if (isn == NULL)
9464 return NULL;
9465 isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9466 if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9467 return NULL;
9468 isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9469 isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9470 isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9471
9472 return p;
9473}
Bram Moolenaarb7c97812021-05-05 22:51:39 +02009474#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02009475
Bram Moolenaar4c137212021-04-19 16:48:48 +02009476/*
Bram Moolenaar7b829262021-10-13 15:04:34 +01009477 * Check if the separator for a :global or :substitute command is OK.
9478 */
9479 int
9480check_global_and_subst(char_u *cmd, char_u *arg)
9481{
Bram Moolenaar7f320922021-10-13 15:28:28 +01009482 if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
Bram Moolenaar7b829262021-10-13 15:04:34 +01009483 {
9484 semsg(_(e_separator_not_supported_str), arg);
9485 return FAIL;
9486 }
9487 if (VIM_ISWHITE(cmd[1]))
9488 {
9489 semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
9490 return FAIL;
9491 }
9492 return OK;
9493}
9494
9495
9496/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02009497 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009498 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02009499 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009500 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02009501add_def_function(ufunc_T *ufunc)
9502{
9503 dfunc_T *dfunc;
9504
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009505 if (def_functions.ga_len == 0)
9506 {
9507 // The first position is not used, so that a zero uf_dfunc_idx means it
9508 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02009509 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009510 return FAIL;
9511 ++def_functions.ga_len;
9512 }
9513
Bram Moolenaar09689a02020-05-09 22:50:08 +02009514 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02009515 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02009516 return FAIL;
9517 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9518 CLEAR_POINTER(dfunc);
9519 dfunc->df_idx = def_functions.ga_len;
9520 ufunc->uf_dfunc_idx = dfunc->df_idx;
9521 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009522 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009523 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009524 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02009525 ++def_functions.ga_len;
9526 return OK;
9527}
9528
9529/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009530 * After ex_function() has collected all the function lines: parse and compile
9531 * the lines into instructions.
9532 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01009533 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9534 * the return statement (used for lambda). When uf_ret_type is already set
9535 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01009536 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009537 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009538 * This can be used recursively through compile_lambda(), which may reallocate
9539 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02009540 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009541 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02009542 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01009543compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02009544 ufunc_T *ufunc,
9545 int check_return_type,
9546 compiletype_T compile_type,
9547 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009548{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009549 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009550 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009551 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009552 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009553 cctx_T cctx;
9554 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01009555 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02009556 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009557 int ret = FAIL;
9558 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009559 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009560 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009561 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009562 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009563#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009564 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009565#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009566 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009567
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009568 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009569 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009570 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009571 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02009572 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9573 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02009574 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009575
9576 switch (compile_type)
9577 {
9578 case CT_PROFILE:
9579#ifdef FEAT_PROFILE
9580 instr_dest = dfunc->df_instr_prof; break;
9581#endif
9582 case CT_NONE: instr_dest = dfunc->df_instr; break;
9583 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
9584 }
9585 if (instr_dest != NULL)
9586 // Was compiled in this mode before: Free old instructions.
9587 delete_def_function_contents(dfunc, FALSE);
9588 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009589 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009590 else
9591 {
9592 if (add_def_function(ufunc) == FAIL)
9593 return FAIL;
9594 new_def_function = TRUE;
9595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009596
Bram Moolenaar985116a2020-07-12 17:31:09 +02009597 ufunc->uf_def_status = UF_COMPILING;
9598
Bram Moolenaara80faa82020-04-12 19:37:17 +02009599 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01009600
Bram Moolenaare99d4222021-06-13 14:01:26 +02009601 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009602 cctx.ctx_ufunc = ufunc;
9603 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009604 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009605 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9606 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9607 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9608 cctx.ctx_type_list = &ufunc->uf_type_list;
9609 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9610 instr = &cctx.ctx_instr;
9611
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009612 // Set the context to the function, it may be compiled when called from
9613 // another script. Set the script version to the most modern one.
9614 // The line number will be set in next_line_from_context().
9615 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009616 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9617
Bram Moolenaardc4c2302021-04-25 13:54:42 +02009618 // Don't use the flag from ":legacy" here.
9619 cmdmod.cmod_flags &= ~CMOD_LEGACY;
9620
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009621 // Make sure error messages are OK.
9622 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9623 if (do_estack_push)
9624 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009625 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009626
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009627 if (ufunc->uf_def_args.ga_len > 0)
9628 {
9629 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009630 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009631 int i;
9632 char_u *arg;
9633 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009634 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009635
9636 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01009637 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009638 for (i = 0; i < count; ++i)
9639 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009640 garray_T *stack = &cctx.ctx_type_stack;
9641 type_T *val_type;
9642 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02009643 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009644 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009645 int jump_instr_idx = instr->ga_len;
9646 isn_T *isn;
9647
9648 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9649 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9650 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01009651
9652 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009653 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009654
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009655 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01009656 r = compile_expr0(&arg, &cctx);
9657
Bram Moolenaar12bce952021-03-11 20:04:04 +01009658 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009659 goto erret;
9660
9661 // If no type specified use the type of the default value.
9662 // Otherwise check that the default value type matches the
9663 // specified type.
9664 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009665 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009666 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009667 {
9668 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009669 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009670 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02009671 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01009672 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009673 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02009674
9675 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009676 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009677
9678 // set instruction index in JUMP_IF_ARG_SET to here
9679 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9680 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009681 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02009682
9683 if (did_set_arg_type)
9684 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009685 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02009686 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01009687
9688 /*
9689 * Loop over all the lines of the function and generate instructions.
9690 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009691 for (;;)
9692 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009693 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02009694 int starts_with_colon = FALSE;
9695 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02009696 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01009697
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009698 // Bail out on the first error to avoid a flood of errors and report
9699 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01009700 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02009701 goto erret;
9702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009703 if (line != NULL && *line == '|')
9704 // the line continues after a '|'
9705 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02009706 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02009707 && !(*line == '#' && (line == cctx.ctx_line_start
9708 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009709 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02009710 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009711 goto erret;
9712 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01009713 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9714 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009715 else
9716 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02009717 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02009718 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009719 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009720 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01009721#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01009722 if (cctx.ctx_skip != SKIP_YES)
9723 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01009724#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009725 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009726 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009727 // Make a copy, splitting off nextcmd and removing trailing spaces
9728 // may change it.
9729 if (line != NULL)
9730 {
9731 line = vim_strsave(line);
9732 vim_free(line_to_free);
9733 line_to_free = line;
9734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009735 }
9736
Bram Moolenaara80faa82020-04-12 19:37:17 +02009737 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009738 ea.cmdlinep = &line;
9739 ea.cmd = skipwhite(line);
9740
Bram Moolenaarb2049902021-01-24 12:53:53 +01009741 if (*ea.cmd == '#')
9742 {
9743 // "#" starts a comment
9744 line = (char_u *)"";
9745 continue;
9746 }
9747
Bram Moolenaarf002a412021-01-24 13:34:18 +01009748#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02009749 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9750 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01009751 {
9752 may_generate_prof_end(&cctx, prof_lnum);
9753
9754 prof_lnum = cctx.ctx_lnum;
9755 generate_instr(&cctx, ISN_PROF_START);
9756 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01009757#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02009758 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9759 && cctx.ctx_skip != SKIP_YES)
9760 {
9761 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02009762 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02009763 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02009764 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01009765
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009766 // Some things can be recognized by the first character.
9767 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009768 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009769 case '}':
9770 {
9771 // "}" ends a block scope
9772 scopetype_T stype = cctx.ctx_scope == NULL
9773 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009774
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009775 if (stype == BLOCK_SCOPE)
9776 {
9777 compile_endblock(&cctx);
9778 line = ea.cmd;
9779 }
9780 else
9781 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009782 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02009783 goto erret;
9784 }
9785 if (line != NULL)
9786 line = skipwhite(ea.cmd + 1);
9787 continue;
9788 }
9789
9790 case '{':
9791 // "{" starts a block scope
9792 // "{'a': 1}->func() is something else
9793 if (ends_excmd(*skipwhite(ea.cmd + 1)))
9794 {
9795 line = compile_block(ea.cmd, &cctx);
9796 continue;
9797 }
9798 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009799 }
9800
9801 /*
9802 * COMMAND MODIFIERS
9803 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02009804 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02009805 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9806 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009807 {
9808 if (errormsg != NULL)
9809 goto erret;
9810 // empty line or comment
9811 line = (char_u *)"";
9812 continue;
9813 }
Bram Moolenaare1004402020-10-24 20:49:43 +02009814 generate_cmdmods(&cctx, &local_cmdmod);
9815 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009816
Bram Moolenaare88c8e82020-11-01 17:03:37 +01009817 // Check if there was a colon after the last command modifier or before
9818 // the current position.
9819 for (p = ea.cmd; p >= line; --p)
9820 {
9821 if (*p == ':')
9822 starts_with_colon = TRUE;
9823 if (p < ea.cmd && !VIM_ISWHITE(*p))
9824 break;
9825 }
9826
Bram Moolenaarce024c32021-06-26 13:00:49 +02009827 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009828 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009829 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02009830 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02009831 if (checkforcmd(&ea.cmd, "call", 3))
9832 {
9833 if (*ea.cmd == '(')
9834 // not for "call()"
9835 ea.cmd = p;
9836 else
9837 ea.cmd = skipwhite(ea.cmd);
9838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009839
Bram Moolenaarce024c32021-06-26 13:00:49 +02009840 if (!starts_with_colon)
9841 {
9842 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02009843
Bram Moolenaarce024c32021-06-26 13:00:49 +02009844 // Check for assignment after command modifiers.
9845 assign = may_compile_assignment(&ea, &line, &cctx);
9846 if (assign == OK)
9847 goto nextline;
9848 if (assign == FAIL)
9849 goto erret;
9850 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009851 }
9852
9853 /*
9854 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009855 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009856 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009857 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009858 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009859 cmd = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02009860 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02009861 && (*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02009862 && (starts_with_colon || !(*cmd == '\''
9863 || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009864 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02009865 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009866 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009867 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009868 if (!starts_with_colon)
9869 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01009870 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009871 goto erret;
9872 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01009873 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009874 if (ends_excmd2(line, ea.cmd))
9875 {
9876 // A range without a command: jump to the line.
9877 // TODO: compile to a more efficient command, possibly
9878 // calling parse_cmd_address().
9879 ea.cmdidx = CMD_SIZE;
9880 line = compile_exec(line, &ea, &cctx);
9881 goto nextline;
9882 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02009883 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009884 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02009885 p = find_ex_command(&ea, NULL,
9886 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01009887 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009888
Bram Moolenaard1510ee2021-01-04 16:15:58 +01009889 if (p == NULL)
9890 {
9891 if (cctx.ctx_skip != SKIP_YES)
9892 emsg(_(e_ambiguous_use_of_user_defined_command));
9893 goto erret;
9894 }
9895
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009896 // When using ":legacy cmd" always use compile_exec().
9897 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009898 {
9899 char_u *start = ea.cmd;
9900
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02009901 switch (ea.cmdidx)
9902 {
9903 case CMD_if:
9904 case CMD_elseif:
9905 case CMD_else:
9906 case CMD_endif:
9907 case CMD_for:
9908 case CMD_endfor:
9909 case CMD_continue:
9910 case CMD_break:
9911 case CMD_while:
9912 case CMD_endwhile:
9913 case CMD_try:
9914 case CMD_catch:
9915 case CMD_finally:
9916 case CMD_endtry:
9917 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9918 goto erret;
9919 default: break;
9920 }
9921
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009922 // ":legacy return expr" needs to be handled differently.
9923 if (checkforcmd(&start, "return", 4))
9924 ea.cmdidx = CMD_return;
9925 else
9926 ea.cmdidx = CMD_legacy;
9927 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02009928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009929 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9930 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009931 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009932 {
9933 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01009934 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009935 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02009936 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009937 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01009938 // CMD_var cannot happen, compile_assignment() above would be
9939 // used. Most likely an assignment to a non-existing variable.
9940 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009941 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009943 }
9944
Bram Moolenaar3988f642020-08-27 22:43:03 +02009945 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01009946 && ea.cmdidx != CMD_elseif
9947 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02009948 && ea.cmdidx != CMD_endif
9949 && ea.cmdidx != CMD_endfor
9950 && ea.cmdidx != CMD_endwhile
9951 && ea.cmdidx != CMD_catch
9952 && ea.cmdidx != CMD_finally
9953 && ea.cmdidx != CMD_endtry)
9954 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02009955 emsg(_(e_unreachable_code_after_return));
9956 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02009957 }
9958
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009959 p = skipwhite(p);
9960 if (ea.cmdidx != CMD_SIZE
9961 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9962 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02009963 if (ea.cmdidx >= 0)
9964 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009965 if ((ea.argt & EX_BANG) && *p == '!')
9966 {
9967 ea.forceit = TRUE;
9968 p = skipwhite(p + 1);
9969 }
9970 }
9971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009972 switch (ea.cmdidx)
9973 {
9974 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +00009975 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02009976 ea.arg = p;
9977 line = compile_nested_function(&ea, &cctx);
9978 break;
9979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009980 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02009981 line = compile_return(p, check_return_type,
9982 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009983 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009984 break;
9985
9986 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02009987 emsg(_(e_cannot_use_let_in_vim9_script));
9988 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02009989 case CMD_var:
9990 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009991 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02009992 case CMD_increment:
9993 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009994 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02009995 if (line == p)
9996 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009997 break;
9998
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009999 case CMD_unlet:
10000 case CMD_unlockvar:
10001 case CMD_lockvar:
10002 line = compile_unletlock(p, &ea, &cctx);
10003 break;
10004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010005 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +020010006 emsg(_(e_import_can_only_be_used_in_script));
10007 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010008 break;
10009
10010 case CMD_if:
10011 line = compile_if(p, &cctx);
10012 break;
10013 case CMD_elseif:
10014 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010015 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010016 break;
10017 case CMD_else:
10018 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010019 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010020 break;
10021 case CMD_endif:
10022 line = compile_endif(p, &cctx);
10023 break;
10024
10025 case CMD_while:
10026 line = compile_while(p, &cctx);
10027 break;
10028 case CMD_endwhile:
10029 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010030 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010031 break;
10032
10033 case CMD_for:
10034 line = compile_for(p, &cctx);
10035 break;
10036 case CMD_endfor:
10037 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010038 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010039 break;
10040 case CMD_continue:
10041 line = compile_continue(p, &cctx);
10042 break;
10043 case CMD_break:
10044 line = compile_break(p, &cctx);
10045 break;
10046
10047 case CMD_try:
10048 line = compile_try(p, &cctx);
10049 break;
10050 case CMD_catch:
10051 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010052 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010053 break;
10054 case CMD_finally:
10055 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +020010056 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010057 break;
10058 case CMD_endtry:
10059 line = compile_endtry(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_throw:
10063 line = compile_throw(p, &cctx);
10064 break;
10065
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010066 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +020010067 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +020010068 break;
10069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010070 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010071 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +010010072 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010073 case CMD_echomsg:
10074 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010075 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010076 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +010010077 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010078
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010079 case CMD_put:
10080 ea.cmd = cmd;
10081 line = compile_put(p, &ea, &cctx);
10082 break;
10083
Bram Moolenaar4c137212021-04-19 16:48:48 +020010084 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +010010085 if (check_global_and_subst(ea.cmd, p) == FAIL)
10086 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +020010087 if (cctx.ctx_skip == SKIP_YES)
10088 line = (char_u *)"";
10089 else
10090 {
10091 ea.arg = p;
10092 line = compile_substitute(line, &ea, &cctx);
10093 }
10094 break;
10095
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010096 case CMD_redir:
10097 ea.arg = p;
10098 line = compile_redir(line, &ea, &cctx);
10099 break;
10100
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010101 case CMD_cexpr:
10102 case CMD_lexpr:
10103 case CMD_caddexpr:
10104 case CMD_laddexpr:
10105 case CMD_cgetexpr:
10106 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010107#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010108 ea.arg = p;
10109 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +020010110#else
10111 ex_ni(&ea);
10112 line = NULL;
10113#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010114 break;
10115
Bram Moolenaarae616492020-07-28 20:07:27 +020010116 case CMD_append:
10117 case CMD_change:
10118 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010010119 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020010120 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +020010121 case CMD_xit:
10122 not_in_vim9(&ea);
10123 goto erret;
10124
Bram Moolenaar002262f2020-07-08 17:47:57 +020010125 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +020010126 if (cctx.ctx_skip != SKIP_YES)
10127 {
10128 semsg(_(e_invalid_command_str), ea.cmd);
10129 goto erret;
10130 }
10131 // We don't check for a next command here.
10132 line = (char_u *)"";
10133 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +020010134
Bram Moolenaar20677332021-06-06 17:02:53 +020010135 case CMD_lua:
10136 case CMD_mzscheme:
10137 case CMD_perl:
10138 case CMD_py3:
10139 case CMD_python3:
10140 case CMD_python:
10141 case CMD_pythonx:
10142 case CMD_ruby:
10143 case CMD_tcl:
10144 ea.arg = p;
10145 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +020010146 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +020010147 else
10148 // heredoc lines have been concatenated with NL
10149 // characters in get_function_body()
10150 line = compile_script(line, &cctx);
10151 break;
10152
Bram Moolenaar7b829262021-10-13 15:04:34 +010010153 case CMD_global:
10154 if (check_global_and_subst(ea.cmd, p) == FAIL)
10155 goto erret;
10156 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +020010157 default:
10158 // Not recognized, execute with do_cmdline_cmd().
10159 ea.arg = p;
10160 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010161 break;
10162 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +020010163nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010164 if (line == NULL)
10165 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +020010166 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010167
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010168 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +020010169 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +020010170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010171 if (cctx.ctx_type_stack.ga_len < 0)
10172 {
10173 iemsg("Type stack underflow");
10174 goto erret;
10175 }
10176 }
10177
10178 if (cctx.ctx_scope != NULL)
10179 {
10180 if (cctx.ctx_scope->se_type == IF_SCOPE)
10181 emsg(_(e_endif));
10182 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
10183 emsg(_(e_endwhile));
10184 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
10185 emsg(_(e_endfor));
10186 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010187 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010188 goto erret;
10189 }
10190
Bram Moolenaarefd88552020-06-18 20:50:10 +020010191 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010192 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +020010193 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
10194 ufunc->uf_ret_type = &t_void;
10195 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010196 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020010197 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010198 goto erret;
10199 }
10200
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010201 // Return void if there is no return at the end.
10202 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010203 }
10204
Bram Moolenaar599410c2021-04-10 14:03:43 +020010205 // When compiled with ":silent!" and there was an error don't consider the
10206 // function compiled.
10207 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010208 {
10209 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10210 + ufunc->uf_dfunc_idx;
10211 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010212 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +010010213#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +020010214 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +010010215 {
10216 dfunc->df_instr_prof = instr->ga_data;
10217 dfunc->df_instr_prof_count = instr->ga_len;
10218 }
10219 else
Bram Moolenaarf002a412021-01-24 13:34:18 +010010220#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +020010221 if (cctx.ctx_compile_type == CT_DEBUG)
10222 {
10223 dfunc->df_instr_debug = instr->ga_data;
10224 dfunc->df_instr_debug_count = instr->ga_len;
10225 }
10226 else
Bram Moolenaarb2049902021-01-24 12:53:53 +010010227 {
10228 dfunc->df_instr = instr->ga_data;
10229 dfunc->df_instr_count = instr->ga_len;
10230 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010231 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010232 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010233 if (cctx.ctx_outer_used)
10234 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010235 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010237
10238 ret = OK;
10239
10240erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +020010241 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010242 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +020010243 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10244 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010245
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010246 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +020010247 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010248 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +020010249 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010250
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010251 // If using the last entry in the table and it was added above, we
10252 // might as well remove it.
10253 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +020010254 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010255 {
Bram Moolenaar20431c92020-03-20 18:39:46 +010010256 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +020010257 ufunc->uf_dfunc_idx = 0;
10258 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +020010259 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010260
Bram Moolenaar3cca2992020-04-02 22:57:36 +020010261 while (cctx.ctx_scope != NULL)
10262 drop_scope(&cctx);
10263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010264 if (errormsg != NULL)
10265 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +010010266 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +020010267 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010268 }
10269
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010270 if (cctx.ctx_redir_lhs.lhs_name != NULL)
10271 {
10272 if (ret == OK)
10273 {
10274 emsg(_(e_missing_redir_end));
10275 ret = FAIL;
10276 }
10277 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +020010278 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010279 }
10280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010281 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +020010282 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +020010283 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +020010284 if (do_estack_push)
10285 estack_pop();
10286
Bram Moolenaarf62d7392021-04-14 12:40:00 +020010287 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010288 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +020010289 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010290 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +020010291 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010292}
10293
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010294 void
10295set_function_type(ufunc_T *ufunc)
10296{
10297 int varargs = ufunc->uf_va_name != NULL;
10298 int argcount = ufunc->uf_args.ga_len;
10299
10300 // Create a type for the function, with the return type and any
10301 // argument types.
10302 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
10303 // The type is included in "tt_args".
10304 if (argcount > 0 || varargs)
10305 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +010010306 if (ufunc->uf_type_list.ga_itemsize == 0)
10307 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010308 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
10309 argcount, &ufunc->uf_type_list);
10310 // Add argument types to the function type.
10311 if (func_type_add_arg_types(ufunc->uf_func_type,
10312 argcount + varargs,
10313 &ufunc->uf_type_list) == FAIL)
10314 return;
10315 ufunc->uf_func_type->tt_argcount = argcount + varargs;
10316 ufunc->uf_func_type->tt_min_argcount =
10317 argcount - ufunc->uf_def_args.ga_len;
10318 if (ufunc->uf_arg_types == NULL)
10319 {
10320 int i;
10321
10322 // lambda does not have argument types.
10323 for (i = 0; i < argcount; ++i)
10324 ufunc->uf_func_type->tt_args[i] = &t_any;
10325 }
10326 else
10327 mch_memmove(ufunc->uf_func_type->tt_args,
10328 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
10329 if (varargs)
10330 {
10331 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +020010332 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +020010333 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
10334 }
10335 }
10336 else
10337 // No arguments, can use a predefined type.
10338 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
10339 argcount, &ufunc->uf_type_list);
10340}
10341
10342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010343/*
10344 * Delete an instruction, free what it contains.
10345 */
Bram Moolenaar20431c92020-03-20 18:39:46 +010010346 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010347delete_instr(isn_T *isn)
10348{
10349 switch (isn->isn_type)
10350 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010351 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010352 case ISN_EXEC:
Bram Moolenaar20677332021-06-06 17:02:53 +020010353 case ISN_EXEC_SPLIT:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +020010354 case ISN_LEGACY_EVAL:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010355 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010356 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010357 case ISN_LOADENV:
10358 case ISN_LOADG:
10359 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010360 case ISN_LOADT:
10361 case ISN_LOADW:
Bram Moolenaaraacc9662021-08-13 19:40:51 +020010362 case ISN_LOCKUNLOCK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010363 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010364 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010365 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +010010366 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +010010367 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010368 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010369 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010370 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +020010371 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +010010372 case ISN_STOREW:
10373 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010374 vim_free(isn->isn_arg.string);
10375 break;
10376
Bram Moolenaar4c137212021-04-19 16:48:48 +020010377 case ISN_SUBSTITUTE:
Bram Moolenaar4f2df372021-04-19 21:06:31 +020010378 {
10379 int idx;
10380 isn_T *list = isn->isn_arg.subs.subs_instr;
10381
10382 vim_free(isn->isn_arg.subs.subs_cmd);
10383 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10384 delete_instr(list + idx);
10385 vim_free(list);
10386 }
Bram Moolenaar4c137212021-04-19 16:48:48 +020010387 break;
10388
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010389 case ISN_INSTR:
10390 {
10391 int idx;
10392 isn_T *list = isn->isn_arg.instr;
10393
10394 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
10395 delete_instr(list + idx);
10396 vim_free(list);
10397 }
10398 break;
10399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010400 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010010401 case ISN_STORES:
10402 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010403 break;
10404
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010405 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020010406 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020010407 vim_free(isn->isn_arg.unlet.ul_name);
10408 break;
10409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010410 case ISN_STOREOPT:
10411 vim_free(isn->isn_arg.storeopt.so_name);
10412 break;
10413
10414 case ISN_PUSHBLOB: // push blob isn_arg.blob
10415 blob_unref(isn->isn_arg.blob);
10416 break;
10417
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010418 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010419#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010420 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010421#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010422 break;
10423
10424 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010425#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010426 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +010010427#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +010010428 break;
10429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010430 case ISN_UCALL:
10431 vim_free(isn->isn_arg.ufunc.cuf_name);
10432 break;
10433
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010434 case ISN_FUNCREF:
10435 {
Bram Moolenaar38453522021-11-28 22:00:12 +000010436 if (isn->isn_arg.funcref.fr_func_name == NULL)
10437 {
10438 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10439 + isn->isn_arg.funcref.fr_dfunc_idx;
10440 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +020010441
Bram Moolenaar38453522021-11-28 22:00:12 +000010442 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
10443 func_ptr_unref(ufunc);
10444 }
10445 else
10446 {
10447 char_u *name = isn->isn_arg.funcref.fr_func_name;
10448
10449 if (name != NULL)
10450 func_unref(name);
10451 vim_free(isn->isn_arg.funcref.fr_func_name);
10452 }
Bram Moolenaara05e5242020-09-19 18:19:19 +020010453 }
10454 break;
10455
10456 case ISN_DCALL:
10457 {
10458 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10459 + isn->isn_arg.dfunc.cdf_idx;
10460
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020010461 if (dfunc->df_ufunc != NULL
10462 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +020010463 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +020010464 }
10465 break;
10466
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010467 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +020010468 {
10469 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
10470 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
10471
10472 if (ufunc != NULL)
10473 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010474 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +020010475 func_ptr_unref(ufunc);
10476 }
10477
10478 vim_free(lambda);
10479 vim_free(isn->isn_arg.newfunc.nf_global);
10480 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +020010481 break;
10482
Bram Moolenaar5e654232020-09-16 15:22:00 +020010483 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +010010484 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +020010485 free_type(isn->isn_arg.type.ct_type);
10486 break;
10487
Bram Moolenaar02194d22020-10-24 23:08:38 +020010488 case ISN_CMDMOD:
10489 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10490 ->cmod_filter_regmatch.regprog);
10491 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10492 break;
10493
Bram Moolenaar4aab88d2020-12-24 21:56:41 +010010494 case ISN_LOADSCRIPT:
10495 case ISN_STORESCRIPT:
10496 vim_free(isn->isn_arg.script.scriptref);
10497 break;
10498
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010499 case ISN_TRY:
10500 vim_free(isn->isn_arg.try.try_ref);
10501 break;
10502
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010503 case ISN_CEXPR_CORE:
Bram Moolenaardc3e2e62021-05-05 22:40:56 +020010504 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010505 vim_free(isn->isn_arg.cexpr.cexpr_ref);
10506 break;
10507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010508 case ISN_2BOOL:
10509 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +020010510 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010511 case ISN_ADDBLOB:
10512 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010513 case ISN_ANYINDEX:
10514 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010515 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +020010516 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +020010517 case ISN_BLOBINDEX:
10518 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010519 case ISN_CATCH:
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +020010520 case ISN_CEXPR_AUCMD:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010521 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010522 case ISN_CHECKNR:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010523 case ISN_CLEARDICT:
Bram Moolenaar02194d22020-10-24 23:08:38 +020010524 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010525 case ISN_COMPAREANY:
10526 case ISN_COMPAREBLOB:
10527 case ISN_COMPAREBOOL:
10528 case ISN_COMPAREDICT:
10529 case ISN_COMPAREFLOAT:
10530 case ISN_COMPAREFUNC:
10531 case ISN_COMPARELIST:
10532 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010533 case ISN_COMPARESPECIAL:
10534 case ISN_COMPARESTRING:
10535 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +020010536 case ISN_COND2BOOL:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010537 case ISN_DEBUG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010538 case ISN_DROP:
10539 case ISN_ECHO:
Bram Moolenaar7de62622021-08-07 15:05:47 +020010540 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020010541 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010542 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010543 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010544 case ISN_EXECCONCAT:
10545 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010010546 case ISN_FINALLY:
Bram Moolenaare99d4222021-06-13 14:01:26 +020010547 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010548 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010549 case ISN_GETITEM:
10550 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +020010551 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +020010552 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +020010553 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +020010554 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010555 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010556 case ISN_LOADBDICT:
10557 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020010558 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010559 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010560 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010561 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +020010562 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +020010563 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010564 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010565 case ISN_NEGATENR:
10566 case ISN_NEWDICT:
10567 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010568 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010569 case ISN_OPFLOAT:
10570 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010571 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +020010572 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +010010573 case ISN_PROF_END:
10574 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010575 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010576 case ISN_PUSHF:
10577 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010578 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +020010579 case ISN_PUT:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010580 case ISN_REDIREND:
10581 case ISN_REDIRSTART:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010582 case ISN_RETURN:
Bram Moolenaarf57b43c2021-06-15 22:13:27 +020010583 case ISN_RETURN_VOID:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010584 case ISN_SHUFFLE:
10585 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010586 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +010010587 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010588 case ISN_STORENR:
10589 case ISN_STOREOUTER:
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +020010590 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010591 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +020010592 case ISN_STOREV:
10593 case ISN_STRINDEX:
10594 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010595 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +010010596 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +010010597 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +010010598 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +010010599 case ISN_UNPACK:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +020010600 case ISN_USEDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010601 // nothing allocated
10602 break;
10603 }
10604}
10605
10606/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010607 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +010010608 */
10609 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010610delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010611{
10612 int idx;
10613
10614 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +020010615 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010616
10617 if (dfunc->df_instr != NULL)
10618 {
10619 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10620 delete_instr(dfunc->df_instr + idx);
10621 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010622 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010623 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +020010624 if (dfunc->df_instr_debug != NULL)
10625 {
10626 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10627 delete_instr(dfunc->df_instr_debug + idx);
10628 VIM_CLEAR(dfunc->df_instr_debug);
10629 dfunc->df_instr_debug = NULL;
10630 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +010010631#ifdef FEAT_PROFILE
10632 if (dfunc->df_instr_prof != NULL)
10633 {
10634 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10635 delete_instr(dfunc->df_instr_prof + idx);
10636 VIM_CLEAR(dfunc->df_instr_prof);
10637 dfunc->df_instr_prof = NULL;
10638 }
10639#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +010010640
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010641 if (mark_deleted)
10642 dfunc->df_deleted = TRUE;
10643 if (dfunc->df_ufunc != NULL)
10644 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +010010645}
10646
10647/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010648 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010649 * function, unless another user function still uses it.
10650 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010651 */
10652 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010653unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010654{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010655 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010656 {
10657 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10658 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010659
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010660 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010661 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +020010662 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010663 ufunc->uf_dfunc_idx = 0;
10664 if (dfunc->df_ufunc == ufunc)
10665 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010666 }
10667}
10668
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010669/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010670 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010671 */
10672 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010673link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010674{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010675 if (ufunc->uf_dfunc_idx > 0)
10676 {
10677 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10678 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010679
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010680 ++dfunc->df_refcount;
10681 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +020010682}
10683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010684#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +010010685/*
10686 * Free all functions defined with ":def".
10687 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010688 void
10689free_def_functions(void)
10690{
Bram Moolenaar20431c92020-03-20 18:39:46 +010010691 int idx;
10692
10693 for (idx = 0; idx < def_functions.ga_len; ++idx)
10694 {
10695 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10696
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010010697 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010010698 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +010010699 }
10700
10701 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010702}
10703#endif
10704
10705
10706#endif // FEAT_EVAL