blob: 0d19b7df52742203695e9022dfb9a507e2357bb6 [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;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100148static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
152 * "lvar->lv_from_outer" is set accordingly.
153 * If "lvar" is NULL only check if the variable can be found.
154 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 static int
157lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158{
159 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100160 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100162 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164
165 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100168 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
169 if (STRNCMP(name, lvp->lv_name, len) == 0
170 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100172 if (lvar != NULL)
173 {
174 *lvar = *lvp;
175 lvar->lv_from_outer = FALSE;
176 }
177 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100184 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lvar != NULL)
187 {
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 }
191 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 }
193 }
194
Bram Moolenaar709664c2020-12-12 14:33:41 +0100195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200206arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200258 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 == OK)
260 {
261 *gen_load_outer = TRUE;
262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270 * Lookup a script-local variable in the current script, possibly defined in a
271 * block that contains the function "cctx->ctx_ufunc".
272 * "cctx" is NULL at the script level.
273 * if "len" is <= 0 "name" must be NUL terminated.
274 * Return NULL when not found.
275 */
276 static sallvar_T *
277find_script_var(char_u *name, size_t len, cctx_T *cctx)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 int cc;
282 sallvar_T *sav;
283 ufunc_T *ufunc;
284
285 // Find the list of all script variables with the right name.
286 if (len > 0)
287 {
288 cc = name[len];
289 name[len] = NUL;
290 }
291 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
292 if (len > 0)
293 name[len] = cc;
294 if (HASHITEM_EMPTY(hi))
295 return NULL;
296
297 sav = HI2SAV(hi);
298 if (sav->sav_block_id == 0 || cctx == NULL)
299 // variable defined in the script scope or not in a function.
300 return sav;
301
302 // Go over the variables with this name and find one that was visible
303 // from the function.
304 ufunc = cctx->ctx_ufunc;
305 while (sav != NULL)
306 {
307 int idx;
308
309 // Go over the blocks that this function was defined in. If the
310 // variable block ID matches it was visible to the function.
311 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
312 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
313 return sav;
314 sav = sav->sav_next;
315 }
316
317 return NULL;
318}
319
320/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100321 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200322 */
323 static int
324script_is_vim9()
325{
326 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200331 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
332 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Returns OK or FAIL.
335 */
336 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 is_vim9_script = script_is_vim9();
344 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 if (is_vim9_script)
347 {
348 // Check script variables that were visible where the function was
349 // defined.
350 if (find_script_var(name, len, cctx) != NULL)
351 return OK;
352 }
353 else
354 {
355 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
356 dictitem_T *di;
357 int cc;
358
359 // Check script variables that are currently visible
360 cc = name[len];
361 name[len] = NUL;
362 di = find_var_in_ht(ht, 0, name, TRUE);
363 name[len] = cc;
364 if (di != NULL)
365 return OK;
366 }
367
368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369}
370
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371/*
372 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200373 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375 * Return FAIL and give an error if it defined.
376 */
377 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200378check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200380 int c = p[len];
381 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382
383 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200384 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100385 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100386 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200388 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200389 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100390 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 // A local or script-local function can shadow a global function.
392 if (ufunc == NULL || !func_is_global(ufunc)
393 || (p[0] == 'g' && p[1] == ':'))
394 {
395 p[len] = c;
396 semsg(_(e_name_already_defined_str), p);
397 return FAIL;
398 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100399 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200400 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 return OK;
402}
403
Bram Moolenaar65b95452020-07-19 14:03:09 +0200404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/////////////////////////////////////////////////////////////////////
406// Following generate_ functions expect the caller to call ga_grow().
407
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200408#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
409#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411/*
412 * Generate an instruction without arguments.
413 * Returns a pointer to the new instruction, NULL if failed.
414 */
415 static isn_T *
416generate_instr(cctx_T *cctx, isntype_T isn_type)
417{
418 garray_T *instr = &cctx->ctx_instr;
419 isn_T *isn;
420
Bram Moolenaar080457c2020-03-03 21:53:32 +0100421 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ga_grow(instr, 1) == FAIL)
423 return NULL;
424 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
425 isn->isn_type = isn_type;
426 isn->isn_lnum = cctx->ctx_lnum + 1;
427 ++instr->ga_len;
428
429 return isn;
430}
431
432/*
433 * Generate an instruction without arguments.
434 * "drop" will be removed from the stack.
435 * Returns a pointer to the new instruction, NULL if failed.
436 */
437 static isn_T *
438generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
439{
440 garray_T *stack = &cctx->ctx_type_stack;
441
Bram Moolenaar080457c2020-03-03 21:53:32 +0100442 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 stack->ga_len -= drop;
444 return generate_instr(cctx, isn_type);
445}
446
447/*
448 * Generate instruction "isn_type" and put "type" on the type stack.
449 */
450 static isn_T *
451generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
452{
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
455
456 if ((isn = generate_instr(cctx, isn_type)) == NULL)
457 return NULL;
458
459 if (ga_grow(stack, 1) == FAIL)
460 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200461 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 ++stack->ga_len;
463
464 return isn;
465}
466
467/*
468 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 */
471 static int
472may_generate_2STRING(int offset, cctx_T *cctx)
473{
474 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200475 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 garray_T *stack = &cctx->ctx_type_stack;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 switch ((*type)->tt_type)
480 {
481 // nothing to be done
482 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 // conversion possible
485 case VAR_SPECIAL:
486 case VAR_BOOL:
487 case VAR_NUMBER:
488 case VAR_FLOAT:
489 break;
490
491 // conversion possible (with runtime check)
492 case VAR_ANY:
493 case VAR_UNKNOWN:
494 isntype = ISN_2STRING_ANY;
495 break;
496
497 // conversion not possible
498 case VAR_VOID:
499 case VAR_BLOB:
500 case VAR_FUNC:
501 case VAR_PARTIAL:
502 case VAR_LIST:
503 case VAR_DICT:
504 case VAR_JOB:
505 case VAR_CHANNEL:
506 to_string_error((*type)->tt_type);
507 return FAIL;
508 }
509
510 *type = &t_string;
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 return FAIL;
513 isn->isn_arg.number = offset;
514
515 return OK;
516}
517
518 static int
519check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
520{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200521 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200526 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 }
531 return OK;
532}
533
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200534 static int
535generate_add_instr(
536 cctx_T *cctx,
537 vartype_T vartype,
538 type_T *type1,
539 type_T *type2)
540{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100541 garray_T *stack = &cctx->ctx_type_stack;
542 isn_T *isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR
544 : vartype == VAR_LIST ? ISN_ADDLIST
545 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550
551 if (vartype != VAR_LIST && vartype != VAR_BLOB
552 && type1->tt_type != VAR_ANY
553 && type2->tt_type != VAR_ANY
554 && check_number_or_float(
555 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
556 return FAIL;
557
558 if (isn != NULL)
559 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560
561 // When concatenating two lists with different member types the member type
562 // becomes "any".
563 if (vartype == VAR_LIST
564 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
565 && type1->tt_member != type2->tt_member)
566 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
567
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200568 return isn == NULL ? FAIL : OK;
569}
570
571/*
572 * Get the type to use for an instruction for an operation on "type1" and
573 * "type2". If they are matching use a type-specific instruction. Otherwise
574 * fall back to runtime type checking.
575 */
576 static vartype_T
577operator_type(type_T *type1, type_T *type2)
578{
579 if (type1->tt_type == type2->tt_type
580 && (type1->tt_type == VAR_NUMBER
581 || type1->tt_type == VAR_LIST
582#ifdef FEAT_FLOAT
583 || type1->tt_type == VAR_FLOAT
584#endif
585 || type1->tt_type == VAR_BLOB))
586 return type1->tt_type;
587 return VAR_ANY;
588}
589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590/*
591 * Generate an instruction with two arguments. The instruction depends on the
592 * type of the arguments.
593 */
594 static int
595generate_two_op(cctx_T *cctx, char_u *op)
596{
597 garray_T *stack = &cctx->ctx_type_stack;
598 type_T *type1;
599 type_T *type2;
600 vartype_T vartype;
601 isn_T *isn;
602
Bram Moolenaar080457c2020-03-03 21:53:32 +0100603 RETURN_OK_IF_SKIP(cctx);
604
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
607 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 switch (*op)
611 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 case '+':
613 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 break;
616
617 case '-':
618 case '*':
619 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
620 op) == FAIL)
621 return FAIL;
622 if (vartype == VAR_NUMBER)
623 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
624#ifdef FEAT_FLOAT
625 else if (vartype == VAR_FLOAT)
626 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
627#endif
628 else
629 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = *op == '*'
632 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
633 break;
634
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type2->tt_type != VAR_NUMBER))
639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 isn = generate_instr_drop(cctx,
644 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
645 if (isn != NULL)
646 isn->isn_arg.op.op_type = EXPR_REM;
647 break;
648 }
649
650 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 type_T *type = &t_any;
654
655#ifdef FEAT_FLOAT
656 // float+number and number+float results in float
657 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
658 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
659 type = &t_float;
660#endif
661 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
662 }
663
664 return OK;
665}
666
667/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200668 * Get the instruction to use for comparing "type1" with "type2"
669 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200671 static isntype_T
672get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 if (type1 == VAR_UNKNOWN)
677 type1 = VAR_ANY;
678 if (type2 == VAR_UNKNOWN)
679 type2 = VAR_ANY;
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (type1 == type2)
682 {
683 switch (type1)
684 {
685 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
686 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
687 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
688 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
689 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
690 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
691 case VAR_LIST: isntype = ISN_COMPARELIST; break;
692 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
693 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 default: isntype = ISN_COMPAREANY; break;
695 }
696 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
699 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
700 isntype = ISN_COMPAREANY;
701
702 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
703 && (isntype == ISN_COMPAREBOOL
704 || isntype == ISN_COMPARESPECIAL
705 || isntype == ISN_COMPARENR
706 || isntype == ISN_COMPAREFLOAT))
707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200708 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 }
712 if (isntype == ISN_DROP
713 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
714 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
715 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
716 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
717 && exptype != EXPR_IS && exptype != EXPR_ISNOT
718 && (type1 == VAR_BLOB || type2 == VAR_BLOB
719 || type1 == VAR_LIST || type2 == VAR_LIST))))
720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200721 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return isntype;
726}
727
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200728 int
729check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
730{
731 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
732 return FAIL;
733 return OK;
734}
735
Bram Moolenaara5565e42020-05-09 15:44:01 +0200736/*
737 * Generate an ISN_COMPARE* instruction with a boolean result.
738 */
739 static int
740generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
741{
742 isntype_T isntype;
743 isn_T *isn;
744 garray_T *stack = &cctx->ctx_type_stack;
745 vartype_T type1;
746 vartype_T type2;
747
748 RETURN_OK_IF_SKIP(cctx);
749
750 // Get the known type of the two items on the stack. If they are matching
751 // use a type-specific instruction. Otherwise fall back to runtime type
752 // checking.
753 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
754 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
755 isntype = get_compare_isn(exptype, type1, type2);
756 if (isntype == ISN_DROP)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if ((isn = generate_instr(cctx, isntype)) == NULL)
760 return FAIL;
761 isn->isn_arg.op.op_type = exptype;
762 isn->isn_arg.op.op_ic = ic;
763
764 // takes two arguments, puts one bool back
765 if (stack->ga_len >= 2)
766 {
767 --stack->ga_len;
768 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
769 }
770
771 return OK;
772}
773
774/*
775 * Generate an ISN_2BOOL instruction.
776 */
777 static int
778generate_2BOOL(cctx_T *cctx, int invert)
779{
780 isn_T *isn;
781 garray_T *stack = &cctx->ctx_type_stack;
782
Bram Moolenaar080457c2020-03-03 21:53:32 +0100783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = invert;
787
788 // type becomes bool
789 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
790
791 return OK;
792}
793
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200794/*
795 * Generate an ISN_COND2BOOL instruction.
796 */
797 static int
798generate_COND2BOOL(cctx_T *cctx)
799{
800 isn_T *isn;
801 garray_T *stack = &cctx->ctx_type_stack;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
805 return FAIL;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200814generate_TYPECHECK(
815 cctx_T *cctx,
816 type_T *expected,
817 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
824 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200825 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 isn->isn_arg.type.ct_off = offset;
827
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 // type becomes expected
829 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return OK;
832}
833
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100834 static int
835generate_SETTYPE(
836 cctx_T *cctx,
837 type_T *expected)
838{
839 isn_T *isn;
840
841 RETURN_OK_IF_SKIP(cctx);
842 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
843 return FAIL;
844 isn->isn_arg.type.ct_type = alloc_type(expected);
845 return OK;
846}
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200849 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
850 * used. Return FALSE if the types will never match.
851 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100852 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200853use_typecheck(type_T *actual, type_T *expected)
854{
855 if (actual->tt_type == VAR_ANY
856 || actual->tt_type == VAR_UNKNOWN
857 || (actual->tt_type == VAR_FUNC
858 && (expected->tt_type == VAR_FUNC
859 || expected->tt_type == VAR_PARTIAL)
860 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
861 return TRUE;
862 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
863 && actual->tt_type == expected->tt_type)
864 // This takes care of a nested list or dict.
865 return use_typecheck(actual->tt_member, expected->tt_member);
866 return FALSE;
867}
868
869/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200871 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 * - "actual" is a type that can be "expected" type: add a runtime check; or
873 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200874 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
875 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200876 */
877 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200878need_type(
879 type_T *actual,
880 type_T *expected,
881 int offset,
882 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200883 int silent,
884 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200885{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200886 if (expected == &t_bool && actual != &t_bool
887 && (actual->tt_flags & TTFLAG_BOOL_OK))
888 {
889 // Using "0", "1" or the result of an expression with "&&" or "||" as a
890 // boolean is OK but requires a conversion.
891 generate_2BOOL(cctx, FALSE);
892 return OK;
893 }
894
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200895 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200896 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200897
898 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200899 // If it's a constant a runtime check makes no sense.
900 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200901 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200902 generate_TYPECHECK(cctx, expected, offset);
903 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200904 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200905
906 if (!silent)
907 type_mismatch(expected, actual);
908 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200909}
910
911/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100912 * Check that the top of the type stack has a type that can be used as a
913 * condition. Give an error and return FAIL if not.
914 */
915 static int
916bool_on_stack(cctx_T *cctx)
917{
918 garray_T *stack = &cctx->ctx_type_stack;
919 type_T *type;
920
921 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
922 if (type == &t_bool)
923 return OK;
924
925 if (type == &t_any || type == &t_number)
926 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
927 // This requires a runtime type check.
928 return generate_COND2BOOL(cctx);
929
930 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
931}
932
933/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 * Generate an ISN_PUSHNR instruction.
935 */
936 static int
937generate_PUSHNR(cctx_T *cctx, varnumber_T number)
938{
939 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200940 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
Bram Moolenaar080457c2020-03-03 21:53:32 +0100942 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
944 return FAIL;
945 isn->isn_arg.number = number;
946
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200947 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200948 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100949 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return OK;
951}
952
953/*
954 * Generate an ISN_PUSHBOOL instruction.
955 */
956 static int
957generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
958{
959 isn_T *isn;
960
Bram Moolenaar080457c2020-03-03 21:53:32 +0100961 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
963 return FAIL;
964 isn->isn_arg.number = number;
965
966 return OK;
967}
968
969/*
970 * Generate an ISN_PUSHSPEC instruction.
971 */
972 static int
973generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
974{
975 isn_T *isn;
976
Bram Moolenaar080457c2020-03-03 21:53:32 +0100977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
979 return FAIL;
980 isn->isn_arg.number = number;
981
982 return OK;
983}
984
985#ifdef FEAT_FLOAT
986/*
987 * Generate an ISN_PUSHF instruction.
988 */
989 static int
990generate_PUSHF(cctx_T *cctx, float_T fnumber)
991{
992 isn_T *isn;
993
Bram Moolenaar080457c2020-03-03 21:53:32 +0100994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
996 return FAIL;
997 isn->isn_arg.fnumber = fnumber;
998
999 return OK;
1000}
1001#endif
1002
1003/*
1004 * Generate an ISN_PUSHS instruction.
1005 * Consumes "str".
1006 */
1007 static int
1008generate_PUSHS(cctx_T *cctx, char_u *str)
1009{
1010 isn_T *isn;
1011
Bram Moolenaar508b5612021-01-02 18:17:26 +01001012 if (cctx->ctx_skip == SKIP_YES)
1013 {
1014 vim_free(str);
1015 return OK;
1016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1018 return FAIL;
1019 isn->isn_arg.string = str;
1020
1021 return OK;
1022}
1023
1024/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001025 * Generate an ISN_PUSHCHANNEL instruction.
1026 * Consumes "channel".
1027 */
1028 static int
1029generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1030{
1031 isn_T *isn;
1032
Bram Moolenaar080457c2020-03-03 21:53:32 +01001033 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001034 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1035 return FAIL;
1036 isn->isn_arg.channel = channel;
1037
1038 return OK;
1039}
1040
1041/*
1042 * Generate an ISN_PUSHJOB instruction.
1043 * Consumes "job".
1044 */
1045 static int
1046generate_PUSHJOB(cctx_T *cctx, job_T *job)
1047{
1048 isn_T *isn;
1049
Bram Moolenaar080457c2020-03-03 21:53:32 +01001050 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001051 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001052 return FAIL;
1053 isn->isn_arg.job = job;
1054
1055 return OK;
1056}
1057
1058/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 * Generate an ISN_PUSHBLOB instruction.
1060 * Consumes "blob".
1061 */
1062 static int
1063generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1064{
1065 isn_T *isn;
1066
Bram Moolenaar080457c2020-03-03 21:53:32 +01001067 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1069 return FAIL;
1070 isn->isn_arg.blob = blob;
1071
1072 return OK;
1073}
1074
1075/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001076 * Generate an ISN_PUSHFUNC instruction with name "name".
1077 * Consumes "name".
1078 */
1079 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001080generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001081{
1082 isn_T *isn;
1083
Bram Moolenaar080457c2020-03-03 21:53:32 +01001084 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001085 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001086 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001087 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001088
1089 return OK;
1090}
1091
1092/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001093 * Generate an ISN_GETITEM instruction with "index".
1094 */
1095 static int
1096generate_GETITEM(cctx_T *cctx, int index)
1097{
1098 isn_T *isn;
1099 garray_T *stack = &cctx->ctx_type_stack;
1100 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1101 type_T *item_type = &t_any;
1102
1103 RETURN_OK_IF_SKIP(cctx);
1104
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001105 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001106 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001107 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001108 emsg(_(e_listreq));
1109 return FAIL;
1110 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001111 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001112 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1113 return FAIL;
1114 isn->isn_arg.number = index;
1115
1116 // add the item type to the type stack
1117 if (ga_grow(stack, 1) == FAIL)
1118 return FAIL;
1119 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1120 ++stack->ga_len;
1121 return OK;
1122}
1123
1124/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001125 * Generate an ISN_SLICE instruction with "count".
1126 */
1127 static int
1128generate_SLICE(cctx_T *cctx, int count)
1129{
1130 isn_T *isn;
1131
1132 RETURN_OK_IF_SKIP(cctx);
1133 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1134 return FAIL;
1135 isn->isn_arg.number = count;
1136 return OK;
1137}
1138
1139/*
1140 * Generate an ISN_CHECKLEN instruction with "min_len".
1141 */
1142 static int
1143generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1144{
1145 isn_T *isn;
1146
1147 RETURN_OK_IF_SKIP(cctx);
1148
1149 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1150 return FAIL;
1151 isn->isn_arg.checklen.cl_min_len = min_len;
1152 isn->isn_arg.checklen.cl_more_OK = more_OK;
1153
1154 return OK;
1155}
1156
1157/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 * Generate an ISN_STORE instruction.
1159 */
1160 static int
1161generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1162{
1163 isn_T *isn;
1164
Bram Moolenaar080457c2020-03-03 21:53:32 +01001165 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1167 return FAIL;
1168 if (name != NULL)
1169 isn->isn_arg.string = vim_strsave(name);
1170 else
1171 isn->isn_arg.number = idx;
1172
1173 return OK;
1174}
1175
1176/*
1177 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1178 */
1179 static int
1180generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1181{
1182 isn_T *isn;
1183
Bram Moolenaar080457c2020-03-03 21:53:32 +01001184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1186 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001187 isn->isn_arg.storenr.stnr_idx = idx;
1188 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189
1190 return OK;
1191}
1192
1193/*
1194 * Generate an ISN_STOREOPT instruction
1195 */
1196 static int
1197generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1198{
1199 isn_T *isn;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001202 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 return FAIL;
1204 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1205 isn->isn_arg.storeopt.so_flags = opt_flags;
1206
1207 return OK;
1208}
1209
1210/*
1211 * Generate an ISN_LOAD or similar instruction.
1212 */
1213 static int
1214generate_LOAD(
1215 cctx_T *cctx,
1216 isntype_T isn_type,
1217 int idx,
1218 char_u *name,
1219 type_T *type)
1220{
1221 isn_T *isn;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1225 return FAIL;
1226 if (name != NULL)
1227 isn->isn_arg.string = vim_strsave(name);
1228 else
1229 isn->isn_arg.number = idx;
1230
1231 return OK;
1232}
1233
1234/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001235 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001236 */
1237 static int
1238generate_LOADV(
1239 cctx_T *cctx,
1240 char_u *name,
1241 int error)
1242{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001243 int di_flags;
1244 int vidx = find_vim_var(name, &di_flags);
1245 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246
Bram Moolenaar080457c2020-03-03 21:53:32 +01001247 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 if (vidx < 0)
1249 {
1250 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001251 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001252 return FAIL;
1253 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001254 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001255
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001256 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001257}
1258
1259/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001260 * Generate an ISN_UNLET instruction.
1261 */
1262 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001263generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001264{
1265 isn_T *isn;
1266
1267 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001268 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001269 return FAIL;
1270 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1271 isn->isn_arg.unlet.ul_forceit = forceit;
1272
1273 return OK;
1274}
1275
1276/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001277 * Generate an ISN_LOCKCONST instruction.
1278 */
1279 static int
1280generate_LOCKCONST(cctx_T *cctx)
1281{
1282 isn_T *isn;
1283
1284 RETURN_OK_IF_SKIP(cctx);
1285 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1286 return FAIL;
1287 return OK;
1288}
1289
1290/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 * Generate an ISN_LOADS instruction.
1292 */
1293 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001294generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001298 int sid,
1299 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300{
1301 isn_T *isn;
1302
Bram Moolenaar080457c2020-03-03 21:53:32 +01001303 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001304 if (isn_type == ISN_LOADS)
1305 isn = generate_instr_type(cctx, isn_type, type);
1306 else
1307 isn = generate_instr_drop(cctx, isn_type, 1);
1308 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001310 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1311 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313 return OK;
1314}
1315
1316/*
1317 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1318 */
1319 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001320generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 cctx_T *cctx,
1322 isntype_T isn_type,
1323 int sid,
1324 int idx,
1325 type_T *type)
1326{
1327 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001328 scriptref_T *sref;
1329 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330
Bram Moolenaar080457c2020-03-03 21:53:32 +01001331 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 if (isn_type == ISN_LOADSCRIPT)
1333 isn = generate_instr_type(cctx, isn_type, type);
1334 else
1335 isn = generate_instr_drop(cctx, isn_type, 1);
1336 if (isn == NULL)
1337 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001338
1339 // This requires three arguments, which doesn't fit in an instruction, thus
1340 // we need to allocate a struct for this.
1341 sref = ALLOC_ONE(scriptref_T);
1342 if (sref == NULL)
1343 return FAIL;
1344 isn->isn_arg.script.scriptref = sref;
1345 sref->sref_sid = sid;
1346 sref->sref_idx = idx;
1347 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001348 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 return OK;
1350}
1351
1352/*
1353 * Generate an ISN_NEWLIST instruction.
1354 */
1355 static int
1356generate_NEWLIST(cctx_T *cctx, int count)
1357{
1358 isn_T *isn;
1359 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 type_T *type;
1361 type_T *member;
1362
Bram Moolenaar080457c2020-03-03 21:53:32 +01001363 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1365 return FAIL;
1366 isn->isn_arg.number = count;
1367
Bram Moolenaar127542b2020-08-09 17:22:04 +02001368 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001369 if (count == 0)
1370 member = &t_void;
1371 else
1372 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001373 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1374 cctx->ctx_type_list);
1375 type = get_list_type(member, cctx->ctx_type_list);
1376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 // drop the value types
1378 stack->ga_len -= count;
1379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 // add the list type to the type stack
1381 if (ga_grow(stack, 1) == FAIL)
1382 return FAIL;
1383 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1384 ++stack->ga_len;
1385
1386 return OK;
1387}
1388
1389/*
1390 * Generate an ISN_NEWDICT instruction.
1391 */
1392 static int
1393generate_NEWDICT(cctx_T *cctx, int count)
1394{
1395 isn_T *isn;
1396 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 type_T *type;
1398 type_T *member;
1399
Bram Moolenaar080457c2020-03-03 21:53:32 +01001400 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1402 return FAIL;
1403 isn->isn_arg.number = count;
1404
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001405 if (count == 0)
1406 member = &t_void;
1407 else
1408 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001409 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1410 cctx->ctx_type_list);
1411 type = get_dict_type(member, cctx->ctx_type_list);
1412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 // drop the key and value types
1414 stack->ga_len -= 2 * count;
1415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 // add the dict type to the type stack
1417 if (ga_grow(stack, 1) == FAIL)
1418 return FAIL;
1419 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1420 ++stack->ga_len;
1421
1422 return OK;
1423}
1424
1425/*
1426 * Generate an ISN_FUNCREF instruction.
1427 */
1428 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001429generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430{
1431 isn_T *isn;
1432 garray_T *stack = &cctx->ctx_type_stack;
1433
Bram Moolenaar080457c2020-03-03 21:53:32 +01001434 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1436 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001437 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001438 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439
1440 if (ga_grow(stack, 1) == FAIL)
1441 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001442 ((type_T **)stack->ga_data)[stack->ga_len] =
1443 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 ++stack->ga_len;
1445
1446 return OK;
1447}
1448
1449/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001450 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001451 * "lambda_name" and "func_name" must be in allocated memory and will be
1452 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001453 */
1454 static int
1455generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1456{
1457 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001458
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001459 if (cctx->ctx_skip == SKIP_YES)
1460 {
1461 vim_free(lambda_name);
1462 vim_free(func_name);
1463 return OK;
1464 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001465 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001466 {
1467 vim_free(lambda_name);
1468 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001469 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001470 }
1471 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001472 isn->isn_arg.newfunc.nf_global = func_name;
1473
1474 return OK;
1475}
1476
1477/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001478 * Generate an ISN_DEF instruction: list functions
1479 */
1480 static int
1481generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1482{
1483 isn_T *isn;
1484
1485 RETURN_OK_IF_SKIP(cctx);
1486 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1487 return FAIL;
1488 if (len > 0)
1489 {
1490 isn->isn_arg.string = vim_strnsave(name, len);
1491 if (isn->isn_arg.string == NULL)
1492 return FAIL;
1493 }
1494 return OK;
1495}
1496
1497/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 * Generate an ISN_JUMP instruction.
1499 */
1500 static int
1501generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1502{
1503 isn_T *isn;
1504 garray_T *stack = &cctx->ctx_type_stack;
1505
Bram Moolenaar080457c2020-03-03 21:53:32 +01001506 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1508 return FAIL;
1509 isn->isn_arg.jump.jump_when = when;
1510 isn->isn_arg.jump.jump_where = where;
1511
1512 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1513 --stack->ga_len;
1514
1515 return OK;
1516}
1517
1518 static int
1519generate_FOR(cctx_T *cctx, int loop_idx)
1520{
1521 isn_T *isn;
1522 garray_T *stack = &cctx->ctx_type_stack;
1523
Bram Moolenaar080457c2020-03-03 21:53:32 +01001524 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1526 return FAIL;
1527 isn->isn_arg.forloop.for_idx = loop_idx;
1528
1529 if (ga_grow(stack, 1) == FAIL)
1530 return FAIL;
1531 // type doesn't matter, will be stored next
1532 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1533 ++stack->ga_len;
1534
1535 return OK;
1536}
1537
1538/*
1539 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001540 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 * Return FAIL if the number of arguments is wrong.
1542 */
1543 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001544generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545{
1546 isn_T *isn;
1547 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001548 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001549 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550
Bram Moolenaar080457c2020-03-03 21:53:32 +01001551 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001552 argoff = check_internal_func(func_idx, argcount);
1553 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 return FAIL;
1555
Bram Moolenaar389df252020-07-09 21:20:47 +02001556 if (method_call && argoff > 1)
1557 {
1558 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1559 return FAIL;
1560 isn->isn_arg.shuffle.shfl_item = argcount;
1561 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1562 }
1563
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001564 if (argcount > 0)
1565 {
1566 // Check the types of the arguments.
1567 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1568 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001569 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001570 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1573 return FAIL;
1574 isn->isn_arg.bfunc.cbf_idx = func_idx;
1575 isn->isn_arg.bfunc.cbf_argcount = argcount;
1576
Bram Moolenaar94738d82020-10-21 14:25:07 +02001577 // Drop the argument types and push the return type.
1578 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 if (ga_grow(stack, 1) == FAIL)
1580 return FAIL;
1581 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001582 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001583 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584
1585 return OK;
1586}
1587
1588/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001589 * Generate an ISN_LISTAPPEND instruction. Works like add().
1590 * Argument count is already checked.
1591 */
1592 static int
1593generate_LISTAPPEND(cctx_T *cctx)
1594{
1595 garray_T *stack = &cctx->ctx_type_stack;
1596 type_T *list_type;
1597 type_T *item_type;
1598 type_T *expected;
1599
1600 // Caller already checked that list_type is a list.
1601 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1602 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1603 expected = list_type->tt_member;
1604 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1605 return FAIL;
1606
1607 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1608 return FAIL;
1609
1610 --stack->ga_len; // drop the argument
1611 return OK;
1612}
1613
1614/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001615 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1616 * Argument count is already checked.
1617 */
1618 static int
1619generate_BLOBAPPEND(cctx_T *cctx)
1620{
1621 garray_T *stack = &cctx->ctx_type_stack;
1622 type_T *item_type;
1623
1624 // Caller already checked that blob_type is a blob.
1625 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1626 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1627 return FAIL;
1628
1629 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1630 return FAIL;
1631
1632 --stack->ga_len; // drop the argument
1633 return OK;
1634}
1635
1636/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 * Generate an ISN_DCALL or ISN_UCALL instruction.
1638 * Return FAIL if the number of arguments is wrong.
1639 */
1640 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001641generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642{
1643 isn_T *isn;
1644 garray_T *stack = &cctx->ctx_type_stack;
1645 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001646 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647
Bram Moolenaar080457c2020-03-03 21:53:32 +01001648 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 if (argcount > regular_args && !has_varargs(ufunc))
1650 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001651 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 return FAIL;
1653 }
1654 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1655 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001656 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 return FAIL;
1658 }
1659
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001660 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001661 {
1662 int i;
1663
1664 for (i = 0; i < argcount; ++i)
1665 {
1666 type_T *expected;
1667 type_T *actual;
1668
1669 if (i < regular_args)
1670 {
1671 if (ufunc->uf_arg_types == NULL)
1672 continue;
1673 expected = ufunc->uf_arg_types[i];
1674 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001675 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1676 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001677 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001678 else
1679 expected = ufunc->uf_va_type->tt_member;
1680 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001681 if (need_type(actual, expected, -argcount + i, cctx,
1682 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001683 {
1684 arg_type_mismatch(expected, actual, i + 1);
1685 return FAIL;
1686 }
1687 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001688 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001689 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1690 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001691 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001692 }
1693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001695 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001696 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001698 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 {
1700 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1701 isn->isn_arg.dfunc.cdf_argcount = argcount;
1702 }
1703 else
1704 {
1705 // A user function may be deleted and redefined later, can't use the
1706 // ufunc pointer, need to look it up again at runtime.
1707 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1708 isn->isn_arg.ufunc.cuf_argcount = argcount;
1709 }
1710
1711 stack->ga_len -= argcount; // drop the arguments
1712 if (ga_grow(stack, 1) == FAIL)
1713 return FAIL;
1714 // add return value
1715 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1716 ++stack->ga_len;
1717
1718 return OK;
1719}
1720
1721/*
1722 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1723 */
1724 static int
1725generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1726{
1727 isn_T *isn;
1728 garray_T *stack = &cctx->ctx_type_stack;
1729
Bram Moolenaar080457c2020-03-03 21:53:32 +01001730 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1732 return FAIL;
1733 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1734 isn->isn_arg.ufunc.cuf_argcount = argcount;
1735
1736 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001737 if (ga_grow(stack, 1) == FAIL)
1738 return FAIL;
1739 // add return value
1740 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1741 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
1743 return OK;
1744}
1745
1746/*
1747 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001748 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 */
1750 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001751generate_PCALL(
1752 cctx_T *cctx,
1753 int argcount,
1754 char_u *name,
1755 type_T *type,
1756 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757{
1758 isn_T *isn;
1759 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001760 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761
Bram Moolenaar080457c2020-03-03 21:53:32 +01001762 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001763
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001764 if (type->tt_type == VAR_ANY)
1765 ret_type = &t_any;
1766 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001767 {
1768 if (type->tt_argcount != -1)
1769 {
1770 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1771
1772 if (argcount < type->tt_min_argcount - varargs)
1773 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001774 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001775 return FAIL;
1776 }
1777 if (!varargs && argcount > type->tt_argcount)
1778 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001779 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001780 return FAIL;
1781 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001782 if (type->tt_args != NULL)
1783 {
1784 int i;
1785
1786 for (i = 0; i < argcount; ++i)
1787 {
1788 int offset = -argcount + i - 1;
1789 type_T *actual = ((type_T **)stack->ga_data)[
1790 stack->ga_len + offset];
1791 type_T *expected;
1792
1793 if (varargs && i >= type->tt_min_argcount - 1)
1794 expected = type->tt_args[
1795 type->tt_min_argcount - 1]->tt_member;
1796 else
1797 expected = type->tt_args[i];
1798 if (need_type(actual, expected, offset,
1799 cctx, TRUE, FALSE) == FAIL)
1800 {
1801 arg_type_mismatch(expected, actual, i + 1);
1802 return FAIL;
1803 }
1804 }
1805 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001806 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001807 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001808 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001809 else
1810 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001811 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001812 return FAIL;
1813 }
1814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1816 return FAIL;
1817 isn->isn_arg.pfunc.cpf_top = at_top;
1818 isn->isn_arg.pfunc.cpf_argcount = argcount;
1819
1820 stack->ga_len -= argcount; // drop the arguments
1821
1822 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001823 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001825 // If partial is above the arguments it must be cleared and replaced with
1826 // the return value.
1827 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1828 return FAIL;
1829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 return OK;
1831}
1832
1833/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001834 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 */
1836 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001837generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838{
1839 isn_T *isn;
1840 garray_T *stack = &cctx->ctx_type_stack;
1841 type_T *type;
1842
Bram Moolenaar080457c2020-03-03 21:53:32 +01001843 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001844 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001846 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001848 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001850 if (type->tt_type != VAR_DICT && type != &t_any)
1851 {
1852 emsg(_(e_dictreq));
1853 return FAIL;
1854 }
1855 // change dict type to dict member type
1856 if (type->tt_type == VAR_DICT)
1857 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858
1859 return OK;
1860}
1861
1862/*
1863 * Generate an ISN_ECHO instruction.
1864 */
1865 static int
1866generate_ECHO(cctx_T *cctx, int with_white, int count)
1867{
1868 isn_T *isn;
1869
Bram Moolenaar080457c2020-03-03 21:53:32 +01001870 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1872 return FAIL;
1873 isn->isn_arg.echo.echo_with_white = with_white;
1874 isn->isn_arg.echo.echo_count = count;
1875
1876 return OK;
1877}
1878
Bram Moolenaarad39c092020-02-26 18:23:43 +01001879/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001880 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001881 */
1882 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001883generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001884{
1885 isn_T *isn;
1886
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001887 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001888 return FAIL;
1889 isn->isn_arg.number = count;
1890
1891 return OK;
1892}
1893
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001894/*
1895 * Generate an ISN_PUT instruction.
1896 */
1897 static int
1898generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1899{
1900 isn_T *isn;
1901
1902 RETURN_OK_IF_SKIP(cctx);
1903 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1904 return FAIL;
1905 isn->isn_arg.put.put_regname = regname;
1906 isn->isn_arg.put.put_lnum = lnum;
1907 return OK;
1908}
1909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 static int
1911generate_EXEC(cctx_T *cctx, char_u *line)
1912{
1913 isn_T *isn;
1914
Bram Moolenaar080457c2020-03-03 21:53:32 +01001915 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1917 return FAIL;
1918 isn->isn_arg.string = vim_strsave(line);
1919 return OK;
1920}
1921
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001922 static int
1923generate_EXECCONCAT(cctx_T *cctx, int count)
1924{
1925 isn_T *isn;
1926
1927 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1928 return FAIL;
1929 isn->isn_arg.number = count;
1930 return OK;
1931}
1932
Bram Moolenaar08597872020-12-10 19:43:40 +01001933/*
1934 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1935 */
1936 static int
1937generate_RANGE(cctx_T *cctx, char_u *range)
1938{
1939 isn_T *isn;
1940 garray_T *stack = &cctx->ctx_type_stack;
1941
1942 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1943 return FAIL;
1944 isn->isn_arg.string = range;
1945
1946 if (ga_grow(stack, 1) == FAIL)
1947 return FAIL;
1948 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1949 ++stack->ga_len;
1950 return OK;
1951}
1952
Bram Moolenaar792f7862020-11-23 08:31:18 +01001953 static int
1954generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1955{
1956 isn_T *isn;
1957
1958 RETURN_OK_IF_SKIP(cctx);
1959 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1960 return FAIL;
1961 isn->isn_arg.unpack.unp_count = var_count;
1962 isn->isn_arg.unpack.unp_semicolon = semicolon;
1963 return OK;
1964}
1965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001967 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001968 */
1969 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001970generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001971{
1972 isn_T *isn;
1973
Bram Moolenaar02194d22020-10-24 23:08:38 +02001974 if (cmod->cmod_flags != 0
1975 || cmod->cmod_split != 0
1976 || cmod->cmod_verbose != 0
1977 || cmod->cmod_tab != 0
1978 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001979 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001980 cctx->ctx_has_cmdmod = TRUE;
1981
1982 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001983 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001984 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1985 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1986 return FAIL;
1987 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001988 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02001989 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001990 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001991
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001992 return OK;
1993}
1994
1995 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001996generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001997{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001998 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1999 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002000 return OK;
2001}
2002
2003/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002005 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002007 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002008reserve_local(
2009 cctx_T *cctx,
2010 char_u *name,
2011 size_t len,
2012 int isConst,
2013 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 lvar_T *lvar;
2016
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002017 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002019 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002020 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 }
2022
2023 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002024 return NULL;
2025 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002026 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002028 // Every local variable uses the next entry on the stack. We could re-use
2029 // the last ones when leaving a scope, but then variables used in a closure
2030 // might get overwritten. To keep things simple do not re-use stack
2031 // entries. This is less efficient, but memory is cheap these days.
2032 lvar->lv_idx = cctx->ctx_locals_count++;
2033
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002034 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 lvar->lv_const = isConst;
2036 lvar->lv_type = type;
2037
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002038 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039}
2040
2041/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002042 * Remove local variables above "new_top".
2043 */
2044 static void
2045unwind_locals(cctx_T *cctx, int new_top)
2046{
2047 if (cctx->ctx_locals.ga_len > new_top)
2048 {
2049 int idx;
2050 lvar_T *lvar;
2051
2052 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2053 {
2054 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2055 vim_free(lvar->lv_name);
2056 }
2057 }
2058 cctx->ctx_locals.ga_len = new_top;
2059}
2060
2061/*
2062 * Free all local variables.
2063 */
2064 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002065free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002066{
2067 unwind_locals(cctx, 0);
2068 ga_clear(&cctx->ctx_locals);
2069}
2070
2071/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 * Find "name" in script-local items of script "sid".
2073 * Returns the index in "sn_var_vals" if found.
2074 * If found but not in "sn_var_vals" returns -1.
2075 * If not found returns -2.
2076 */
2077 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002078get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079{
2080 hashtab_T *ht;
2081 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002082 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002083 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 int idx;
2085
Bram Moolenaare3d46852020-08-29 13:39:17 +02002086 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002088 if (sid == current_sctx.sc_sid)
2089 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002090 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002091
2092 if (sav == NULL)
2093 return -2;
2094 idx = sav->sav_var_vals_idx;
2095 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2096 if (check_writable && sv->sv_const)
2097 semsg(_(e_readonlyvar), name);
2098 return idx;
2099 }
2100
2101 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 ht = &SCRIPT_VARS(sid);
2103 di = find_var_in_ht(ht, 0, name, TRUE);
2104 if (di == NULL)
2105 return -2;
2106
2107 // Now find the svar_T index in sn_var_vals.
2108 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2109 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002110 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 if (sv->sv_tv == &di->di_tv)
2112 {
2113 if (check_writable && sv->sv_const)
2114 semsg(_(e_readonlyvar), name);
2115 return idx;
2116 }
2117 }
2118 return -1;
2119}
2120
2121/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002122 * Find "name" in imported items of the current script or in "cctx" if not
2123 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 */
2125 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002126find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 int idx;
2129
Bram Moolenaare3d46852020-08-29 13:39:17 +02002130 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002131 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 if (cctx != NULL)
2133 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2134 {
2135 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2136 + idx;
2137
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002138 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2139 : STRLEN(import->imp_name) == len
2140 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 return import;
2142 }
2143
Bram Moolenaarefa94442020-08-08 22:16:00 +02002144 return find_imported_in_script(name, len, current_sctx.sc_sid);
2145}
2146
2147 imported_T *
2148find_imported_in_script(char_u *name, size_t len, int sid)
2149{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002150 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002151 int idx;
2152
Bram Moolenaare3d46852020-08-29 13:39:17 +02002153 if (!SCRIPT_ID_VALID(sid))
2154 return NULL;
2155 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2157 {
2158 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2159
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002160 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2161 : STRLEN(import->imp_name) == len
2162 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 return import;
2164 }
2165 return NULL;
2166}
2167
2168/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002169 * Free all imported variables.
2170 */
2171 static void
2172free_imported(cctx_T *cctx)
2173{
2174 int idx;
2175
2176 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2177 {
2178 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2179
2180 vim_free(import->imp_name);
2181 }
2182 ga_clear(&cctx->ctx_imports);
2183}
2184
2185/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002186 * Return a pointer to the next line that isn't empty or only contains a
2187 * comment. Skips over white space.
2188 * Returns NULL if there is none.
2189 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002190 char_u *
2191peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002192{
2193 int lnum = cctx->ctx_lnum;
2194
2195 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2196 {
2197 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002198 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002199
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002200 // ignore NULLs inserted for continuation lines
2201 if (line != NULL)
2202 {
2203 p = skipwhite(line);
2204 if (*p != NUL && !vim9_comment_start(p))
2205 return p;
2206 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002207 }
2208 return NULL;
2209}
2210
2211/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002212 * Called when checking for a following operator at "arg". When the rest of
2213 * the line is empty or only a comment, peek the next line. If there is a next
2214 * line return a pointer to it and set "nextp".
2215 * Otherwise skip over white space.
2216 */
2217 static char_u *
2218may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2219{
2220 char_u *p = skipwhite(arg);
2221
2222 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002223 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002224 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002225 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002226 if (*nextp != NULL)
2227 return *nextp;
2228 }
2229 return p;
2230}
2231
2232/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002233 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002234 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002235 * Returns NULL when at the end.
2236 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002237 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002238next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002239{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002240 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002241
2242 do
2243 {
2244 ++cctx->ctx_lnum;
2245 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002246 {
2247 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002248 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002249 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002250 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002251 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002252 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002253 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002254 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002255 return line;
2256}
2257
2258/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002259 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002260 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002261 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002262 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2263 */
2264 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002265may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002266{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002267 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002268 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002269 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002270 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002271
2272 if (next == NULL)
2273 return FAIL;
2274 *arg = skipwhite(next);
2275 }
2276 return OK;
2277}
2278
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002279/*
2280 * Idem, and give an error when failed.
2281 */
2282 static int
2283may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2284{
2285 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2286 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002287 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002288 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002289 return FAIL;
2290 }
2291 return OK;
2292}
2293
2294
Bram Moolenaara5565e42020-05-09 15:44:01 +02002295// Structure passed between the compile_expr* functions to keep track of
2296// constants that have been parsed but for which no code was produced yet. If
2297// possible expressions on these constants are applied at compile time. If
2298// that is not possible, the code to push the constants needs to be generated
2299// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002300// Using 50 should be more than enough of 5 levels of ().
2301#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002302typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002303 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002304 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002305 int pp_is_const; // all generated code was constants, used for a
2306 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002307} ppconst_T;
2308
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002309static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002310static int compile_expr0(char_u **arg, cctx_T *cctx);
2311static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2312
Bram Moolenaara5565e42020-05-09 15:44:01 +02002313/*
2314 * Generate a PUSH instruction for "tv".
2315 * "tv" will be consumed or cleared.
2316 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2317 */
2318 static int
2319generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2320{
2321 if (tv != NULL)
2322 {
2323 switch (tv->v_type)
2324 {
2325 case VAR_UNKNOWN:
2326 break;
2327 case VAR_BOOL:
2328 generate_PUSHBOOL(cctx, tv->vval.v_number);
2329 break;
2330 case VAR_SPECIAL:
2331 generate_PUSHSPEC(cctx, tv->vval.v_number);
2332 break;
2333 case VAR_NUMBER:
2334 generate_PUSHNR(cctx, tv->vval.v_number);
2335 break;
2336#ifdef FEAT_FLOAT
2337 case VAR_FLOAT:
2338 generate_PUSHF(cctx, tv->vval.v_float);
2339 break;
2340#endif
2341 case VAR_BLOB:
2342 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2343 tv->vval.v_blob = NULL;
2344 break;
2345 case VAR_STRING:
2346 generate_PUSHS(cctx, tv->vval.v_string);
2347 tv->vval.v_string = NULL;
2348 break;
2349 default:
2350 iemsg("constant type not supported");
2351 clear_tv(tv);
2352 return FAIL;
2353 }
2354 tv->v_type = VAR_UNKNOWN;
2355 }
2356 return OK;
2357}
2358
2359/*
2360 * Generate code for any ppconst entries.
2361 */
2362 static int
2363generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2364{
2365 int i;
2366 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002367 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002368
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002369 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002370 for (i = 0; i < ppconst->pp_used; ++i)
2371 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2372 ret = FAIL;
2373 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002374 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002375 return ret;
2376}
2377
2378/*
2379 * Clear ppconst constants. Used when failing.
2380 */
2381 static void
2382clear_ppconst(ppconst_T *ppconst)
2383{
2384 int i;
2385
2386 for (i = 0; i < ppconst->pp_used; ++i)
2387 clear_tv(&ppconst->pp_tv[i]);
2388 ppconst->pp_used = 0;
2389}
2390
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002391/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002392 * Generate an instruction to load script-local variable "name", without the
2393 * leading "s:".
2394 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 */
2396 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002397compile_load_scriptvar(
2398 cctx_T *cctx,
2399 char_u *name, // variable NUL terminated
2400 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002401 char_u **end, // end of variable
2402 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002404 scriptitem_T *si;
2405 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 imported_T *import;
2407
Bram Moolenaare3d46852020-08-29 13:39:17 +02002408 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2409 return FAIL;
2410 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002411 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002412 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002414 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002415 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2416 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 }
2418 if (idx >= 0)
2419 {
2420 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2421
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002422 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 current_sctx.sc_sid, idx, sv->sv_type);
2424 return OK;
2425 }
2426
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002427 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 if (import != NULL)
2429 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002430 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002431 {
2432 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002433 char_u *exp_name;
2434 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002435 ufunc_T *ufunc;
2436 type_T *type;
2437
2438 // Used "import * as Name", need to lookup the member.
2439 if (*p != '.')
2440 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002441 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002442 return FAIL;
2443 }
2444 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002445 if (VIM_ISWHITE(*p))
2446 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002447 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002448 return FAIL;
2449 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002450
Bram Moolenaar1c991142020-07-04 13:15:31 +02002451 // isolate one name
2452 exp_name = p;
2453 while (eval_isnamec(*p))
2454 ++p;
2455 cc = *p;
2456 *p = NUL;
2457
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002458 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002459 *p = cc;
2460 p = skipwhite(p);
2461
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002462 // TODO: what if it is a function?
2463 if (idx < 0)
2464 return FAIL;
2465 *end = p;
2466
2467 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2468 import->imp_sid,
2469 idx,
2470 type);
2471 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002472 else if (import->imp_funcname != NULL)
2473 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002474 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002475 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2476 import->imp_sid,
2477 import->imp_var_vals_idx,
2478 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 return OK;
2480 }
2481
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002482 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002483 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 return FAIL;
2485}
2486
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002487 static int
2488generate_funcref(cctx_T *cctx, char_u *name)
2489{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002490 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002491
2492 if (ufunc == NULL)
2493 return FAIL;
2494
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002495 // Need to compile any default values to get the argument types.
2496 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2497 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2498 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002499 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002500}
2501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502/*
2503 * Compile a variable name into a load instruction.
2504 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002505 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 * When "error" is FALSE do not give an error when not found.
2507 */
2508 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002509compile_load(
2510 char_u **arg,
2511 char_u *end_arg,
2512 cctx_T *cctx,
2513 int is_expr,
2514 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515{
2516 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002517 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002518 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002520 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521
2522 if (*(*arg + 1) == ':')
2523 {
2524 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002525 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002526 {
2527 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002529 switch (**arg)
2530 {
2531 case 'g': isn_type = ISN_LOADGDICT; break;
2532 case 'w': isn_type = ISN_LOADWDICT; break;
2533 case 't': isn_type = ISN_LOADTDICT; break;
2534 case 'b': isn_type = ISN_LOADBDICT; break;
2535 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002536 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002537 goto theend;
2538 }
2539 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2540 goto theend;
2541 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 else
2544 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002545 isntype_T isn_type = ISN_DROP;
2546
2547 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2548 if (name == NULL)
2549 return FAIL;
2550
2551 switch (**arg)
2552 {
2553 case 'v': res = generate_LOADV(cctx, name, error);
2554 break;
2555 case 's': res = compile_load_scriptvar(cctx, name,
2556 NULL, NULL, error);
2557 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002558 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2559 isn_type = ISN_LOADG;
2560 else
2561 {
2562 isn_type = ISN_LOADAUTO;
2563 vim_free(name);
2564 name = vim_strnsave(*arg, end - *arg);
2565 if (name == NULL)
2566 return FAIL;
2567 }
2568 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002569 case 'w': isn_type = ISN_LOADW; break;
2570 case 't': isn_type = ISN_LOADT; break;
2571 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002572 default: // cannot happen, just in case
2573 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002574 goto theend;
2575 }
2576 if (isn_type != ISN_DROP)
2577 {
2578 // Global, Buffer-local, Window-local and Tabpage-local
2579 // variables can be defined later, thus we don't check if it
2580 // exists, give error at runtime.
2581 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 }
2584 }
2585 else
2586 {
2587 size_t len = end - *arg;
2588 int idx;
2589 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002590 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591
2592 name = vim_strnsave(*arg, end - *arg);
2593 if (name == NULL)
2594 return FAIL;
2595
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002596 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002598 if (!gen_load_outer)
2599 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 }
2601 else
2602 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002603 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002604
Bram Moolenaar709664c2020-12-12 14:33:41 +01002605 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002607 type = lvar.lv_type;
2608 idx = lvar.lv_idx;
2609 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002610 gen_load_outer = TRUE;
2611 else
2612 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 }
2614 else
2615 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002616 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002617 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002618 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002619 || find_imported(name, 0, cctx) != NULL)
2620 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002621
Bram Moolenaar0f769812020-09-12 18:32:34 +02002622 // When evaluating an expression and the name starts with an
2623 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002624 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002625 if (res == FAIL && is_expr
2626 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002627 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 }
2629 }
2630 if (gen_load)
2631 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002632 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002633 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002634 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002635 cctx->ctx_outer_used = TRUE;
2636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 }
2638
2639 *arg = end;
2640
2641theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002642 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002643 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 vim_free(name);
2645 return res;
2646}
2647
2648/*
2649 * Compile the argument expressions.
2650 * "arg" points to just after the "(" and is advanced to after the ")"
2651 */
2652 static int
2653compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2654{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002655 char_u *p = *arg;
2656 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002657 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658
Bram Moolenaare6085c52020-04-12 20:19:16 +02002659 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002661 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2662 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002663 if (*p == ')')
2664 {
2665 *arg = p + 1;
2666 return OK;
2667 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002668 if (must_end)
2669 {
2670 semsg(_(e_missing_comma_before_argument_str), p);
2671 return FAIL;
2672 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002673
Bram Moolenaara5565e42020-05-09 15:44:01 +02002674 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 return FAIL;
2676 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002677
2678 if (*p != ',' && *skipwhite(p) == ',')
2679 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002680 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002681 p = skipwhite(p);
2682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002684 {
2685 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002686 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002687 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002688 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002689 else
2690 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002691 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002692 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002694failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002695 emsg(_(e_missing_close));
2696 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697}
2698
2699/*
2700 * Compile a function call: name(arg1, arg2)
2701 * "arg" points to "name", "arg + varlen" to the "(".
2702 * "argcount_init" is 1 for "value->method()"
2703 * Instructions:
2704 * EVAL arg1
2705 * EVAL arg2
2706 * BCALL / DCALL / UCALL
2707 */
2708 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002709compile_call(
2710 char_u **arg,
2711 size_t varlen,
2712 cctx_T *cctx,
2713 ppconst_T *ppconst,
2714 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715{
2716 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002717 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 int argcount = argcount_init;
2719 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002720 char_u fname_buf[FLEN_FIXED + 1];
2721 char_u *tofree = NULL;
2722 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002723 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002724 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002725 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726
Bram Moolenaara5565e42020-05-09 15:44:01 +02002727 // we can evaluate "has('name')" at compile time
2728 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2729 {
2730 char_u *s = skipwhite(*arg + varlen + 1);
2731 typval_T argvars[2];
2732
2733 argvars[0].v_type = VAR_UNKNOWN;
2734 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002735 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002736 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002737 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002738 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002739 if (*s == ')' && argvars[0].v_type == VAR_STRING
2740 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002741 {
2742 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2743
2744 *arg = s + 1;
2745 argvars[1].v_type = VAR_UNKNOWN;
2746 tv->v_type = VAR_NUMBER;
2747 tv->vval.v_number = 0;
2748 f_has(argvars, tv);
2749 clear_tv(&argvars[0]);
2750 ++ppconst->pp_used;
2751 return OK;
2752 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002753 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002754 }
2755
2756 if (generate_ppconst(cctx, ppconst) == FAIL)
2757 return FAIL;
2758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 if (varlen >= sizeof(namebuf))
2760 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002761 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 return FAIL;
2763 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002764 vim_strncpy(namebuf, *arg, varlen);
2765 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766
2767 *arg = skipwhite(*arg + varlen + 1);
2768 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002769 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770
Bram Moolenaar03290b82020-12-19 16:30:44 +01002771 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002772 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 {
2774 int idx;
2775
2776 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002777 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002779 {
2780 if (STRCMP(name, "add") == 0 && argcount == 2)
2781 {
2782 garray_T *stack = &cctx->ctx_type_stack;
2783 type_T *type = ((type_T **)stack->ga_data)[
2784 stack->ga_len - 2];
2785
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002786 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002787 if (type->tt_type == VAR_LIST)
2788 {
2789 // inline "add(list, item)" so that the type can be checked
2790 res = generate_LISTAPPEND(cctx);
2791 idx = -1;
2792 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002793 else if (type->tt_type == VAR_BLOB)
2794 {
2795 // inline "add(blob, nr)" so that the type can be checked
2796 res = generate_BLOBAPPEND(cctx);
2797 idx = -1;
2798 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002799 }
2800
2801 if (idx >= 0)
2802 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2803 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002804 else
2805 semsg(_(e_unknownfunc), namebuf);
2806 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 }
2808
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002809 // An argument or local variable can be a function reference, this
2810 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002811 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002812 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002813 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002814 // If we can find the function by name generate the right call.
2815 // Skip global functions here, a local funcref takes precedence.
2816 ufunc = find_func(name, FALSE, cctx);
2817 if (ufunc != NULL && !func_is_global(ufunc))
2818 {
2819 res = generate_CALL(cctx, ufunc, argcount);
2820 goto theend;
2821 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823
2824 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002825 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002826 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002828 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002829 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002830 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002831 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002832 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002833
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002834 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002835 goto theend;
2836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837
Bram Moolenaar0f769812020-09-12 18:32:34 +02002838 // If we can find a global function by name generate the right call.
2839 if (ufunc != NULL)
2840 {
2841 res = generate_CALL(cctx, ufunc, argcount);
2842 goto theend;
2843 }
2844
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002845 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002846 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002847 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002848 res = generate_UCALL(cctx, name, argcount);
2849 else
2850 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002851
2852theend:
2853 vim_free(tofree);
2854 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855}
2856
2857// like NAMESPACE_CHAR but with 'a' and 'l'.
2858#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2859
2860/*
2861 * Find the end of a variable or function name. Unlike find_name_end() this
2862 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002863 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 * Return a pointer to just after the name. Equal to "arg" if there is no
2865 * valid name.
2866 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002867 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002868to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869{
2870 char_u *p;
2871
2872 // Quick check for valid starting character.
2873 if (!eval_isnamec1(*arg))
2874 return arg;
2875
2876 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2877 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2878 // and can be used in slice "[n:]".
2879 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002880 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2882 break;
2883 return p;
2884}
2885
2886/*
2887 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002888 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 */
2890 char_u *
2891to_name_const_end(char_u *arg)
2892{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002893 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 typval_T rettv;
2895
2896 if (p == arg && *arg == '[')
2897 {
2898
2899 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002900 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 p = arg;
2902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 return p;
2904}
2905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906/*
2907 * parse a list: [expr, expr]
2908 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002909 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 */
2911 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002912compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913{
2914 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002915 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002917 int is_const;
2918 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002920 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002922 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002923 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002924 semsg(_(e_list_end), *arg);
2925 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002926 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002927 if (*p == ',')
2928 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002929 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002930 return FAIL;
2931 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002932 if (*p == ']')
2933 {
2934 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002935 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002936 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002937 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002938 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002939 if (!is_const)
2940 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 ++count;
2942 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002943 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002945 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2946 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002947 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002948 return FAIL;
2949 }
2950 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002951 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 p = skipwhite(p);
2953 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002954 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002956 ppconst->pp_is_const = is_all_const;
2957 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958}
2959
2960/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002961 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01002963 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 */
2965 static int
2966compile_lambda(char_u **arg, cctx_T *cctx)
2967{
Bram Moolenaare462f522020-12-27 14:43:30 +01002968 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 typval_T rettv;
2970 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002971 evalarg_T evalarg;
2972
2973 CLEAR_FIELD(evalarg);
2974 evalarg.eval_flags = EVAL_EVALUATE;
2975 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976
2977 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01002978 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
2979 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002980 {
2981 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01002982 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002983 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002984
Bram Moolenaar65c44152020-12-24 15:14:01 +01002985 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002987 ++ufunc->uf_refcount;
2988 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989
Bram Moolenaar65c44152020-12-24 15:14:01 +01002990 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002991 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002993 clear_evalarg(&evalarg, NULL);
2994
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002995 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002996 {
2997 // The return type will now be known.
2998 set_function_type(ufunc);
2999
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003000 // The function reference count will be 1. When the ISN_FUNCREF
3001 // instruction is deleted the reference count is decremented and the
3002 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003003 return generate_FUNCREF(cctx, ufunc);
3004 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003005
3006 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 return FAIL;
3008}
3009
3010/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003011 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003013 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 */
3015 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003016compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017{
3018 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003019 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 int count = 0;
3021 dict_T *d = dict_alloc();
3022 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003023 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003024 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003025 int is_const;
3026 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027
3028 if (d == NULL)
3029 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003030 if (generate_ppconst(cctx, ppconst) == FAIL)
3031 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003032 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003034 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
Bram Moolenaar23c55272020-06-21 16:58:13 +02003036 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003037 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003038 *arg = NULL;
3039 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003040 }
3041
3042 if (**arg == '}')
3043 break;
3044
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003045 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003047 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003049 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003050 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003051 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3054 if (isn->isn_type == ISN_PUSHS)
3055 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003056 else
3057 {
3058 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003059 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003060 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003061 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003062 return FAIL;
3063 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003064 *arg = skipwhite(*arg);
3065 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003066 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003067 emsg(_(e_missing_matching_bracket_after_dict_key));
3068 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003069 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003070 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003072 else
3073 {
3074 // {"name": value},
3075 // {'name': value},
3076 // {name: value} use "name" as a literal key
3077 key = get_literal_key(arg);
3078 if (key == NULL)
3079 return FAIL;
3080 if (generate_PUSHS(cctx, key) == FAIL)
3081 return FAIL;
3082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083
3084 // Check for duplicate keys, if using string keys.
3085 if (key != NULL)
3086 {
3087 item = dict_find(d, key, -1);
3088 if (item != NULL)
3089 {
3090 semsg(_(e_duplicate_key), key);
3091 goto failret;
3092 }
3093 item = dictitem_alloc(key);
3094 if (item != NULL)
3095 {
3096 item->di_tv.v_type = VAR_UNKNOWN;
3097 item->di_tv.v_lock = 0;
3098 if (dict_add(d, item) == FAIL)
3099 dictitem_free(item);
3100 }
3101 }
3102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 if (**arg != ':')
3104 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003105 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003106 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003107 else
3108 semsg(_(e_missing_dict_colon), *arg);
3109 return FAIL;
3110 }
3111 whitep = *arg + 1;
3112 if (!IS_WHITE_OR_NUL(*whitep))
3113 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003114 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 return FAIL;
3116 }
3117
Bram Moolenaar23c55272020-06-21 16:58:13 +02003118 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003119 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003120 *arg = NULL;
3121 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 }
3123
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003124 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003126 if (!is_const)
3127 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 ++count;
3129
Bram Moolenaar2c330432020-04-13 14:41:35 +02003130 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003131 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003132 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003133 *arg = NULL;
3134 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 if (**arg == '}')
3137 break;
3138 if (**arg != ',')
3139 {
3140 semsg(_(e_missing_dict_comma), *arg);
3141 goto failret;
3142 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003143 if (IS_WHITE_OR_NUL(*whitep))
3144 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003145 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003146 return FAIL;
3147 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003148 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003149 if (!IS_WHITE_OR_NUL(*whitep))
3150 {
3151 semsg(_(e_white_space_required_after_str), ",");
3152 return FAIL;
3153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 *arg = skipwhite(*arg + 1);
3155 }
3156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 *arg = *arg + 1;
3158
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003159 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003160 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003161 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003162 *arg += STRLEN(*arg);
3163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003165 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 return generate_NEWDICT(cctx, count);
3167
3168failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003169 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003170 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003171 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003172 *arg = (char_u *)"";
3173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 dict_unref(d);
3175 return FAIL;
3176}
3177
3178/*
3179 * Compile "&option".
3180 */
3181 static int
3182compile_get_option(char_u **arg, cctx_T *cctx)
3183{
3184 typval_T rettv;
3185 char_u *start = *arg;
3186 int ret;
3187
3188 // parse the option and get the current value to get the type.
3189 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003190 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 if (ret == OK)
3192 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003193 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003194 char_u *name = vim_strnsave(start, *arg - start);
3195 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3196 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197
3198 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3199 vim_free(name);
3200 }
3201 clear_tv(&rettv);
3202
3203 return ret;
3204}
3205
3206/*
3207 * Compile "$VAR".
3208 */
3209 static int
3210compile_get_env(char_u **arg, cctx_T *cctx)
3211{
3212 char_u *start = *arg;
3213 int len;
3214 int ret;
3215 char_u *name;
3216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 ++*arg;
3218 len = get_env_len(arg);
3219 if (len == 0)
3220 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003221 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 return FAIL;
3223 }
3224
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003225 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 name = vim_strnsave(start, len + 1);
3227 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3228 vim_free(name);
3229 return ret;
3230}
3231
3232/*
3233 * Compile "@r".
3234 */
3235 static int
3236compile_get_register(char_u **arg, cctx_T *cctx)
3237{
3238 int ret;
3239
3240 ++*arg;
3241 if (**arg == NUL)
3242 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003243 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 return FAIL;
3245 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003246 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 {
3248 emsg_invreg(**arg);
3249 return FAIL;
3250 }
3251 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3252 ++*arg;
3253 return ret;
3254}
3255
3256/*
3257 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003258 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 */
3260 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003261apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003263 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264
3265 // this works from end to start
3266 while (p > start)
3267 {
3268 --p;
3269 if (*p == '-' || *p == '+')
3270 {
3271 // only '-' has an effect, for '+' we only check the type
3272#ifdef FEAT_FLOAT
3273 if (rettv->v_type == VAR_FLOAT)
3274 {
3275 if (*p == '-')
3276 rettv->vval.v_float = -rettv->vval.v_float;
3277 }
3278 else
3279#endif
3280 {
3281 varnumber_T val;
3282 int error = FALSE;
3283
3284 // tv_get_number_chk() accepts a string, but we don't want that
3285 // here
3286 if (check_not_string(rettv) == FAIL)
3287 return FAIL;
3288 val = tv_get_number_chk(rettv, &error);
3289 clear_tv(rettv);
3290 if (error)
3291 return FAIL;
3292 if (*p == '-')
3293 val = -val;
3294 rettv->v_type = VAR_NUMBER;
3295 rettv->vval.v_number = val;
3296 }
3297 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003298 else if (numeric_only)
3299 {
3300 ++p;
3301 break;
3302 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003303 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 {
3305 int v = tv2bool(rettv);
3306
3307 // '!' is permissive in the type.
3308 clear_tv(rettv);
3309 rettv->v_type = VAR_BOOL;
3310 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3311 }
3312 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003313 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 return OK;
3315}
3316
3317/*
3318 * Recognize v: variables that are constants and set "rettv".
3319 */
3320 static void
3321get_vim_constant(char_u **arg, typval_T *rettv)
3322{
3323 if (STRNCMP(*arg, "v:true", 6) == 0)
3324 {
3325 rettv->v_type = VAR_BOOL;
3326 rettv->vval.v_number = VVAL_TRUE;
3327 *arg += 6;
3328 }
3329 else if (STRNCMP(*arg, "v:false", 7) == 0)
3330 {
3331 rettv->v_type = VAR_BOOL;
3332 rettv->vval.v_number = VVAL_FALSE;
3333 *arg += 7;
3334 }
3335 else if (STRNCMP(*arg, "v:null", 6) == 0)
3336 {
3337 rettv->v_type = VAR_SPECIAL;
3338 rettv->vval.v_number = VVAL_NULL;
3339 *arg += 6;
3340 }
3341 else if (STRNCMP(*arg, "v:none", 6) == 0)
3342 {
3343 rettv->v_type = VAR_SPECIAL;
3344 rettv->vval.v_number = VVAL_NONE;
3345 *arg += 6;
3346 }
3347}
3348
Bram Moolenaar696ba232020-07-29 21:20:41 +02003349 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003350get_compare_type(char_u *p, int *len, int *type_is)
3351{
3352 exptype_T type = EXPR_UNKNOWN;
3353 int i;
3354
3355 switch (p[0])
3356 {
3357 case '=': if (p[1] == '=')
3358 type = EXPR_EQUAL;
3359 else if (p[1] == '~')
3360 type = EXPR_MATCH;
3361 break;
3362 case '!': if (p[1] == '=')
3363 type = EXPR_NEQUAL;
3364 else if (p[1] == '~')
3365 type = EXPR_NOMATCH;
3366 break;
3367 case '>': if (p[1] != '=')
3368 {
3369 type = EXPR_GREATER;
3370 *len = 1;
3371 }
3372 else
3373 type = EXPR_GEQUAL;
3374 break;
3375 case '<': if (p[1] != '=')
3376 {
3377 type = EXPR_SMALLER;
3378 *len = 1;
3379 }
3380 else
3381 type = EXPR_SEQUAL;
3382 break;
3383 case 'i': if (p[1] == 's')
3384 {
3385 // "is" and "isnot"; but not a prefix of a name
3386 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3387 *len = 5;
3388 i = p[*len];
3389 if (!isalnum(i) && i != '_')
3390 {
3391 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3392 *type_is = TRUE;
3393 }
3394 }
3395 break;
3396 }
3397 return type;
3398}
3399
3400/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003401 * Skip over an expression, ignoring most errors.
3402 */
3403 static void
3404skip_expr_cctx(char_u **arg, cctx_T *cctx)
3405{
3406 evalarg_T evalarg;
3407
3408 CLEAR_FIELD(evalarg);
3409 evalarg.eval_cctx = cctx;
3410 skip_expr(arg, &evalarg);
3411}
3412
3413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003415 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 */
3417 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003418compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003420 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421
3422 // this works from end to start
3423 while (p > start)
3424 {
3425 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003426 while (VIM_ISWHITE(*p))
3427 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 if (*p == '-' || *p == '+')
3429 {
3430 int negate = *p == '-';
3431 isn_T *isn;
3432
3433 // TODO: check type
3434 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3435 {
3436 --p;
3437 if (*p == '-')
3438 negate = !negate;
3439 }
3440 // only '-' has an effect, for '+' we only check the type
3441 if (negate)
3442 isn = generate_instr(cctx, ISN_NEGATENR);
3443 else
3444 isn = generate_instr(cctx, ISN_CHECKNR);
3445 if (isn == NULL)
3446 return FAIL;
3447 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003448 else if (numeric_only)
3449 {
3450 ++p;
3451 break;
3452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 else
3454 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003455 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003457 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003459 if (p[-1] == '!')
3460 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 }
3463 if (generate_2BOOL(cctx, invert) == FAIL)
3464 return FAIL;
3465 }
3466 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003467 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 return OK;
3469}
3470
3471/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003472 * Compile "(expression)": recursive!
3473 * Return FAIL/OK.
3474 */
3475 static int
3476compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3477{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003478 int ret;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003479
3480 *arg = skipwhite(*arg + 1);
3481 if (ppconst->pp_used <= PPSIZE - 10)
3482 {
3483 ret = compile_expr1(arg, cctx, ppconst);
3484 }
3485 else
3486 {
3487 // Not enough space in ppconst, flush constants.
3488 if (generate_ppconst(cctx, ppconst) == FAIL)
3489 return FAIL;
3490 ret = compile_expr0(arg, cctx);
3491 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003492 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3493 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003494 if (**arg == ')')
3495 ++*arg;
3496 else if (ret == OK)
3497 {
3498 emsg(_(e_missing_close));
3499 ret = FAIL;
3500 }
3501 return ret;
3502}
3503
3504/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003506 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 */
3508 static int
3509compile_subscript(
3510 char_u **arg,
3511 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003512 char_u *start_leader,
3513 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003514 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003516 char_u *name_start = *end_leader;
3517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 for (;;)
3519 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003520 char_u *p = skipwhite(*arg);
3521
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003522 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003523 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003524 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003525
3526 // If a following line starts with "->{" or "->X" advance to that
3527 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003528 // Also if a following line starts with ".x".
3529 if (next != NULL &&
3530 ((next[0] == '-' && next[1] == '>'
3531 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003532 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003533 {
3534 next = next_line_from_context(cctx, TRUE);
3535 if (next == NULL)
3536 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003537 *arg = next;
3538 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003539 }
3540 }
3541
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003542 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003543 // not a function call.
3544 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003546 garray_T *stack = &cctx->ctx_type_stack;
3547 type_T *type;
3548 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003550 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003551 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003552 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003555 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3556
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003557 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3559 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003560 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 return FAIL;
3562 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003563 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003565 char_u *pstart = p;
3566
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003567 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003568 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003569 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 // something->method()
3572 // Apply the '!', '-' and '+' first:
3573 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003574 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003577 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003578 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003579 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003580 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003581 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003582 int argcount = 1;
3583 char_u *expr;
3584 garray_T *stack;
3585 type_T *type;
3586
3587 // Funcref call: list->(Refs[2])(arg)
3588 // or lambda: list->((arg) => expr)(arg)
3589 // Fist compile the arguments.
3590 expr = *arg;
3591 *arg = skipwhite(*arg + 1);
3592 skip_expr_cctx(arg, cctx);
3593 *arg = skipwhite(*arg);
3594 if (**arg != ')')
3595 {
3596 semsg(_(e_missing_paren), *arg);
3597 return FAIL;
3598 }
3599 ++*arg;
3600 if (**arg != '(')
3601 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003602 if (*skipwhite(*arg) == '(')
3603 emsg(_(e_nowhitespace));
3604 else
3605 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003606 return FAIL;
3607 }
3608
3609 *arg = skipwhite(*arg + 1);
3610 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3611 return FAIL;
3612
3613 // Compile the function expression.
3614 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3615 return FAIL;
3616
3617 stack = &cctx->ctx_type_stack;
3618 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3619 if (generate_PCALL(cctx, argcount,
3620 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 return FAIL;
3622 }
3623 else
3624 {
3625 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003626 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003627 if (!eval_isnamec1(*p))
3628 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003629 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003630 return FAIL;
3631 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003632 if (ASCII_ISALPHA(*p) && p[1] == ':')
3633 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003634 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 ;
3636 if (*p != '(')
3637 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003638 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 return FAIL;
3640 }
3641 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003642 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 return FAIL;
3644 }
3645 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003646 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003648 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003649 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003650 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003651 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003652 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003653
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003654 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003655 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003656 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003657 // TODO: blob index
3658 // TODO: more arguments
3659 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003660 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003661 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003662 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003663
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003664 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003665 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003666 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003667 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003668 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003669 // missing first index is equal to zero
3670 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003671 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003672 else
3673 {
3674 if (compile_expr0(arg, cctx) == FAIL)
3675 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003676 if (**arg == ':')
3677 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003678 semsg(_(e_white_space_required_before_and_after_str_at_str),
3679 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003680 return FAIL;
3681 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003682 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003683 return FAIL;
3684 *arg = skipwhite(*arg);
3685 }
3686 if (**arg == ':')
3687 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003688 is_slice = TRUE;
3689 ++*arg;
3690 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3691 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003692 semsg(_(e_white_space_required_before_and_after_str_at_str),
3693 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003694 return FAIL;
3695 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003696 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003697 return FAIL;
3698 if (**arg == ']')
3699 // missing second index is equal to end of string
3700 generate_PUSHNR(cctx, -1);
3701 else
3702 {
3703 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003704 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003705 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003706 return FAIL;
3707 *arg = skipwhite(*arg);
3708 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710
3711 if (**arg != ']')
3712 {
3713 emsg(_(e_missbrac));
3714 return FAIL;
3715 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003716 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003718 // We can index a list and a dict. If we don't know the type
3719 // we can use the index value type.
3720 // TODO: If we don't know use an instruction to figure it out at
3721 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003722 typep = ((type_T **)stack->ga_data) + stack->ga_len
3723 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003724 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003725 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3726 // If the index is a string, the variable must be a Dict.
3727 if (*typep == &t_any && valtype == &t_string)
3728 vtype = VAR_DICT;
3729 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003730 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003731 if (need_type(valtype, &t_number, -1, cctx,
3732 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003733 return FAIL;
3734 if (is_slice)
3735 {
3736 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003737 if (need_type(valtype, &t_number, -2, cctx,
3738 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003739 return FAIL;
3740 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003741 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003742
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003743 if (vtype == VAR_DICT)
3744 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003745 if (is_slice)
3746 {
3747 emsg(_(e_cannot_slice_dictionary));
3748 return FAIL;
3749 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003750 if ((*typep)->tt_type == VAR_DICT)
3751 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003752 else
3753 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003754 if (need_type(*typep, &t_dict_any, -2, cctx,
3755 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003756 return FAIL;
3757 *typep = &t_any;
3758 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003759 if (may_generate_2STRING(-1, cctx) == FAIL)
3760 return FAIL;
3761 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3762 return FAIL;
3763 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003764 else if (vtype == VAR_STRING)
3765 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003766 *typep = &t_string;
3767 if ((is_slice
3768 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3769 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003770 return FAIL;
3771 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003772 else if (vtype == VAR_BLOB)
3773 {
3774 emsg("Sorry, blob index and slice not implemented yet");
3775 return FAIL;
3776 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003777 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003778 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003779 if (is_slice)
3780 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003781 if (generate_instr_drop(cctx,
3782 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3783 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003784 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003785 }
Bram Moolenaared591872020-08-15 22:14:53 +02003786 else
3787 {
3788 if ((*typep)->tt_type == VAR_LIST)
3789 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003790 if (generate_instr_drop(cctx,
3791 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3792 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003793 return FAIL;
3794 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003795 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003796 else
3797 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003798 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003799 return FAIL;
3800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003802 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003804 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003805 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003806 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003807 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003808
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003809 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003810 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003811 {
3812 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003813 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003814 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003815 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003816 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 while (eval_isnamec(*p))
3818 MB_PTR_ADV(p);
3819 if (p == *arg)
3820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003821 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 return FAIL;
3823 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003824 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 return FAIL;
3826 *arg = p;
3827 }
3828 else
3829 break;
3830 }
3831
3832 // TODO - see handle_subscript():
3833 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3834 // Don't do this when "Func" is already a partial that was bound
3835 // explicitly (pt_auto is FALSE).
3836
3837 return OK;
3838}
3839
3840/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003841 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3842 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003844 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003845 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003846 *
3847 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 */
3849
3850/*
3851 * number number constant
3852 * 0zFFFFFFFF Blob constant
3853 * "string" string constant
3854 * 'string' literal string constant
3855 * &option-name option value
3856 * @r register contents
3857 * identifier variable value
3858 * function() function call
3859 * $VAR environment variable
3860 * (expression) nested expression
3861 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003862 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 *
3864 * Also handle:
3865 * ! in front logical NOT
3866 * - in front unary minus
3867 * + in front unary plus (ignored)
3868 * trailing (arg) funcref/partial call
3869 * trailing [] subscript in String or List
3870 * trailing .name entry in Dictionary
3871 * trailing ->name() method call
3872 */
3873 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003874compile_expr7(
3875 char_u **arg,
3876 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003877 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 char_u *start_leader, *end_leader;
3880 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003881 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003882 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003884 ppconst->pp_is_const = FALSE;
3885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 /*
3887 * Skip '!', '-' and '+' characters. They are handled later.
3888 */
3889 start_leader = *arg;
3890 while (**arg == '!' || **arg == '-' || **arg == '+')
3891 *arg = skipwhite(*arg + 1);
3892 end_leader = *arg;
3893
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003894 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 switch (**arg)
3896 {
3897 /*
3898 * Number constant.
3899 */
3900 case '0': // also for blob starting with 0z
3901 case '1':
3902 case '2':
3903 case '3':
3904 case '4':
3905 case '5':
3906 case '6':
3907 case '7':
3908 case '8':
3909 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003910 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003912 // Apply "-" and "+" just before the number now, right to
3913 // left. Matters especially when "->" follows. Stops at
3914 // '!'.
3915 if (apply_leader(rettv, TRUE,
3916 start_leader, &end_leader) == FAIL)
3917 {
3918 clear_tv(rettv);
3919 return FAIL;
3920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 break;
3922
3923 /*
3924 * String constant: "string".
3925 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003926 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 return FAIL;
3928 break;
3929
3930 /*
3931 * Literal string constant: 'str''ing'.
3932 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003933 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 return FAIL;
3935 break;
3936
3937 /*
3938 * Constant Vim variable.
3939 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003940 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 ret = NOTDONE;
3942 break;
3943
3944 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 * "true" constant
3946 */
3947 case 't': if (STRNCMP(*arg, "true", 4) == 0
3948 && !eval_isnamec((*arg)[4]))
3949 {
3950 *arg += 4;
3951 rettv->v_type = VAR_BOOL;
3952 rettv->vval.v_number = VVAL_TRUE;
3953 }
3954 else
3955 ret = NOTDONE;
3956 break;
3957
3958 /*
3959 * "false" constant
3960 */
3961 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3962 && !eval_isnamec((*arg)[5]))
3963 {
3964 *arg += 5;
3965 rettv->v_type = VAR_BOOL;
3966 rettv->vval.v_number = VVAL_FALSE;
3967 }
3968 else
3969 ret = NOTDONE;
3970 break;
3971
3972 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01003973 * "null" constant
3974 */
3975 case 'n': if (STRNCMP(*arg, "null", 4) == 0
3976 && !eval_isnamec((*arg)[5]))
3977 {
3978 *arg += 4;
3979 rettv->v_type = VAR_SPECIAL;
3980 rettv->vval.v_number = VVAL_NULL;
3981 }
3982 else
3983 ret = NOTDONE;
3984 break;
3985
3986 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 * List: [expr, expr]
3988 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003989 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 break;
3991
3992 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 * Dictionary: {'key': val, 'key': val}
3994 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003995 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 break;
3997
3998 /*
3999 * Option value: &name
4000 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004001 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4002 return FAIL;
4003 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 break;
4005
4006 /*
4007 * Environment variable: $VAR.
4008 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004009 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4010 return FAIL;
4011 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 break;
4013
4014 /*
4015 * Register contents: @r.
4016 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004017 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4018 return FAIL;
4019 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 break;
4021 /*
4022 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004023 * lambda: (arg, arg) => expr
4024 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004026 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4027 ret = compile_lambda(arg, cctx);
4028 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004029 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 break;
4031
4032 default: ret = NOTDONE;
4033 break;
4034 }
4035 if (ret == FAIL)
4036 return FAIL;
4037
Bram Moolenaar1c747212020-05-09 18:28:34 +02004038 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004040 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004041 clear_tv(rettv);
4042 else
4043 // A constant expression can possibly be handled compile time,
4044 // return the value instead of generating code.
4045 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 }
4047 else if (ret == NOTDONE)
4048 {
4049 char_u *p;
4050 int r;
4051
4052 if (!eval_isnamec1(**arg))
4053 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004054 if (ends_excmd(*skipwhite(*arg)))
4055 semsg(_(e_empty_expression_str), *arg);
4056 else
4057 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return FAIL;
4059 }
4060
4061 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004062 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 {
4065 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004068 {
4069 if (generate_ppconst(cctx, ppconst) == FAIL)
4070 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004071 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 if (r == FAIL)
4074 return FAIL;
4075 }
4076
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004077 // Handle following "[]", ".member", etc.
4078 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004079 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 ppconst) == FAIL)
4081 return FAIL;
4082 if (ppconst->pp_used > 0)
4083 {
4084 // apply the '!', '-' and '+' before the constant
4085 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004086 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004087 return FAIL;
4088 return OK;
4089 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004090 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004092 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093}
4094
4095/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004096 * Give the "white on both sides" error, taking the operator from "p[len]".
4097 */
4098 void
4099error_white_both(char_u *op, int len)
4100{
4101 char_u buf[10];
4102
4103 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004104 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004105}
4106
4107/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004108 * <type>expr7: runtime type check / conversion
4109 */
4110 static int
4111compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4112{
4113 type_T *want_type = NULL;
4114
4115 // Recognize <type>
4116 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4117 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004118 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004119 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4120 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004121 return FAIL;
4122
4123 if (**arg != '>')
4124 {
4125 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004126 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004127 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004128 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004129 return FAIL;
4130 }
4131 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004132 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004133 return FAIL;
4134 }
4135
4136 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4137 return FAIL;
4138
4139 if (want_type != NULL)
4140 {
4141 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004142 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004143
Bram Moolenaard1103582020-08-14 22:44:25 +02004144 generate_ppconst(cctx, ppconst);
4145 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004146 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004147 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004148 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004149 return FAIL;
4150 }
4151 }
4152
4153 return OK;
4154}
4155
4156/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 * * number multiplication
4158 * / number division
4159 * % number modulo
4160 */
4161 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163{
4164 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004165 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004166 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004168 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004169 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 return FAIL;
4171
4172 /*
4173 * Repeat computing, until no "*", "/" or "%" is following.
4174 */
4175 for (;;)
4176 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004177 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 if (*op != '*' && *op != '/' && *op != '%')
4179 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004180 if (next != NULL)
4181 {
4182 *arg = next_line_from_context(cctx, TRUE);
4183 op = skipwhite(*arg);
4184 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004185
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004186 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004188 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004189 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004191 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004192 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004194 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004195 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004197
4198 if (ppconst->pp_used == ppconst_used + 2
4199 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4200 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004201 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004202 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4203 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004204 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004206 // both are numbers: compute the result
4207 switch (*op)
4208 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004209 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004210 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004211 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004212 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004213 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004214 break;
4215 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004216 tv1->vval.v_number = res;
4217 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218 }
4219 else
4220 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004221 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004222 generate_two_op(cctx, op);
4223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 }
4225
4226 return OK;
4227}
4228
4229/*
4230 * + number addition
4231 * - number subtraction
4232 * .. string concatenation
4233 */
4234 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236{
4237 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004238 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004240 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241
4242 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004243 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 return FAIL;
4245
4246 /*
4247 * Repeat computing, until no "+", "-" or ".." is following.
4248 */
4249 for (;;)
4250 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004251 op = may_peek_next_line(cctx, *arg, &next);
4252 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 break;
4254 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004255 if (next != NULL)
4256 {
4257 *arg = next_line_from_context(cctx, TRUE);
4258 op = skipwhite(*arg);
4259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004261 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004263 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004264 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 }
4266
Bram Moolenaare0de1712020-12-02 17:36:54 +01004267 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004268 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004270 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004271 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 return FAIL;
4273
Bram Moolenaara5565e42020-05-09 15:44:01 +02004274 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004275 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004276 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4277 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4278 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4279 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004281 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4282 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004283
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004284 // concat/subtract/add constant numbers
4285 if (*op == '+')
4286 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4287 else if (*op == '-')
4288 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4289 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004290 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004291 // concatenate constant strings
4292 char_u *s1 = tv1->vval.v_string;
4293 char_u *s2 = tv2->vval.v_string;
4294 size_t len1 = STRLEN(s1);
4295
4296 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4297 if (tv1->vval.v_string == NULL)
4298 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004299 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004300 return FAIL;
4301 }
4302 mch_memmove(tv1->vval.v_string, s1, len1);
4303 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004304 vim_free(s1);
4305 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004307 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 }
4309 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004310 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004312 if (*op == '.')
4313 {
4314 if (may_generate_2STRING(-2, cctx) == FAIL
4315 || may_generate_2STRING(-1, cctx) == FAIL)
4316 return FAIL;
4317 generate_instr_drop(cctx, ISN_CONCAT, 1);
4318 }
4319 else
4320 generate_two_op(cctx, op);
4321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 }
4323
4324 return OK;
4325}
4326
4327/*
4328 * expr5a == expr5b
4329 * expr5a =~ expr5b
4330 * expr5a != expr5b
4331 * expr5a !~ expr5b
4332 * expr5a > expr5b
4333 * expr5a >= expr5b
4334 * expr5a < expr5b
4335 * expr5a <= expr5b
4336 * expr5a is expr5b
4337 * expr5a isnot expr5b
4338 *
4339 * Produces instructions:
4340 * EVAL expr5a Push result of "expr5a"
4341 * EVAL expr5b Push result of "expr5b"
4342 * COMPARE one of the compare instructions
4343 */
4344 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004345compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346{
4347 exptype_T type = EXPR_UNKNOWN;
4348 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004349 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353
4354 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004355 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 return FAIL;
4357
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004358 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004359 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360
4361 /*
4362 * If there is a comparative operator, use it.
4363 */
4364 if (type != EXPR_UNKNOWN)
4365 {
4366 int ic = FALSE; // Default: do not ignore case
4367
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004368 if (next != NULL)
4369 {
4370 *arg = next_line_from_context(cctx, TRUE);
4371 p = skipwhite(*arg);
4372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 if (type_is && (p[len] == '?' || p[len] == '#'))
4374 {
4375 semsg(_(e_invexpr2), *arg);
4376 return FAIL;
4377 }
4378 // extra question mark appended: ignore case
4379 if (p[len] == '?')
4380 {
4381 ic = TRUE;
4382 ++len;
4383 }
4384 // extra '#' appended: match case (ignored)
4385 else if (p[len] == '#')
4386 ++len;
4387 // nothing appended: match case
4388
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004389 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004391 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004392 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 }
4394
4395 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004396 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004397 return FAIL;
4398
Bram Moolenaara5565e42020-05-09 15:44:01 +02004399 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 return FAIL;
4401
Bram Moolenaara5565e42020-05-09 15:44:01 +02004402 if (ppconst->pp_used == ppconst_used + 2)
4403 {
4404 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4405 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4406 int ret;
4407
4408 // Both sides are a constant, compute the result now.
4409 // First check for a valid combination of types, this is more
4410 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004411 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412 ret = FAIL;
4413 else
4414 {
4415 ret = typval_compare(tv1, tv2, type, ic);
4416 tv1->v_type = VAR_BOOL;
4417 tv1->vval.v_number = tv1->vval.v_number
4418 ? VVAL_TRUE : VVAL_FALSE;
4419 clear_tv(tv2);
4420 --ppconst->pp_used;
4421 }
4422 return ret;
4423 }
4424
4425 generate_ppconst(cctx, ppconst);
4426 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 }
4428
4429 return OK;
4430}
4431
Bram Moolenaar7f141552020-05-09 17:35:53 +02004432static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434/*
4435 * Compile || or &&.
4436 */
4437 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004438compile_and_or(
4439 char_u **arg,
4440 cctx_T *cctx,
4441 char *op,
4442 ppconst_T *ppconst,
4443 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004445 char_u *next;
4446 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 int opchar = *op;
4448
4449 if (p[0] == opchar && p[1] == opchar)
4450 {
4451 garray_T *instr = &cctx->ctx_instr;
4452 garray_T end_ga;
4453
4454 /*
4455 * Repeat until there is no following "||" or "&&"
4456 */
4457 ga_init2(&end_ga, sizeof(int), 10);
4458 while (p[0] == opchar && p[1] == opchar)
4459 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004460 if (next != NULL)
4461 {
4462 *arg = next_line_from_context(cctx, TRUE);
4463 p = skipwhite(*arg);
4464 }
4465
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004466 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4467 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004468 semsg(_(e_white_space_required_before_and_after_str_at_str),
4469 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004470 return FAIL;
4471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004473 // TODO: use ppconst if the value is a constant and check
4474 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004475 generate_ppconst(cctx, ppconst);
4476
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004477 // Every part must evaluate to a bool.
4478 if (bool_on_stack(cctx) == FAIL)
4479 {
4480 ga_clear(&end_ga);
4481 return FAIL;
4482 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 if (ga_grow(&end_ga, 1) == FAIL)
4485 {
4486 ga_clear(&end_ga);
4487 return FAIL;
4488 }
4489 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4490 ++end_ga.ga_len;
4491 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004492 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493
4494 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004495 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004496 {
4497 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004499 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004500
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4502 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 {
4504 ga_clear(&end_ga);
4505 return FAIL;
4506 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004507
4508 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004512 // Every part must evaluate to a bool.
4513 if (bool_on_stack(cctx) == FAIL)
4514 {
4515 ga_clear(&end_ga);
4516 return FAIL;
4517 }
4518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 // Fill in the end label in all jumps.
4520 while (end_ga.ga_len > 0)
4521 {
4522 isn_T *isn;
4523
4524 --end_ga.ga_len;
4525 isn = ((isn_T *)instr->ga_data)
4526 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4527 isn->isn_arg.jump.jump_where = instr->ga_len;
4528 }
4529 ga_clear(&end_ga);
4530 }
4531
4532 return OK;
4533}
4534
4535/*
4536 * expr4a && expr4a && expr4a logical AND
4537 *
4538 * Produces instructions:
4539 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004540 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004541 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004543 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 * EVAL expr4c Push result of "expr4c"
4545 * end:
4546 */
4547 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004548compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 int ppconst_used = ppconst->pp_used;
4551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004553 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 return FAIL;
4555
4556 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558}
4559
4560/*
4561 * expr3a || expr3b || expr3c logical OR
4562 *
4563 * Produces instructions:
4564 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004565 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004566 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004568 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 * EVAL expr3c Push result of "expr3c"
4570 * end:
4571 */
4572 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 int ppconst_used = ppconst->pp_used;
4576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 return FAIL;
4580
4581 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583}
4584
4585/*
4586 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004588 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 * JUMP_IF_FALSE alt jump if false
4590 * EVAL expr1a
4591 * JUMP_ALWAYS end
4592 * alt: EVAL expr1b
4593 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004594 *
4595 * Toplevel expression: expr2 ?? expr1
4596 * Produces instructions:
4597 * EVAL expr2 Push result of "expr2"
4598 * JUMP_AND_KEEP_IF_TRUE end jump if true
4599 * EVAL expr1
4600 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 */
4602 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004603compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604{
4605 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004606 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004607 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608
Bram Moolenaar3988f642020-08-27 22:43:03 +02004609 // Ignore all kinds of errors when not producing code.
4610 if (cctx->ctx_skip == SKIP_YES)
4611 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004612 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004613 return OK;
4614 }
4615
Bram Moolenaar61a89812020-05-07 16:58:17 +02004616 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 return FAIL;
4619
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004620 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 if (*p == '?')
4622 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004623 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 garray_T *instr = &cctx->ctx_instr;
4625 garray_T *stack = &cctx->ctx_type_stack;
4626 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004627 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004629 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004630 int has_const_expr = FALSE;
4631 int const_value = FALSE;
4632 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004634 if (next != NULL)
4635 {
4636 *arg = next_line_from_context(cctx, TRUE);
4637 p = skipwhite(*arg);
4638 }
4639
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004640 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004641 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004642 semsg(_(e_white_space_required_before_and_after_str_at_str),
4643 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004644 return FAIL;
4645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647 if (ppconst->pp_used == ppconst_used + 1)
4648 {
4649 // the condition is a constant, we know whether the ? or the :
4650 // expression is to be evaluated.
4651 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004652 if (op_falsy)
4653 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4654 else
4655 {
4656 int error = FALSE;
4657
4658 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4659 &error);
4660 if (error)
4661 return FAIL;
4662 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004663 cctx->ctx_skip = save_skip == SKIP_YES ||
4664 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4665
4666 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4667 // "left ?? right" and "left" is truthy: produce "left"
4668 generate_ppconst(cctx, ppconst);
4669 else
4670 {
4671 clear_tv(&ppconst->pp_tv[ppconst_used]);
4672 --ppconst->pp_used;
4673 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004674 }
4675 else
4676 {
4677 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004678 if (op_falsy)
4679 end_idx = instr->ga_len;
4680 generate_JUMP(cctx, op_falsy
4681 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4682 if (op_falsy)
4683 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685
4686 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004687 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004688 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004689 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 if (!has_const_expr)
4693 {
4694 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004696 if (!op_falsy)
4697 {
4698 // remember the type and drop it
4699 --stack->ga_len;
4700 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004702 end_idx = instr->ga_len;
4703 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004704
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004705 // jump here from JUMP_IF_FALSE
4706 isn = ((isn_T *)instr->ga_data) + alt_idx;
4707 isn->isn_arg.jump.jump_where = instr->ga_len;
4708 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004711 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004713 // Check for the ":".
4714 p = may_peek_next_line(cctx, *arg, &next);
4715 if (*p != ':')
4716 {
4717 emsg(_(e_missing_colon));
4718 return FAIL;
4719 }
4720 if (next != NULL)
4721 {
4722 *arg = next_line_from_context(cctx, TRUE);
4723 p = skipwhite(*arg);
4724 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004725
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004726 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4727 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004728 semsg(_(e_white_space_required_before_and_after_str_at_str),
4729 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004730 return FAIL;
4731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004733 // evaluate the third expression
4734 if (has_const_expr)
4735 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004736 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004737 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004738 return FAIL;
4739 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4740 return FAIL;
4741 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742
Bram Moolenaara5565e42020-05-09 15:44:01 +02004743 if (!has_const_expr)
4744 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004745 type_T **typep;
4746
Bram Moolenaara5565e42020-05-09 15:44:01 +02004747 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748
Bram Moolenaara5565e42020-05-09 15:44:01 +02004749 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004750 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4751 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004752
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004753 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 isn = ((isn_T *)instr->ga_data) + end_idx;
4755 isn->isn_arg.jump.jump_where = instr->ga_len;
4756 }
4757
4758 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 }
4760 return OK;
4761}
4762
4763/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004764 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004765 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4766 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 */
4768 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004769compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770{
4771 ppconst_T ppconst;
4772
4773 CLEAR_FIELD(ppconst);
4774 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4775 {
4776 clear_ppconst(&ppconst);
4777 return FAIL;
4778 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004779 if (is_const != NULL)
4780 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004781 if (generate_ppconst(cctx, &ppconst) == FAIL)
4782 return FAIL;
4783 return OK;
4784}
4785
4786/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004787 * Toplevel expression.
4788 */
4789 static int
4790compile_expr0(char_u **arg, cctx_T *cctx)
4791{
4792 return compile_expr0_ext(arg, cctx, NULL);
4793}
4794
4795/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 * compile "return [expr]"
4797 */
4798 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004799compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800{
4801 char_u *p = arg;
4802 garray_T *stack = &cctx->ctx_type_stack;
4803 type_T *stack_type;
4804
4805 if (*p != NUL && *p != '|' && *p != '\n')
4806 {
4807 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004808 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 return NULL;
4810
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004811 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004812 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004813 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004814 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
4815 || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004816 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004817 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004818 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004819 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004820 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004821 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4822 && stack_type->tt_type != VAR_VOID
4823 && stack_type->tt_type != VAR_UNKNOWN)
4824 {
4825 emsg(_(e_returning_value_in_function_without_return_type));
4826 return NULL;
4827 }
4828 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004829 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004830 return NULL;
4831 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 }
4834 else
4835 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004836 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004837 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004838 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4839 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004841 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 return NULL;
4843 }
4844
4845 // No argument, return zero.
4846 generate_PUSHNR(cctx, 0);
4847 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004848 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 return NULL;
4850
4851 // "return val | endif" is possible
4852 return skipwhite(p);
4853}
4854
4855/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004856 * Get a line from the compilation context, compatible with exarg_T getline().
4857 * Return a pointer to the line in allocated memory.
4858 * Return NULL for end-of-file or some error.
4859 */
4860 static char_u *
4861exarg_getline(
4862 int c UNUSED,
4863 void *cookie,
4864 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004865 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004866{
4867 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004868 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004869
Bram Moolenaar66250c92020-08-20 15:02:42 +02004870 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004871 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004872 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004873 return NULL;
4874 ++cctx->ctx_lnum;
4875 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4876 // Comment lines result in NULL pointers, skip them.
4877 if (p != NULL)
4878 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004879 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880}
4881
4882/*
4883 * Compile a nested :def command.
4884 */
4885 static char_u *
4886compile_nested_function(exarg_T *eap, cctx_T *cctx)
4887{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004888 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004889 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004890 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004891 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004892 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004893 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004894
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004895 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004896 {
4897 emsg(_(e_cannot_use_bang_with_nested_def));
4898 return NULL;
4899 }
4900
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004901 if (*name_start == '/')
4902 {
4903 name_end = skip_regexp(name_start + 1, '/', TRUE);
4904 if (*name_end == '/')
4905 ++name_end;
4906 eap->nextcmd = check_nextcmd(name_end);
4907 }
4908 if (name_end == name_start || *skipwhite(name_end) != '(')
4909 {
4910 if (!ends_excmd2(name_start, name_end))
4911 {
4912 semsg(_(e_invalid_command_str), eap->cmd);
4913 return NULL;
4914 }
4915
4916 // "def" or "def Name": list functions
4917 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4918 return NULL;
4919 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4920 }
4921
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004922 // Only g:Func() can use a namespace.
4923 if (name_start[1] == ':' && !is_global)
4924 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004925 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004926 return NULL;
4927 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004928 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4929 return NULL;
4930
Bram Moolenaar04b12692020-05-04 23:24:44 +02004931 eap->arg = name_end;
4932 eap->getline = exarg_getline;
4933 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004934 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004935 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004936 lambda_name = vim_strsave(get_lambda_name());
4937 if (lambda_name == NULL)
4938 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004939 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004940
Bram Moolenaar822ba242020-05-24 23:00:18 +02004941 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004942 {
4943 r = eap->skip ? OK : FAIL;
4944 goto theend;
4945 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004946 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004947 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004948 {
4949 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004950 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004951 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004952
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004953 if (is_global)
4954 {
4955 char_u *func_name = vim_strnsave(name_start + 2,
4956 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004957
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004958 if (func_name == NULL)
4959 r = FAIL;
4960 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004961 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004962 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004963 lambda_name = NULL;
4964 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004965 }
4966 else
4967 {
4968 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004969 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004970 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004971 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004972
Bram Moolenaareef21022020-08-01 22:16:43 +02004973 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004974 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004975 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004976 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004977 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004978
4979 // copy over the block scope IDs
4980 if (block_depth > 0)
4981 {
4982 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4983 if (ufunc->uf_block_ids != NULL)
4984 {
4985 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4986 sizeof(int) * block_depth);
4987 ufunc->uf_block_depth = block_depth;
4988 }
4989 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004990 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02004991 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004992 r = OK;
4993
4994theend:
4995 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004996 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004997}
4998
4999/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 * Return the length of an assignment operator, or zero if there isn't one.
5001 */
5002 int
5003assignment_len(char_u *p, int *heredoc)
5004{
5005 if (*p == '=')
5006 {
5007 if (p[1] == '<' && p[2] == '<')
5008 {
5009 *heredoc = TRUE;
5010 return 3;
5011 }
5012 return 1;
5013 }
5014 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5015 return 2;
5016 if (STRNCMP(p, "..=", 3) == 0)
5017 return 3;
5018 return 0;
5019}
5020
5021// words that cannot be used as a variable
5022static char *reserved[] = {
5023 "true",
5024 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005025 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 NULL
5027};
5028
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005029typedef enum {
5030 dest_local,
5031 dest_option,
5032 dest_env,
5033 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005034 dest_buffer,
5035 dest_window,
5036 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005037 dest_vimvar,
5038 dest_script,
5039 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005040 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005041} assign_dest_T;
5042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005044 * Generate the load instruction for "name".
5045 */
5046 static void
5047generate_loadvar(
5048 cctx_T *cctx,
5049 assign_dest_T dest,
5050 char_u *name,
5051 lvar_T *lvar,
5052 type_T *type)
5053{
5054 switch (dest)
5055 {
5056 case dest_option:
5057 // TODO: check the option exists
5058 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5059 break;
5060 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005061 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5062 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5063 else
5064 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005065 break;
5066 case dest_buffer:
5067 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5068 break;
5069 case dest_window:
5070 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5071 break;
5072 case dest_tab:
5073 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5074 break;
5075 case dest_script:
5076 compile_load_scriptvar(cctx,
5077 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5078 break;
5079 case dest_env:
5080 // Include $ in the name here
5081 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5082 break;
5083 case dest_reg:
5084 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5085 break;
5086 case dest_vimvar:
5087 generate_LOADV(cctx, name + 2, TRUE);
5088 break;
5089 case dest_local:
5090 if (lvar->lv_from_outer)
5091 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5092 NULL, type);
5093 else
5094 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5095 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005096 case dest_expr:
5097 // list or dict value should already be on the stack.
5098 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005099 }
5100}
5101
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005102/*
5103 * Skip over "[expr]" or ".member".
5104 * Does not check for any errors.
5105 */
5106 static char_u *
5107skip_index(char_u *start)
5108{
5109 char_u *p = start;
5110
5111 if (*p == '[')
5112 {
5113 p = skipwhite(p + 1);
5114 (void)skip_expr(&p, NULL);
5115 p = skipwhite(p);
5116 if (*p == ']')
5117 return p + 1;
5118 return p;
5119 }
5120 // if (*p == '.')
5121 return to_name_end(p + 1, TRUE);
5122}
5123
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005124 void
5125vim9_declare_error(char_u *name)
5126{
5127 char *scope = "";
5128
5129 switch (*name)
5130 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005131 case 'g': scope = _("global"); break;
5132 case 'b': scope = _("buffer"); break;
5133 case 'w': scope = _("window"); break;
5134 case 't': scope = _("tab"); break;
5135 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005136 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005137 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005138 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005139 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005140 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005141 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005142 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005143 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005144 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005145}
5146
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005147/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005148 * For one assignment figure out the type of destination. Return it in "dest".
5149 * When not recognized "dest" is not set.
5150 * For an option "opt_flags" is set.
5151 * For a v:var "vimvaridx" is set.
5152 * "type" is set to the destination type if known, unchanted otherwise.
5153 * Return FAIL if an error message was given.
5154 */
5155 static int
5156get_var_dest(
5157 char_u *name,
5158 assign_dest_T *dest,
5159 int cmdidx,
5160 int *opt_flags,
5161 int *vimvaridx,
5162 type_T **type,
5163 cctx_T *cctx)
5164{
5165 char_u *p;
5166
5167 if (*name == '&')
5168 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005169 int cc;
5170 long numval;
5171 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005172
5173 *dest = dest_option;
5174 if (cmdidx == CMD_final || cmdidx == CMD_const)
5175 {
5176 emsg(_(e_const_option));
5177 return FAIL;
5178 }
5179 p = name;
5180 p = find_option_end(&p, opt_flags);
5181 if (p == NULL)
5182 {
5183 // cannot happen?
5184 emsg(_(e_letunexp));
5185 return FAIL;
5186 }
5187 cc = *p;
5188 *p = NUL;
5189 opt_type = get_option_value(skip_option_env_lead(name),
5190 &numval, NULL, *opt_flags);
5191 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005192 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005193 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005194 case gov_unknown:
5195 semsg(_(e_unknown_option), name);
5196 return FAIL;
5197 case gov_string:
5198 case gov_hidden_string:
5199 *type = &t_string;
5200 break;
5201 case gov_bool:
5202 case gov_hidden_bool:
5203 *type = &t_bool;
5204 break;
5205 case gov_number:
5206 case gov_hidden_number:
5207 *type = &t_number;
5208 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005209 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005210 }
5211 else if (*name == '$')
5212 {
5213 *dest = dest_env;
5214 *type = &t_string;
5215 }
5216 else if (*name == '@')
5217 {
5218 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5219 {
5220 emsg_invreg(name[1]);
5221 return FAIL;
5222 }
5223 *dest = dest_reg;
5224 *type = &t_string;
5225 }
5226 else if (STRNCMP(name, "g:", 2) == 0)
5227 {
5228 *dest = dest_global;
5229 }
5230 else if (STRNCMP(name, "b:", 2) == 0)
5231 {
5232 *dest = dest_buffer;
5233 }
5234 else if (STRNCMP(name, "w:", 2) == 0)
5235 {
5236 *dest = dest_window;
5237 }
5238 else if (STRNCMP(name, "t:", 2) == 0)
5239 {
5240 *dest = dest_tab;
5241 }
5242 else if (STRNCMP(name, "v:", 2) == 0)
5243 {
5244 typval_T *vtv;
5245 int di_flags;
5246
5247 *vimvaridx = find_vim_var(name + 2, &di_flags);
5248 if (*vimvaridx < 0)
5249 {
5250 semsg(_(e_variable_not_found_str), name);
5251 return FAIL;
5252 }
5253 // We use the current value of "sandbox" here, is that OK?
5254 if (var_check_ro(di_flags, name, FALSE))
5255 return FAIL;
5256 *dest = dest_vimvar;
5257 vtv = get_vim_var_tv(*vimvaridx);
5258 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5259 }
5260 return OK;
5261}
5262
5263/*
5264 * Generate a STORE instruction for "dest", not being "dest_local".
5265 * Return FAIL when out of memory.
5266 */
5267 static int
5268generate_store_var(
5269 cctx_T *cctx,
5270 assign_dest_T dest,
5271 int opt_flags,
5272 int vimvaridx,
5273 int scriptvar_idx,
5274 int scriptvar_sid,
5275 type_T *type,
5276 char_u *name)
5277{
5278 switch (dest)
5279 {
5280 case dest_option:
5281 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5282 opt_flags);
5283 case dest_global:
5284 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005285 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5286 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005287 case dest_buffer:
5288 // include b: with the name, easier to execute that way
5289 return generate_STORE(cctx, ISN_STOREB, 0, name);
5290 case dest_window:
5291 // include w: with the name, easier to execute that way
5292 return generate_STORE(cctx, ISN_STOREW, 0, name);
5293 case dest_tab:
5294 // include t: with the name, easier to execute that way
5295 return generate_STORE(cctx, ISN_STORET, 0, name);
5296 case dest_env:
5297 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5298 case dest_reg:
5299 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5300 case dest_vimvar:
5301 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5302 case dest_script:
5303 if (scriptvar_idx < 0)
5304 {
5305 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005306 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005307
Bram Moolenaar41d61962020-12-06 20:12:43 +01005308 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005309 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5310 scriptvar_sid, type);
5311 if (name_s != name)
5312 vim_free(name_s);
5313 return r;
5314 }
5315 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5316 scriptvar_sid, scriptvar_idx, type);
5317 case dest_local:
5318 case dest_expr:
5319 // cannot happen
5320 break;
5321 }
5322 return FAIL;
5323}
5324
5325/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005326 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005327 * "let name"
5328 * "var name = expr"
5329 * "final name = expr"
5330 * "const name = expr"
5331 * "name = expr"
5332 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 * Return NULL for an error.
5334 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 */
5336 static char_u *
5337compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5338{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005339 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 char_u *ret = NULL;
5343 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005344 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005345 int scriptvar_sid = 0;
5346 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005349 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 int oplen = 0;
5352 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005353 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005354 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005355 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005356 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005358 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5359 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 // Skip over the "var" or "[var, var]" to get to any "=".
5362 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5363 if (p == NULL)
5364 return *arg == '[' ? arg : NULL;
5365
5366 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005368 // TODO: should we allow this, and figure out type inference from list
5369 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005370 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 return NULL;
5372 }
5373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 sp = p;
5375 p = skipwhite(p);
5376 op = p;
5377 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378
5379 if (var_count > 0 && oplen == 0)
5380 // can be something like "[1, 2]->func()"
5381 return arg;
5382
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005383 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005385 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005387 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 if (heredoc)
5390 {
5391 list_T *l;
5392 listitem_T *li;
5393
5394 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005395 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005397 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005398 if (l == NULL)
5399 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400
Bram Moolenaar078269b2020-09-21 20:35:55 +02005401 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005403 // Push each line and the create the list.
5404 FOR_ALL_LIST_ITEMS(l, li)
5405 {
5406 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5407 li->li_tv.vval.v_string = NULL;
5408 }
5409 generate_NEWLIST(cctx, l->lv_len);
5410 type = &t_list_string;
5411 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 list_free(l);
5414 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005417 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005419 char_u *wp;
5420
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005421 // for "[var, var] = expr" evaluate the expression here, loop over the
5422 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005423 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005424
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005425 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005426 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005427 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 if (compile_expr0(&p, cctx) == FAIL)
5429 return NULL;
5430 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005432 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005434 type_T *stacktype;
5435
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005436 stacktype = stack->ga_len == 0 ? &t_void
5437 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005440 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005441 goto theend;
5442 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005443 if (need_type(stacktype, &t_list_any, -1, cctx,
5444 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005445 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005446 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005447 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5448 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005449 if (stacktype->tt_member != NULL)
5450 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005451 }
5452 }
5453
5454 /*
5455 * Loop over variables in "[var, var] = expr".
5456 * For "var = expr" and "let var: type" this is done only once.
5457 */
5458 if (var_count > 0)
5459 var_start = skipwhite(arg + 1); // skip over the "["
5460 else
5461 var_start = arg;
5462 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5463 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005464 char_u *var_end;
5465 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 size_t varlen;
5467 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005468 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005469 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005470 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005471 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005472 lvar_T *lvar = NULL;
5473 lvar_T arg_lvar;
5474 int has_type = FALSE;
5475 int has_index = FALSE;
5476 int instr_count = -1;
5477
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005478 // "dest_end" is the end of the destination, including "[expr]" or
5479 // ".name".
5480 // "var_end" is the end of the variable/option/etc. name.
5481 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005482 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005483 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005484 else
5485 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005486 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005487 var_end = skip_option_env_lead(var_start);
5488 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005489 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005490
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005491 // "a: type" is declaring variable "a" with a type, not dict "a:".
5492 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5493 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5495 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005496
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005497 // compute the length of the destination without "[expr]" or ".name"
5498 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 vim_free(name);
5500 name = vim_strnsave(var_start, varlen);
5501 if (name == NULL)
5502 return NULL;
5503 if (!heredoc)
5504 type = &t_any;
5505
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005506 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005507 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005508 int declare_error = FALSE;
5509
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005510 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5511 &vimvaridx, &type, cctx) == FAIL)
5512 goto theend;
5513 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005514 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005515 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005516 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 }
5518 else
5519 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005520 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005522 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005523 for (idx = 0; reserved[idx] != NULL; ++idx)
5524 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005525 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005526 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005527 goto theend;
5528 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005529
Bram Moolenaar709664c2020-12-12 14:33:41 +01005530
5531 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5532 lvar = &local_lvar;
5533 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005534 {
5535 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005536 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005537 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5538 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005539 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005540 if (is_decl)
5541 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005542 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 goto theend;
5544 }
5545 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005546 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005547 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005548 if (lvar != NULL)
5549 {
5550 if (is_decl)
5551 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005552 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005553 goto theend;
5554 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005556 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005557 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005558 int script_namespace = varlen > 1
5559 && STRNCMP(var_start, "s:", 2) == 0;
5560 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005561 ? script_var_exists(var_start + 2, varlen - 2,
5562 FALSE, cctx)
5563 : script_var_exists(var_start, varlen,
5564 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005565 imported_T *import =
5566 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005567
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005568 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005569 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005570 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5571
5572 if (is_decl)
5573 {
5574 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005575 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005576 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005577 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005578 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005579 name);
5580 goto theend;
5581 }
5582 else if (cctx->ctx_ufunc->uf_script_ctx_version
5583 == SCRIPT_VERSION_VIM9
5584 && script_namespace
5585 && !script_var && import == NULL)
5586 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005587 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005588 goto theend;
5589 }
5590
5591 dest = dest_script;
5592
5593 // existing script-local variables should have a type
5594 scriptvar_sid = current_sctx.sc_sid;
5595 if (import != NULL)
5596 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005597 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005598 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005599 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005600 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005601 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005602 {
5603 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5604 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005605 ((svar_T *)si->sn_var_vals.ga_data)
5606 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005607 type = sv->sv_type;
5608 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005609 }
5610 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005611 else if (check_defined(var_start, varlen, cctx) == FAIL)
5612 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005615
5616 if (declare_error)
5617 {
5618 vim9_declare_error(name);
5619 goto theend;
5620 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005621 }
5622
5623 // handle "a:name" as a name, not index "name" on "a"
5624 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005625 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005626
5627 if (dest != dest_option)
5628 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005629 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005630 {
5631 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005632 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005633 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005634 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005635 goto theend;
5636 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005637 p = skipwhite(var_end + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005638 type = parse_type(&p, cctx->ctx_type_list, TRUE);
5639 if (type == NULL)
5640 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005641 has_type = TRUE;
5642 }
5643 else if (lvar != NULL)
5644 type = lvar->lv_type;
5645 }
5646
5647 if (oplen == 3 && !heredoc && dest != dest_global
5648 && type->tt_type != VAR_STRING
5649 && type->tt_type != VAR_ANY)
5650 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005651 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005652 goto theend;
5653 }
5654
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005655 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005656 {
5657 if (oplen > 1 && !heredoc)
5658 {
5659 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005660 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005661 goto theend;
5662 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01005663 if (!is_decl)
5664 {
5665 semsg(_(e_unknown_variable_str), name);
5666 goto theend;
5667 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005668
5669 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005670 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5671 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005672 goto theend;
5673 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005674 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005675 if (lvar == NULL)
5676 goto theend;
5677 new_local = TRUE;
5678 }
5679
5680 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005681 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005682 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005683 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005684 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685 if (is_decl)
5686 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005687 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005688 goto theend;
5689 }
5690
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005691 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005692 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005693 char_u *after = var_start + varlen;
5694
5695 // Only the last index is used below, if there are others
5696 // before it generate code for the expression. Thus for
5697 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5698 for (;;)
5699 {
5700 p = skip_index(after);
5701 if (*p != '[' && *p != '.')
5702 break;
5703 after = p;
5704 }
5705 if (after > var_start + varlen)
5706 {
5707 varlen = after - var_start;
5708 dest = dest_expr;
5709 // We don't know the type before evaluating the expression,
5710 // use "any" until then.
5711 type = &t_any;
5712 }
5713
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005714 has_index = TRUE;
5715 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005716 member_type = &t_any;
5717 else
5718 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005719 }
5720 else
5721 {
5722 semsg("Not supported yet: %s", var_start);
5723 goto theend;
5724 }
5725 }
5726 else if (lvar == &arg_lvar)
5727 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005728 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005729 goto theend;
5730 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005731 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5732 {
5733 semsg(_(e_cannot_assign_to_constant), name);
5734 goto theend;
5735 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005736
5737 if (!heredoc)
5738 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005739 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005740 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005741 if (oplen > 0 && var_count == 0)
5742 {
5743 // skip over the "=" and the expression
5744 p = skipwhite(op + oplen);
5745 compile_expr0(&p, cctx);
5746 }
5747 }
5748 else if (oplen > 0)
5749 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005750 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005751 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005752
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005753 // For "var = expr" evaluate the expression.
5754 if (var_count == 0)
5755 {
5756 int r;
5757
5758 // for "+=", "*=", "..=" etc. first load the current value
5759 if (*op != '=')
5760 {
5761 generate_loadvar(cctx, dest, name, lvar, type);
5762
5763 if (has_index)
5764 {
5765 // TODO: get member from list or dict
5766 emsg("Index with operation not supported yet");
5767 goto theend;
5768 }
5769 }
5770
5771 // Compile the expression. Temporarily hide the new local
5772 // variable here, it is not available to this expression.
5773 if (new_local)
5774 --cctx->ctx_locals.ga_len;
5775 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005776 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005777 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005778 {
5779 if (new_local)
5780 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005781 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005782 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005783 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005784 if (new_local)
5785 ++cctx->ctx_locals.ga_len;
5786 if (r == FAIL)
5787 goto theend;
5788 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005789 else if (semicolon && var_idx == var_count - 1)
5790 {
5791 // For "[var; var] = expr" get the rest of the list
5792 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5793 goto theend;
5794 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005795 else
5796 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005797 // For "[var, var] = expr" get the "var_idx" item from the
5798 // list.
5799 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005800 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005801 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005802
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005803 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005804 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005805 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005806 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005807 if ((rhs_type->tt_type == VAR_FUNC
5808 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005809 && var_wrong_func_name(name, TRUE))
5810 goto theend;
5811
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005812 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005813 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005814 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005815 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005816 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005817 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005818 }
5819 else
5820 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005821 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005822 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005823 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005824 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005825 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005826 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005827 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005828 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005829 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005830 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005831 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005832 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005833 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005834 {
5835 type_T *use_type = lvar->lv_type;
5836
Bram Moolenaar71abe482020-09-14 22:28:30 +02005837 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005838 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005839 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005840 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005841 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005842 goto theend;
5843 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005844 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005845 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005846 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005847 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005849 else if (cmdidx == CMD_final)
5850 {
5851 emsg(_(e_final_requires_a_value));
5852 goto theend;
5853 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005854 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005855 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005856 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005857 goto theend;
5858 }
5859 else if (!has_type || dest == dest_option)
5860 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005861 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005862 goto theend;
5863 }
5864 else
5865 {
5866 // variables are always initialized
5867 if (ga_grow(instr, 1) == FAIL)
5868 goto theend;
5869 switch (member_type->tt_type)
5870 {
5871 case VAR_BOOL:
5872 generate_PUSHBOOL(cctx, VVAL_FALSE);
5873 break;
5874 case VAR_FLOAT:
5875#ifdef FEAT_FLOAT
5876 generate_PUSHF(cctx, 0.0);
5877#endif
5878 break;
5879 case VAR_STRING:
5880 generate_PUSHS(cctx, NULL);
5881 break;
5882 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005883 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005884 break;
5885 case VAR_FUNC:
5886 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5887 break;
5888 case VAR_LIST:
5889 generate_NEWLIST(cctx, 0);
5890 break;
5891 case VAR_DICT:
5892 generate_NEWDICT(cctx, 0);
5893 break;
5894 case VAR_JOB:
5895 generate_PUSHJOB(cctx, NULL);
5896 break;
5897 case VAR_CHANNEL:
5898 generate_PUSHCHANNEL(cctx, NULL);
5899 break;
5900 case VAR_NUMBER:
5901 case VAR_UNKNOWN:
5902 case VAR_ANY:
5903 case VAR_PARTIAL:
5904 case VAR_VOID:
5905 case VAR_SPECIAL: // cannot happen
5906 generate_PUSHNR(cctx, 0);
5907 break;
5908 }
5909 }
5910 if (var_count == 0)
5911 end = p;
5912 }
5913
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005914 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005915 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005916 break;
5917
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005918 if (oplen > 0 && *op != '=')
5919 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005920 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005921 type_T *stacktype;
5922
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005923 if (*op == '.')
5924 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005925 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005926 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005927 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005928 if (
5929#ifdef FEAT_FLOAT
5930 // If variable is float operation with number is OK.
5931 !(expected == &t_float && stacktype == &t_number) &&
5932#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005933 need_type(stacktype, expected, -1, cctx,
5934 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005935 goto theend;
5936
5937 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005938 {
5939 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5940 goto theend;
5941 }
5942 else if (*op == '+')
5943 {
5944 if (generate_add_instr(cctx,
5945 operator_type(member_type, stacktype),
5946 member_type, stacktype) == FAIL)
5947 goto theend;
5948 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005949 else if (generate_two_op(cctx, op) == FAIL)
5950 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005953 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005954 {
Bram Moolenaard24602f2020-12-20 15:20:56 +01005955 int r;
5956 vartype_T dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005957
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005958 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005959 p = var_start + varlen;
5960 if (*p == '[')
5961 {
5962 p = skipwhite(p + 1);
5963 r = compile_expr0(&p, cctx);
5964 if (r == OK && *skipwhite(p) != ']')
5965 {
5966 // this should not happen
5967 emsg(_(e_missbrac));
5968 r = FAIL;
5969 }
5970 }
5971 else // if (*p == '.')
5972 {
5973 char_u *key_end = to_name_end(p + 1, TRUE);
5974 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5975
5976 r = generate_PUSHS(cctx, key);
5977 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005978 if (r == FAIL)
5979 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005980
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005981 if (type == &t_any)
5982 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005983 // Index on variable of unknown type: check at runtime.
5984 dest_type = VAR_ANY;
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005985 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005986 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005987 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005988 dest_type = type->tt_type;
5989 if (dest_type == VAR_DICT
5990 && may_generate_2STRING(-1, cctx) == FAIL)
5991 goto theend;
5992 if (dest_type == VAR_LIST
5993 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5994 != VAR_NUMBER)
5995 {
5996 emsg(_(e_number_exp));
5997 goto theend;
5998 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005999 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006000
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006001 // Load the dict or list. On the stack we then have:
6002 // - value
6003 // - index
6004 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006005 if (dest == dest_expr)
6006 {
6007 int c = var_start[varlen];
6008
6009 // Evaluate "ll[expr]" of "ll[expr][idx]"
6010 p = var_start;
6011 var_start[varlen] = NUL;
6012 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6013 {
6014 // this should not happen
6015 emsg(_(e_missbrac));
6016 goto theend;
6017 }
6018 var_start[varlen] = c;
6019
6020 type = stack->ga_len == 0 ? &t_void
6021 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6022 // now we can properly check the type
6023 if (type->tt_member != NULL
6024 && need_type(rhs_type, type->tt_member, -2, cctx,
6025 FALSE, FALSE) == FAIL)
6026 goto theend;
6027 }
6028 else
6029 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006030
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006031 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6032 || dest_type == VAR_ANY)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006033 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006034 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6035
6036 if (isn == NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006037 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006038 isn->isn_arg.vartype = dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006039 }
6040 else
6041 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006042 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006043 goto theend;
6044 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006045 }
6046 else
6047 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006048 if (is_decl && cmdidx == CMD_const
6049 && (dest == dest_script || dest == dest_local))
6050 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006051 generate_LOCKCONST(cctx);
6052
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006053 if (is_decl
6054 && (type->tt_type == VAR_DICT || type->tt_type == VAR_LIST)
6055 && type->tt_member != NULL
6056 && type->tt_member != &t_any
6057 && type->tt_member != &t_unknown)
6058 // Set the type in the list or dict, so that it can be checked,
6059 // also in legacy script.
6060 generate_SETTYPE(cctx, type);
6061
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006062 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006063 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006064 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6065 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
6066 goto theend;
6067 }
6068 else if (lvar != NULL)
6069 {
6070 isn_T *isn = ((isn_T *)instr->ga_data)
6071 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006072
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006073 // optimization: turn "var = 123" from ISN_PUSHNR +
6074 // ISN_STORE into ISN_STORENR
6075 if (!lvar->lv_from_outer
6076 && instr->ga_len == instr_count + 1
6077 && isn->isn_type == ISN_PUSHNR)
6078 {
6079 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006080
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006081 isn->isn_type = ISN_STORENR;
6082 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
6083 isn->isn_arg.storenr.stnr_val = val;
6084 if (stack->ga_len > 0)
6085 --stack->ga_len;
6086 }
6087 else if (lvar->lv_from_outer)
6088 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
6089 else
6090 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006091 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006092 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006093
6094 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006095 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006097
6098 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006099 if (var_count > 0 && !semicolon)
6100 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006101 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006102 goto theend;
6103 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006104
Bram Moolenaarb2097502020-07-19 17:17:02 +02006105 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106
6107theend:
6108 vim_free(name);
6109 return ret;
6110}
6111
6112/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006113 * Check if "name" can be "unlet".
6114 */
6115 int
6116check_vim9_unlet(char_u *name)
6117{
6118 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6119 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006120 // "unlet s:var" is allowed in legacy script.
6121 if (*name == 's' && !script_is_vim9())
6122 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006123 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006124 return FAIL;
6125 }
6126 return OK;
6127}
6128
6129/*
6130 * Callback passed to ex_unletlock().
6131 */
6132 static int
6133compile_unlet(
6134 lval_T *lvp,
6135 char_u *name_end,
6136 exarg_T *eap,
6137 int deep UNUSED,
6138 void *coookie)
6139{
6140 cctx_T *cctx = coookie;
6141
6142 if (lvp->ll_tv == NULL)
6143 {
6144 char_u *p = lvp->ll_name;
6145 int cc = *name_end;
6146 int ret = OK;
6147
6148 // Normal name. Only supports g:, w:, t: and b: namespaces.
6149 *name_end = NUL;
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006150 if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6151 {
6152 *name_end = cc;
6153 goto failed;
6154 }
6155
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006156 if (*p == '$')
6157 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6158 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006159 ret = FAIL;
6160 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006161 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006162
6163 *name_end = cc;
6164 return ret;
6165 }
6166
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006167failed:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006168 // TODO: unlet {list}[idx]
6169 // TODO: unlet {dict}[key]
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006170 // complication: {list} can be global while "idx" is local, thus we can't
6171 // call ex_unlet().
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006172 emsg("Sorry, :unlet not fully implemented yet");
6173 return FAIL;
6174}
6175
6176/*
6177 * compile "unlet var", "lock var" and "unlock var"
6178 * "arg" points to "var".
6179 */
6180 static char_u *
6181compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6182{
6183 char_u *p = arg;
6184
6185 if (eap->cmdidx != CMD_unlet)
6186 {
6187 emsg("Sorry, :lock and unlock not implemented yet");
6188 return NULL;
6189 }
6190
Bram Moolenaar3862ea32021-01-01 21:05:55 +01006191 // TODO: this doesn't work for local variables
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006192 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6193 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006194 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6195}
6196
6197/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198 * Compile an :import command.
6199 */
6200 static char_u *
6201compile_import(char_u *arg, cctx_T *cctx)
6202{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006203 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204}
6205
6206/*
6207 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6208 */
6209 static int
6210compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6211{
6212 garray_T *instr = &cctx->ctx_instr;
6213 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6214
6215 if (endlabel == NULL)
6216 return FAIL;
6217 endlabel->el_next = *el;
6218 *el = endlabel;
6219 endlabel->el_end_label = instr->ga_len;
6220
6221 generate_JUMP(cctx, when, 0);
6222 return OK;
6223}
6224
6225 static void
6226compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6227{
6228 garray_T *instr = &cctx->ctx_instr;
6229
6230 while (*el != NULL)
6231 {
6232 endlabel_T *cur = (*el);
6233 isn_T *isn;
6234
6235 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6236 isn->isn_arg.jump.jump_where = instr->ga_len;
6237 *el = cur->el_next;
6238 vim_free(cur);
6239 }
6240}
6241
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006242 static void
6243compile_free_jump_to_end(endlabel_T **el)
6244{
6245 while (*el != NULL)
6246 {
6247 endlabel_T *cur = (*el);
6248
6249 *el = cur->el_next;
6250 vim_free(cur);
6251 }
6252}
6253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254/*
6255 * Create a new scope and set up the generic items.
6256 */
6257 static scope_T *
6258new_scope(cctx_T *cctx, scopetype_T type)
6259{
6260 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6261
6262 if (scope == NULL)
6263 return NULL;
6264 scope->se_outer = cctx->ctx_scope;
6265 cctx->ctx_scope = scope;
6266 scope->se_type = type;
6267 scope->se_local_count = cctx->ctx_locals.ga_len;
6268 return scope;
6269}
6270
6271/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006272 * Free the current scope and go back to the outer scope.
6273 */
6274 static void
6275drop_scope(cctx_T *cctx)
6276{
6277 scope_T *scope = cctx->ctx_scope;
6278
6279 if (scope == NULL)
6280 {
6281 iemsg("calling drop_scope() without a scope");
6282 return;
6283 }
6284 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006285 switch (scope->se_type)
6286 {
6287 case IF_SCOPE:
6288 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6289 case FOR_SCOPE:
6290 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6291 case WHILE_SCOPE:
6292 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6293 case TRY_SCOPE:
6294 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6295 case NO_SCOPE:
6296 case BLOCK_SCOPE:
6297 break;
6298 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006299 vim_free(scope);
6300}
6301
6302/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 * compile "if expr"
6304 *
6305 * "if expr" Produces instructions:
6306 * EVAL expr Push result of "expr"
6307 * JUMP_IF_FALSE end
6308 * ... body ...
6309 * end:
6310 *
6311 * "if expr | else" Produces instructions:
6312 * EVAL expr Push result of "expr"
6313 * JUMP_IF_FALSE else
6314 * ... body ...
6315 * JUMP_ALWAYS end
6316 * else:
6317 * ... body ...
6318 * end:
6319 *
6320 * "if expr1 | elseif expr2 | else" Produces instructions:
6321 * EVAL expr Push result of "expr"
6322 * JUMP_IF_FALSE elseif
6323 * ... body ...
6324 * JUMP_ALWAYS end
6325 * elseif:
6326 * EVAL expr Push result of "expr"
6327 * JUMP_IF_FALSE else
6328 * ... body ...
6329 * JUMP_ALWAYS end
6330 * else:
6331 * ... body ...
6332 * end:
6333 */
6334 static char_u *
6335compile_if(char_u *arg, cctx_T *cctx)
6336{
6337 char_u *p = arg;
6338 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006339 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006341 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006342 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343
Bram Moolenaara5565e42020-05-09 15:44:01 +02006344 CLEAR_FIELD(ppconst);
6345 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006346 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006347 clear_ppconst(&ppconst);
6348 return NULL;
6349 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006350 if (cctx->ctx_skip == SKIP_YES)
6351 clear_ppconst(&ppconst);
6352 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006353 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006354 int error = FALSE;
6355 int v;
6356
Bram Moolenaara5565e42020-05-09 15:44:01 +02006357 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006358 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006359 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006360 if (error)
6361 return NULL;
6362 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006363 }
6364 else
6365 {
6366 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006367 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006368 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006369 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006370 if (bool_on_stack(cctx) == FAIL)
6371 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373
6374 scope = new_scope(cctx, IF_SCOPE);
6375 if (scope == NULL)
6376 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006377 scope->se_skip_save = skip_save;
6378 // "is_had_return" will be reset if any block does not end in :return
6379 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006381 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006382 {
6383 // "where" is set when ":elseif", "else" or ":endif" is found
6384 scope->se_u.se_if.is_if_label = instr->ga_len;
6385 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6386 }
6387 else
6388 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389
6390 return p;
6391}
6392
6393 static char_u *
6394compile_elseif(char_u *arg, cctx_T *cctx)
6395{
6396 char_u *p = arg;
6397 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006398 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 isn_T *isn;
6400 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006401 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006402 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403
6404 if (scope == NULL || scope->se_type != IF_SCOPE)
6405 {
6406 emsg(_(e_elseif_without_if));
6407 return NULL;
6408 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006409 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006410 if (!cctx->ctx_had_return)
6411 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006413 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006414 {
6415 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006417 return NULL;
6418 // previous "if" or "elseif" jumps here
6419 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6420 isn->isn_arg.jump.jump_where = instr->ga_len;
6421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006423 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006424 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006425 if (cctx->ctx_skip == SKIP_YES)
6426 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006427 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006428 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006429 clear_ppconst(&ppconst);
6430 return NULL;
6431 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006432 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006433 if (scope->se_skip_save == SKIP_YES)
6434 clear_ppconst(&ppconst);
6435 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006436 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006437 int error = FALSE;
6438 int v;
6439
Bram Moolenaar7f141552020-05-09 17:35:53 +02006440 // The expression results in a constant.
6441 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006442 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6443 if (error)
6444 return NULL;
6445 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006446 clear_ppconst(&ppconst);
6447 scope->se_u.se_if.is_if_label = -1;
6448 }
6449 else
6450 {
6451 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006452 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006453 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006454 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006455 if (bool_on_stack(cctx) == FAIL)
6456 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006458 // "where" is set when ":elseif", "else" or ":endif" is found
6459 scope->se_u.se_if.is_if_label = instr->ga_len;
6460 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462
6463 return p;
6464}
6465
6466 static char_u *
6467compile_else(char_u *arg, cctx_T *cctx)
6468{
6469 char_u *p = arg;
6470 garray_T *instr = &cctx->ctx_instr;
6471 isn_T *isn;
6472 scope_T *scope = cctx->ctx_scope;
6473
6474 if (scope == NULL || scope->se_type != IF_SCOPE)
6475 {
6476 emsg(_(e_else_without_if));
6477 return NULL;
6478 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006479 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006480 if (!cctx->ctx_had_return)
6481 scope->se_u.se_if.is_had_return = FALSE;
6482 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483
Bram Moolenaarefd88552020-06-18 20:50:10 +02006484 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006485 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006486 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006487 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006488 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006489 if (!cctx->ctx_had_return
6490 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6491 JUMP_ALWAYS, cctx) == FAIL)
6492 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006493 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006494
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006495 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006496 {
6497 if (scope->se_u.se_if.is_if_label >= 0)
6498 {
6499 // previous "if" or "elseif" jumps here
6500 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6501 isn->isn_arg.jump.jump_where = instr->ga_len;
6502 scope->se_u.se_if.is_if_label = -1;
6503 }
6504 }
6505
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006506 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006507 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509
6510 return p;
6511}
6512
6513 static char_u *
6514compile_endif(char_u *arg, cctx_T *cctx)
6515{
6516 scope_T *scope = cctx->ctx_scope;
6517 ifscope_T *ifscope;
6518 garray_T *instr = &cctx->ctx_instr;
6519 isn_T *isn;
6520
6521 if (scope == NULL || scope->se_type != IF_SCOPE)
6522 {
6523 emsg(_(e_endif_without_if));
6524 return NULL;
6525 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006526 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006527 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006528 if (!cctx->ctx_had_return)
6529 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006531 if (scope->se_u.se_if.is_if_label >= 0)
6532 {
6533 // previous "if" or "elseif" jumps here
6534 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6535 isn->isn_arg.jump.jump_where = instr->ga_len;
6536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 // Fill in the "end" label in jumps at the end of the blocks.
6538 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006539 cctx->ctx_skip = scope->se_skip_save;
6540
6541 // If all the blocks end in :return and there is an :else then the
6542 // had_return flag is set.
6543 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006545 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 return arg;
6547}
6548
6549/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006550 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 *
6552 * Produces instructions:
6553 * PUSHNR -1
6554 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006555 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6557 * - if beyond end, jump to "end"
6558 * - otherwise get item from list and push it
6559 * STORE var Store item in "var"
6560 * ... body ...
6561 * JUMP top Jump back to repeat
6562 * end: DROP Drop the result of "expr"
6563 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006564 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6565 * UNPACK 2 Split item in 2
6566 * STORE var1 Store item in "var1"
6567 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 */
6569 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006570compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006572 char_u *arg;
6573 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006574 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006576 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006577 int var_count = 0;
6578 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579 size_t varlen;
6580 garray_T *instr = &cctx->ctx_instr;
6581 garray_T *stack = &cctx->ctx_type_stack;
6582 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006583 lvar_T *loop_lvar; // loop iteration variable
6584 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006586 type_T *item_type = &t_any;
6587 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588
Bram Moolenaar792f7862020-11-23 08:31:18 +01006589 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6590 if (var_count == 0)
6591 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592
6593 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006594 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006595 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6596 return NULL;
6597 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 {
6599 emsg(_(e_missing_in));
6600 return NULL;
6601 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006602 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006603 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6604 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 scope = new_scope(cctx, FOR_SCOPE);
6607 if (scope == NULL)
6608 return NULL;
6609
Bram Moolenaar792f7862020-11-23 08:31:18 +01006610 // Reserve a variable to store the loop iteration counter and initialize it
6611 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006612 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6613 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006614 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006615 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006616 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006618 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006619 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620
6621 // compile "expr", it remains on the stack until "endfor"
6622 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006623 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006624 {
6625 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006627 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006628 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006630 // Now that we know the type of "var", check that it is a list, now or at
6631 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006633 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006635 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 return NULL;
6637 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006638
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006639 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006640 {
6641 if (var_count == 1)
6642 item_type = vartype->tt_member;
6643 else if (vartype->tt_member->tt_type == VAR_LIST
6644 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6645 item_type = vartype->tt_member->tt_member;
6646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647
6648 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006649 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006650 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651
Bram Moolenaar792f7862020-11-23 08:31:18 +01006652 arg = arg_start;
6653 if (var_count > 1)
6654 {
6655 generate_UNPACK(cctx, var_count, semicolon);
6656 arg = skipwhite(arg + 1); // skip white after '['
6657
6658 // the list item is replaced by a number of items
6659 if (ga_grow(stack, var_count - 1) == FAIL)
6660 {
6661 drop_scope(cctx);
6662 return NULL;
6663 }
6664 --stack->ga_len;
6665 for (idx = 0; idx < var_count; ++idx)
6666 {
6667 ((type_T **)stack->ga_data)[stack->ga_len] =
6668 (semicolon && idx == 0) ? vartype : item_type;
6669 ++stack->ga_len;
6670 }
6671 }
6672
6673 for (idx = 0; idx < var_count; ++idx)
6674 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006675 assign_dest_T dest = dest_local;
6676 int opt_flags = 0;
6677 int vimvaridx = -1;
6678 type_T *type = &t_any;
6679
6680 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006681 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006682 name = vim_strnsave(arg, varlen);
6683 if (name == NULL)
6684 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006685
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006686 // TODO: script var not supported?
6687 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6688 &vimvaridx, &type, cctx) == FAIL)
6689 goto failed;
6690 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006691 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006692 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6693 0, 0, type, name) == FAIL)
6694 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006695 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006696 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006697 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006698 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006699 {
6700 semsg(_(e_variable_already_declared), arg);
6701 goto failed;
6702 }
6703
Bram Moolenaarea870692020-12-02 14:24:30 +01006704 if (STRNCMP(name, "s:", 2) == 0)
6705 {
6706 semsg(_(e_cannot_declare_script_variable_in_function), name);
6707 goto failed;
6708 }
6709
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006710 // Reserve a variable to store "var".
6711 // TODO: check for type
6712 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6713 if (var_lvar == NULL)
6714 // out of memory or used as an argument
6715 goto failed;
6716
6717 if (semicolon && idx == var_count - 1)
6718 var_lvar->lv_type = vartype;
6719 else
6720 var_lvar->lv_type = item_type;
6721 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6722 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006723
6724 if (*p == ',' || *p == ';')
6725 ++p;
6726 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006727 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006728 }
6729
6730 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006731
6732failed:
6733 vim_free(name);
6734 drop_scope(cctx);
6735 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736}
6737
6738/*
6739 * compile "endfor"
6740 */
6741 static char_u *
6742compile_endfor(char_u *arg, cctx_T *cctx)
6743{
6744 garray_T *instr = &cctx->ctx_instr;
6745 scope_T *scope = cctx->ctx_scope;
6746 forscope_T *forscope;
6747 isn_T *isn;
6748
6749 if (scope == NULL || scope->se_type != FOR_SCOPE)
6750 {
6751 emsg(_(e_for));
6752 return NULL;
6753 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006754 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006756 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757
6758 // At end of ":for" scope jump back to the FOR instruction.
6759 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6760
6761 // Fill in the "end" label in the FOR statement so it can jump here
6762 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6763 isn->isn_arg.forloop.for_end = instr->ga_len;
6764
6765 // Fill in the "end" label any BREAK statements
6766 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6767
6768 // Below the ":for" scope drop the "expr" list from the stack.
6769 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6770 return NULL;
6771
6772 vim_free(scope);
6773
6774 return arg;
6775}
6776
6777/*
6778 * compile "while expr"
6779 *
6780 * Produces instructions:
6781 * top: EVAL expr Push result of "expr"
6782 * JUMP_IF_FALSE end jump if false
6783 * ... body ...
6784 * JUMP top Jump back to repeat
6785 * end:
6786 *
6787 */
6788 static char_u *
6789compile_while(char_u *arg, cctx_T *cctx)
6790{
6791 char_u *p = arg;
6792 garray_T *instr = &cctx->ctx_instr;
6793 scope_T *scope;
6794
6795 scope = new_scope(cctx, WHILE_SCOPE);
6796 if (scope == NULL)
6797 return NULL;
6798
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006799 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800
6801 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006802 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 return NULL;
6804
Bram Moolenaar13106602020-10-04 16:06:05 +02006805 if (bool_on_stack(cctx) == FAIL)
6806 return FAIL;
6807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006809 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810 JUMP_IF_FALSE, cctx) == FAIL)
6811 return FAIL;
6812
6813 return p;
6814}
6815
6816/*
6817 * compile "endwhile"
6818 */
6819 static char_u *
6820compile_endwhile(char_u *arg, cctx_T *cctx)
6821{
6822 scope_T *scope = cctx->ctx_scope;
6823
6824 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6825 {
6826 emsg(_(e_while));
6827 return NULL;
6828 }
6829 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006830 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831
6832 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006833 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834
6835 // Fill in the "end" label in the WHILE statement so it can jump here.
6836 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006837 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838
6839 vim_free(scope);
6840
6841 return arg;
6842}
6843
6844/*
6845 * compile "continue"
6846 */
6847 static char_u *
6848compile_continue(char_u *arg, cctx_T *cctx)
6849{
6850 scope_T *scope = cctx->ctx_scope;
6851
6852 for (;;)
6853 {
6854 if (scope == NULL)
6855 {
6856 emsg(_(e_continue));
6857 return NULL;
6858 }
6859 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6860 break;
6861 scope = scope->se_outer;
6862 }
6863
6864 // Jump back to the FOR or WHILE instruction.
6865 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006866 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6867 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 return arg;
6869}
6870
6871/*
6872 * compile "break"
6873 */
6874 static char_u *
6875compile_break(char_u *arg, cctx_T *cctx)
6876{
6877 scope_T *scope = cctx->ctx_scope;
6878 endlabel_T **el;
6879
6880 for (;;)
6881 {
6882 if (scope == NULL)
6883 {
6884 emsg(_(e_break));
6885 return NULL;
6886 }
6887 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6888 break;
6889 scope = scope->se_outer;
6890 }
6891
6892 // Jump to the end of the FOR or WHILE loop.
6893 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006894 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006896 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6898 return FAIL;
6899
6900 return arg;
6901}
6902
6903/*
6904 * compile "{" start of block
6905 */
6906 static char_u *
6907compile_block(char_u *arg, cctx_T *cctx)
6908{
6909 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6910 return NULL;
6911 return skipwhite(arg + 1);
6912}
6913
6914/*
6915 * compile end of block: drop one scope
6916 */
6917 static void
6918compile_endblock(cctx_T *cctx)
6919{
6920 scope_T *scope = cctx->ctx_scope;
6921
6922 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006923 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 vim_free(scope);
6925}
6926
6927/*
6928 * compile "try"
6929 * Creates a new scope for the try-endtry, pointing to the first catch and
6930 * finally.
6931 * Creates another scope for the "try" block itself.
6932 * TRY instruction sets up exception handling at runtime.
6933 *
6934 * "try"
6935 * TRY -> catch1, -> finally push trystack entry
6936 * ... try block
6937 * "throw {exception}"
6938 * EVAL {exception}
6939 * THROW create exception
6940 * ... try block
6941 * " catch {expr}"
6942 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006943 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 * EVAL {expr}
6945 * MATCH
6946 * JUMP nomatch -> catch2
6947 * CATCH remove exception
6948 * ... catch block
6949 * " catch"
6950 * JUMP -> finally
6951 * catch2: CATCH remove exception
6952 * ... catch block
6953 * " finally"
6954 * finally:
6955 * ... finally block
6956 * " endtry"
6957 * ENDTRY pop trystack entry, may rethrow
6958 */
6959 static char_u *
6960compile_try(char_u *arg, cctx_T *cctx)
6961{
6962 garray_T *instr = &cctx->ctx_instr;
6963 scope_T *try_scope;
6964 scope_T *scope;
6965
6966 // scope that holds the jumps that go to catch/finally/endtry
6967 try_scope = new_scope(cctx, TRY_SCOPE);
6968 if (try_scope == NULL)
6969 return NULL;
6970
Bram Moolenaar69f70502021-01-01 16:10:46 +01006971 if (cctx->ctx_skip != SKIP_YES)
6972 {
6973 // "catch" is set when the first ":catch" is found.
6974 // "finally" is set when ":finally" or ":endtry" is found
6975 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
6976 if (generate_instr(cctx, ISN_TRY) == NULL)
6977 return NULL;
6978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979
6980 // scope for the try block itself
6981 scope = new_scope(cctx, BLOCK_SCOPE);
6982 if (scope == NULL)
6983 return NULL;
6984
6985 return arg;
6986}
6987
6988/*
6989 * compile "catch {expr}"
6990 */
6991 static char_u *
6992compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6993{
6994 scope_T *scope = cctx->ctx_scope;
6995 garray_T *instr = &cctx->ctx_instr;
6996 char_u *p;
6997 isn_T *isn;
6998
6999 // end block scope from :try or :catch
7000 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7001 compile_endblock(cctx);
7002 scope = cctx->ctx_scope;
7003
7004 // Error if not in a :try scope
7005 if (scope == NULL || scope->se_type != TRY_SCOPE)
7006 {
7007 emsg(_(e_catch));
7008 return NULL;
7009 }
7010
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007011 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007013 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 return NULL;
7015 }
7016
Bram Moolenaar69f70502021-01-01 16:10:46 +01007017 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007019 // Jump from end of previous block to :finally or :endtry
7020 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7021 JUMP_ALWAYS, cctx) == FAIL)
7022 return NULL;
7023
7024 // End :try or :catch scope: set value in ISN_TRY instruction
7025 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7026 if (isn->isn_arg.try.try_catch == 0)
7027 isn->isn_arg.try.try_catch = instr->ga_len;
7028 if (scope->se_u.se_try.ts_catch_label != 0)
7029 {
7030 // Previous catch without match jumps here
7031 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7032 isn->isn_arg.jump.jump_where = instr->ga_len;
7033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 }
7035
7036 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007037 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007039 scope->se_u.se_try.ts_caught_all = TRUE;
7040 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 }
7042 else
7043 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007044 char_u *end;
7045 char_u *pat;
7046 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007047 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007048 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 // Push v:exception, push {expr} and MATCH
7051 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7052
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007053 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007054 if (*end != *p)
7055 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007056 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007057 vim_free(tofree);
7058 return FAIL;
7059 }
7060 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007061 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007062 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007063 len = (int)(end - tofree);
7064 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007065 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007066 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007067 if (pat == NULL)
7068 return FAIL;
7069 if (generate_PUSHS(cctx, pat) == FAIL)
7070 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7073 return NULL;
7074
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007075 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7077 return NULL;
7078 }
7079
Bram Moolenaar69f70502021-01-01 16:10:46 +01007080 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 return NULL;
7082
7083 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7084 return NULL;
7085 return p;
7086}
7087
7088 static char_u *
7089compile_finally(char_u *arg, cctx_T *cctx)
7090{
7091 scope_T *scope = cctx->ctx_scope;
7092 garray_T *instr = &cctx->ctx_instr;
7093 isn_T *isn;
7094
7095 // end block scope from :try or :catch
7096 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7097 compile_endblock(cctx);
7098 scope = cctx->ctx_scope;
7099
7100 // Error if not in a :try scope
7101 if (scope == NULL || scope->se_type != TRY_SCOPE)
7102 {
7103 emsg(_(e_finally));
7104 return NULL;
7105 }
7106
7107 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007108 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109 if (isn->isn_arg.try.try_finally != 0)
7110 {
7111 emsg(_(e_finally_dup));
7112 return NULL;
7113 }
7114
7115 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007116 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117
Bram Moolenaar585fea72020-04-02 22:33:21 +02007118 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007119 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 {
7121 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007122 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007124 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125 }
7126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 // TODO: set index in ts_finally_label jumps
7128
7129 return arg;
7130}
7131
7132 static char_u *
7133compile_endtry(char_u *arg, cctx_T *cctx)
7134{
7135 scope_T *scope = cctx->ctx_scope;
7136 garray_T *instr = &cctx->ctx_instr;
7137 isn_T *isn;
7138
7139 // end block scope from :catch or :finally
7140 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7141 compile_endblock(cctx);
7142 scope = cctx->ctx_scope;
7143
7144 // Error if not in a :try scope
7145 if (scope == NULL || scope->se_type != TRY_SCOPE)
7146 {
7147 if (scope == NULL)
7148 emsg(_(e_no_endtry));
7149 else if (scope->se_type == WHILE_SCOPE)
7150 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007151 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 emsg(_(e_endfor));
7153 else
7154 emsg(_(e_endif));
7155 return NULL;
7156 }
7157
Bram Moolenaar69f70502021-01-01 16:10:46 +01007158 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007160 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7161 if (isn->isn_arg.try.try_catch == 0
7162 && isn->isn_arg.try.try_finally == 0)
7163 {
7164 emsg(_(e_missing_catch_or_finally));
7165 return NULL;
7166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167
Bram Moolenaar69f70502021-01-01 16:10:46 +01007168 // Fill in the "end" label in jumps at the end of the blocks, if not
7169 // done by ":finally".
7170 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171
Bram Moolenaar69f70502021-01-01 16:10:46 +01007172 // End :catch or :finally scope: set value in ISN_TRY instruction
7173 if (isn->isn_arg.try.try_catch == 0)
7174 isn->isn_arg.try.try_catch = instr->ga_len;
7175 if (isn->isn_arg.try.try_finally == 0)
7176 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007177
Bram Moolenaar69f70502021-01-01 16:10:46 +01007178 if (scope->se_u.se_try.ts_catch_label != 0)
7179 {
7180 // Last catch without match jumps here
7181 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7182 isn->isn_arg.jump.jump_where = instr->ga_len;
7183 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007184 }
7185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 compile_endblock(cctx);
7187
Bram Moolenaar69f70502021-01-01 16:10:46 +01007188 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 return NULL;
7190 return arg;
7191}
7192
7193/*
7194 * compile "throw {expr}"
7195 */
7196 static char_u *
7197compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7198{
7199 char_u *p = skipwhite(arg);
7200
Bram Moolenaara5565e42020-05-09 15:44:01 +02007201 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 return NULL;
7203 if (may_generate_2STRING(-1, cctx) == FAIL)
7204 return NULL;
7205 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7206 return NULL;
7207
7208 return p;
7209}
7210
7211/*
7212 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007213 * compile "echomsg expr"
7214 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007215 * compile "execute expr"
7216 */
7217 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007218compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007219{
7220 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007221 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007222 int count = 0;
7223
7224 for (;;)
7225 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007226 if (ends_excmd2(prev, p))
7227 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007228 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007229 return NULL;
7230 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007231 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007232 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007233 }
7234
Bram Moolenaare4984292020-12-13 14:19:25 +01007235 if (count > 0)
7236 {
7237 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7238 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7239 else if (cmdidx == CMD_execute)
7240 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7241 else if (cmdidx == CMD_echomsg)
7242 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7243 else
7244 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 return p;
7247}
7248
7249/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007250 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007251 * instruction to compute it and return OK.
7252 * Otherwise return FAIL, the caller must deal with any range.
7253 */
7254 static int
7255compile_variable_range(exarg_T *eap, cctx_T *cctx)
7256{
7257 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7258 char_u *p = skipdigits(eap->cmd);
7259
7260 if (p == range_end)
7261 return FAIL;
7262 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7263}
7264
7265/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007266 * :put r
7267 * :put ={expr}
7268 */
7269 static char_u *
7270compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7271{
7272 char_u *line = arg;
7273 linenr_T lnum;
7274 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007275 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007276
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007277 eap->regname = *line;
7278
7279 if (eap->regname == '=')
7280 {
7281 char_u *p = line + 1;
7282
7283 if (compile_expr0(&p, cctx) == FAIL)
7284 return NULL;
7285 line = p;
7286 }
7287 else if (eap->regname != NUL)
7288 ++line;
7289
Bram Moolenaar08597872020-12-10 19:43:40 +01007290 if (compile_variable_range(eap, cctx) == OK)
7291 {
7292 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7293 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007294 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007295 {
7296 // Either no range or a number.
7297 // "errormsg" will not be set because the range is ADDR_LINES.
7298 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007299 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007300 return NULL;
7301 if (eap->addr_count == 0)
7302 lnum = -1;
7303 else
7304 lnum = eap->line2;
7305 if (above)
7306 --lnum;
7307 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007308
7309 generate_PUT(cctx, eap->regname, lnum);
7310 return line;
7311}
7312
7313/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007314 * A command that is not compiled, execute with legacy code.
7315 */
7316 static char_u *
7317compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7318{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007319 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007320 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007321 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007322
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007323 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007324 goto theend;
7325
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007326 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007327 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007328 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007329 int usefilter = FALSE;
7330
7331 has_expr = argt & (EX_XFILE | EX_EXPAND);
7332
7333 // If the command can be followed by a bar, find the bar and truncate
7334 // it, so that the following command can be compiled.
7335 // The '|' is overwritten with a NUL, it is put back below.
7336 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7337 && *eap->arg == '!')
7338 // :w !filter or :r !filter or :r! filter
7339 usefilter = TRUE;
7340 if ((argt & EX_TRLBAR) && !usefilter)
7341 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007342 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007343 separate_nextcmd(eap);
7344 if (eap->nextcmd != NULL)
7345 nextcmd = eap->nextcmd;
7346 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007347 else if (eap->cmdidx == CMD_wincmd)
7348 {
7349 p = eap->arg;
7350 if (*p != NUL)
7351 ++p;
7352 if (*p == 'g' || *p == Ctrl_G)
7353 ++p;
7354 p = skipwhite(p);
7355 if (*p == '|')
7356 {
7357 *p = NUL;
7358 nextcmd = p + 1;
7359 }
7360 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007361 }
7362
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007363 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7364 {
7365 // expand filename in "syntax include [@group] filename"
7366 has_expr = TRUE;
7367 eap->arg = skipwhite(eap->arg + 7);
7368 if (*eap->arg == '@')
7369 eap->arg = skiptowhite(eap->arg);
7370 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007371
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007372 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7373 && STRLEN(eap->arg) > 4)
7374 {
7375 int delim = *eap->arg;
7376
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007377 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007378 if (*p == delim)
7379 {
7380 eap->arg = p + 1;
7381 has_expr = TRUE;
7382 }
7383 }
7384
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007385 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007386 {
7387 int count = 0;
7388 char_u *start = skipwhite(line);
7389
7390 // :cmd xxx`=expr1`yyy`=expr2`zzz
7391 // PUSHS ":cmd xxx"
7392 // eval expr1
7393 // PUSHS "yyy"
7394 // eval expr2
7395 // PUSHS "zzz"
7396 // EXECCONCAT 5
7397 for (;;)
7398 {
7399 if (p > start)
7400 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007401 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007402 ++count;
7403 }
7404 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007405 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007406 return NULL;
7407 may_generate_2STRING(-1, cctx);
7408 ++count;
7409 p = skipwhite(p);
7410 if (*p != '`')
7411 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007412 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007413 return NULL;
7414 }
7415 start = p + 1;
7416
7417 p = (char_u *)strstr((char *)start, "`=");
7418 if (p == NULL)
7419 {
7420 if (*skipwhite(start) != NUL)
7421 {
7422 generate_PUSHS(cctx, vim_strsave(start));
7423 ++count;
7424 }
7425 break;
7426 }
7427 }
7428 generate_EXECCONCAT(cctx, count);
7429 }
7430 else
7431 generate_EXEC(cctx, line);
7432
7433theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007434 if (*nextcmd != NUL)
7435 {
7436 // the parser expects a pointer to the bar, put it back
7437 --nextcmd;
7438 *nextcmd = '|';
7439 }
7440
7441 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007442}
7443
7444/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007445 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007446 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007447 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007448 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007449add_def_function(ufunc_T *ufunc)
7450{
7451 dfunc_T *dfunc;
7452
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007453 if (def_functions.ga_len == 0)
7454 {
7455 // The first position is not used, so that a zero uf_dfunc_idx means it
7456 // wasn't set.
7457 if (ga_grow(&def_functions, 1) == FAIL)
7458 return FAIL;
7459 ++def_functions.ga_len;
7460 }
7461
Bram Moolenaar09689a02020-05-09 22:50:08 +02007462 // Add the function to "def_functions".
7463 if (ga_grow(&def_functions, 1) == FAIL)
7464 return FAIL;
7465 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7466 CLEAR_POINTER(dfunc);
7467 dfunc->df_idx = def_functions.ga_len;
7468 ufunc->uf_dfunc_idx = dfunc->df_idx;
7469 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007470 dfunc->df_name = vim_strsave(ufunc->uf_name);
7471 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007472 ++def_functions.ga_len;
7473 return OK;
7474}
7475
7476/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 * After ex_function() has collected all the function lines: parse and compile
7478 * the lines into instructions.
7479 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007480 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7481 * the return statement (used for lambda). When uf_ret_type is already set
7482 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007483 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007484 * This can be used recursively through compile_lambda(), which may reallocate
7485 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007486 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007488 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007489compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007490{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 char_u *line = NULL;
7492 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 cctx_T cctx;
7495 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007496 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 int ret = FAIL;
7498 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007499 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007500 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007501 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007503 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007504 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007505 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007507 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7508 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007509 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007511 else
7512 {
7513 if (add_def_function(ufunc) == FAIL)
7514 return FAIL;
7515 new_def_function = TRUE;
7516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517
Bram Moolenaar985116a2020-07-12 17:31:09 +02007518 ufunc->uf_def_status = UF_COMPILING;
7519
Bram Moolenaara80faa82020-04-12 19:37:17 +02007520 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 cctx.ctx_ufunc = ufunc;
7522 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007523 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7525 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7526 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7527 cctx.ctx_type_list = &ufunc->uf_type_list;
7528 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7529 instr = &cctx.ctx_instr;
7530
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007531 // Set the context to the function, it may be compiled when called from
7532 // another script. Set the script version to the most modern one.
7533 // The line number will be set in next_line_from_context().
7534 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007535 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7536
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007537 // Make sure error messages are OK.
7538 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7539 if (do_estack_push)
7540 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007541 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007542
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007543 if (ufunc->uf_def_args.ga_len > 0)
7544 {
7545 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007546 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007547 int i;
7548 char_u *arg;
7549 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007550 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007551
7552 // Produce instructions for the default values of optional arguments.
7553 // Store the instruction index in uf_def_arg_idx[] so that we know
7554 // where to start when the function is called, depending on the number
7555 // of arguments.
7556 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7557 if (ufunc->uf_def_arg_idx == NULL)
7558 goto erret;
7559 for (i = 0; i < count; ++i)
7560 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007561 garray_T *stack = &cctx.ctx_type_stack;
7562 type_T *val_type;
7563 int arg_idx = first_def_arg + i;
7564
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007565 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7566 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007567 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007568 goto erret;
7569
7570 // If no type specified use the type of the default value.
7571 // Otherwise check that the default value type matches the
7572 // specified type.
7573 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7574 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007575 {
7576 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007577 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007578 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007579 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7580 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007581 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007582
7583 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007584 goto erret;
7585 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007586 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007587
7588 if (did_set_arg_type)
7589 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007590 }
7591
7592 /*
7593 * Loop over all the lines of the function and generate instructions.
7594 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 for (;;)
7596 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007597 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007598 int starts_with_colon = FALSE;
7599 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007600 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007601
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007602 // Bail out on the first error to avoid a flood of errors and report
7603 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007604 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007605 goto erret;
7606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 if (line != NULL && *line == '|')
7608 // the line continues after a '|'
7609 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007610 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007611 && !(*line == '#' && (line == cctx.ctx_line_start
7612 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007614 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 goto erret;
7616 }
7617 else
7618 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007619 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007620 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007621 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007622 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 }
7624
Bram Moolenaara80faa82020-04-12 19:37:17 +02007625 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 ea.cmdlinep = &line;
7627 ea.cmd = skipwhite(line);
7628
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007629 // Some things can be recognized by the first character.
7630 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007632 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007633 // "#" starts a comment
7634 line = (char_u *)"";
7635 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007637 case '}':
7638 {
7639 // "}" ends a block scope
7640 scopetype_T stype = cctx.ctx_scope == NULL
7641 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007643 if (stype == BLOCK_SCOPE)
7644 {
7645 compile_endblock(&cctx);
7646 line = ea.cmd;
7647 }
7648 else
7649 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007650 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007651 goto erret;
7652 }
7653 if (line != NULL)
7654 line = skipwhite(ea.cmd + 1);
7655 continue;
7656 }
7657
7658 case '{':
7659 // "{" starts a block scope
7660 // "{'a': 1}->func() is something else
7661 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7662 {
7663 line = compile_block(ea.cmd, &cctx);
7664 continue;
7665 }
7666 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 }
7668
7669 /*
7670 * COMMAND MODIFIERS
7671 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007672 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007673 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7674 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675 {
7676 if (errormsg != NULL)
7677 goto erret;
7678 // empty line or comment
7679 line = (char_u *)"";
7680 continue;
7681 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007682 generate_cmdmods(&cctx, &local_cmdmod);
7683 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007685 // Check if there was a colon after the last command modifier or before
7686 // the current position.
7687 for (p = ea.cmd; p >= line; --p)
7688 {
7689 if (*p == ':')
7690 starts_with_colon = TRUE;
7691 if (p < ea.cmd && !VIM_ISWHITE(*p))
7692 break;
7693 }
7694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007695 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007696 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007698 {
7699 if (*ea.cmd == '(')
7700 // not for "call()"
7701 ea.cmd = p;
7702 else
7703 ea.cmd = skipwhite(ea.cmd);
7704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007706 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007708 char_u *pskip;
7709
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007710 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007711 // find what follows.
7712 // Skip over "var.member", "var[idx]" and the like.
7713 // Also "&opt = val", "$ENV = val" and "@r = val".
7714 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007715 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007716 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007717 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007719 char_u *var_end;
7720 int oplen;
7721 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722
Bram Moolenaar65821722020-08-02 18:58:54 +02007723 if (ea.cmd[0] == '@')
7724 var_end = ea.cmd + 2;
7725 else
7726 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007727 FNE_CHECK_START | FNE_INCL_BR);
7728 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007729 if (oplen > 0)
7730 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007731 size_t len = p - ea.cmd;
7732
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007733 // Recognize an assignment if we recognize the variable
7734 // name:
7735 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007736 // "local = expr" where "local" is a local var.
7737 // "script = expr" where "script" is a script-local var.
7738 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007739 // "&opt = expr"
7740 // "$ENV = expr"
7741 // "@r = expr"
7742 if (*ea.cmd == '&'
7743 || *ea.cmd == '$'
7744 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007745 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007746 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007747 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007748 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007749 || script_var_exists(ea.cmd, len,
7750 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007751 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007752 {
7753 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007754 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007755 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007756 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 }
7759 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007760
7761 if (*ea.cmd == '[')
7762 {
7763 // [var, var] = expr
7764 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7765 if (line == NULL)
7766 goto erret;
7767 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007768 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770 }
7771
7772 /*
7773 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007774 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007776 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007777 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007778 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007779 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007780 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007781 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007782 if (!starts_with_colon)
7783 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01007784 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007785 goto erret;
7786 }
7787 if (ends_excmd2(line, ea.cmd))
7788 {
7789 // A range without a command: jump to the line.
7790 // TODO: compile to a more efficient command, possibly
7791 // calling parse_cmd_address().
7792 ea.cmdidx = CMD_SIZE;
7793 line = compile_exec(line, &ea, &cctx);
7794 goto nextline;
7795 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007796 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007797 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007798 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007799 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007800 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801
Bram Moolenaard1510ee2021-01-04 16:15:58 +01007802 if (p == NULL)
7803 {
7804 if (cctx.ctx_skip != SKIP_YES)
7805 emsg(_(e_ambiguous_use_of_user_defined_command));
7806 goto erret;
7807 }
7808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7810 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007811 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007812 {
7813 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007814 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007815 }
7816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007818 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01007820 // CMD_var cannot happen, compile_assignment() above would be
7821 // used. Most likely an assignment to a non-existing variable.
7822 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007823 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 }
7826
Bram Moolenaar3988f642020-08-27 22:43:03 +02007827 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007828 && ea.cmdidx != CMD_elseif
7829 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007830 && ea.cmdidx != CMD_endif
7831 && ea.cmdidx != CMD_endfor
7832 && ea.cmdidx != CMD_endwhile
7833 && ea.cmdidx != CMD_catch
7834 && ea.cmdidx != CMD_finally
7835 && ea.cmdidx != CMD_endtry)
7836 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007837 emsg(_(e_unreachable_code_after_return));
7838 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007839 }
7840
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007841 p = skipwhite(p);
7842 if (ea.cmdidx != CMD_SIZE
7843 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7844 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007845 if (ea.cmdidx >= 0)
7846 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007847 if ((ea.argt & EX_BANG) && *p == '!')
7848 {
7849 ea.forceit = TRUE;
7850 p = skipwhite(p + 1);
7851 }
7852 }
7853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854 switch (ea.cmdidx)
7855 {
7856 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007857 ea.arg = p;
7858 line = compile_nested_function(&ea, &cctx);
7859 break;
7860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007862 // TODO: should we allow this, e.g. to declare a global
7863 // function?
7864 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 goto erret;
7866
7867 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007868 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007869 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 break;
7871
7872 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007873 emsg(_(e_cannot_use_let_in_vim9_script));
7874 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007875 case CMD_var:
7876 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 case CMD_const:
7878 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007879 if (line == p)
7880 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 break;
7882
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007883 case CMD_unlet:
7884 case CMD_unlockvar:
7885 case CMD_lockvar:
7886 line = compile_unletlock(p, &ea, &cctx);
7887 break;
7888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007889 case CMD_import:
7890 line = compile_import(p, &cctx);
7891 break;
7892
7893 case CMD_if:
7894 line = compile_if(p, &cctx);
7895 break;
7896 case CMD_elseif:
7897 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007898 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 break;
7900 case CMD_else:
7901 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007902 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007903 break;
7904 case CMD_endif:
7905 line = compile_endif(p, &cctx);
7906 break;
7907
7908 case CMD_while:
7909 line = compile_while(p, &cctx);
7910 break;
7911 case CMD_endwhile:
7912 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007913 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914 break;
7915
7916 case CMD_for:
7917 line = compile_for(p, &cctx);
7918 break;
7919 case CMD_endfor:
7920 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007921 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922 break;
7923 case CMD_continue:
7924 line = compile_continue(p, &cctx);
7925 break;
7926 case CMD_break:
7927 line = compile_break(p, &cctx);
7928 break;
7929
7930 case CMD_try:
7931 line = compile_try(p, &cctx);
7932 break;
7933 case CMD_catch:
7934 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007935 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936 break;
7937 case CMD_finally:
7938 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007939 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940 break;
7941 case CMD_endtry:
7942 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007943 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 break;
7945 case CMD_throw:
7946 line = compile_throw(p, &cctx);
7947 break;
7948
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007949 case CMD_eval:
7950 if (compile_expr0(&p, &cctx) == FAIL)
7951 goto erret;
7952
Bram Moolenaar3988f642020-08-27 22:43:03 +02007953 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007954 generate_instr_drop(&cctx, ISN_DROP, 1);
7955
7956 line = skipwhite(p);
7957 break;
7958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007959 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007960 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007961 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007962 case CMD_echomsg:
7963 case CMD_echoerr:
7964 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007965 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007966
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007967 case CMD_put:
7968 ea.cmd = cmd;
7969 line = compile_put(p, &ea, &cctx);
7970 break;
7971
Bram Moolenaar3988f642020-08-27 22:43:03 +02007972 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007973
Bram Moolenaarae616492020-07-28 20:07:27 +02007974 case CMD_append:
7975 case CMD_change:
7976 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007977 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007978 case CMD_xit:
7979 not_in_vim9(&ea);
7980 goto erret;
7981
Bram Moolenaar002262f2020-07-08 17:47:57 +02007982 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007983 if (cctx.ctx_skip != SKIP_YES)
7984 {
7985 semsg(_(e_invalid_command_str), ea.cmd);
7986 goto erret;
7987 }
7988 // We don't check for a next command here.
7989 line = (char_u *)"";
7990 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007992 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007993 if (cctx.ctx_skip == SKIP_YES)
7994 {
7995 // We don't check for a next command here.
7996 line = (char_u *)"";
7997 }
7998 else
7999 {
8000 // Not recognized, execute with do_cmdline_cmd().
8001 ea.arg = p;
8002 line = compile_exec(line, &ea, &cctx);
8003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004 break;
8005 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008006nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008007 if (line == NULL)
8008 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008009 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008011 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008012 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 if (cctx.ctx_type_stack.ga_len < 0)
8015 {
8016 iemsg("Type stack underflow");
8017 goto erret;
8018 }
8019 }
8020
8021 if (cctx.ctx_scope != NULL)
8022 {
8023 if (cctx.ctx_scope->se_type == IF_SCOPE)
8024 emsg(_(e_endif));
8025 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8026 emsg(_(e_endwhile));
8027 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8028 emsg(_(e_endfor));
8029 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008030 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008031 goto erret;
8032 }
8033
Bram Moolenaarefd88552020-06-18 20:50:10 +02008034 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035 {
8036 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8037 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008038 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039 goto erret;
8040 }
8041
8042 // Return zero if there is no return at the end.
8043 generate_PUSHNR(&cctx, 0);
8044 generate_instr(&cctx, ISN_RETURN);
8045 }
8046
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008047 {
8048 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8049 + ufunc->uf_dfunc_idx;
8050 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008051 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008052 dfunc->df_instr = instr->ga_data;
8053 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008054 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008055 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008056 if (cctx.ctx_outer_used)
8057 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008058 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008060
8061 ret = OK;
8062
8063erret:
8064 if (ret == FAIL)
8065 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008066 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008067 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8068 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008069
8070 for (idx = 0; idx < instr->ga_len; ++idx)
8071 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008072 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008073 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008074
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008075 // If using the last entry in the table and it was added above, we
8076 // might as well remove it.
8077 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008078 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008079 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008080 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008081 ufunc->uf_dfunc_idx = 0;
8082 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008083 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008084
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008085 while (cctx.ctx_scope != NULL)
8086 drop_scope(&cctx);
8087
Bram Moolenaar20431c92020-03-20 18:39:46 +01008088 // Don't execute this function body.
8089 ga_clear_strings(&ufunc->uf_lines);
8090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091 if (errormsg != NULL)
8092 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008093 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008094 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 }
8096
8097 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008098 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008099 if (do_estack_push)
8100 estack_pop();
8101
Bram Moolenaar20431c92020-03-20 18:39:46 +01008102 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008103 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008105 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106}
8107
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008108 void
8109set_function_type(ufunc_T *ufunc)
8110{
8111 int varargs = ufunc->uf_va_name != NULL;
8112 int argcount = ufunc->uf_args.ga_len;
8113
8114 // Create a type for the function, with the return type and any
8115 // argument types.
8116 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8117 // The type is included in "tt_args".
8118 if (argcount > 0 || varargs)
8119 {
8120 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8121 argcount, &ufunc->uf_type_list);
8122 // Add argument types to the function type.
8123 if (func_type_add_arg_types(ufunc->uf_func_type,
8124 argcount + varargs,
8125 &ufunc->uf_type_list) == FAIL)
8126 return;
8127 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8128 ufunc->uf_func_type->tt_min_argcount =
8129 argcount - ufunc->uf_def_args.ga_len;
8130 if (ufunc->uf_arg_types == NULL)
8131 {
8132 int i;
8133
8134 // lambda does not have argument types.
8135 for (i = 0; i < argcount; ++i)
8136 ufunc->uf_func_type->tt_args[i] = &t_any;
8137 }
8138 else
8139 mch_memmove(ufunc->uf_func_type->tt_args,
8140 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8141 if (varargs)
8142 {
8143 ufunc->uf_func_type->tt_args[argcount] =
8144 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8145 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8146 }
8147 }
8148 else
8149 // No arguments, can use a predefined type.
8150 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8151 argcount, &ufunc->uf_type_list);
8152}
8153
8154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155/*
8156 * Delete an instruction, free what it contains.
8157 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008158 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159delete_instr(isn_T *isn)
8160{
8161 switch (isn->isn_type)
8162 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008163 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008164 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008165 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008166 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008167 case ISN_LOADENV:
8168 case ISN_LOADG:
8169 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008170 case ISN_LOADT:
8171 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008172 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008173 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008175 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008176 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008177 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008178 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008180 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008181 case ISN_STOREW:
8182 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183 vim_free(isn->isn_arg.string);
8184 break;
8185
8186 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008187 case ISN_STORES:
8188 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189 break;
8190
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008191 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008192 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008193 vim_free(isn->isn_arg.unlet.ul_name);
8194 break;
8195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 case ISN_STOREOPT:
8197 vim_free(isn->isn_arg.storeopt.so_name);
8198 break;
8199
8200 case ISN_PUSHBLOB: // push blob isn_arg.blob
8201 blob_unref(isn->isn_arg.blob);
8202 break;
8203
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008204 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008205#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008206 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008207#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008208 break;
8209
8210 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008211#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008212 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008213#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008214 break;
8215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008216 case ISN_UCALL:
8217 vim_free(isn->isn_arg.ufunc.cuf_name);
8218 break;
8219
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008220 case ISN_FUNCREF:
8221 {
8222 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8223 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008224 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008225
Bram Moolenaar077a4232020-12-22 18:33:27 +01008226 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8227 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008228 }
8229 break;
8230
8231 case ISN_DCALL:
8232 {
8233 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8234 + isn->isn_arg.dfunc.cdf_idx;
8235
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008236 if (dfunc->df_ufunc != NULL
8237 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008238 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008239 }
8240 break;
8241
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008242 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008243 {
8244 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8245 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8246
8247 if (ufunc != NULL)
8248 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008249 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008250 func_ptr_unref(ufunc);
8251 }
8252
8253 vim_free(lambda);
8254 vim_free(isn->isn_arg.newfunc.nf_global);
8255 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008256 break;
8257
Bram Moolenaar5e654232020-09-16 15:22:00 +02008258 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008259 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008260 free_type(isn->isn_arg.type.ct_type);
8261 break;
8262
Bram Moolenaar02194d22020-10-24 23:08:38 +02008263 case ISN_CMDMOD:
8264 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8265 ->cmod_filter_regmatch.regprog);
8266 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8267 break;
8268
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008269 case ISN_LOADSCRIPT:
8270 case ISN_STORESCRIPT:
8271 vim_free(isn->isn_arg.script.scriptref);
8272 break;
8273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008274 case ISN_2BOOL:
8275 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008276 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008277 case ISN_ADDBLOB:
8278 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008279 case ISN_ANYINDEX:
8280 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008281 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008282 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008284 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008286 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 case ISN_COMPAREANY:
8288 case ISN_COMPAREBLOB:
8289 case ISN_COMPAREBOOL:
8290 case ISN_COMPAREDICT:
8291 case ISN_COMPAREFLOAT:
8292 case ISN_COMPAREFUNC:
8293 case ISN_COMPARELIST:
8294 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295 case ISN_COMPARESPECIAL:
8296 case ISN_COMPARESTRING:
8297 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008298 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008299 case ISN_DROP:
8300 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008301 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008302 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008303 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008304 case ISN_EXECCONCAT:
8305 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008307 case ISN_GETITEM:
8308 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008309 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008310 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008311 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008313 case ISN_LOADBDICT:
8314 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008315 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008317 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008319 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008320 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008321 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008322 case ISN_NEGATENR:
8323 case ISN_NEWDICT:
8324 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008325 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008326 case ISN_OPFLOAT:
8327 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008328 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008329 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008330 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331 case ISN_PUSHF:
8332 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008333 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008334 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008335 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008336 case ISN_SHUFFLE:
8337 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008338 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008339 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008340 case ISN_STORENR:
8341 case ISN_STOREOUTER:
8342 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008343 case ISN_STOREV:
8344 case ISN_STRINDEX:
8345 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346 case ISN_THROW:
8347 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008348 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008349 // nothing allocated
8350 break;
8351 }
8352}
8353
8354/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008355 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008356 */
8357 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008358delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008359{
8360 int idx;
8361
8362 ga_clear(&dfunc->df_def_args_isn);
8363
8364 if (dfunc->df_instr != NULL)
8365 {
8366 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8367 delete_instr(dfunc->df_instr + idx);
8368 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008369 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008370 }
8371
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008372 if (mark_deleted)
8373 dfunc->df_deleted = TRUE;
8374 if (dfunc->df_ufunc != NULL)
8375 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008376}
8377
8378/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008379 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008380 * function, unless another user function still uses it.
8381 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 */
8383 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008384unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008385{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008386 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008387 {
8388 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8389 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008390
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008391 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008392 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008393 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008394 ufunc->uf_dfunc_idx = 0;
8395 if (dfunc->df_ufunc == ufunc)
8396 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 }
8398}
8399
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008400/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008401 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008402 */
8403 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008404link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008405{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008406 if (ufunc->uf_dfunc_idx > 0)
8407 {
8408 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8409 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008410
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008411 ++dfunc->df_refcount;
8412 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008413}
8414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008416/*
8417 * Free all functions defined with ":def".
8418 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419 void
8420free_def_functions(void)
8421{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008422 int idx;
8423
8424 for (idx = 0; idx < def_functions.ga_len; ++idx)
8425 {
8426 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8427
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008428 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008429 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008430 }
8431
8432 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433}
8434#endif
8435
8436
8437#endif // FEAT_EVAL