blob: 8ef1f087341af08381b4076d052939f825dd1a22 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200198arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200250 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200262 * Lookup a script-local variable in the current script, possibly defined in a
263 * block that contains the function "cctx->ctx_ufunc".
264 * "cctx" is NULL at the script level.
265 * if "len" is <= 0 "name" must be NUL terminated.
266 * Return NULL when not found.
267 */
268 static sallvar_T *
269find_script_var(char_u *name, size_t len, cctx_T *cctx)
270{
271 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
272 hashitem_T *hi;
273 int cc;
274 sallvar_T *sav;
275 ufunc_T *ufunc;
276
277 // Find the list of all script variables with the right name.
278 if (len > 0)
279 {
280 cc = name[len];
281 name[len] = NUL;
282 }
283 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
284 if (len > 0)
285 name[len] = cc;
286 if (HASHITEM_EMPTY(hi))
287 return NULL;
288
289 sav = HI2SAV(hi);
290 if (sav->sav_block_id == 0 || cctx == NULL)
291 // variable defined in the script scope or not in a function.
292 return sav;
293
294 // Go over the variables with this name and find one that was visible
295 // from the function.
296 ufunc = cctx->ctx_ufunc;
297 while (sav != NULL)
298 {
299 int idx;
300
301 // Go over the blocks that this function was defined in. If the
302 // variable block ID matches it was visible to the function.
303 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
304 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
305 return sav;
306 sav = sav->sav_next;
307 }
308
309 return NULL;
310}
311
312/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200313 * Returnd TRUE if the script context is Vim9 script.
314 */
315 static int
316script_is_vim9()
317{
318 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
319}
320
321/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200322 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200323 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
324 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200325 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 * Returns OK or FAIL.
327 */
328 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200333 if (current_sctx.sc_sid <= 0)
334 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 is_vim9_script = script_is_vim9();
336 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200337 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200338 if (is_vim9_script)
339 {
340 // Check script variables that were visible where the function was
341 // defined.
342 if (find_script_var(name, len, cctx) != NULL)
343 return OK;
344 }
345 else
346 {
347 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
348 dictitem_T *di;
349 int cc;
350
351 // Check script variables that are currently visible
352 cc = name[len];
353 name[len] = NUL;
354 di = find_var_in_ht(ht, 0, name, TRUE);
355 name[len] = cc;
356 if (di != NULL)
357 return OK;
358 }
359
360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361}
362
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100363/*
364 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200365 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200366 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100367 * Return FAIL and give an error if it defined.
368 */
369 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200370check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200372 int c = p[len];
373 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200374
375 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200376 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100377 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200378 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200380 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200381 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100382 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 // A local or script-local function can shadow a global function.
384 if (ufunc == NULL || !func_is_global(ufunc)
385 || (p[0] == 'g' && p[1] == ':'))
386 {
387 p[len] = c;
388 semsg(_(e_name_already_defined_str), p);
389 return FAIL;
390 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100391 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200392 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 return OK;
394}
395
Bram Moolenaar65b95452020-07-19 14:03:09 +0200396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397/////////////////////////////////////////////////////////////////////
398// Following generate_ functions expect the caller to call ga_grow().
399
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200400#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
401#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403/*
404 * Generate an instruction without arguments.
405 * Returns a pointer to the new instruction, NULL if failed.
406 */
407 static isn_T *
408generate_instr(cctx_T *cctx, isntype_T isn_type)
409{
410 garray_T *instr = &cctx->ctx_instr;
411 isn_T *isn;
412
Bram Moolenaar080457c2020-03-03 21:53:32 +0100413 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (ga_grow(instr, 1) == FAIL)
415 return NULL;
416 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
417 isn->isn_type = isn_type;
418 isn->isn_lnum = cctx->ctx_lnum + 1;
419 ++instr->ga_len;
420
421 return isn;
422}
423
424/*
425 * Generate an instruction without arguments.
426 * "drop" will be removed from the stack.
427 * Returns a pointer to the new instruction, NULL if failed.
428 */
429 static isn_T *
430generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
431{
432 garray_T *stack = &cctx->ctx_type_stack;
433
Bram Moolenaar080457c2020-03-03 21:53:32 +0100434 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 stack->ga_len -= drop;
436 return generate_instr(cctx, isn_type);
437}
438
439/*
440 * Generate instruction "isn_type" and put "type" on the type stack.
441 */
442 static isn_T *
443generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
444{
445 isn_T *isn;
446 garray_T *stack = &cctx->ctx_type_stack;
447
448 if ((isn = generate_instr(cctx, isn_type)) == NULL)
449 return NULL;
450
451 if (ga_grow(stack, 1) == FAIL)
452 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200453 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 ++stack->ga_len;
455
456 return isn;
457}
458
459/*
460 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200461 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 */
463 static int
464may_generate_2STRING(int offset, cctx_T *cctx)
465{
466 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200467 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 garray_T *stack = &cctx->ctx_type_stack;
469 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
470
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200471 switch ((*type)->tt_type)
472 {
473 // nothing to be done
474 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200476 // conversion possible
477 case VAR_SPECIAL:
478 case VAR_BOOL:
479 case VAR_NUMBER:
480 case VAR_FLOAT:
481 break;
482
483 // conversion possible (with runtime check)
484 case VAR_ANY:
485 case VAR_UNKNOWN:
486 isntype = ISN_2STRING_ANY;
487 break;
488
489 // conversion not possible
490 case VAR_VOID:
491 case VAR_BLOB:
492 case VAR_FUNC:
493 case VAR_PARTIAL:
494 case VAR_LIST:
495 case VAR_DICT:
496 case VAR_JOB:
497 case VAR_CHANNEL:
498 to_string_error((*type)->tt_type);
499 return FAIL;
500 }
501
502 *type = &t_string;
503 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 return FAIL;
505 isn->isn_arg.number = offset;
506
507 return OK;
508}
509
510 static int
511check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
512{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200513 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 {
517 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200518 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 return FAIL;
522 }
523 return OK;
524}
525
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200526 static int
527generate_add_instr(
528 cctx_T *cctx,
529 vartype_T vartype,
530 type_T *type1,
531 type_T *type2)
532{
533 isn_T *isn = generate_instr_drop(cctx,
534 vartype == VAR_NUMBER ? ISN_OPNR
535 : vartype == VAR_LIST ? ISN_ADDLIST
536 : vartype == VAR_BLOB ? ISN_ADDBLOB
537#ifdef FEAT_FLOAT
538 : vartype == VAR_FLOAT ? ISN_OPFLOAT
539#endif
540 : ISN_OPANY, 1);
541
542 if (vartype != VAR_LIST && vartype != VAR_BLOB
543 && type1->tt_type != VAR_ANY
544 && type2->tt_type != VAR_ANY
545 && check_number_or_float(
546 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
547 return FAIL;
548
549 if (isn != NULL)
550 isn->isn_arg.op.op_type = EXPR_ADD;
551 return isn == NULL ? FAIL : OK;
552}
553
554/*
555 * Get the type to use for an instruction for an operation on "type1" and
556 * "type2". If they are matching use a type-specific instruction. Otherwise
557 * fall back to runtime type checking.
558 */
559 static vartype_T
560operator_type(type_T *type1, type_T *type2)
561{
562 if (type1->tt_type == type2->tt_type
563 && (type1->tt_type == VAR_NUMBER
564 || type1->tt_type == VAR_LIST
565#ifdef FEAT_FLOAT
566 || type1->tt_type == VAR_FLOAT
567#endif
568 || type1->tt_type == VAR_BLOB))
569 return type1->tt_type;
570 return VAR_ANY;
571}
572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573/*
574 * Generate an instruction with two arguments. The instruction depends on the
575 * type of the arguments.
576 */
577 static int
578generate_two_op(cctx_T *cctx, char_u *op)
579{
580 garray_T *stack = &cctx->ctx_type_stack;
581 type_T *type1;
582 type_T *type2;
583 vartype_T vartype;
584 isn_T *isn;
585
Bram Moolenaar080457c2020-03-03 21:53:32 +0100586 RETURN_OK_IF_SKIP(cctx);
587
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200588 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
590 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200591 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592
593 switch (*op)
594 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200595 case '+':
596 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 break;
599
600 case '-':
601 case '*':
602 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
603 op) == FAIL)
604 return FAIL;
605 if (vartype == VAR_NUMBER)
606 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
607#ifdef FEAT_FLOAT
608 else if (vartype == VAR_FLOAT)
609 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
610#endif
611 else
612 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = *op == '*'
615 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
616 break;
617
Bram Moolenaar4c683752020-04-05 21:38:23 +0200618 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type2->tt_type != VAR_NUMBER))
622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200623 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 }
626 isn = generate_instr_drop(cctx,
627 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
628 if (isn != NULL)
629 isn->isn_arg.op.op_type = EXPR_REM;
630 break;
631 }
632
633 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200634 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 {
636 type_T *type = &t_any;
637
638#ifdef FEAT_FLOAT
639 // float+number and number+float results in float
640 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
641 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
642 type = &t_float;
643#endif
644 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
645 }
646
647 return OK;
648}
649
650/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200651 * Get the instruction to use for comparing "type1" with "type2"
652 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200654 static isntype_T
655get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656{
657 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658
Bram Moolenaar4c683752020-04-05 21:38:23 +0200659 if (type1 == VAR_UNKNOWN)
660 type1 = VAR_ANY;
661 if (type2 == VAR_UNKNOWN)
662 type2 = VAR_ANY;
663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 if (type1 == type2)
665 {
666 switch (type1)
667 {
668 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
669 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
670 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
671 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
672 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
673 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
674 case VAR_LIST: isntype = ISN_COMPARELIST; break;
675 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
676 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 default: isntype = ISN_COMPAREANY; break;
678 }
679 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
682 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
683 isntype = ISN_COMPAREANY;
684
685 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
686 && (isntype == ISN_COMPAREBOOL
687 || isntype == ISN_COMPARESPECIAL
688 || isntype == ISN_COMPARENR
689 || isntype == ISN_COMPAREFLOAT))
690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200691 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200693 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 }
695 if (isntype == ISN_DROP
696 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
697 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
698 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
699 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
700 && exptype != EXPR_IS && exptype != EXPR_ISNOT
701 && (type1 == VAR_BLOB || type2 == VAR_BLOB
702 || type1 == VAR_LIST || type2 == VAR_LIST))))
703 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200704 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200706 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return isntype;
709}
710
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200711 int
712check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
713{
714 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
715 return FAIL;
716 return OK;
717}
718
Bram Moolenaara5565e42020-05-09 15:44:01 +0200719/*
720 * Generate an ISN_COMPARE* instruction with a boolean result.
721 */
722 static int
723generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
724{
725 isntype_T isntype;
726 isn_T *isn;
727 garray_T *stack = &cctx->ctx_type_stack;
728 vartype_T type1;
729 vartype_T type2;
730
731 RETURN_OK_IF_SKIP(cctx);
732
733 // Get the known type of the two items on the stack. If they are matching
734 // use a type-specific instruction. Otherwise fall back to runtime type
735 // checking.
736 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
737 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
738 isntype = get_compare_isn(exptype, type1, type2);
739 if (isntype == ISN_DROP)
740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
742 if ((isn = generate_instr(cctx, isntype)) == NULL)
743 return FAIL;
744 isn->isn_arg.op.op_type = exptype;
745 isn->isn_arg.op.op_ic = ic;
746
747 // takes two arguments, puts one bool back
748 if (stack->ga_len >= 2)
749 {
750 --stack->ga_len;
751 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
752 }
753
754 return OK;
755}
756
757/*
758 * Generate an ISN_2BOOL instruction.
759 */
760 static int
761generate_2BOOL(cctx_T *cctx, int invert)
762{
763 isn_T *isn;
764 garray_T *stack = &cctx->ctx_type_stack;
765
Bram Moolenaar080457c2020-03-03 21:53:32 +0100766 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
768 return FAIL;
769 isn->isn_arg.number = invert;
770
771 // type becomes bool
772 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
773
774 return OK;
775}
776
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200777/*
778 * Generate an ISN_COND2BOOL instruction.
779 */
780 static int
781generate_COND2BOOL(cctx_T *cctx)
782{
783 isn_T *isn;
784 garray_T *stack = &cctx->ctx_type_stack;
785
786 RETURN_OK_IF_SKIP(cctx);
787 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
788 return FAIL;
789
790 // type becomes bool
791 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
792
793 return OK;
794}
795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200797generate_TYPECHECK(
798 cctx_T *cctx,
799 type_T *expected,
800 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801{
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804
Bram Moolenaar080457c2020-03-03 21:53:32 +0100805 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
807 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200808 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 isn->isn_arg.type.ct_off = offset;
810
Bram Moolenaar5e654232020-09-16 15:22:00 +0200811 // type becomes expected
812 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 return OK;
815}
816
817/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200818 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
819 * used. Return FALSE if the types will never match.
820 */
821 static int
822use_typecheck(type_T *actual, type_T *expected)
823{
824 if (actual->tt_type == VAR_ANY
825 || actual->tt_type == VAR_UNKNOWN
826 || (actual->tt_type == VAR_FUNC
827 && (expected->tt_type == VAR_FUNC
828 || expected->tt_type == VAR_PARTIAL)
829 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
830 return TRUE;
831 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
832 && actual->tt_type == expected->tt_type)
833 // This takes care of a nested list or dict.
834 return use_typecheck(actual->tt_member, expected->tt_member);
835 return FALSE;
836}
837
838/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200839 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200840 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * - "actual" is a type that can be "expected" type: add a runtime check; or
842 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200843 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
844 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200845 */
846 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200847need_type(
848 type_T *actual,
849 type_T *expected,
850 int offset,
851 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200852 int silent,
853 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200854{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200855 if (expected == &t_bool && actual != &t_bool
856 && (actual->tt_flags & TTFLAG_BOOL_OK))
857 {
858 // Using "0", "1" or the result of an expression with "&&" or "||" as a
859 // boolean is OK but requires a conversion.
860 generate_2BOOL(cctx, FALSE);
861 return OK;
862 }
863
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200864 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200865 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200866
867 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200868 // If it's a constant a runtime check makes no sense.
869 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200871 generate_TYPECHECK(cctx, expected, offset);
872 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200873 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200874
875 if (!silent)
876 type_mismatch(expected, actual);
877 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878}
879
880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 * Generate an ISN_PUSHNR instruction.
882 */
883 static int
884generate_PUSHNR(cctx_T *cctx, varnumber_T number)
885{
886 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200887 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar080457c2020-03-03 21:53:32 +0100889 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
891 return FAIL;
892 isn->isn_arg.number = number;
893
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200894 if (number == 0 || number == 1)
895 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200896 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200897
898 // A 0 or 1 number can also be used as a bool.
899 if (type != NULL)
900 {
901 type->tt_type = VAR_NUMBER;
902 type->tt_flags = TTFLAG_BOOL_OK;
903 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
904 }
905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 return OK;
907}
908
909/*
910 * Generate an ISN_PUSHBOOL instruction.
911 */
912 static int
913generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
914{
915 isn_T *isn;
916
Bram Moolenaar080457c2020-03-03 21:53:32 +0100917 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
919 return FAIL;
920 isn->isn_arg.number = number;
921
922 return OK;
923}
924
925/*
926 * Generate an ISN_PUSHSPEC instruction.
927 */
928 static int
929generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
930{
931 isn_T *isn;
932
Bram Moolenaar080457c2020-03-03 21:53:32 +0100933 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
935 return FAIL;
936 isn->isn_arg.number = number;
937
938 return OK;
939}
940
941#ifdef FEAT_FLOAT
942/*
943 * Generate an ISN_PUSHF instruction.
944 */
945 static int
946generate_PUSHF(cctx_T *cctx, float_T fnumber)
947{
948 isn_T *isn;
949
Bram Moolenaar080457c2020-03-03 21:53:32 +0100950 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
952 return FAIL;
953 isn->isn_arg.fnumber = fnumber;
954
955 return OK;
956}
957#endif
958
959/*
960 * Generate an ISN_PUSHS instruction.
961 * Consumes "str".
962 */
963 static int
964generate_PUSHS(cctx_T *cctx, char_u *str)
965{
966 isn_T *isn;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
970 return FAIL;
971 isn->isn_arg.string = str;
972
973 return OK;
974}
975
976/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100977 * Generate an ISN_PUSHCHANNEL instruction.
978 * Consumes "channel".
979 */
980 static int
981generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
982{
983 isn_T *isn;
984
Bram Moolenaar080457c2020-03-03 21:53:32 +0100985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100986 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
987 return FAIL;
988 isn->isn_arg.channel = channel;
989
990 return OK;
991}
992
993/*
994 * Generate an ISN_PUSHJOB instruction.
995 * Consumes "job".
996 */
997 static int
998generate_PUSHJOB(cctx_T *cctx, job_T *job)
999{
1000 isn_T *isn;
1001
Bram Moolenaar080457c2020-03-03 21:53:32 +01001002 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001003 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001004 return FAIL;
1005 isn->isn_arg.job = job;
1006
1007 return OK;
1008}
1009
1010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 * Generate an ISN_PUSHBLOB instruction.
1012 * Consumes "blob".
1013 */
1014 static int
1015generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1016{
1017 isn_T *isn;
1018
Bram Moolenaar080457c2020-03-03 21:53:32 +01001019 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.blob = blob;
1023
1024 return OK;
1025}
1026
1027/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 * Generate an ISN_PUSHFUNC instruction with name "name".
1029 * Consumes "name".
1030 */
1031 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001032generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001037 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001038 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001039 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040
1041 return OK;
1042}
1043
1044/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 * Generate an ISN_GETITEM instruction with "index".
1046 */
1047 static int
1048generate_GETITEM(cctx_T *cctx, int index)
1049{
1050 isn_T *isn;
1051 garray_T *stack = &cctx->ctx_type_stack;
1052 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1053 type_T *item_type = &t_any;
1054
1055 RETURN_OK_IF_SKIP(cctx);
1056
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001057 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001059 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 emsg(_(e_listreq));
1061 return FAIL;
1062 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001063 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001064 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = index;
1067
1068 // add the item type to the type stack
1069 if (ga_grow(stack, 1) == FAIL)
1070 return FAIL;
1071 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1072 ++stack->ga_len;
1073 return OK;
1074}
1075
1076/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001077 * Generate an ISN_SLICE instruction with "count".
1078 */
1079 static int
1080generate_SLICE(cctx_T *cctx, int count)
1081{
1082 isn_T *isn;
1083
1084 RETURN_OK_IF_SKIP(cctx);
1085 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.number = count;
1088 return OK;
1089}
1090
1091/*
1092 * Generate an ISN_CHECKLEN instruction with "min_len".
1093 */
1094 static int
1095generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1096{
1097 isn_T *isn;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100
1101 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.checklen.cl_min_len = min_len;
1104 isn->isn_arg.checklen.cl_more_OK = more_OK;
1105
1106 return OK;
1107}
1108
1109/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 * Generate an ISN_STORE instruction.
1111 */
1112 static int
1113generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1114{
1115 isn_T *isn;
1116
Bram Moolenaar080457c2020-03-03 21:53:32 +01001117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1119 return FAIL;
1120 if (name != NULL)
1121 isn->isn_arg.string = vim_strsave(name);
1122 else
1123 isn->isn_arg.number = idx;
1124
1125 return OK;
1126}
1127
1128/*
1129 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1130 */
1131 static int
1132generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1133{
1134 isn_T *isn;
1135
Bram Moolenaar080457c2020-03-03 21:53:32 +01001136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1138 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001139 isn->isn_arg.storenr.stnr_idx = idx;
1140 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141
1142 return OK;
1143}
1144
1145/*
1146 * Generate an ISN_STOREOPT instruction
1147 */
1148 static int
1149generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1150{
1151 isn_T *isn;
1152
Bram Moolenaar080457c2020-03-03 21:53:32 +01001153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001154 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 return FAIL;
1156 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1157 isn->isn_arg.storeopt.so_flags = opt_flags;
1158
1159 return OK;
1160}
1161
1162/*
1163 * Generate an ISN_LOAD or similar instruction.
1164 */
1165 static int
1166generate_LOAD(
1167 cctx_T *cctx,
1168 isntype_T isn_type,
1169 int idx,
1170 char_u *name,
1171 type_T *type)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1177 return FAIL;
1178 if (name != NULL)
1179 isn->isn_arg.string = vim_strsave(name);
1180 else
1181 isn->isn_arg.number = idx;
1182
1183 return OK;
1184}
1185
1186/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001187 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188 */
1189 static int
1190generate_LOADV(
1191 cctx_T *cctx,
1192 char_u *name,
1193 int error)
1194{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001195 int di_flags;
1196 int vidx = find_vim_var(name, &di_flags);
1197 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200 if (vidx < 0)
1201 {
1202 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001203 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001204 return FAIL;
1205 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001206 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001207
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001208 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209}
1210
1211/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001212 * Generate an ISN_UNLET instruction.
1213 */
1214 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001215generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001216{
1217 isn_T *isn;
1218
1219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001220 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001221 return FAIL;
1222 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1223 isn->isn_arg.unlet.ul_forceit = forceit;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001229 * Generate an ISN_LOCKCONST instruction.
1230 */
1231 static int
1232generate_LOCKCONST(cctx_T *cctx)
1233{
1234 isn_T *isn;
1235
1236 RETURN_OK_IF_SKIP(cctx);
1237 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1238 return FAIL;
1239 return OK;
1240}
1241
1242/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 * Generate an ISN_LOADS instruction.
1244 */
1245 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250 int sid,
1251 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252{
1253 isn_T *isn;
1254
Bram Moolenaar080457c2020-03-03 21:53:32 +01001255 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001256 if (isn_type == ISN_LOADS)
1257 isn = generate_instr_type(cctx, isn_type, type);
1258 else
1259 isn = generate_instr_drop(cctx, isn_type, 1);
1260 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001262 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1263 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 return OK;
1266}
1267
1268/*
1269 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1270 */
1271 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 cctx_T *cctx,
1274 isntype_T isn_type,
1275 int sid,
1276 int idx,
1277 type_T *type)
1278{
1279 isn_T *isn;
1280
Bram Moolenaar080457c2020-03-03 21:53:32 +01001281 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 if (isn_type == ISN_LOADSCRIPT)
1283 isn = generate_instr_type(cctx, isn_type, type);
1284 else
1285 isn = generate_instr_drop(cctx, isn_type, 1);
1286 if (isn == NULL)
1287 return FAIL;
1288 isn->isn_arg.script.script_sid = sid;
1289 isn->isn_arg.script.script_idx = idx;
1290 return OK;
1291}
1292
1293/*
1294 * Generate an ISN_NEWLIST instruction.
1295 */
1296 static int
1297generate_NEWLIST(cctx_T *cctx, int count)
1298{
1299 isn_T *isn;
1300 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 type_T *type;
1302 type_T *member;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1306 return FAIL;
1307 isn->isn_arg.number = count;
1308
Bram Moolenaar127542b2020-08-09 17:22:04 +02001309 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001310 if (count == 0)
1311 member = &t_void;
1312 else
1313 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001314 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1315 cctx->ctx_type_list);
1316 type = get_list_type(member, cctx->ctx_type_list);
1317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 // drop the value types
1319 stack->ga_len -= count;
1320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 // add the list type to the type stack
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1325 ++stack->ga_len;
1326
1327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_NEWDICT instruction.
1332 */
1333 static int
1334generate_NEWDICT(cctx_T *cctx, int count)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 type_T *type;
1339 type_T *member;
1340
Bram Moolenaar080457c2020-03-03 21:53:32 +01001341 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.number = count;
1345
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001346 if (count == 0)
1347 member = &t_void;
1348 else
1349 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001350 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1351 cctx->ctx_type_list);
1352 type = get_dict_type(member, cctx->ctx_type_list);
1353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 // drop the key and value types
1355 stack->ga_len -= 2 * count;
1356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 // add the dict type to the type stack
1358 if (ga_grow(stack, 1) == FAIL)
1359 return FAIL;
1360 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1361 ++stack->ga_len;
1362
1363 return OK;
1364}
1365
1366/*
1367 * Generate an ISN_FUNCREF instruction.
1368 */
1369 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001370generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371{
1372 isn_T *isn;
1373 garray_T *stack = &cctx->ctx_type_stack;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1377 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001378 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001379 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 if (ga_grow(stack, 1) == FAIL)
1382 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001383 ((type_T **)stack->ga_data)[stack->ga_len] =
1384 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001391 * Generate an ISN_NEWFUNC instruction.
1392 */
1393 static int
1394generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1395{
1396 isn_T *isn;
1397 char_u *name;
1398
1399 RETURN_OK_IF_SKIP(cctx);
1400 name = vim_strsave(lambda_name);
1401 if (name == NULL)
1402 return FAIL;
1403 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1404 return FAIL;
1405 isn->isn_arg.newfunc.nf_lambda = name;
1406 isn->isn_arg.newfunc.nf_global = func_name;
1407
1408 return OK;
1409}
1410
1411/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 * Generate an ISN_JUMP instruction.
1413 */
1414 static int
1415generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1416{
1417 isn_T *isn;
1418 garray_T *stack = &cctx->ctx_type_stack;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.jump.jump_when = when;
1424 isn->isn_arg.jump.jump_where = where;
1425
1426 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1427 --stack->ga_len;
1428
1429 return OK;
1430}
1431
1432 static int
1433generate_FOR(cctx_T *cctx, int loop_idx)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
1437
Bram Moolenaar080457c2020-03-03 21:53:32 +01001438 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1440 return FAIL;
1441 isn->isn_arg.forloop.for_idx = loop_idx;
1442
1443 if (ga_grow(stack, 1) == FAIL)
1444 return FAIL;
1445 // type doesn't matter, will be stored next
1446 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1447 ++stack->ga_len;
1448
1449 return OK;
1450}
1451
1452/*
1453 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001454 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 * Return FAIL if the number of arguments is wrong.
1456 */
1457 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001458generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001462 int argoff;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001463 type_T **argtypes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464
Bram Moolenaar080457c2020-03-03 21:53:32 +01001465 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001466 argoff = check_internal_func(func_idx, argcount);
1467 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 return FAIL;
1469
Bram Moolenaar389df252020-07-09 21:20:47 +02001470 if (method_call && argoff > 1)
1471 {
1472 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1473 return FAIL;
1474 isn->isn_arg.shuffle.shfl_item = argcount;
1475 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1476 }
1477
Bram Moolenaar94738d82020-10-21 14:25:07 +02001478 // Check the types of the arguments.
1479 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1480 if (argcount > 0 && internal_func_check_arg_types(
Bram Moolenaarca174532020-10-21 16:42:22 +02001481 argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001482 return FAIL;
1483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1485 return FAIL;
1486 isn->isn_arg.bfunc.cbf_idx = func_idx;
1487 isn->isn_arg.bfunc.cbf_argcount = argcount;
1488
Bram Moolenaar94738d82020-10-21 14:25:07 +02001489 // Drop the argument types and push the return type.
1490 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if (ga_grow(stack, 1) == FAIL)
1492 return FAIL;
1493 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001494 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496
1497 return OK;
1498}
1499
1500/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001501 * Generate an ISN_LISTAPPEND instruction. Works like add().
1502 * Argument count is already checked.
1503 */
1504 static int
1505generate_LISTAPPEND(cctx_T *cctx)
1506{
1507 garray_T *stack = &cctx->ctx_type_stack;
1508 type_T *list_type;
1509 type_T *item_type;
1510 type_T *expected;
1511
1512 // Caller already checked that list_type is a list.
1513 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1514 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1515 expected = list_type->tt_member;
1516 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1517 return FAIL;
1518
1519 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1520 return FAIL;
1521
1522 --stack->ga_len; // drop the argument
1523 return OK;
1524}
1525
1526/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001527 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1528 * Argument count is already checked.
1529 */
1530 static int
1531generate_BLOBAPPEND(cctx_T *cctx)
1532{
1533 garray_T *stack = &cctx->ctx_type_stack;
1534 type_T *item_type;
1535
1536 // Caller already checked that blob_type is a blob.
1537 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1538 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1539 return FAIL;
1540
1541 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1542 return FAIL;
1543
1544 --stack->ga_len; // drop the argument
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 * Generate an ISN_DCALL or ISN_UCALL instruction.
1550 * Return FAIL if the number of arguments is wrong.
1551 */
1552 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001553generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554{
1555 isn_T *isn;
1556 garray_T *stack = &cctx->ctx_type_stack;
1557 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001558 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if (argcount > regular_args && !has_varargs(ufunc))
1562 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001563 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 return FAIL;
1565 }
1566 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1567 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001568 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 return FAIL;
1570 }
1571
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001572 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001573 {
1574 int i;
1575
1576 for (i = 0; i < argcount; ++i)
1577 {
1578 type_T *expected;
1579 type_T *actual;
1580
1581 if (i < regular_args)
1582 {
1583 if (ufunc->uf_arg_types == NULL)
1584 continue;
1585 expected = ufunc->uf_arg_types[i];
1586 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001587 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1588 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001589 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001590 else
1591 expected = ufunc->uf_va_type->tt_member;
1592 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001593 if (need_type(actual, expected, -argcount + i, cctx,
1594 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001595 {
1596 arg_type_mismatch(expected, actual, i + 1);
1597 return FAIL;
1598 }
1599 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001600 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001601 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1602 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001603 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001604 }
1605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001607 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001608 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001610 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 {
1612 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1613 isn->isn_arg.dfunc.cdf_argcount = argcount;
1614 }
1615 else
1616 {
1617 // A user function may be deleted and redefined later, can't use the
1618 // ufunc pointer, need to look it up again at runtime.
1619 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1620 isn->isn_arg.ufunc.cuf_argcount = argcount;
1621 }
1622
1623 stack->ga_len -= argcount; // drop the arguments
1624 if (ga_grow(stack, 1) == FAIL)
1625 return FAIL;
1626 // add return value
1627 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1628 ++stack->ga_len;
1629
1630 return OK;
1631}
1632
1633/*
1634 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1635 */
1636 static int
1637generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1638{
1639 isn_T *isn;
1640 garray_T *stack = &cctx->ctx_type_stack;
1641
Bram Moolenaar080457c2020-03-03 21:53:32 +01001642 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1644 return FAIL;
1645 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1646 isn->isn_arg.ufunc.cuf_argcount = argcount;
1647
1648 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001649 if (ga_grow(stack, 1) == FAIL)
1650 return FAIL;
1651 // add return value
1652 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1653 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654
1655 return OK;
1656}
1657
1658/*
1659 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001660 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 */
1662 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001663generate_PCALL(
1664 cctx_T *cctx,
1665 int argcount,
1666 char_u *name,
1667 type_T *type,
1668 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669{
1670 isn_T *isn;
1671 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001672 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
Bram Moolenaar080457c2020-03-03 21:53:32 +01001674 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001675
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001676 if (type->tt_type == VAR_ANY)
1677 ret_type = &t_any;
1678 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001679 {
1680 if (type->tt_argcount != -1)
1681 {
1682 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1683
1684 if (argcount < type->tt_min_argcount - varargs)
1685 {
1686 semsg(_(e_toofewarg), "[reference]");
1687 return FAIL;
1688 }
1689 if (!varargs && argcount > type->tt_argcount)
1690 {
1691 semsg(_(e_toomanyarg), "[reference]");
1692 return FAIL;
1693 }
1694 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001695 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001696 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001697 else
1698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001699 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001700 return FAIL;
1701 }
1702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1704 return FAIL;
1705 isn->isn_arg.pfunc.cpf_top = at_top;
1706 isn->isn_arg.pfunc.cpf_argcount = argcount;
1707
1708 stack->ga_len -= argcount; // drop the arguments
1709
1710 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001711 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001713 // If partial is above the arguments it must be cleared and replaced with
1714 // the return value.
1715 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1716 return FAIL;
1717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 return OK;
1719}
1720
1721/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001722 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 */
1724 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001725generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726{
1727 isn_T *isn;
1728 garray_T *stack = &cctx->ctx_type_stack;
1729 type_T *type;
1730
Bram Moolenaar080457c2020-03-03 21:53:32 +01001731 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001732 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001734 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001736 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001738 if (type->tt_type != VAR_DICT && type != &t_any)
1739 {
1740 emsg(_(e_dictreq));
1741 return FAIL;
1742 }
1743 // change dict type to dict member type
1744 if (type->tt_type == VAR_DICT)
1745 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746
1747 return OK;
1748}
1749
1750/*
1751 * Generate an ISN_ECHO instruction.
1752 */
1753 static int
1754generate_ECHO(cctx_T *cctx, int with_white, int count)
1755{
1756 isn_T *isn;
1757
Bram Moolenaar080457c2020-03-03 21:53:32 +01001758 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1760 return FAIL;
1761 isn->isn_arg.echo.echo_with_white = with_white;
1762 isn->isn_arg.echo.echo_count = count;
1763
1764 return OK;
1765}
1766
Bram Moolenaarad39c092020-02-26 18:23:43 +01001767/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001768 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001769 */
1770 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001771generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001772{
1773 isn_T *isn;
1774
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001775 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001776 return FAIL;
1777 isn->isn_arg.number = count;
1778
1779 return OK;
1780}
1781
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001782/*
1783 * Generate an ISN_PUT instruction.
1784 */
1785 static int
1786generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1787{
1788 isn_T *isn;
1789
1790 RETURN_OK_IF_SKIP(cctx);
1791 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1792 return FAIL;
1793 isn->isn_arg.put.put_regname = regname;
1794 isn->isn_arg.put.put_lnum = lnum;
1795 return OK;
1796}
1797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 static int
1799generate_EXEC(cctx_T *cctx, char_u *line)
1800{
1801 isn_T *isn;
1802
Bram Moolenaar080457c2020-03-03 21:53:32 +01001803 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1805 return FAIL;
1806 isn->isn_arg.string = vim_strsave(line);
1807 return OK;
1808}
1809
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001810 static int
1811generate_EXECCONCAT(cctx_T *cctx, int count)
1812{
1813 isn_T *isn;
1814
1815 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1816 return FAIL;
1817 isn->isn_arg.number = count;
1818 return OK;
1819}
1820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821/*
1822 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001823 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001825 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001826reserve_local(
1827 cctx_T *cctx,
1828 char_u *name,
1829 size_t len,
1830 int isConst,
1831 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 lvar_T *lvar;
1834
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001835 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001837 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001838 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 }
1840
1841 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001842 return NULL;
1843 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001844 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001846 // Every local variable uses the next entry on the stack. We could re-use
1847 // the last ones when leaving a scope, but then variables used in a closure
1848 // might get overwritten. To keep things simple do not re-use stack
1849 // entries. This is less efficient, but memory is cheap these days.
1850 lvar->lv_idx = cctx->ctx_locals_count++;
1851
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001852 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 lvar->lv_const = isConst;
1854 lvar->lv_type = type;
1855
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001856 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857}
1858
1859/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001860 * Remove local variables above "new_top".
1861 */
1862 static void
1863unwind_locals(cctx_T *cctx, int new_top)
1864{
1865 if (cctx->ctx_locals.ga_len > new_top)
1866 {
1867 int idx;
1868 lvar_T *lvar;
1869
1870 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1871 {
1872 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1873 vim_free(lvar->lv_name);
1874 }
1875 }
1876 cctx->ctx_locals.ga_len = new_top;
1877}
1878
1879/*
1880 * Free all local variables.
1881 */
1882 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001883free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001884{
1885 unwind_locals(cctx, 0);
1886 ga_clear(&cctx->ctx_locals);
1887}
1888
1889/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 * Find "name" in script-local items of script "sid".
1891 * Returns the index in "sn_var_vals" if found.
1892 * If found but not in "sn_var_vals" returns -1.
1893 * If not found returns -2.
1894 */
1895 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001896get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897{
1898 hashtab_T *ht;
1899 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001900 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001901 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 int idx;
1903
Bram Moolenaare3d46852020-08-29 13:39:17 +02001904 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001906 if (sid == current_sctx.sc_sid)
1907 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001908 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001909
1910 if (sav == NULL)
1911 return -2;
1912 idx = sav->sav_var_vals_idx;
1913 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1914 if (check_writable && sv->sv_const)
1915 semsg(_(e_readonlyvar), name);
1916 return idx;
1917 }
1918
1919 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 ht = &SCRIPT_VARS(sid);
1921 di = find_var_in_ht(ht, 0, name, TRUE);
1922 if (di == NULL)
1923 return -2;
1924
1925 // Now find the svar_T index in sn_var_vals.
1926 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1927 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001928 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 if (sv->sv_tv == &di->di_tv)
1930 {
1931 if (check_writable && sv->sv_const)
1932 semsg(_(e_readonlyvar), name);
1933 return idx;
1934 }
1935 }
1936 return -1;
1937}
1938
1939/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001940 * Find "name" in imported items of the current script or in "cctx" if not
1941 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 */
1943 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001944find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 int idx;
1947
Bram Moolenaare3d46852020-08-29 13:39:17 +02001948 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001949 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 if (cctx != NULL)
1951 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1952 {
1953 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1954 + idx;
1955
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001956 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1957 : STRLEN(import->imp_name) == len
1958 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 return import;
1960 }
1961
Bram Moolenaarefa94442020-08-08 22:16:00 +02001962 return find_imported_in_script(name, len, current_sctx.sc_sid);
1963}
1964
1965 imported_T *
1966find_imported_in_script(char_u *name, size_t len, int sid)
1967{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001968 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001969 int idx;
1970
Bram Moolenaare3d46852020-08-29 13:39:17 +02001971 if (!SCRIPT_ID_VALID(sid))
1972 return NULL;
1973 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1975 {
1976 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1977
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001978 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1979 : STRLEN(import->imp_name) == len
1980 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 return import;
1982 }
1983 return NULL;
1984}
1985
1986/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001987 * Free all imported variables.
1988 */
1989 static void
1990free_imported(cctx_T *cctx)
1991{
1992 int idx;
1993
1994 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1995 {
1996 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1997
1998 vim_free(import->imp_name);
1999 }
2000 ga_clear(&cctx->ctx_imports);
2001}
2002
2003/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002004 * Return TRUE if "p" points at a "#" but not at "#{".
2005 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002006 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002007vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002008{
2009 return p[0] == '#' && p[1] != '{';
2010}
2011
2012/*
2013 * Return a pointer to the next line that isn't empty or only contains a
2014 * comment. Skips over white space.
2015 * Returns NULL if there is none.
2016 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002017 char_u *
2018peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002019{
2020 int lnum = cctx->ctx_lnum;
2021
2022 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2023 {
2024 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002025 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002026
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002027 // ignore NULLs inserted for continuation lines
2028 if (line != NULL)
2029 {
2030 p = skipwhite(line);
2031 if (*p != NUL && !vim9_comment_start(p))
2032 return p;
2033 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002034 }
2035 return NULL;
2036}
2037
2038/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002039 * Called when checking for a following operator at "arg". When the rest of
2040 * the line is empty or only a comment, peek the next line. If there is a next
2041 * line return a pointer to it and set "nextp".
2042 * Otherwise skip over white space.
2043 */
2044 static char_u *
2045may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2046{
2047 char_u *p = skipwhite(arg);
2048
2049 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002050 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002051 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002052 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002053 if (*nextp != NULL)
2054 return *nextp;
2055 }
2056 return p;
2057}
2058
2059/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002060 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002061 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002062 * Returns NULL when at the end.
2063 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002064 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002065next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002066{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002067 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002068
2069 do
2070 {
2071 ++cctx->ctx_lnum;
2072 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002073 {
2074 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002075 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002076 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002077 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002078 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002079 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002080 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002081 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002082 return line;
2083}
2084
2085/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002086 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002087 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002088 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2089 */
2090 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002091may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002092{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002093 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002094 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002095 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002096
2097 if (next == NULL)
2098 return FAIL;
2099 *arg = skipwhite(next);
2100 }
2101 return OK;
2102}
2103
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002104/*
2105 * Idem, and give an error when failed.
2106 */
2107 static int
2108may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2109{
2110 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2111 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002112 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002113 return FAIL;
2114 }
2115 return OK;
2116}
2117
2118
Bram Moolenaara5565e42020-05-09 15:44:01 +02002119// Structure passed between the compile_expr* functions to keep track of
2120// constants that have been parsed but for which no code was produced yet. If
2121// possible expressions on these constants are applied at compile time. If
2122// that is not possible, the code to push the constants needs to be generated
2123// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002124// Using 50 should be more than enough of 5 levels of ().
2125#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002126typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002127 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002128 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002129 int pp_is_const; // all generated code was constants, used for a
2130 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002131} ppconst_T;
2132
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002133static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002134static int compile_expr0(char_u **arg, cctx_T *cctx);
2135static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2136
Bram Moolenaara5565e42020-05-09 15:44:01 +02002137/*
2138 * Generate a PUSH instruction for "tv".
2139 * "tv" will be consumed or cleared.
2140 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2141 */
2142 static int
2143generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2144{
2145 if (tv != NULL)
2146 {
2147 switch (tv->v_type)
2148 {
2149 case VAR_UNKNOWN:
2150 break;
2151 case VAR_BOOL:
2152 generate_PUSHBOOL(cctx, tv->vval.v_number);
2153 break;
2154 case VAR_SPECIAL:
2155 generate_PUSHSPEC(cctx, tv->vval.v_number);
2156 break;
2157 case VAR_NUMBER:
2158 generate_PUSHNR(cctx, tv->vval.v_number);
2159 break;
2160#ifdef FEAT_FLOAT
2161 case VAR_FLOAT:
2162 generate_PUSHF(cctx, tv->vval.v_float);
2163 break;
2164#endif
2165 case VAR_BLOB:
2166 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2167 tv->vval.v_blob = NULL;
2168 break;
2169 case VAR_STRING:
2170 generate_PUSHS(cctx, tv->vval.v_string);
2171 tv->vval.v_string = NULL;
2172 break;
2173 default:
2174 iemsg("constant type not supported");
2175 clear_tv(tv);
2176 return FAIL;
2177 }
2178 tv->v_type = VAR_UNKNOWN;
2179 }
2180 return OK;
2181}
2182
2183/*
2184 * Generate code for any ppconst entries.
2185 */
2186 static int
2187generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2188{
2189 int i;
2190 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002191 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002192
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002193 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002194 for (i = 0; i < ppconst->pp_used; ++i)
2195 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2196 ret = FAIL;
2197 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002198 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002199 return ret;
2200}
2201
2202/*
2203 * Clear ppconst constants. Used when failing.
2204 */
2205 static void
2206clear_ppconst(ppconst_T *ppconst)
2207{
2208 int i;
2209
2210 for (i = 0; i < ppconst->pp_used; ++i)
2211 clear_tv(&ppconst->pp_tv[i]);
2212 ppconst->pp_used = 0;
2213}
2214
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002215/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002216 * Generate an instruction to load script-local variable "name", without the
2217 * leading "s:".
2218 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 */
2220 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002221compile_load_scriptvar(
2222 cctx_T *cctx,
2223 char_u *name, // variable NUL terminated
2224 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002225 char_u **end, // end of variable
2226 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002228 scriptitem_T *si;
2229 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 imported_T *import;
2231
Bram Moolenaare3d46852020-08-29 13:39:17 +02002232 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2233 return FAIL;
2234 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002235 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002236 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002238 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002239 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2240 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
2242 if (idx >= 0)
2243 {
2244 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2245
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002246 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 current_sctx.sc_sid, idx, sv->sv_type);
2248 return OK;
2249 }
2250
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002251 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 if (import != NULL)
2253 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002254 if (import->imp_all)
2255 {
2256 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002257 char_u *exp_name;
2258 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002259 ufunc_T *ufunc;
2260 type_T *type;
2261
2262 // Used "import * as Name", need to lookup the member.
2263 if (*p != '.')
2264 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002265 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002266 return FAIL;
2267 }
2268 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002269 if (VIM_ISWHITE(*p))
2270 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002271 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002272 return FAIL;
2273 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002274
Bram Moolenaar1c991142020-07-04 13:15:31 +02002275 // isolate one name
2276 exp_name = p;
2277 while (eval_isnamec(*p))
2278 ++p;
2279 cc = *p;
2280 *p = NUL;
2281
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002282 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002283 *p = cc;
2284 p = skipwhite(p);
2285
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002286 // TODO: what if it is a function?
2287 if (idx < 0)
2288 return FAIL;
2289 *end = p;
2290
2291 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2292 import->imp_sid,
2293 idx,
2294 type);
2295 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002296 else if (import->imp_funcname != NULL)
2297 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002298 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002299 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2300 import->imp_sid,
2301 import->imp_var_vals_idx,
2302 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 return OK;
2304 }
2305
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002306 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002307 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 return FAIL;
2309}
2310
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002311 static int
2312generate_funcref(cctx_T *cctx, char_u *name)
2313{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002314 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002315
2316 if (ufunc == NULL)
2317 return FAIL;
2318
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002319 // Need to compile any default values to get the argument types.
2320 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2321 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2322 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002323 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002324}
2325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326/*
2327 * Compile a variable name into a load instruction.
2328 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002329 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 * When "error" is FALSE do not give an error when not found.
2331 */
2332 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002333compile_load(
2334 char_u **arg,
2335 char_u *end_arg,
2336 cctx_T *cctx,
2337 int is_expr,
2338 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339{
2340 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002341 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002342 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002344 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345
2346 if (*(*arg + 1) == ':')
2347 {
2348 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002349 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002350 {
2351 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002353 switch (**arg)
2354 {
2355 case 'g': isn_type = ISN_LOADGDICT; break;
2356 case 'w': isn_type = ISN_LOADWDICT; break;
2357 case 't': isn_type = ISN_LOADTDICT; break;
2358 case 'b': isn_type = ISN_LOADBDICT; break;
2359 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002360 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002361 goto theend;
2362 }
2363 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2364 goto theend;
2365 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 else
2368 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002369 isntype_T isn_type = ISN_DROP;
2370
2371 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2372 if (name == NULL)
2373 return FAIL;
2374
2375 switch (**arg)
2376 {
2377 case 'v': res = generate_LOADV(cctx, name, error);
2378 break;
2379 case 's': res = compile_load_scriptvar(cctx, name,
2380 NULL, NULL, error);
2381 break;
2382 case 'g': isn_type = ISN_LOADG; break;
2383 case 'w': isn_type = ISN_LOADW; break;
2384 case 't': isn_type = ISN_LOADT; break;
2385 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002386 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002387 goto theend;
2388 }
2389 if (isn_type != ISN_DROP)
2390 {
2391 // Global, Buffer-local, Window-local and Tabpage-local
2392 // variables can be defined later, thus we don't check if it
2393 // exists, give error at runtime.
2394 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 }
2397 }
2398 else
2399 {
2400 size_t len = end - *arg;
2401 int idx;
2402 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002403 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
2405 name = vim_strnsave(*arg, end - *arg);
2406 if (name == NULL)
2407 return FAIL;
2408
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002409 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002411 if (!gen_load_outer)
2412 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 }
2414 else
2415 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002416 lvar_T *lvar = lookup_local(*arg, len, cctx);
2417
2418 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002420 type = lvar->lv_type;
2421 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002422 if (lvar->lv_from_outer)
2423 gen_load_outer = TRUE;
2424 else
2425 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 }
2427 else
2428 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002429 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002430 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002431 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002432 || find_imported(name, 0, cctx) != NULL)
2433 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002434
Bram Moolenaar0f769812020-09-12 18:32:34 +02002435 // When evaluating an expression and the name starts with an
2436 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002437 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002438 if (res == FAIL && is_expr
2439 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002440 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 }
2442 }
2443 if (gen_load)
2444 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002445 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002446 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002447 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002448 cctx->ctx_outer_used = TRUE;
2449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 }
2451
2452 *arg = end;
2453
2454theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002455 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002456 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 vim_free(name);
2458 return res;
2459}
2460
2461/*
2462 * Compile the argument expressions.
2463 * "arg" points to just after the "(" and is advanced to after the ")"
2464 */
2465 static int
2466compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2467{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002468 char_u *p = *arg;
2469 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002470 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471
Bram Moolenaare6085c52020-04-12 20:19:16 +02002472 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002474 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2475 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002476 if (*p == ')')
2477 {
2478 *arg = p + 1;
2479 return OK;
2480 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002481 if (must_end)
2482 {
2483 semsg(_(e_missing_comma_before_argument_str), p);
2484 return FAIL;
2485 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002486
Bram Moolenaara5565e42020-05-09 15:44:01 +02002487 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 return FAIL;
2489 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002490
2491 if (*p != ',' && *skipwhite(p) == ',')
2492 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002493 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002494 p = skipwhite(p);
2495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002497 {
2498 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002499 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002500 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002501 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002502 else
2503 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002504 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002505 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002507failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002508 emsg(_(e_missing_close));
2509 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510}
2511
2512/*
2513 * Compile a function call: name(arg1, arg2)
2514 * "arg" points to "name", "arg + varlen" to the "(".
2515 * "argcount_init" is 1 for "value->method()"
2516 * Instructions:
2517 * EVAL arg1
2518 * EVAL arg2
2519 * BCALL / DCALL / UCALL
2520 */
2521 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002522compile_call(
2523 char_u **arg,
2524 size_t varlen,
2525 cctx_T *cctx,
2526 ppconst_T *ppconst,
2527 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528{
2529 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002530 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 int argcount = argcount_init;
2532 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002533 char_u fname_buf[FLEN_FIXED + 1];
2534 char_u *tofree = NULL;
2535 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002537 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002538 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539
Bram Moolenaara5565e42020-05-09 15:44:01 +02002540 // we can evaluate "has('name')" at compile time
2541 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2542 {
2543 char_u *s = skipwhite(*arg + varlen + 1);
2544 typval_T argvars[2];
2545
2546 argvars[0].v_type = VAR_UNKNOWN;
2547 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002548 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002549 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002550 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002551 s = skipwhite(s);
2552 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2553 {
2554 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2555
2556 *arg = s + 1;
2557 argvars[1].v_type = VAR_UNKNOWN;
2558 tv->v_type = VAR_NUMBER;
2559 tv->vval.v_number = 0;
2560 f_has(argvars, tv);
2561 clear_tv(&argvars[0]);
2562 ++ppconst->pp_used;
2563 return OK;
2564 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002565 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002566 }
2567
2568 if (generate_ppconst(cctx, ppconst) == FAIL)
2569 return FAIL;
2570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 if (varlen >= sizeof(namebuf))
2572 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002573 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 return FAIL;
2575 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002576 vim_strncpy(namebuf, *arg, varlen);
2577 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578
2579 *arg = skipwhite(*arg + varlen + 1);
2580 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002581 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582
Bram Moolenaara1773442020-08-12 15:21:22 +02002583 is_autoload = vim_strchr(name, '#') != NULL;
2584 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 {
2586 int idx;
2587
2588 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002589 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002591 {
2592 if (STRCMP(name, "add") == 0 && argcount == 2)
2593 {
2594 garray_T *stack = &cctx->ctx_type_stack;
2595 type_T *type = ((type_T **)stack->ga_data)[
2596 stack->ga_len - 2];
2597
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002598 if (type->tt_type == VAR_LIST)
2599 {
2600 // inline "add(list, item)" so that the type can be checked
2601 res = generate_LISTAPPEND(cctx);
2602 idx = -1;
2603 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002604 else if (type->tt_type == VAR_BLOB)
2605 {
2606 // inline "add(blob, nr)" so that the type can be checked
2607 res = generate_BLOBAPPEND(cctx);
2608 idx = -1;
2609 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002610 }
2611
2612 if (idx >= 0)
2613 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2614 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002615 else
2616 semsg(_(e_unknownfunc), namebuf);
2617 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 }
2619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002621 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002622 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002623 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002624 {
2625 res = generate_CALL(cctx, ufunc, argcount);
2626 goto theend;
2627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628
2629 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002630 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002631 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002633 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002634 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002635 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002636 garray_T *stack = &cctx->ctx_type_stack;
2637 type_T *type;
2638
2639 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2640 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002641 goto theend;
2642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
Bram Moolenaar0f769812020-09-12 18:32:34 +02002644 // If we can find a global function by name generate the right call.
2645 if (ufunc != NULL)
2646 {
2647 res = generate_CALL(cctx, ufunc, argcount);
2648 goto theend;
2649 }
2650
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002651 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002652 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002653 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002654 res = generate_UCALL(cctx, name, argcount);
2655 else
2656 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002657
2658theend:
2659 vim_free(tofree);
2660 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661}
2662
2663// like NAMESPACE_CHAR but with 'a' and 'l'.
2664#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2665
2666/*
2667 * Find the end of a variable or function name. Unlike find_name_end() this
2668 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002669 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 * Return a pointer to just after the name. Equal to "arg" if there is no
2671 * valid name.
2672 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002673 static char_u *
2674to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675{
2676 char_u *p;
2677
2678 // Quick check for valid starting character.
2679 if (!eval_isnamec1(*arg))
2680 return arg;
2681
2682 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2683 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2684 // and can be used in slice "[n:]".
2685 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002686 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2688 break;
2689 return p;
2690}
2691
2692/*
2693 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002694 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 */
2696 char_u *
2697to_name_const_end(char_u *arg)
2698{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002699 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 typval_T rettv;
2701
2702 if (p == arg && *arg == '[')
2703 {
2704
2705 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002706 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 p = arg;
2708 }
2709 else if (p == arg && *arg == '#' && arg[1] == '{')
2710 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002711 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002713 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 p = arg;
2715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716
2717 return p;
2718}
2719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720/*
2721 * parse a list: [expr, expr]
2722 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002723 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 */
2725 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002726compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727{
2728 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002729 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002731 int is_const;
2732 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002734 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002736 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002737 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002738 semsg(_(e_list_end), *arg);
2739 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002740 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002741 if (*p == ',')
2742 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002743 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002744 return FAIL;
2745 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002746 if (*p == ']')
2747 {
2748 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002749 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002750 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002751 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002752 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002753 if (!is_const)
2754 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 ++count;
2756 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002757 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002759 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2760 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002761 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002762 return FAIL;
2763 }
2764 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002765 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 p = skipwhite(p);
2767 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002768 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002770 ppconst->pp_is_const = is_all_const;
2771 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772}
2773
2774/*
2775 * parse a lambda: {arg, arg -> expr}
2776 * "*arg" points to the '{'.
2777 */
2778 static int
2779compile_lambda(char_u **arg, cctx_T *cctx)
2780{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 typval_T rettv;
2782 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002783 evalarg_T evalarg;
2784
2785 CLEAR_FIELD(evalarg);
2786 evalarg.eval_flags = EVAL_EVALUATE;
2787 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788
2789 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002790 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002791 {
2792 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002794 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002797 ++ufunc->uf_refcount;
2798 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002799 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800
2801 // The function will have one line: "return {expr}".
2802 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002803 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002805 clear_evalarg(&evalarg, NULL);
2806
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002807 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002808 {
2809 // The return type will now be known.
2810 set_function_type(ufunc);
2811
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002812 // The function reference count will be 1. When the ISN_FUNCREF
2813 // instruction is deleted the reference count is decremented and the
2814 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002815 return generate_FUNCREF(cctx, ufunc);
2816 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002817
2818 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 return FAIL;
2820}
2821
2822/*
2823 * Compile a lamda call: expr->{lambda}(args)
2824 * "arg" points to the "{".
2825 */
2826 static int
2827compile_lambda_call(char_u **arg, cctx_T *cctx)
2828{
2829 ufunc_T *ufunc;
2830 typval_T rettv;
2831 int argcount = 1;
2832 int ret = FAIL;
2833
2834 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002835 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 return FAIL;
2837
2838 if (**arg != '(')
2839 {
2840 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002841 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 else
2843 semsg(_(e_missing_paren), "lambda");
2844 clear_tv(&rettv);
2845 return FAIL;
2846 }
2847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 ufunc = rettv.vval.v_partial->pt_func;
2849 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002850 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002851 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002852
Bram Moolenaara05e5242020-09-19 18:19:19 +02002853 // The function will have one line: "return {expr}". Compile it into
2854 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002855 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856
2857 // compile the arguments
2858 *arg = skipwhite(*arg + 1);
2859 if (compile_arguments(arg, cctx, &argcount) == OK)
2860 // call the compiled function
2861 ret = generate_CALL(cctx, ufunc, argcount);
2862
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002863 if (ret == FAIL)
2864 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 return ret;
2866}
2867
2868/*
2869 * parse a dict: {'key': val} or #{key: val}
2870 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 */
2873 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002874compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875{
2876 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002877 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 int count = 0;
2879 dict_T *d = dict_alloc();
2880 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002881 char_u *whitep = *arg;
2882 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002883 int is_const;
2884 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885
2886 if (d == NULL)
2887 return FAIL;
2888 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002889 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 {
2891 char_u *key = NULL;
2892
Bram Moolenaar23c55272020-06-21 16:58:13 +02002893 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002894 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002895 *arg = NULL;
2896 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002897 }
2898
2899 if (**arg == '}')
2900 break;
2901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 if (literal)
2903 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002904 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905
Bram Moolenaar2c330432020-04-13 14:41:35 +02002906 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002908 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 return FAIL;
2910 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002911 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 if (generate_PUSHS(cctx, key) == FAIL)
2913 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002914 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 }
2916 else
2917 {
2918 isn_T *isn;
2919
Bram Moolenaara5565e42020-05-09 15:44:01 +02002920 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2923 if (isn->isn_type == ISN_PUSHS)
2924 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002925 else
2926 {
2927 type_T *keytype = ((type_T **)stack->ga_data)
2928 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002929 if (need_type(keytype, &t_string, -1, cctx,
2930 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002931 return FAIL;
2932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 }
2934
2935 // Check for duplicate keys, if using string keys.
2936 if (key != NULL)
2937 {
2938 item = dict_find(d, key, -1);
2939 if (item != NULL)
2940 {
2941 semsg(_(e_duplicate_key), key);
2942 goto failret;
2943 }
2944 item = dictitem_alloc(key);
2945 if (item != NULL)
2946 {
2947 item->di_tv.v_type = VAR_UNKNOWN;
2948 item->di_tv.v_lock = 0;
2949 if (dict_add(d, item) == FAIL)
2950 dictitem_free(item);
2951 }
2952 }
2953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 if (**arg != ':')
2955 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002956 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002957 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002958 else
2959 semsg(_(e_missing_dict_colon), *arg);
2960 return FAIL;
2961 }
2962 whitep = *arg + 1;
2963 if (!IS_WHITE_OR_NUL(*whitep))
2964 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002965 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 return FAIL;
2967 }
2968
2969 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002970 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002971 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002972 *arg = NULL;
2973 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002974 }
2975
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002976 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002978 if (!is_const)
2979 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 ++count;
2981
Bram Moolenaar2c330432020-04-13 14:41:35 +02002982 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002983 *arg = skipwhite(*arg);
2984 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002985 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002986 *arg = NULL;
2987 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 if (**arg == '}')
2990 break;
2991 if (**arg != ',')
2992 {
2993 semsg(_(e_missing_dict_comma), *arg);
2994 goto failret;
2995 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002996 if (IS_WHITE_OR_NUL(*whitep))
2997 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002998 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002999 return FAIL;
3000 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003001 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003002 if (!IS_WHITE_OR_NUL(*whitep))
3003 {
3004 semsg(_(e_white_space_required_after_str), ",");
3005 return FAIL;
3006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 *arg = skipwhite(*arg + 1);
3008 }
3009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 *arg = *arg + 1;
3011
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003012 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003013 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003014 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003015 *arg += STRLEN(*arg);
3016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003018 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 return generate_NEWDICT(cctx, count);
3020
3021failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003022 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003023 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003024 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003025 *arg = (char_u *)"";
3026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 dict_unref(d);
3028 return FAIL;
3029}
3030
3031/*
3032 * Compile "&option".
3033 */
3034 static int
3035compile_get_option(char_u **arg, cctx_T *cctx)
3036{
3037 typval_T rettv;
3038 char_u *start = *arg;
3039 int ret;
3040
3041 // parse the option and get the current value to get the type.
3042 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003043 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 if (ret == OK)
3045 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003046 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 char_u *name = vim_strnsave(start, *arg - start);
3048 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3049
3050 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3051 vim_free(name);
3052 }
3053 clear_tv(&rettv);
3054
3055 return ret;
3056}
3057
3058/*
3059 * Compile "$VAR".
3060 */
3061 static int
3062compile_get_env(char_u **arg, cctx_T *cctx)
3063{
3064 char_u *start = *arg;
3065 int len;
3066 int ret;
3067 char_u *name;
3068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 ++*arg;
3070 len = get_env_len(arg);
3071 if (len == 0)
3072 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003073 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 return FAIL;
3075 }
3076
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003077 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 name = vim_strnsave(start, len + 1);
3079 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3080 vim_free(name);
3081 return ret;
3082}
3083
3084/*
3085 * Compile "@r".
3086 */
3087 static int
3088compile_get_register(char_u **arg, cctx_T *cctx)
3089{
3090 int ret;
3091
3092 ++*arg;
3093 if (**arg == NUL)
3094 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003095 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 return FAIL;
3097 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003098 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 {
3100 emsg_invreg(**arg);
3101 return FAIL;
3102 }
3103 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3104 ++*arg;
3105 return ret;
3106}
3107
3108/*
3109 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003110 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 */
3112 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003113apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003115 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116
3117 // this works from end to start
3118 while (p > start)
3119 {
3120 --p;
3121 if (*p == '-' || *p == '+')
3122 {
3123 // only '-' has an effect, for '+' we only check the type
3124#ifdef FEAT_FLOAT
3125 if (rettv->v_type == VAR_FLOAT)
3126 {
3127 if (*p == '-')
3128 rettv->vval.v_float = -rettv->vval.v_float;
3129 }
3130 else
3131#endif
3132 {
3133 varnumber_T val;
3134 int error = FALSE;
3135
3136 // tv_get_number_chk() accepts a string, but we don't want that
3137 // here
3138 if (check_not_string(rettv) == FAIL)
3139 return FAIL;
3140 val = tv_get_number_chk(rettv, &error);
3141 clear_tv(rettv);
3142 if (error)
3143 return FAIL;
3144 if (*p == '-')
3145 val = -val;
3146 rettv->v_type = VAR_NUMBER;
3147 rettv->vval.v_number = val;
3148 }
3149 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003150 else if (numeric_only)
3151 {
3152 ++p;
3153 break;
3154 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003155 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 {
3157 int v = tv2bool(rettv);
3158
3159 // '!' is permissive in the type.
3160 clear_tv(rettv);
3161 rettv->v_type = VAR_BOOL;
3162 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3163 }
3164 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003165 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 return OK;
3167}
3168
3169/*
3170 * Recognize v: variables that are constants and set "rettv".
3171 */
3172 static void
3173get_vim_constant(char_u **arg, typval_T *rettv)
3174{
3175 if (STRNCMP(*arg, "v:true", 6) == 0)
3176 {
3177 rettv->v_type = VAR_BOOL;
3178 rettv->vval.v_number = VVAL_TRUE;
3179 *arg += 6;
3180 }
3181 else if (STRNCMP(*arg, "v:false", 7) == 0)
3182 {
3183 rettv->v_type = VAR_BOOL;
3184 rettv->vval.v_number = VVAL_FALSE;
3185 *arg += 7;
3186 }
3187 else if (STRNCMP(*arg, "v:null", 6) == 0)
3188 {
3189 rettv->v_type = VAR_SPECIAL;
3190 rettv->vval.v_number = VVAL_NULL;
3191 *arg += 6;
3192 }
3193 else if (STRNCMP(*arg, "v:none", 6) == 0)
3194 {
3195 rettv->v_type = VAR_SPECIAL;
3196 rettv->vval.v_number = VVAL_NONE;
3197 *arg += 6;
3198 }
3199}
3200
Bram Moolenaar696ba232020-07-29 21:20:41 +02003201 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003202get_compare_type(char_u *p, int *len, int *type_is)
3203{
3204 exptype_T type = EXPR_UNKNOWN;
3205 int i;
3206
3207 switch (p[0])
3208 {
3209 case '=': if (p[1] == '=')
3210 type = EXPR_EQUAL;
3211 else if (p[1] == '~')
3212 type = EXPR_MATCH;
3213 break;
3214 case '!': if (p[1] == '=')
3215 type = EXPR_NEQUAL;
3216 else if (p[1] == '~')
3217 type = EXPR_NOMATCH;
3218 break;
3219 case '>': if (p[1] != '=')
3220 {
3221 type = EXPR_GREATER;
3222 *len = 1;
3223 }
3224 else
3225 type = EXPR_GEQUAL;
3226 break;
3227 case '<': if (p[1] != '=')
3228 {
3229 type = EXPR_SMALLER;
3230 *len = 1;
3231 }
3232 else
3233 type = EXPR_SEQUAL;
3234 break;
3235 case 'i': if (p[1] == 's')
3236 {
3237 // "is" and "isnot"; but not a prefix of a name
3238 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3239 *len = 5;
3240 i = p[*len];
3241 if (!isalnum(i) && i != '_')
3242 {
3243 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3244 *type_is = TRUE;
3245 }
3246 }
3247 break;
3248 }
3249 return type;
3250}
3251
3252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003254 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 */
3256 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003257compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003259 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260
3261 // this works from end to start
3262 while (p > start)
3263 {
3264 --p;
3265 if (*p == '-' || *p == '+')
3266 {
3267 int negate = *p == '-';
3268 isn_T *isn;
3269
3270 // TODO: check type
3271 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3272 {
3273 --p;
3274 if (*p == '-')
3275 negate = !negate;
3276 }
3277 // only '-' has an effect, for '+' we only check the type
3278 if (negate)
3279 isn = generate_instr(cctx, ISN_NEGATENR);
3280 else
3281 isn = generate_instr(cctx, ISN_CHECKNR);
3282 if (isn == NULL)
3283 return FAIL;
3284 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003285 else if (numeric_only)
3286 {
3287 ++p;
3288 break;
3289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 else
3291 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003292 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003294 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003296 if (p[-1] == '!')
3297 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
3300 if (generate_2BOOL(cctx, invert) == FAIL)
3301 return FAIL;
3302 }
3303 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003304 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 return OK;
3306}
3307
3308/*
3309 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003310 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 */
3312 static int
3313compile_subscript(
3314 char_u **arg,
3315 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003316 char_u *start_leader,
3317 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003318 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003320 char_u *name_start = *end_leader;
3321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 for (;;)
3323 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003324 char_u *p = skipwhite(*arg);
3325
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003326 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003327 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003328 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003329
3330 // If a following line starts with "->{" or "->X" advance to that
3331 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003332 // Also if a following line starts with ".x".
3333 if (next != NULL &&
3334 ((next[0] == '-' && next[1] == '>'
3335 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003336 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003337 {
3338 next = next_line_from_context(cctx, TRUE);
3339 if (next == NULL)
3340 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003341 *arg = next;
3342 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003343 }
3344 }
3345
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003346 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3347 // not a function call.
3348 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003350 garray_T *stack = &cctx->ctx_type_stack;
3351 type_T *type;
3352 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003354 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003355 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003356 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003359 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3360
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003361 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3363 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003364 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 return FAIL;
3366 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003367 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003369 char_u *pstart = p;
3370
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003371 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003372 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003373 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 // something->method()
3376 // Apply the '!', '-' and '+' first:
3377 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003378 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003381 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003382 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003383 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 if (**arg == '{')
3385 {
3386 // lambda call: list->{lambda}
3387 if (compile_lambda_call(arg, cctx) == FAIL)
3388 return FAIL;
3389 }
3390 else
3391 {
3392 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003393 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003394 if (!eval_isnamec1(*p))
3395 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003396 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003397 return FAIL;
3398 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003399 if (ASCII_ISALPHA(*p) && p[1] == ':')
3400 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003401 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 ;
3403 if (*p != '(')
3404 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003405 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 return FAIL;
3407 }
3408 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003409 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 return FAIL;
3411 }
3412 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003413 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003415 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003416 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003417 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003418 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003419 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003420
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003421 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003422 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003423 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003424 // TODO: blob index
3425 // TODO: more arguments
3426 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003427 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003428 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003429 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003430
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003431 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003432 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003433 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003434 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003435 if (**arg == ':')
3436 // missing first index is equal to zero
3437 generate_PUSHNR(cctx, 0);
3438 else
3439 {
3440 if (compile_expr0(arg, cctx) == FAIL)
3441 return FAIL;
3442 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3443 return FAIL;
3444 *arg = skipwhite(*arg);
3445 }
3446 if (**arg == ':')
3447 {
3448 *arg = skipwhite(*arg + 1);
3449 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3450 return FAIL;
3451 if (**arg == ']')
3452 // missing second index is equal to end of string
3453 generate_PUSHNR(cctx, -1);
3454 else
3455 {
3456 if (compile_expr0(arg, cctx) == FAIL)
3457 return FAIL;
3458 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3459 return FAIL;
3460 *arg = skipwhite(*arg);
3461 }
3462 is_slice = TRUE;
3463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464
3465 if (**arg != ']')
3466 {
3467 emsg(_(e_missbrac));
3468 return FAIL;
3469 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003470 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003472 // We can index a list and a dict. If we don't know the type
3473 // we can use the index value type.
3474 // TODO: If we don't know use an instruction to figure it out at
3475 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003476 typep = ((type_T **)stack->ga_data) + stack->ga_len
3477 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003478 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003479 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3480 // If the index is a string, the variable must be a Dict.
3481 if (*typep == &t_any && valtype == &t_string)
3482 vtype = VAR_DICT;
3483 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003484 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003485 if (need_type(valtype, &t_number, -1, cctx,
3486 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003487 return FAIL;
3488 if (is_slice)
3489 {
3490 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003491 if (need_type(valtype, &t_number, -2, cctx,
3492 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003493 return FAIL;
3494 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003495 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003496
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003497 if (vtype == VAR_DICT)
3498 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003499 if (is_slice)
3500 {
3501 emsg(_(e_cannot_slice_dictionary));
3502 return FAIL;
3503 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003504 if ((*typep)->tt_type == VAR_DICT)
3505 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003506 else
3507 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003508 if (need_type(*typep, &t_dict_any, -2, cctx,
3509 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003510 return FAIL;
3511 *typep = &t_any;
3512 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003513 if (may_generate_2STRING(-1, cctx) == FAIL)
3514 return FAIL;
3515 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3516 return FAIL;
3517 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003518 else if (vtype == VAR_STRING)
3519 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003520 *typep = &t_string;
3521 if ((is_slice
3522 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3523 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003524 return FAIL;
3525 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003526 else if (vtype == VAR_BLOB)
3527 {
3528 emsg("Sorry, blob index and slice not implemented yet");
3529 return FAIL;
3530 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003531 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003532 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003533 if (is_slice)
3534 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003535 if (generate_instr_drop(cctx,
3536 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3537 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003538 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003539 }
Bram Moolenaared591872020-08-15 22:14:53 +02003540 else
3541 {
3542 if ((*typep)->tt_type == VAR_LIST)
3543 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003544 if (generate_instr_drop(cctx,
3545 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3546 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003547 return FAIL;
3548 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003549 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003550 else
3551 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003552 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003553 return FAIL;
3554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003556 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003558 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003559 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003560 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003561 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003562
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003563 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003564 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003565 {
3566 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003567 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003568 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003569 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003570 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 while (eval_isnamec(*p))
3572 MB_PTR_ADV(p);
3573 if (p == *arg)
3574 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003575 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 return FAIL;
3577 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003578 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 return FAIL;
3580 *arg = p;
3581 }
3582 else
3583 break;
3584 }
3585
3586 // TODO - see handle_subscript():
3587 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3588 // Don't do this when "Func" is already a partial that was bound
3589 // explicitly (pt_auto is FALSE).
3590
3591 return OK;
3592}
3593
3594/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003595 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3596 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003598 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003599 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003600 *
3601 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 */
3603
3604/*
3605 * number number constant
3606 * 0zFFFFFFFF Blob constant
3607 * "string" string constant
3608 * 'string' literal string constant
3609 * &option-name option value
3610 * @r register contents
3611 * identifier variable value
3612 * function() function call
3613 * $VAR environment variable
3614 * (expression) nested expression
3615 * [expr, expr] List
3616 * {key: val, key: val} Dictionary
3617 * #{key: val, key: val} Dictionary with literal keys
3618 *
3619 * Also handle:
3620 * ! in front logical NOT
3621 * - in front unary minus
3622 * + in front unary plus (ignored)
3623 * trailing (arg) funcref/partial call
3624 * trailing [] subscript in String or List
3625 * trailing .name entry in Dictionary
3626 * trailing ->name() method call
3627 */
3628 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003629compile_expr7(
3630 char_u **arg,
3631 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003632 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 char_u *start_leader, *end_leader;
3635 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003636 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003637 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003639 ppconst->pp_is_const = FALSE;
3640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 /*
3642 * Skip '!', '-' and '+' characters. They are handled later.
3643 */
3644 start_leader = *arg;
3645 while (**arg == '!' || **arg == '-' || **arg == '+')
3646 *arg = skipwhite(*arg + 1);
3647 end_leader = *arg;
3648
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003649 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 switch (**arg)
3651 {
3652 /*
3653 * Number constant.
3654 */
3655 case '0': // also for blob starting with 0z
3656 case '1':
3657 case '2':
3658 case '3':
3659 case '4':
3660 case '5':
3661 case '6':
3662 case '7':
3663 case '8':
3664 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003665 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003667 // Apply "-" and "+" just before the number now, right to
3668 // left. Matters especially when "->" follows. Stops at
3669 // '!'.
3670 if (apply_leader(rettv, TRUE,
3671 start_leader, &end_leader) == FAIL)
3672 {
3673 clear_tv(rettv);
3674 return FAIL;
3675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
3677
3678 /*
3679 * String constant: "string".
3680 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003681 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 return FAIL;
3683 break;
3684
3685 /*
3686 * Literal string constant: 'str''ing'.
3687 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003688 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 return FAIL;
3690 break;
3691
3692 /*
3693 * Constant Vim variable.
3694 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003695 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 ret = NOTDONE;
3697 break;
3698
3699 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003700 * "true" constant
3701 */
3702 case 't': if (STRNCMP(*arg, "true", 4) == 0
3703 && !eval_isnamec((*arg)[4]))
3704 {
3705 *arg += 4;
3706 rettv->v_type = VAR_BOOL;
3707 rettv->vval.v_number = VVAL_TRUE;
3708 }
3709 else
3710 ret = NOTDONE;
3711 break;
3712
3713 /*
3714 * "false" constant
3715 */
3716 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3717 && !eval_isnamec((*arg)[5]))
3718 {
3719 *arg += 5;
3720 rettv->v_type = VAR_BOOL;
3721 rettv->vval.v_number = VVAL_FALSE;
3722 }
3723 else
3724 ret = NOTDONE;
3725 break;
3726
3727 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 * List: [expr, expr]
3729 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003730 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 break;
3732
3733 /*
3734 * Dictionary: #{key: val, key: val}
3735 */
3736 case '#': if ((*arg)[1] == '{')
3737 {
3738 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003739 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 }
3741 else
3742 ret = NOTDONE;
3743 break;
3744
3745 /*
3746 * Lambda: {arg, arg -> expr}
3747 * Dictionary: {'key': val, 'key': val}
3748 */
3749 case '{': {
3750 char_u *start = skipwhite(*arg + 1);
3751
3752 // Find out what comes after the arguments.
3753 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003754 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 if (ret != FAIL && *start == '>')
3756 ret = compile_lambda(arg, cctx);
3757 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003758 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 }
3760 break;
3761
3762 /*
3763 * Option value: &name
3764 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003765 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3766 return FAIL;
3767 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 break;
3769
3770 /*
3771 * Environment variable: $VAR.
3772 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003773 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3774 return FAIL;
3775 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 break;
3777
3778 /*
3779 * Register contents: @r.
3780 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003781 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3782 return FAIL;
3783 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 break;
3785 /*
3786 * nested expression: (expression).
3787 */
3788 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003789
3790 // recursive!
3791 if (ppconst->pp_used <= PPSIZE - 10)
3792 {
3793 ret = compile_expr1(arg, cctx, ppconst);
3794 }
3795 else
3796 {
3797 // Not enough space in ppconst, flush constants.
3798 if (generate_ppconst(cctx, ppconst) == FAIL)
3799 return FAIL;
3800 ret = compile_expr0(arg, cctx);
3801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 *arg = skipwhite(*arg);
3803 if (**arg == ')')
3804 ++*arg;
3805 else if (ret == OK)
3806 {
3807 emsg(_(e_missing_close));
3808 ret = FAIL;
3809 }
3810 break;
3811
3812 default: ret = NOTDONE;
3813 break;
3814 }
3815 if (ret == FAIL)
3816 return FAIL;
3817
Bram Moolenaar1c747212020-05-09 18:28:34 +02003818 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003820 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003821 clear_tv(rettv);
3822 else
3823 // A constant expression can possibly be handled compile time,
3824 // return the value instead of generating code.
3825 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 }
3827 else if (ret == NOTDONE)
3828 {
3829 char_u *p;
3830 int r;
3831
3832 if (!eval_isnamec1(**arg))
3833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003834 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 return FAIL;
3836 }
3837
3838 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003839 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003841 {
3842 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003845 {
3846 if (generate_ppconst(cctx, ppconst) == FAIL)
3847 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003848 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 if (r == FAIL)
3851 return FAIL;
3852 }
3853
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003854 // Handle following "[]", ".member", etc.
3855 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003856 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003857 ppconst) == FAIL)
3858 return FAIL;
3859 if (ppconst->pp_used > 0)
3860 {
3861 // apply the '!', '-' and '+' before the constant
3862 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003863 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003864 return FAIL;
3865 return OK;
3866 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003867 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003869 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870}
3871
3872/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003873 * Give the "white on both sides" error, taking the operator from "p[len]".
3874 */
3875 void
3876error_white_both(char_u *op, int len)
3877{
3878 char_u buf[10];
3879
3880 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003881 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003882}
3883
3884/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003885 * <type>expr7: runtime type check / conversion
3886 */
3887 static int
3888compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3889{
3890 type_T *want_type = NULL;
3891
3892 // Recognize <type>
3893 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3894 {
3895 int called_emsg_before = called_emsg;
3896
3897 ++*arg;
3898 want_type = parse_type(arg, cctx->ctx_type_list);
3899 if (called_emsg != called_emsg_before)
3900 return FAIL;
3901
3902 if (**arg != '>')
3903 {
3904 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003905 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003906 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003907 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003908 return FAIL;
3909 }
3910 ++*arg;
3911 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3912 return FAIL;
3913 }
3914
3915 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3916 return FAIL;
3917
3918 if (want_type != NULL)
3919 {
3920 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003921 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003922
Bram Moolenaard1103582020-08-14 22:44:25 +02003923 generate_ppconst(cctx, ppconst);
3924 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003925 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003926 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003927 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003928 return FAIL;
3929 }
3930 }
3931
3932 return OK;
3933}
3934
3935/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 * * number multiplication
3937 * / number division
3938 * % number modulo
3939 */
3940 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003941compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942{
3943 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003944 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003945 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003947 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003948 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return FAIL;
3950
3951 /*
3952 * Repeat computing, until no "*", "/" or "%" is following.
3953 */
3954 for (;;)
3955 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003956 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 if (*op != '*' && *op != '/' && *op != '%')
3958 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003959 if (next != NULL)
3960 {
3961 *arg = next_line_from_context(cctx, TRUE);
3962 op = skipwhite(*arg);
3963 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003964
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003965 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003967 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003968 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 }
3970 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003971 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003972 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003974 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003975 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003977
3978 if (ppconst->pp_used == ppconst_used + 2
3979 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3980 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003981 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003982 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3983 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003984 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003986 // both are numbers: compute the result
3987 switch (*op)
3988 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003989 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003990 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003991 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003992 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003993 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003994 break;
3995 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003996 tv1->vval.v_number = res;
3997 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003998 }
3999 else
4000 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004001 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004002 generate_two_op(cctx, op);
4003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 }
4005
4006 return OK;
4007}
4008
4009/*
4010 * + number addition
4011 * - number subtraction
4012 * .. string concatenation
4013 */
4014 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016{
4017 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004018 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004020 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
4022 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 return FAIL;
4025
4026 /*
4027 * Repeat computing, until no "+", "-" or ".." is following.
4028 */
4029 for (;;)
4030 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004031 op = may_peek_next_line(cctx, *arg, &next);
4032 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 break;
4034 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004035 if (next != NULL)
4036 {
4037 *arg = next_line_from_context(cctx, TRUE);
4038 op = skipwhite(*arg);
4039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004041 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004043 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004044 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 }
4046
4047 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004048 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004049 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004051 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004052 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 return FAIL;
4054
Bram Moolenaara5565e42020-05-09 15:44:01 +02004055 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004056 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4058 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4059 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4060 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004062 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4063 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004064
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004065 // concat/subtract/add constant numbers
4066 if (*op == '+')
4067 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4068 else if (*op == '-')
4069 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4070 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004071 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004072 // concatenate constant strings
4073 char_u *s1 = tv1->vval.v_string;
4074 char_u *s2 = tv2->vval.v_string;
4075 size_t len1 = STRLEN(s1);
4076
4077 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4078 if (tv1->vval.v_string == NULL)
4079 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004081 return FAIL;
4082 }
4083 mch_memmove(tv1->vval.v_string, s1, len1);
4084 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004085 vim_free(s1);
4086 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004087 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 }
4090 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004091 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004092 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004093 if (*op == '.')
4094 {
4095 if (may_generate_2STRING(-2, cctx) == FAIL
4096 || may_generate_2STRING(-1, cctx) == FAIL)
4097 return FAIL;
4098 generate_instr_drop(cctx, ISN_CONCAT, 1);
4099 }
4100 else
4101 generate_two_op(cctx, op);
4102 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 }
4104
4105 return OK;
4106}
4107
4108/*
4109 * expr5a == expr5b
4110 * expr5a =~ expr5b
4111 * expr5a != expr5b
4112 * expr5a !~ expr5b
4113 * expr5a > expr5b
4114 * expr5a >= expr5b
4115 * expr5a < expr5b
4116 * expr5a <= expr5b
4117 * expr5a is expr5b
4118 * expr5a isnot expr5b
4119 *
4120 * Produces instructions:
4121 * EVAL expr5a Push result of "expr5a"
4122 * EVAL expr5b Push result of "expr5b"
4123 * COMPARE one of the compare instructions
4124 */
4125 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004126compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127{
4128 exptype_T type = EXPR_UNKNOWN;
4129 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004130 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004133 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134
4135 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 return FAIL;
4138
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004139 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004140 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141
4142 /*
4143 * If there is a comparative operator, use it.
4144 */
4145 if (type != EXPR_UNKNOWN)
4146 {
4147 int ic = FALSE; // Default: do not ignore case
4148
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004149 if (next != NULL)
4150 {
4151 *arg = next_line_from_context(cctx, TRUE);
4152 p = skipwhite(*arg);
4153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 if (type_is && (p[len] == '?' || p[len] == '#'))
4155 {
4156 semsg(_(e_invexpr2), *arg);
4157 return FAIL;
4158 }
4159 // extra question mark appended: ignore case
4160 if (p[len] == '?')
4161 {
4162 ic = TRUE;
4163 ++len;
4164 }
4165 // extra '#' appended: match case (ignored)
4166 else if (p[len] == '#')
4167 ++len;
4168 // nothing appended: match case
4169
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004170 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004172 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004173 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 }
4175
4176 // get the second variable
4177 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004178 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004179 return FAIL;
4180
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 return FAIL;
4183
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 if (ppconst->pp_used == ppconst_used + 2)
4185 {
4186 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4187 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4188 int ret;
4189
4190 // Both sides are a constant, compute the result now.
4191 // First check for a valid combination of types, this is more
4192 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004193 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004194 ret = FAIL;
4195 else
4196 {
4197 ret = typval_compare(tv1, tv2, type, ic);
4198 tv1->v_type = VAR_BOOL;
4199 tv1->vval.v_number = tv1->vval.v_number
4200 ? VVAL_TRUE : VVAL_FALSE;
4201 clear_tv(tv2);
4202 --ppconst->pp_used;
4203 }
4204 return ret;
4205 }
4206
4207 generate_ppconst(cctx, ppconst);
4208 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 }
4210
4211 return OK;
4212}
4213
Bram Moolenaar7f141552020-05-09 17:35:53 +02004214static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216/*
4217 * Compile || or &&.
4218 */
4219 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220compile_and_or(
4221 char_u **arg,
4222 cctx_T *cctx,
4223 char *op,
4224 ppconst_T *ppconst,
4225 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004227 char_u *next;
4228 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 int opchar = *op;
4230
4231 if (p[0] == opchar && p[1] == opchar)
4232 {
4233 garray_T *instr = &cctx->ctx_instr;
4234 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004235 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004236 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237
4238 /*
4239 * Repeat until there is no following "||" or "&&"
4240 */
4241 ga_init2(&end_ga, sizeof(int), 10);
4242 while (p[0] == opchar && p[1] == opchar)
4243 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004244 if (next != NULL)
4245 {
4246 *arg = next_line_from_context(cctx, TRUE);
4247 p = skipwhite(*arg);
4248 }
4249
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004250 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4251 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004252 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004253 return FAIL;
4254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004256 // TODO: use ppconst if the value is a constant and check
4257 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 generate_ppconst(cctx, ppconst);
4259
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004260 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4261 all_bool_values = FALSE;
4262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 if (ga_grow(&end_ga, 1) == FAIL)
4264 {
4265 ga_clear(&end_ga);
4266 return FAIL;
4267 }
4268 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4269 ++end_ga.ga_len;
4270 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004271 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272
4273 // eval the next expression
4274 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004275 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004276 return FAIL;
4277
Bram Moolenaara5565e42020-05-09 15:44:01 +02004278 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4279 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 {
4281 ga_clear(&end_ga);
4282 return FAIL;
4283 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004284
4285 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288
4289 // Fill in the end label in all jumps.
4290 while (end_ga.ga_len > 0)
4291 {
4292 isn_T *isn;
4293
4294 --end_ga.ga_len;
4295 isn = ((isn_T *)instr->ga_data)
4296 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4297 isn->isn_arg.jump.jump_where = instr->ga_len;
4298 }
4299 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004300
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004301 // The resulting type is converted to bool if needed.
4302 if (!all_bool_values)
4303 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 }
4305
4306 return OK;
4307}
4308
4309/*
4310 * expr4a && expr4a && expr4a logical AND
4311 *
4312 * Produces instructions:
4313 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004314 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004316 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004318 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 * end:
4320 */
4321 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004324 int ppconst_used = ppconst->pp_used;
4325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 return FAIL;
4329
4330 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004331 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332}
4333
4334/*
4335 * expr3a || expr3b || expr3c logical OR
4336 *
4337 * Produces instructions:
4338 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004339 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004341 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004343 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 * end:
4345 */
4346 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349 int ppconst_used = ppconst->pp_used;
4350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 return FAIL;
4354
4355 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004356 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357}
4358
4359/*
4360 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004362 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 * JUMP_IF_FALSE alt jump if false
4364 * EVAL expr1a
4365 * JUMP_ALWAYS end
4366 * alt: EVAL expr1b
4367 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004368 *
4369 * Toplevel expression: expr2 ?? expr1
4370 * Produces instructions:
4371 * EVAL expr2 Push result of "expr2"
4372 * JUMP_AND_KEEP_IF_TRUE end jump if true
4373 * EVAL expr1
4374 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 */
4376 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004377compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378{
4379 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004380 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004381 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382
Bram Moolenaar3988f642020-08-27 22:43:03 +02004383 // Ignore all kinds of errors when not producing code.
4384 if (cctx->ctx_skip == SKIP_YES)
4385 {
4386 skip_expr(arg);
4387 return OK;
4388 }
4389
Bram Moolenaar61a89812020-05-07 16:58:17 +02004390 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 return FAIL;
4393
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004394 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 if (*p == '?')
4396 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004397 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 garray_T *instr = &cctx->ctx_instr;
4399 garray_T *stack = &cctx->ctx_type_stack;
4400 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004401 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004403 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004404 int has_const_expr = FALSE;
4405 int const_value = FALSE;
4406 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004408 if (next != NULL)
4409 {
4410 *arg = next_line_from_context(cctx, TRUE);
4411 p = skipwhite(*arg);
4412 }
4413
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004414 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004415 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004416 semsg(_(e_white_space_required_before_and_after_str),
4417 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004418 return FAIL;
4419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421 if (ppconst->pp_used == ppconst_used + 1)
4422 {
4423 // the condition is a constant, we know whether the ? or the :
4424 // expression is to be evaluated.
4425 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004426 if (op_falsy)
4427 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4428 else
4429 {
4430 int error = FALSE;
4431
4432 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4433 &error);
4434 if (error)
4435 return FAIL;
4436 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004437 cctx->ctx_skip = save_skip == SKIP_YES ||
4438 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4439
4440 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4441 // "left ?? right" and "left" is truthy: produce "left"
4442 generate_ppconst(cctx, ppconst);
4443 else
4444 {
4445 clear_tv(&ppconst->pp_tv[ppconst_used]);
4446 --ppconst->pp_used;
4447 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448 }
4449 else
4450 {
4451 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004452 if (op_falsy)
4453 end_idx = instr->ga_len;
4454 generate_JUMP(cctx, op_falsy
4455 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4456 if (op_falsy)
4457 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459
4460 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004461 *arg = skipwhite(p + 1 + op_falsy);
4462 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004463 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004465 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466
Bram Moolenaara5565e42020-05-09 15:44:01 +02004467 if (!has_const_expr)
4468 {
4469 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004471 if (!op_falsy)
4472 {
4473 // remember the type and drop it
4474 --stack->ga_len;
4475 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004477 end_idx = instr->ga_len;
4478 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004479
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004480 // jump here from JUMP_IF_FALSE
4481 isn = ((isn_T *)instr->ga_data) + alt_idx;
4482 isn->isn_arg.jump.jump_where = instr->ga_len;
4483 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004486 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004488 // Check for the ":".
4489 p = may_peek_next_line(cctx, *arg, &next);
4490 if (*p != ':')
4491 {
4492 emsg(_(e_missing_colon));
4493 return FAIL;
4494 }
4495 if (next != NULL)
4496 {
4497 *arg = next_line_from_context(cctx, TRUE);
4498 p = skipwhite(*arg);
4499 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004500
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004501 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4502 {
4503 semsg(_(e_white_space_required_before_and_after_str), ":");
4504 return FAIL;
4505 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004507 // evaluate the third expression
4508 if (has_const_expr)
4509 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004510 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004511 *arg = skipwhite(p + 1);
4512 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4513 return FAIL;
4514 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4515 return FAIL;
4516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517
Bram Moolenaara5565e42020-05-09 15:44:01 +02004518 if (!has_const_expr)
4519 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004520 type_T **typep;
4521
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523
Bram Moolenaara5565e42020-05-09 15:44:01 +02004524 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004525 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4526 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004528 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 isn = ((isn_T *)instr->ga_data) + end_idx;
4530 isn->isn_arg.jump.jump_where = instr->ga_len;
4531 }
4532
4533 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 }
4535 return OK;
4536}
4537
4538/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004539 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004540 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4541 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004542 */
4543 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004544compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004545{
4546 ppconst_T ppconst;
4547
4548 CLEAR_FIELD(ppconst);
4549 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4550 {
4551 clear_ppconst(&ppconst);
4552 return FAIL;
4553 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004554 if (is_const != NULL)
4555 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004556 if (generate_ppconst(cctx, &ppconst) == FAIL)
4557 return FAIL;
4558 return OK;
4559}
4560
4561/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004562 * Toplevel expression.
4563 */
4564 static int
4565compile_expr0(char_u **arg, cctx_T *cctx)
4566{
4567 return compile_expr0_ext(arg, cctx, NULL);
4568}
4569
4570/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 * compile "return [expr]"
4572 */
4573 static char_u *
4574compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4575{
4576 char_u *p = arg;
4577 garray_T *stack = &cctx->ctx_type_stack;
4578 type_T *stack_type;
4579
4580 if (*p != NUL && *p != '|' && *p != '\n')
4581 {
4582 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004583 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 return NULL;
4585
4586 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4587 if (set_return_type)
4588 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004589 else
4590 {
4591 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4592 && stack_type->tt_type != VAR_VOID
4593 && stack_type->tt_type != VAR_UNKNOWN)
4594 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004595 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004596 return NULL;
4597 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004598 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004599 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004600 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 }
4603 else
4604 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004605 // "set_return_type" cannot be TRUE, only used for a lambda which
4606 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004607 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4608 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004610 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 return NULL;
4612 }
4613
4614 // No argument, return zero.
4615 generate_PUSHNR(cctx, 0);
4616 }
4617
4618 if (generate_instr(cctx, ISN_RETURN) == NULL)
4619 return NULL;
4620
4621 // "return val | endif" is possible
4622 return skipwhite(p);
4623}
4624
4625/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004626 * Get a line from the compilation context, compatible with exarg_T getline().
4627 * Return a pointer to the line in allocated memory.
4628 * Return NULL for end-of-file or some error.
4629 */
4630 static char_u *
4631exarg_getline(
4632 int c UNUSED,
4633 void *cookie,
4634 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004635 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004636{
4637 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004638 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004639
Bram Moolenaar66250c92020-08-20 15:02:42 +02004640 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004641 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004642 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004643 return NULL;
4644 ++cctx->ctx_lnum;
4645 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4646 // Comment lines result in NULL pointers, skip them.
4647 if (p != NULL)
4648 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004649 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004650}
4651
4652/*
4653 * Compile a nested :def command.
4654 */
4655 static char_u *
4656compile_nested_function(exarg_T *eap, cctx_T *cctx)
4657{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004658 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004659 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004660 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004661 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004662 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004663 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004664
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004665 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004666 {
4667 emsg(_(e_cannot_use_bang_with_nested_def));
4668 return NULL;
4669 }
4670
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004671 // Only g:Func() can use a namespace.
4672 if (name_start[1] == ':' && !is_global)
4673 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004674 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004675 return NULL;
4676 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004677 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4678 return NULL;
4679
Bram Moolenaar04b12692020-05-04 23:24:44 +02004680 eap->arg = name_end;
4681 eap->getline = exarg_getline;
4682 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004683 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004684 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004685 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004686 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004687
Bram Moolenaar822ba242020-05-24 23:00:18 +02004688 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004689 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004690 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004691 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004692 {
4693 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004694 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004695 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004696
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004697 if (is_global)
4698 {
4699 char_u *func_name = vim_strnsave(name_start + 2,
4700 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004701
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004702 if (func_name == NULL)
4703 r = FAIL;
4704 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004705 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004706 }
4707 else
4708 {
4709 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004710 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004711 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004712 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004713
Bram Moolenaareef21022020-08-01 22:16:43 +02004714 if (lvar == NULL)
4715 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004716 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004717 return NULL;
4718 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004719
4720 // copy over the block scope IDs
4721 if (block_depth > 0)
4722 {
4723 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4724 if (ufunc->uf_block_ids != NULL)
4725 {
4726 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4727 sizeof(int) * block_depth);
4728 ufunc->uf_block_depth = block_depth;
4729 }
4730 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004731 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004732
Bram Moolenaar61a89812020-05-07 16:58:17 +02004733 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004734 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004735}
4736
4737/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 * Return the length of an assignment operator, or zero if there isn't one.
4739 */
4740 int
4741assignment_len(char_u *p, int *heredoc)
4742{
4743 if (*p == '=')
4744 {
4745 if (p[1] == '<' && p[2] == '<')
4746 {
4747 *heredoc = TRUE;
4748 return 3;
4749 }
4750 return 1;
4751 }
4752 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4753 return 2;
4754 if (STRNCMP(p, "..=", 3) == 0)
4755 return 3;
4756 return 0;
4757}
4758
4759// words that cannot be used as a variable
4760static char *reserved[] = {
4761 "true",
4762 "false",
4763 NULL
4764};
4765
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004766typedef enum {
4767 dest_local,
4768 dest_option,
4769 dest_env,
4770 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004771 dest_buffer,
4772 dest_window,
4773 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004774 dest_vimvar,
4775 dest_script,
4776 dest_reg,
4777} assign_dest_T;
4778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004780 * Generate the load instruction for "name".
4781 */
4782 static void
4783generate_loadvar(
4784 cctx_T *cctx,
4785 assign_dest_T dest,
4786 char_u *name,
4787 lvar_T *lvar,
4788 type_T *type)
4789{
4790 switch (dest)
4791 {
4792 case dest_option:
4793 // TODO: check the option exists
4794 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4795 break;
4796 case dest_global:
4797 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4798 break;
4799 case dest_buffer:
4800 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4801 break;
4802 case dest_window:
4803 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4804 break;
4805 case dest_tab:
4806 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4807 break;
4808 case dest_script:
4809 compile_load_scriptvar(cctx,
4810 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4811 break;
4812 case dest_env:
4813 // Include $ in the name here
4814 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4815 break;
4816 case dest_reg:
4817 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4818 break;
4819 case dest_vimvar:
4820 generate_LOADV(cctx, name + 2, TRUE);
4821 break;
4822 case dest_local:
4823 if (lvar->lv_from_outer)
4824 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4825 NULL, type);
4826 else
4827 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4828 break;
4829 }
4830}
4831
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004832 void
4833vim9_declare_error(char_u *name)
4834{
4835 char *scope = "";
4836
4837 switch (*name)
4838 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004839 case 'g': scope = _("global"); break;
4840 case 'b': scope = _("buffer"); break;
4841 case 'w': scope = _("window"); break;
4842 case 't': scope = _("tab"); break;
4843 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004844 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004845 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004846 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004847 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004848 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004849 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004850 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004851 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004852 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004853}
4854
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004855/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004856 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004857 * "let name"
4858 * "var name = expr"
4859 * "final name = expr"
4860 * "const name = expr"
4861 * "name = expr"
4862 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004863 * Return NULL for an error.
4864 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 */
4866 static char_u *
4867compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4868{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004871 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 char_u *ret = NULL;
4873 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004874 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004875 int scriptvar_sid = 0;
4876 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004879 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 int oplen = 0;
4882 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004883 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004884 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004887 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4888 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004890 // Skip over the "var" or "[var, var]" to get to any "=".
4891 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4892 if (p == NULL)
4893 return *arg == '[' ? arg : NULL;
4894
4895 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004897 // TODO: should we allow this, and figure out type inference from list
4898 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004899 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 return NULL;
4901 }
4902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 sp = p;
4904 p = skipwhite(p);
4905 op = p;
4906 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004907
4908 if (var_count > 0 && oplen == 0)
4909 // can be something like "[1, 2]->func()"
4910 return arg;
4911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4913 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004914 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004916 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 if (heredoc)
4919 {
4920 list_T *l;
4921 listitem_T *li;
4922
4923 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004924 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004926 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004927 if (l == NULL)
4928 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929
Bram Moolenaar078269b2020-09-21 20:35:55 +02004930 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004932 // Push each line and the create the list.
4933 FOR_ALL_LIST_ITEMS(l, li)
4934 {
4935 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4936 li->li_tv.vval.v_string = NULL;
4937 }
4938 generate_NEWLIST(cctx, l->lv_len);
4939 type = &t_list_string;
4940 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 list_free(l);
4943 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004944 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 // for "[var, var] = expr" evaluate the expression here, loop over the
4949 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004950
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004951 p = skipwhite(op + oplen);
4952 if (compile_expr0(&p, cctx) == FAIL)
4953 return NULL;
4954 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004956 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004958 type_T *stacktype;
4959
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004960 stacktype = stack->ga_len == 0 ? &t_void
4961 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004964 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004965 goto theend;
4966 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004967 if (need_type(stacktype, &t_list_any, -1, cctx,
4968 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004969 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004970 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004971 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4972 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004973 }
4974 }
4975
4976 /*
4977 * Loop over variables in "[var, var] = expr".
4978 * For "var = expr" and "let var: type" this is done only once.
4979 */
4980 if (var_count > 0)
4981 var_start = skipwhite(arg + 1); // skip over the "["
4982 else
4983 var_start = arg;
4984 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4985 {
4986 char_u *var_end = skip_var_one(var_start, FALSE);
4987 size_t varlen;
4988 int new_local = FALSE;
4989 int opt_type;
4990 int opt_flags = 0;
4991 assign_dest_T dest = dest_local;
4992 int vimvaridx = -1;
4993 lvar_T *lvar = NULL;
4994 lvar_T arg_lvar;
4995 int has_type = FALSE;
4996 int has_index = FALSE;
4997 int instr_count = -1;
4998
Bram Moolenaar65821722020-08-02 18:58:54 +02004999 if (*var_start == '@')
5000 p = var_start + 2;
5001 else
5002 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005003 // skip over the leading "&", "&l:", "&g:" and "$"
5004 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005005 p = to_name_end(p, TRUE);
5006 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007
5008 // "a: type" is declaring variable "a" with a type, not "a:".
5009 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5010 --var_end;
5011 if (is_decl && p == var_start + 2 && p[-1] == ':')
5012 --p;
5013
5014 varlen = p - var_start;
5015 vim_free(name);
5016 name = vim_strnsave(var_start, varlen);
5017 if (name == NULL)
5018 return NULL;
5019 if (!heredoc)
5020 type = &t_any;
5021
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005022 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005024 int declare_error = FALSE;
5025
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 if (*var_start == '&')
5027 {
5028 int cc;
5029 long numval;
5030
5031 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005032 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 emsg(_(e_const_option));
5035 goto theend;
5036 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005037 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005038 p = var_start;
5039 p = find_option_end(&p, &opt_flags);
5040 if (p == NULL)
5041 {
5042 // cannot happen?
5043 emsg(_(e_letunexp));
5044 goto theend;
5045 }
5046 cc = *p;
5047 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005048 opt_type = get_option_value(skip_option_env_lead(var_start),
5049 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005050 *p = cc;
5051 if (opt_type == -3)
5052 {
5053 semsg(_(e_unknown_option), var_start);
5054 goto theend;
5055 }
5056 if (opt_type == -2 || opt_type == 0)
5057 type = &t_string;
5058 else
5059 type = &t_number; // both number and boolean option
5060 }
5061 else if (*var_start == '$')
5062 {
5063 dest = dest_env;
5064 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005065 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 }
5067 else if (*var_start == '@')
5068 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005069 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005070 {
5071 emsg_invreg(var_start[1]);
5072 goto theend;
5073 }
5074 dest = dest_reg;
5075 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005076 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005078 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 {
5080 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005081 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005082 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005083 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 {
5085 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005086 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005088 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 {
5090 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005091 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005093 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 {
5095 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005096 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005097 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005098 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 {
5100 typval_T *vtv;
5101 int di_flags;
5102
5103 vimvaridx = find_vim_var(name + 2, &di_flags);
5104 if (vimvaridx < 0)
5105 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005106 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 goto theend;
5108 }
5109 // We use the current value of "sandbox" here, is that OK?
5110 if (var_check_ro(di_flags, name, FALSE))
5111 goto theend;
5112 dest = dest_vimvar;
5113 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005114 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005115 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005116 }
5117 else
5118 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005119 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120
5121 for (idx = 0; reserved[idx] != NULL; ++idx)
5122 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005123 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005124 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005125 goto theend;
5126 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127
5128 lvar = lookup_local(var_start, varlen, cctx);
5129 if (lvar == NULL)
5130 {
5131 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005132 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005133 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5134 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005135 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 if (is_decl)
5137 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005138 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 goto theend;
5140 }
5141 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005142 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005143 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005144 if (lvar != NULL)
5145 {
5146 if (is_decl)
5147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005148 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005149 goto theend;
5150 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005152 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005154 int script_namespace = varlen > 1
5155 && STRNCMP(var_start, "s:", 2) == 0;
5156 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005157 ? script_var_exists(var_start + 2, varlen - 2,
5158 FALSE, cctx)
5159 : script_var_exists(var_start, varlen,
5160 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005161 imported_T *import =
5162 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005163
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005164 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005166 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5167
5168 if (is_decl)
5169 {
5170 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005171 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005172 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005173 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005174 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005175 name);
5176 goto theend;
5177 }
5178 else if (cctx->ctx_ufunc->uf_script_ctx_version
5179 == SCRIPT_VERSION_VIM9
5180 && script_namespace
5181 && !script_var && import == NULL)
5182 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005183 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005184 goto theend;
5185 }
5186
5187 dest = dest_script;
5188
5189 // existing script-local variables should have a type
5190 scriptvar_sid = current_sctx.sc_sid;
5191 if (import != NULL)
5192 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005193 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005194 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005195 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005196 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005197 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005198 {
5199 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5200 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005201 ((svar_T *)si->sn_var_vals.ga_data)
5202 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005203 type = sv->sv_type;
5204 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005205 }
5206 }
5207 else if (name[1] == ':' && name[2] != NUL)
5208 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005209 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 goto theend;
5211 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005212 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005213 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005214 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005215 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005216 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005217 else if (check_defined(var_start, varlen, cctx) == FAIL)
5218 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005221
5222 if (declare_error)
5223 {
5224 vim9_declare_error(name);
5225 goto theend;
5226 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005227 }
5228
5229 // handle "a:name" as a name, not index "name" on "a"
5230 if (varlen > 1 || var_start[varlen] != ':')
5231 p = var_end;
5232
5233 if (dest != dest_option)
5234 {
5235 if (is_decl && *p == ':')
5236 {
5237 // parse optional type: "let var: type = expr"
5238 if (!VIM_ISWHITE(p[1]))
5239 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005240 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 goto theend;
5242 }
5243 p = skipwhite(p + 1);
5244 type = parse_type(&p, cctx->ctx_type_list);
5245 has_type = TRUE;
5246 }
5247 else if (lvar != NULL)
5248 type = lvar->lv_type;
5249 }
5250
5251 if (oplen == 3 && !heredoc && dest != dest_global
5252 && type->tt_type != VAR_STRING
5253 && type->tt_type != VAR_ANY)
5254 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005255 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 goto theend;
5257 }
5258
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005259 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260 {
5261 if (oplen > 1 && !heredoc)
5262 {
5263 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005264 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 goto theend;
5266 }
5267
5268 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005269 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5270 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 goto theend;
5272 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005273 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005274 if (lvar == NULL)
5275 goto theend;
5276 new_local = TRUE;
5277 }
5278
5279 member_type = type;
5280 if (var_end > var_start + varlen)
5281 {
5282 // Something follows after the variable: "var[idx]".
5283 if (is_decl)
5284 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005285 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005286 goto theend;
5287 }
5288
5289 if (var_start[varlen] == '[')
5290 {
5291 has_index = TRUE;
5292 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005293 member_type = &t_any;
5294 else
5295 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005296 }
5297 else
5298 {
5299 semsg("Not supported yet: %s", var_start);
5300 goto theend;
5301 }
5302 }
5303 else if (lvar == &arg_lvar)
5304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005305 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 goto theend;
5307 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005308 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5309 {
5310 semsg(_(e_cannot_assign_to_constant), name);
5311 goto theend;
5312 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005313
5314 if (!heredoc)
5315 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005316 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005317 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005318 if (oplen > 0 && var_count == 0)
5319 {
5320 // skip over the "=" and the expression
5321 p = skipwhite(op + oplen);
5322 compile_expr0(&p, cctx);
5323 }
5324 }
5325 else if (oplen > 0)
5326 {
5327 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005328 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005329
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005330 // For "var = expr" evaluate the expression.
5331 if (var_count == 0)
5332 {
5333 int r;
5334
5335 // for "+=", "*=", "..=" etc. first load the current value
5336 if (*op != '=')
5337 {
5338 generate_loadvar(cctx, dest, name, lvar, type);
5339
5340 if (has_index)
5341 {
5342 // TODO: get member from list or dict
5343 emsg("Index with operation not supported yet");
5344 goto theend;
5345 }
5346 }
5347
5348 // Compile the expression. Temporarily hide the new local
5349 // variable here, it is not available to this expression.
5350 if (new_local)
5351 --cctx->ctx_locals.ga_len;
5352 instr_count = instr->ga_len;
5353 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005354 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005355 if (new_local)
5356 ++cctx->ctx_locals.ga_len;
5357 if (r == FAIL)
5358 goto theend;
5359 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005360 else if (semicolon && var_idx == var_count - 1)
5361 {
5362 // For "[var; var] = expr" get the rest of the list
5363 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5364 goto theend;
5365 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005366 else
5367 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368 // For "[var, var] = expr" get the "var_idx" item from the
5369 // list.
5370 if (generate_GETITEM(cctx, var_idx) == FAIL)
5371 return FAIL;
5372 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005373
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005374 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005375 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005376 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005377 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005378 if ((stacktype->tt_type == VAR_FUNC
5379 || stacktype->tt_type == VAR_PARTIAL)
5380 && var_wrong_func_name(name, TRUE))
5381 goto theend;
5382
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005383 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005384 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005385 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005387 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005388 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005389 }
5390 else
5391 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005392 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005393 // for a variable that implies &t_any.
5394 if (stacktype == &t_list_empty)
5395 lvar->lv_type = &t_list_any;
5396 else if (stacktype == &t_dict_empty)
5397 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005398 else if (stacktype == &t_unknown)
5399 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005400 else
5401 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005402 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005403 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005404 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005405 {
5406 type_T *use_type = lvar->lv_type;
5407
Bram Moolenaar71abe482020-09-14 22:28:30 +02005408 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005409 if (has_index)
5410 {
5411 use_type = use_type->tt_member;
5412 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005413 // could be indexing "any"
5414 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005415 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005416 if (need_type(stacktype, use_type, -1, cctx,
5417 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005418 goto theend;
5419 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005420 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005421 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005422 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005423 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005425 else if (cmdidx == CMD_final)
5426 {
5427 emsg(_(e_final_requires_a_value));
5428 goto theend;
5429 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005432 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 goto theend;
5434 }
5435 else if (!has_type || dest == dest_option)
5436 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005437 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 goto theend;
5439 }
5440 else
5441 {
5442 // variables are always initialized
5443 if (ga_grow(instr, 1) == FAIL)
5444 goto theend;
5445 switch (member_type->tt_type)
5446 {
5447 case VAR_BOOL:
5448 generate_PUSHBOOL(cctx, VVAL_FALSE);
5449 break;
5450 case VAR_FLOAT:
5451#ifdef FEAT_FLOAT
5452 generate_PUSHF(cctx, 0.0);
5453#endif
5454 break;
5455 case VAR_STRING:
5456 generate_PUSHS(cctx, NULL);
5457 break;
5458 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005459 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 break;
5461 case VAR_FUNC:
5462 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5463 break;
5464 case VAR_LIST:
5465 generate_NEWLIST(cctx, 0);
5466 break;
5467 case VAR_DICT:
5468 generate_NEWDICT(cctx, 0);
5469 break;
5470 case VAR_JOB:
5471 generate_PUSHJOB(cctx, NULL);
5472 break;
5473 case VAR_CHANNEL:
5474 generate_PUSHCHANNEL(cctx, NULL);
5475 break;
5476 case VAR_NUMBER:
5477 case VAR_UNKNOWN:
5478 case VAR_ANY:
5479 case VAR_PARTIAL:
5480 case VAR_VOID:
5481 case VAR_SPECIAL: // cannot happen
5482 generate_PUSHNR(cctx, 0);
5483 break;
5484 }
5485 }
5486 if (var_count == 0)
5487 end = p;
5488 }
5489
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005490 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005491 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005492 break;
5493
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 if (oplen > 0 && *op != '=')
5495 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005496 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005497 type_T *stacktype;
5498
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 if (*op == '.')
5500 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005501 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005502 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005503 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005504 if (
5505#ifdef FEAT_FLOAT
5506 // If variable is float operation with number is OK.
5507 !(expected == &t_float && stacktype == &t_number) &&
5508#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005509 need_type(stacktype, expected, -1, cctx,
5510 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005511 goto theend;
5512
5513 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005514 {
5515 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5516 goto theend;
5517 }
5518 else if (*op == '+')
5519 {
5520 if (generate_add_instr(cctx,
5521 operator_type(member_type, stacktype),
5522 member_type, stacktype) == FAIL)
5523 goto theend;
5524 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005525 else if (generate_two_op(cctx, op) == FAIL)
5526 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005529 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005530 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005531 int r;
5532
5533 // Compile the "idx" in "var[idx]".
5534 if (new_local)
5535 --cctx->ctx_locals.ga_len;
5536 p = skipwhite(var_start + varlen + 1);
5537 r = compile_expr0(&p, cctx);
5538 if (new_local)
5539 ++cctx->ctx_locals.ga_len;
5540 if (r == FAIL)
5541 goto theend;
5542 if (*skipwhite(p) != ']')
5543 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005544 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 emsg(_(e_missbrac));
5546 goto theend;
5547 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005548 if (type == &t_any)
5549 {
5550 type_T *idx_type = ((type_T **)stack->ga_data)[
5551 stack->ga_len - 1];
5552 // Index on variable of unknown type: guess the type from the
5553 // index type: number is dict, otherwise dict.
5554 // TODO: should do the assignment at runtime
5555 if (idx_type->tt_type == VAR_NUMBER)
5556 type = &t_list_any;
5557 else
5558 type = &t_dict_any;
5559 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 if (type->tt_type == VAR_DICT
5561 && may_generate_2STRING(-1, cctx) == FAIL)
5562 goto theend;
5563 if (type->tt_type == VAR_LIST
5564 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005565 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005566 {
5567 emsg(_(e_number_exp));
5568 goto theend;
5569 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005570
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 // Load the dict or list. On the stack we then have:
5572 // - value
5573 // - index
5574 // - variable
5575 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005576
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005577 if (type->tt_type == VAR_LIST)
5578 {
5579 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5580 return FAIL;
5581 }
5582 else if (type->tt_type == VAR_DICT)
5583 {
5584 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5585 return FAIL;
5586 }
5587 else
5588 {
5589 emsg(_(e_listreq));
5590 goto theend;
5591 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005592 }
5593 else
5594 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005595 if (is_decl && cmdidx == CMD_const
5596 && (dest == dest_script || dest == dest_local))
5597 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005598 generate_LOCKCONST(cctx);
5599
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005600 switch (dest)
5601 {
5602 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005603 generate_STOREOPT(cctx, skip_option_env_lead(name),
5604 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005605 break;
5606 case dest_global:
5607 // include g: with the name, easier to execute that way
5608 generate_STORE(cctx, ISN_STOREG, 0, name);
5609 break;
5610 case dest_buffer:
5611 // include b: with the name, easier to execute that way
5612 generate_STORE(cctx, ISN_STOREB, 0, name);
5613 break;
5614 case dest_window:
5615 // include w: with the name, easier to execute that way
5616 generate_STORE(cctx, ISN_STOREW, 0, name);
5617 break;
5618 case dest_tab:
5619 // include t: with the name, easier to execute that way
5620 generate_STORE(cctx, ISN_STORET, 0, name);
5621 break;
5622 case dest_env:
5623 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5624 break;
5625 case dest_reg:
5626 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5627 break;
5628 case dest_vimvar:
5629 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5630 break;
5631 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005632 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005633 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005634 {
5635 char_u *name_s = name;
5636
5637 // Include s: in the name for store_var()
5638 if (name[1] != ':')
5639 {
5640 int len = (int)STRLEN(name) + 3;
5641
5642 name_s = alloc(len);
5643 if (name_s == NULL)
5644 name_s = name;
5645 else
5646 vim_snprintf((char *)name_s, len,
5647 "s:%s", name);
5648 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005649 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5650 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005651 if (name_s != name)
5652 vim_free(name_s);
5653 }
5654 else
5655 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005656 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005657 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005658 break;
5659 case dest_local:
5660 if (lvar != NULL)
5661 {
5662 isn_T *isn = ((isn_T *)instr->ga_data)
5663 + instr->ga_len - 1;
5664
5665 // optimization: turn "var = 123" from ISN_PUSHNR +
5666 // ISN_STORE into ISN_STORENR
5667 if (!lvar->lv_from_outer
5668 && instr->ga_len == instr_count + 1
5669 && isn->isn_type == ISN_PUSHNR)
5670 {
5671 varnumber_T val = isn->isn_arg.number;
5672
5673 isn->isn_type = ISN_STORENR;
5674 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5675 isn->isn_arg.storenr.stnr_val = val;
5676 if (stack->ga_len > 0)
5677 --stack->ga_len;
5678 }
5679 else if (lvar->lv_from_outer)
5680 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005681 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005682 else
5683 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5684 }
5685 break;
5686 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005687 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005688
5689 if (var_idx + 1 < var_count)
5690 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005692
5693 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005694 if (var_count > 0 && !semicolon)
5695 {
5696 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5697 goto theend;
5698 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005699
Bram Moolenaarb2097502020-07-19 17:17:02 +02005700 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701
5702theend:
5703 vim_free(name);
5704 return ret;
5705}
5706
5707/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005708 * Check if "name" can be "unlet".
5709 */
5710 int
5711check_vim9_unlet(char_u *name)
5712{
5713 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5714 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005715 // "unlet s:var" is allowed in legacy script.
5716 if (*name == 's' && !script_is_vim9())
5717 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005718 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005719 return FAIL;
5720 }
5721 return OK;
5722}
5723
5724/*
5725 * Callback passed to ex_unletlock().
5726 */
5727 static int
5728compile_unlet(
5729 lval_T *lvp,
5730 char_u *name_end,
5731 exarg_T *eap,
5732 int deep UNUSED,
5733 void *coookie)
5734{
5735 cctx_T *cctx = coookie;
5736
5737 if (lvp->ll_tv == NULL)
5738 {
5739 char_u *p = lvp->ll_name;
5740 int cc = *name_end;
5741 int ret = OK;
5742
5743 // Normal name. Only supports g:, w:, t: and b: namespaces.
5744 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005745 if (*p == '$')
5746 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5747 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005748 ret = FAIL;
5749 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005750 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005751
5752 *name_end = cc;
5753 return ret;
5754 }
5755
5756 // TODO: unlet {list}[idx]
5757 // TODO: unlet {dict}[key]
5758 emsg("Sorry, :unlet not fully implemented yet");
5759 return FAIL;
5760}
5761
5762/*
5763 * compile "unlet var", "lock var" and "unlock var"
5764 * "arg" points to "var".
5765 */
5766 static char_u *
5767compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5768{
5769 char_u *p = arg;
5770
5771 if (eap->cmdidx != CMD_unlet)
5772 {
5773 emsg("Sorry, :lock and unlock not implemented yet");
5774 return NULL;
5775 }
5776
5777 if (*p == '!')
5778 {
5779 p = skipwhite(p + 1);
5780 eap->forceit = TRUE;
5781 }
5782
5783 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5784 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5785}
5786
5787/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 * Compile an :import command.
5789 */
5790 static char_u *
5791compile_import(char_u *arg, cctx_T *cctx)
5792{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005793 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794}
5795
5796/*
5797 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5798 */
5799 static int
5800compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5801{
5802 garray_T *instr = &cctx->ctx_instr;
5803 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5804
5805 if (endlabel == NULL)
5806 return FAIL;
5807 endlabel->el_next = *el;
5808 *el = endlabel;
5809 endlabel->el_end_label = instr->ga_len;
5810
5811 generate_JUMP(cctx, when, 0);
5812 return OK;
5813}
5814
5815 static void
5816compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5817{
5818 garray_T *instr = &cctx->ctx_instr;
5819
5820 while (*el != NULL)
5821 {
5822 endlabel_T *cur = (*el);
5823 isn_T *isn;
5824
5825 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5826 isn->isn_arg.jump.jump_where = instr->ga_len;
5827 *el = cur->el_next;
5828 vim_free(cur);
5829 }
5830}
5831
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005832 static void
5833compile_free_jump_to_end(endlabel_T **el)
5834{
5835 while (*el != NULL)
5836 {
5837 endlabel_T *cur = (*el);
5838
5839 *el = cur->el_next;
5840 vim_free(cur);
5841 }
5842}
5843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844/*
5845 * Create a new scope and set up the generic items.
5846 */
5847 static scope_T *
5848new_scope(cctx_T *cctx, scopetype_T type)
5849{
5850 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5851
5852 if (scope == NULL)
5853 return NULL;
5854 scope->se_outer = cctx->ctx_scope;
5855 cctx->ctx_scope = scope;
5856 scope->se_type = type;
5857 scope->se_local_count = cctx->ctx_locals.ga_len;
5858 return scope;
5859}
5860
5861/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005862 * Free the current scope and go back to the outer scope.
5863 */
5864 static void
5865drop_scope(cctx_T *cctx)
5866{
5867 scope_T *scope = cctx->ctx_scope;
5868
5869 if (scope == NULL)
5870 {
5871 iemsg("calling drop_scope() without a scope");
5872 return;
5873 }
5874 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005875 switch (scope->se_type)
5876 {
5877 case IF_SCOPE:
5878 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5879 case FOR_SCOPE:
5880 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5881 case WHILE_SCOPE:
5882 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5883 case TRY_SCOPE:
5884 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5885 case NO_SCOPE:
5886 case BLOCK_SCOPE:
5887 break;
5888 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005889 vim_free(scope);
5890}
5891
5892/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005893 * Check that the top of the type stack has a type that can be used as a
5894 * condition. Give an error and return FAIL if not.
5895 */
5896 static int
5897bool_on_stack(cctx_T *cctx)
5898{
5899 garray_T *stack = &cctx->ctx_type_stack;
5900 type_T *type;
5901
5902 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5903 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005904 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005905 return FAIL;
5906 return OK;
5907}
5908
5909/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910 * compile "if expr"
5911 *
5912 * "if expr" Produces instructions:
5913 * EVAL expr Push result of "expr"
5914 * JUMP_IF_FALSE end
5915 * ... body ...
5916 * end:
5917 *
5918 * "if expr | else" Produces instructions:
5919 * EVAL expr Push result of "expr"
5920 * JUMP_IF_FALSE else
5921 * ... body ...
5922 * JUMP_ALWAYS end
5923 * else:
5924 * ... body ...
5925 * end:
5926 *
5927 * "if expr1 | elseif expr2 | else" Produces instructions:
5928 * EVAL expr Push result of "expr"
5929 * JUMP_IF_FALSE elseif
5930 * ... body ...
5931 * JUMP_ALWAYS end
5932 * elseif:
5933 * EVAL expr Push result of "expr"
5934 * JUMP_IF_FALSE else
5935 * ... body ...
5936 * JUMP_ALWAYS end
5937 * else:
5938 * ... body ...
5939 * end:
5940 */
5941 static char_u *
5942compile_if(char_u *arg, cctx_T *cctx)
5943{
5944 char_u *p = arg;
5945 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005946 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005948 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005949 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950
Bram Moolenaara5565e42020-05-09 15:44:01 +02005951 CLEAR_FIELD(ppconst);
5952 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005953 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005954 clear_ppconst(&ppconst);
5955 return NULL;
5956 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005957 if (cctx->ctx_skip == SKIP_YES)
5958 clear_ppconst(&ppconst);
5959 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005960 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005961 int error = FALSE;
5962 int v;
5963
Bram Moolenaara5565e42020-05-09 15:44:01 +02005964 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005965 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005966 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005967 if (error)
5968 return NULL;
5969 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005970 }
5971 else
5972 {
5973 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005974 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005975 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005976 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005977 if (bool_on_stack(cctx) == FAIL)
5978 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980
5981 scope = new_scope(cctx, IF_SCOPE);
5982 if (scope == NULL)
5983 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005984 scope->se_skip_save = skip_save;
5985 // "is_had_return" will be reset if any block does not end in :return
5986 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005988 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005989 {
5990 // "where" is set when ":elseif", "else" or ":endif" is found
5991 scope->se_u.se_if.is_if_label = instr->ga_len;
5992 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5993 }
5994 else
5995 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996
5997 return p;
5998}
5999
6000 static char_u *
6001compile_elseif(char_u *arg, cctx_T *cctx)
6002{
6003 char_u *p = arg;
6004 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006005 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 isn_T *isn;
6007 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006008 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006009 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010
6011 if (scope == NULL || scope->se_type != IF_SCOPE)
6012 {
6013 emsg(_(e_elseif_without_if));
6014 return NULL;
6015 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006016 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006017 if (!cctx->ctx_had_return)
6018 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006020 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006021 {
6022 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006024 return NULL;
6025 // previous "if" or "elseif" jumps here
6026 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6027 isn->isn_arg.jump.jump_where = instr->ga_len;
6028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006031 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006032 if (cctx->ctx_skip == SKIP_YES)
6033 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006034 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006035 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006036 clear_ppconst(&ppconst);
6037 return NULL;
6038 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006039 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006040 if (scope->se_skip_save == SKIP_YES)
6041 clear_ppconst(&ppconst);
6042 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006043 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006044 int error = FALSE;
6045 int v;
6046
Bram Moolenaar7f141552020-05-09 17:35:53 +02006047 // The expression results in a constant.
6048 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006049 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6050 if (error)
6051 return NULL;
6052 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006053 clear_ppconst(&ppconst);
6054 scope->se_u.se_if.is_if_label = -1;
6055 }
6056 else
6057 {
6058 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006059 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006060 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006061 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006062 if (bool_on_stack(cctx) == FAIL)
6063 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006065 // "where" is set when ":elseif", "else" or ":endif" is found
6066 scope->se_u.se_if.is_if_label = instr->ga_len;
6067 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069
6070 return p;
6071}
6072
6073 static char_u *
6074compile_else(char_u *arg, cctx_T *cctx)
6075{
6076 char_u *p = arg;
6077 garray_T *instr = &cctx->ctx_instr;
6078 isn_T *isn;
6079 scope_T *scope = cctx->ctx_scope;
6080
6081 if (scope == NULL || scope->se_type != IF_SCOPE)
6082 {
6083 emsg(_(e_else_without_if));
6084 return NULL;
6085 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006086 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006087 if (!cctx->ctx_had_return)
6088 scope->se_u.se_if.is_had_return = FALSE;
6089 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090
Bram Moolenaarefd88552020-06-18 20:50:10 +02006091 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006092 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006093 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006094 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006095 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006096 if (!cctx->ctx_had_return
6097 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6098 JUMP_ALWAYS, cctx) == FAIL)
6099 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006100 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006101
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006102 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006103 {
6104 if (scope->se_u.se_if.is_if_label >= 0)
6105 {
6106 // previous "if" or "elseif" jumps here
6107 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6108 isn->isn_arg.jump.jump_where = instr->ga_len;
6109 scope->se_u.se_if.is_if_label = -1;
6110 }
6111 }
6112
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006113 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006114 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116
6117 return p;
6118}
6119
6120 static char_u *
6121compile_endif(char_u *arg, cctx_T *cctx)
6122{
6123 scope_T *scope = cctx->ctx_scope;
6124 ifscope_T *ifscope;
6125 garray_T *instr = &cctx->ctx_instr;
6126 isn_T *isn;
6127
6128 if (scope == NULL || scope->se_type != IF_SCOPE)
6129 {
6130 emsg(_(e_endif_without_if));
6131 return NULL;
6132 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006133 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006134 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006135 if (!cctx->ctx_had_return)
6136 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006138 if (scope->se_u.se_if.is_if_label >= 0)
6139 {
6140 // previous "if" or "elseif" jumps here
6141 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6142 isn->isn_arg.jump.jump_where = instr->ga_len;
6143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 // Fill in the "end" label in jumps at the end of the blocks.
6145 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006146 cctx->ctx_skip = scope->se_skip_save;
6147
6148 // If all the blocks end in :return and there is an :else then the
6149 // had_return flag is set.
6150 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006152 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153 return arg;
6154}
6155
6156/*
6157 * compile "for var in expr"
6158 *
6159 * Produces instructions:
6160 * PUSHNR -1
6161 * STORE loop-idx Set index to -1
6162 * EVAL expr Push result of "expr"
6163 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6164 * - if beyond end, jump to "end"
6165 * - otherwise get item from list and push it
6166 * STORE var Store item in "var"
6167 * ... body ...
6168 * JUMP top Jump back to repeat
6169 * end: DROP Drop the result of "expr"
6170 *
6171 */
6172 static char_u *
6173compile_for(char_u *arg, cctx_T *cctx)
6174{
6175 char_u *p;
6176 size_t varlen;
6177 garray_T *instr = &cctx->ctx_instr;
6178 garray_T *stack = &cctx->ctx_type_stack;
6179 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006180 lvar_T *loop_lvar; // loop iteration variable
6181 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182 type_T *vartype;
6183
6184 // TODO: list of variables: "for [key, value] in dict"
6185 // parse "var"
6186 for (p = arg; eval_isnamec1(*p); ++p)
6187 ;
6188 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006189 var_lvar = lookup_local(arg, varlen, cctx);
6190 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006192 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 return NULL;
6194 }
6195
6196 // consume "in"
6197 p = skipwhite(p);
6198 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6199 {
6200 emsg(_(e_missing_in));
6201 return NULL;
6202 }
6203 p = skipwhite(p + 2);
6204
6205
6206 scope = new_scope(cctx, FOR_SCOPE);
6207 if (scope == NULL)
6208 return NULL;
6209
6210 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006211 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6212 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006213 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006214 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006215 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218
6219 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006220 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6221 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006222 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006223 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006224 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006228 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
6230 // compile "expr", it remains on the stack until "endfor"
6231 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006232 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006233 {
6234 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006238 // Now that we know the type of "var", check that it is a list, now or at
6239 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006241 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006243 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 return NULL;
6245 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006246 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006247 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248
6249 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006250 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006252 generate_FOR(cctx, loop_lvar->lv_idx);
6253 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254
6255 return arg;
6256}
6257
6258/*
6259 * compile "endfor"
6260 */
6261 static char_u *
6262compile_endfor(char_u *arg, cctx_T *cctx)
6263{
6264 garray_T *instr = &cctx->ctx_instr;
6265 scope_T *scope = cctx->ctx_scope;
6266 forscope_T *forscope;
6267 isn_T *isn;
6268
6269 if (scope == NULL || scope->se_type != FOR_SCOPE)
6270 {
6271 emsg(_(e_for));
6272 return NULL;
6273 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006274 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006276 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
6278 // At end of ":for" scope jump back to the FOR instruction.
6279 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6280
6281 // Fill in the "end" label in the FOR statement so it can jump here
6282 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6283 isn->isn_arg.forloop.for_end = instr->ga_len;
6284
6285 // Fill in the "end" label any BREAK statements
6286 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6287
6288 // Below the ":for" scope drop the "expr" list from the stack.
6289 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6290 return NULL;
6291
6292 vim_free(scope);
6293
6294 return arg;
6295}
6296
6297/*
6298 * compile "while expr"
6299 *
6300 * Produces instructions:
6301 * top: EVAL expr Push result of "expr"
6302 * JUMP_IF_FALSE end jump if false
6303 * ... body ...
6304 * JUMP top Jump back to repeat
6305 * end:
6306 *
6307 */
6308 static char_u *
6309compile_while(char_u *arg, cctx_T *cctx)
6310{
6311 char_u *p = arg;
6312 garray_T *instr = &cctx->ctx_instr;
6313 scope_T *scope;
6314
6315 scope = new_scope(cctx, WHILE_SCOPE);
6316 if (scope == NULL)
6317 return NULL;
6318
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006319 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320
6321 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006322 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 return NULL;
6324
Bram Moolenaar13106602020-10-04 16:06:05 +02006325 if (bool_on_stack(cctx) == FAIL)
6326 return FAIL;
6327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006329 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330 JUMP_IF_FALSE, cctx) == FAIL)
6331 return FAIL;
6332
6333 return p;
6334}
6335
6336/*
6337 * compile "endwhile"
6338 */
6339 static char_u *
6340compile_endwhile(char_u *arg, cctx_T *cctx)
6341{
6342 scope_T *scope = cctx->ctx_scope;
6343
6344 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6345 {
6346 emsg(_(e_while));
6347 return NULL;
6348 }
6349 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006350 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351
6352 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006353 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354
6355 // Fill in the "end" label in the WHILE statement so it can jump here.
6356 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006357 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358
6359 vim_free(scope);
6360
6361 return arg;
6362}
6363
6364/*
6365 * compile "continue"
6366 */
6367 static char_u *
6368compile_continue(char_u *arg, cctx_T *cctx)
6369{
6370 scope_T *scope = cctx->ctx_scope;
6371
6372 for (;;)
6373 {
6374 if (scope == NULL)
6375 {
6376 emsg(_(e_continue));
6377 return NULL;
6378 }
6379 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6380 break;
6381 scope = scope->se_outer;
6382 }
6383
6384 // Jump back to the FOR or WHILE instruction.
6385 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006386 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6387 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388 return arg;
6389}
6390
6391/*
6392 * compile "break"
6393 */
6394 static char_u *
6395compile_break(char_u *arg, cctx_T *cctx)
6396{
6397 scope_T *scope = cctx->ctx_scope;
6398 endlabel_T **el;
6399
6400 for (;;)
6401 {
6402 if (scope == NULL)
6403 {
6404 emsg(_(e_break));
6405 return NULL;
6406 }
6407 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6408 break;
6409 scope = scope->se_outer;
6410 }
6411
6412 // Jump to the end of the FOR or WHILE loop.
6413 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006414 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006416 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6418 return FAIL;
6419
6420 return arg;
6421}
6422
6423/*
6424 * compile "{" start of block
6425 */
6426 static char_u *
6427compile_block(char_u *arg, cctx_T *cctx)
6428{
6429 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6430 return NULL;
6431 return skipwhite(arg + 1);
6432}
6433
6434/*
6435 * compile end of block: drop one scope
6436 */
6437 static void
6438compile_endblock(cctx_T *cctx)
6439{
6440 scope_T *scope = cctx->ctx_scope;
6441
6442 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006443 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444 vim_free(scope);
6445}
6446
6447/*
6448 * compile "try"
6449 * Creates a new scope for the try-endtry, pointing to the first catch and
6450 * finally.
6451 * Creates another scope for the "try" block itself.
6452 * TRY instruction sets up exception handling at runtime.
6453 *
6454 * "try"
6455 * TRY -> catch1, -> finally push trystack entry
6456 * ... try block
6457 * "throw {exception}"
6458 * EVAL {exception}
6459 * THROW create exception
6460 * ... try block
6461 * " catch {expr}"
6462 * JUMP -> finally
6463 * catch1: PUSH exeception
6464 * EVAL {expr}
6465 * MATCH
6466 * JUMP nomatch -> catch2
6467 * CATCH remove exception
6468 * ... catch block
6469 * " catch"
6470 * JUMP -> finally
6471 * catch2: CATCH remove exception
6472 * ... catch block
6473 * " finally"
6474 * finally:
6475 * ... finally block
6476 * " endtry"
6477 * ENDTRY pop trystack entry, may rethrow
6478 */
6479 static char_u *
6480compile_try(char_u *arg, cctx_T *cctx)
6481{
6482 garray_T *instr = &cctx->ctx_instr;
6483 scope_T *try_scope;
6484 scope_T *scope;
6485
6486 // scope that holds the jumps that go to catch/finally/endtry
6487 try_scope = new_scope(cctx, TRY_SCOPE);
6488 if (try_scope == NULL)
6489 return NULL;
6490
6491 // "catch" is set when the first ":catch" is found.
6492 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006493 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 if (generate_instr(cctx, ISN_TRY) == NULL)
6495 return NULL;
6496
6497 // scope for the try block itself
6498 scope = new_scope(cctx, BLOCK_SCOPE);
6499 if (scope == NULL)
6500 return NULL;
6501
6502 return arg;
6503}
6504
6505/*
6506 * compile "catch {expr}"
6507 */
6508 static char_u *
6509compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6510{
6511 scope_T *scope = cctx->ctx_scope;
6512 garray_T *instr = &cctx->ctx_instr;
6513 char_u *p;
6514 isn_T *isn;
6515
6516 // end block scope from :try or :catch
6517 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6518 compile_endblock(cctx);
6519 scope = cctx->ctx_scope;
6520
6521 // Error if not in a :try scope
6522 if (scope == NULL || scope->se_type != TRY_SCOPE)
6523 {
6524 emsg(_(e_catch));
6525 return NULL;
6526 }
6527
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006528 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006530 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531 return NULL;
6532 }
6533
6534 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006535 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 JUMP_ALWAYS, cctx) == FAIL)
6537 return NULL;
6538
6539 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006540 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006541 if (isn->isn_arg.try.try_catch == 0)
6542 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006543 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 {
6545 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006546 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 isn->isn_arg.jump.jump_where = instr->ga_len;
6548 }
6549
6550 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006551 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006553 scope->se_u.se_try.ts_caught_all = TRUE;
6554 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 }
6556 else
6557 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006558 char_u *end;
6559 char_u *pat;
6560 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006561 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006562 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 // Push v:exception, push {expr} and MATCH
6565 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6566
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006567 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006568 if (*end != *p)
6569 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006570 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006571 vim_free(tofree);
6572 return FAIL;
6573 }
6574 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006575 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006576 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006577 len = (int)(end - tofree);
6578 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006579 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006580 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006581 if (pat == NULL)
6582 return FAIL;
6583 if (generate_PUSHS(cctx, pat) == FAIL)
6584 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6587 return NULL;
6588
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006589 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6591 return NULL;
6592 }
6593
6594 if (generate_instr(cctx, ISN_CATCH) == NULL)
6595 return NULL;
6596
6597 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6598 return NULL;
6599 return p;
6600}
6601
6602 static char_u *
6603compile_finally(char_u *arg, cctx_T *cctx)
6604{
6605 scope_T *scope = cctx->ctx_scope;
6606 garray_T *instr = &cctx->ctx_instr;
6607 isn_T *isn;
6608
6609 // end block scope from :try or :catch
6610 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6611 compile_endblock(cctx);
6612 scope = cctx->ctx_scope;
6613
6614 // Error if not in a :try scope
6615 if (scope == NULL || scope->se_type != TRY_SCOPE)
6616 {
6617 emsg(_(e_finally));
6618 return NULL;
6619 }
6620
6621 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006622 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623 if (isn->isn_arg.try.try_finally != 0)
6624 {
6625 emsg(_(e_finally_dup));
6626 return NULL;
6627 }
6628
6629 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006630 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631
Bram Moolenaar585fea72020-04-02 22:33:21 +02006632 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006633 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 {
6635 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006636 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006638 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 }
6640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 // TODO: set index in ts_finally_label jumps
6642
6643 return arg;
6644}
6645
6646 static char_u *
6647compile_endtry(char_u *arg, cctx_T *cctx)
6648{
6649 scope_T *scope = cctx->ctx_scope;
6650 garray_T *instr = &cctx->ctx_instr;
6651 isn_T *isn;
6652
6653 // end block scope from :catch or :finally
6654 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6655 compile_endblock(cctx);
6656 scope = cctx->ctx_scope;
6657
6658 // Error if not in a :try scope
6659 if (scope == NULL || scope->se_type != TRY_SCOPE)
6660 {
6661 if (scope == NULL)
6662 emsg(_(e_no_endtry));
6663 else if (scope->se_type == WHILE_SCOPE)
6664 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006665 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 emsg(_(e_endfor));
6667 else
6668 emsg(_(e_endif));
6669 return NULL;
6670 }
6671
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006672 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6674 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006675 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 return NULL;
6677 }
6678
6679 // Fill in the "end" label in jumps at the end of the blocks, if not done
6680 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006681 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682
6683 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006684 if (isn->isn_arg.try.try_catch == 0)
6685 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 if (isn->isn_arg.try.try_finally == 0)
6687 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006688
6689 if (scope->se_u.se_try.ts_catch_label != 0)
6690 {
6691 // Last catch without match jumps here
6692 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6693 isn->isn_arg.jump.jump_where = instr->ga_len;
6694 }
6695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 compile_endblock(cctx);
6697
6698 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6699 return NULL;
6700 return arg;
6701}
6702
6703/*
6704 * compile "throw {expr}"
6705 */
6706 static char_u *
6707compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6708{
6709 char_u *p = skipwhite(arg);
6710
Bram Moolenaara5565e42020-05-09 15:44:01 +02006711 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 return NULL;
6713 if (may_generate_2STRING(-1, cctx) == FAIL)
6714 return NULL;
6715 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6716 return NULL;
6717
6718 return p;
6719}
6720
6721/*
6722 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006723 * compile "echomsg expr"
6724 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006725 * compile "execute expr"
6726 */
6727 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006728compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006729{
6730 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006731 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006732 int count = 0;
6733
6734 for (;;)
6735 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006736 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006737 return NULL;
6738 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006739 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006740 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006741 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006742 break;
6743 }
6744
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006745 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6746 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6747 else if (cmdidx == CMD_execute)
6748 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6749 else if (cmdidx == CMD_echomsg)
6750 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6751 else
6752 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753 return p;
6754}
6755
6756/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006757 * :put r
6758 * :put ={expr}
6759 */
6760 static char_u *
6761compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6762{
6763 char_u *line = arg;
6764 linenr_T lnum;
6765 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006766 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006767
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006768 eap->regname = *line;
6769
6770 if (eap->regname == '=')
6771 {
6772 char_u *p = line + 1;
6773
6774 if (compile_expr0(&p, cctx) == FAIL)
6775 return NULL;
6776 line = p;
6777 }
6778 else if (eap->regname != NUL)
6779 ++line;
6780
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006781 // "errormsg" will not be set because the range is ADDR_LINES.
6782 // TODO: if the range contains something like "$" or "." need to evaluate
6783 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006784 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6785 return NULL;
6786 if (eap->addr_count == 0)
6787 lnum = -1;
6788 else
6789 lnum = eap->line2;
6790 if (above)
6791 --lnum;
6792
6793 generate_PUT(cctx, eap->regname, lnum);
6794 return line;
6795}
6796
6797/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006798 * A command that is not compiled, execute with legacy code.
6799 */
6800 static char_u *
6801compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6802{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006803 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006804 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006805 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006806
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006807 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006808 goto theend;
6809
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006810 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006811 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006812 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006813 int usefilter = FALSE;
6814
6815 has_expr = argt & (EX_XFILE | EX_EXPAND);
6816
6817 // If the command can be followed by a bar, find the bar and truncate
6818 // it, so that the following command can be compiled.
6819 // The '|' is overwritten with a NUL, it is put back below.
6820 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6821 && *eap->arg == '!')
6822 // :w !filter or :r !filter or :r! filter
6823 usefilter = TRUE;
6824 if ((argt & EX_TRLBAR) && !usefilter)
6825 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006826 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006827 separate_nextcmd(eap);
6828 if (eap->nextcmd != NULL)
6829 nextcmd = eap->nextcmd;
6830 }
6831 }
6832
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006833 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6834 {
6835 // expand filename in "syntax include [@group] filename"
6836 has_expr = TRUE;
6837 eap->arg = skipwhite(eap->arg + 7);
6838 if (*eap->arg == '@')
6839 eap->arg = skiptowhite(eap->arg);
6840 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006841
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006842 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006843 {
6844 int count = 0;
6845 char_u *start = skipwhite(line);
6846
6847 // :cmd xxx`=expr1`yyy`=expr2`zzz
6848 // PUSHS ":cmd xxx"
6849 // eval expr1
6850 // PUSHS "yyy"
6851 // eval expr2
6852 // PUSHS "zzz"
6853 // EXECCONCAT 5
6854 for (;;)
6855 {
6856 if (p > start)
6857 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006858 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006859 ++count;
6860 }
6861 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006862 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006863 return NULL;
6864 may_generate_2STRING(-1, cctx);
6865 ++count;
6866 p = skipwhite(p);
6867 if (*p != '`')
6868 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006869 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006870 return NULL;
6871 }
6872 start = p + 1;
6873
6874 p = (char_u *)strstr((char *)start, "`=");
6875 if (p == NULL)
6876 {
6877 if (*skipwhite(start) != NUL)
6878 {
6879 generate_PUSHS(cctx, vim_strsave(start));
6880 ++count;
6881 }
6882 break;
6883 }
6884 }
6885 generate_EXECCONCAT(cctx, count);
6886 }
6887 else
6888 generate_EXEC(cctx, line);
6889
6890theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006891 if (*nextcmd != NUL)
6892 {
6893 // the parser expects a pointer to the bar, put it back
6894 --nextcmd;
6895 *nextcmd = '|';
6896 }
6897
6898 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006899}
6900
6901/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006902 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006903 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006904 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006905 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006906add_def_function(ufunc_T *ufunc)
6907{
6908 dfunc_T *dfunc;
6909
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006910 if (def_functions.ga_len == 0)
6911 {
6912 // The first position is not used, so that a zero uf_dfunc_idx means it
6913 // wasn't set.
6914 if (ga_grow(&def_functions, 1) == FAIL)
6915 return FAIL;
6916 ++def_functions.ga_len;
6917 }
6918
Bram Moolenaar09689a02020-05-09 22:50:08 +02006919 // Add the function to "def_functions".
6920 if (ga_grow(&def_functions, 1) == FAIL)
6921 return FAIL;
6922 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6923 CLEAR_POINTER(dfunc);
6924 dfunc->df_idx = def_functions.ga_len;
6925 ufunc->uf_dfunc_idx = dfunc->df_idx;
6926 dfunc->df_ufunc = ufunc;
6927 ++def_functions.ga_len;
6928 return OK;
6929}
6930
6931/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 * After ex_function() has collected all the function lines: parse and compile
6933 * the lines into instructions.
6934 * Adds the function to "def_functions".
6935 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6936 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006937 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006938 * This can be used recursively through compile_lambda(), which may reallocate
6939 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006940 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006942 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006943compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 char_u *line = NULL;
6946 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006947 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 cctx_T cctx;
6949 garray_T *instr;
6950 int called_emsg_before = called_emsg;
6951 int ret = FAIL;
6952 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006953 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006954 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006955 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006956 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006958 // When using a function that was compiled before: Free old instructions.
6959 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006960 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006961 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006962 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6963 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006964 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006966 else
6967 {
6968 if (add_def_function(ufunc) == FAIL)
6969 return FAIL;
6970 new_def_function = TRUE;
6971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972
Bram Moolenaar985116a2020-07-12 17:31:09 +02006973 ufunc->uf_def_status = UF_COMPILING;
6974
Bram Moolenaara80faa82020-04-12 19:37:17 +02006975 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 cctx.ctx_ufunc = ufunc;
6977 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006978 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6980 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6981 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6982 cctx.ctx_type_list = &ufunc->uf_type_list;
6983 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6984 instr = &cctx.ctx_instr;
6985
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006986 // Set the context to the function, it may be compiled when called from
6987 // another script. Set the script version to the most modern one.
6988 // The line number will be set in next_line_from_context().
6989 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6991
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006992 // Make sure error messages are OK.
6993 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6994 if (do_estack_push)
6995 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006996 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006997
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006998 if (ufunc->uf_def_args.ga_len > 0)
6999 {
7000 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007001 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007002 int i;
7003 char_u *arg;
7004 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007005 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007006
7007 // Produce instructions for the default values of optional arguments.
7008 // Store the instruction index in uf_def_arg_idx[] so that we know
7009 // where to start when the function is called, depending on the number
7010 // of arguments.
7011 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7012 if (ufunc->uf_def_arg_idx == NULL)
7013 goto erret;
7014 for (i = 0; i < count; ++i)
7015 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007016 garray_T *stack = &cctx.ctx_type_stack;
7017 type_T *val_type;
7018 int arg_idx = first_def_arg + i;
7019
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007020 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7021 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007022 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007023 goto erret;
7024
7025 // If no type specified use the type of the default value.
7026 // Otherwise check that the default value type matches the
7027 // specified type.
7028 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7029 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007030 {
7031 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007032 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007033 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007034 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7035 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007036 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007037
7038 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007039 goto erret;
7040 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007041 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007042
7043 if (did_set_arg_type)
7044 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007045 }
7046
7047 /*
7048 * Loop over all the lines of the function and generate instructions.
7049 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 for (;;)
7051 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007052 exarg_T ea;
7053 cmdmod_T save_cmdmod;
7054 int starts_with_colon = FALSE;
7055 char_u *cmd;
7056 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007057
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007058 // Bail out on the first error to avoid a flood of errors and report
7059 // the right line number when inside try/catch.
7060 if (emsg_before != called_emsg)
7061 goto erret;
7062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 if (line != NULL && *line == '|')
7064 // the line continues after a '|'
7065 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007066 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007067 && !(*line == '#' && (line == cctx.ctx_line_start
7068 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007070 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 goto erret;
7072 }
7073 else
7074 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007075 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007076 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007077 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007080 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081
Bram Moolenaara80faa82020-04-12 19:37:17 +02007082 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 ea.cmdlinep = &line;
7084 ea.cmd = skipwhite(line);
7085
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007086 // Some things can be recognized by the first character.
7087 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007089 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007090 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007091 if (ea.cmd[1] != '{')
7092 {
7093 line = (char_u *)"";
7094 continue;
7095 }
7096 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007098 case '}':
7099 {
7100 // "}" ends a block scope
7101 scopetype_T stype = cctx.ctx_scope == NULL
7102 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007104 if (stype == BLOCK_SCOPE)
7105 {
7106 compile_endblock(&cctx);
7107 line = ea.cmd;
7108 }
7109 else
7110 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007111 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007112 goto erret;
7113 }
7114 if (line != NULL)
7115 line = skipwhite(ea.cmd + 1);
7116 continue;
7117 }
7118
7119 case '{':
7120 // "{" starts a block scope
7121 // "{'a': 1}->func() is something else
7122 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7123 {
7124 line = compile_block(ea.cmd, &cctx);
7125 continue;
7126 }
7127 break;
7128
7129 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007130 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007131 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 }
7133
7134 /*
7135 * COMMAND MODIFIERS
7136 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007137 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7139 {
7140 if (errormsg != NULL)
7141 goto erret;
7142 // empty line or comment
7143 line = (char_u *)"";
7144 continue;
7145 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007146 // TODO: use modifiers in the command
7147 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007148 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149
7150 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007151 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007153 {
7154 if (*ea.cmd == '(')
7155 // not for "call()"
7156 ea.cmd = p;
7157 else
7158 ea.cmd = skipwhite(ea.cmd);
7159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007161 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007163 char_u *pskip;
7164
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007165 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007166 // find what follows.
7167 // Skip over "var.member", "var[idx]" and the like.
7168 // Also "&opt = val", "$ENV = val" and "@r = val".
7169 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007170 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007171 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007172 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007174 char_u *var_end;
7175 int oplen;
7176 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
Bram Moolenaar65821722020-08-02 18:58:54 +02007178 if (ea.cmd[0] == '@')
7179 var_end = ea.cmd + 2;
7180 else
7181 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007182 FNE_CHECK_START | FNE_INCL_BR);
7183 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007184 if (oplen > 0)
7185 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007186 size_t len = p - ea.cmd;
7187
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007188 // Recognize an assignment if we recognize the variable
7189 // name:
7190 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007191 // "local = expr" where "local" is a local var.
7192 // "script = expr" where "script" is a script-local var.
7193 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007194 // "&opt = expr"
7195 // "$ENV = expr"
7196 // "@r = expr"
7197 if (*ea.cmd == '&'
7198 || *ea.cmd == '$'
7199 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007200 || ((len) > 2 && ea.cmd[1] == ':')
7201 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007202 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007203 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007204 || script_var_exists(ea.cmd, len,
7205 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007206 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007207 {
7208 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007209 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007210 goto erret;
7211 continue;
7212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007213 }
7214 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007215
7216 if (*ea.cmd == '[')
7217 {
7218 // [var, var] = expr
7219 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7220 if (line == NULL)
7221 goto erret;
7222 if (line != ea.cmd)
7223 continue;
7224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 }
7226
7227 /*
7228 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007229 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007231 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007232 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007233 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007234 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007235 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007236 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007237 if (!starts_with_colon)
7238 {
7239 emsg(_(e_colon_required_before_a_range));
7240 goto erret;
7241 }
7242 if (ends_excmd2(line, ea.cmd))
7243 {
7244 // A range without a command: jump to the line.
7245 // TODO: compile to a more efficient command, possibly
7246 // calling parse_cmd_address().
7247 ea.cmdidx = CMD_SIZE;
7248 line = compile_exec(line, &ea, &cctx);
7249 goto nextline;
7250 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007251 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007252 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007253 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007254 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007255 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256
7257 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7258 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007259 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007260 {
7261 line += STRLEN(line);
7262 continue;
7263 }
7264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007266 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007268 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007269 iemsg("Command from find_ex_command() not handled");
7270 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 }
7273
Bram Moolenaar3988f642020-08-27 22:43:03 +02007274 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007275 && ea.cmdidx != CMD_elseif
7276 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007277 && ea.cmdidx != CMD_endif
7278 && ea.cmdidx != CMD_endfor
7279 && ea.cmdidx != CMD_endwhile
7280 && ea.cmdidx != CMD_catch
7281 && ea.cmdidx != CMD_finally
7282 && ea.cmdidx != CMD_endtry)
7283 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007284 emsg(_(e_unreachable_code_after_return));
7285 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007286 }
7287
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007288 p = skipwhite(p);
7289 if (ea.cmdidx != CMD_SIZE
7290 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7291 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007292 if (ea.cmdidx >= 0)
7293 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007294 if ((ea.argt & EX_BANG) && *p == '!')
7295 {
7296 ea.forceit = TRUE;
7297 p = skipwhite(p + 1);
7298 }
7299 }
7300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 switch (ea.cmdidx)
7302 {
7303 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007304 ea.arg = p;
7305 line = compile_nested_function(&ea, &cctx);
7306 break;
7307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007309 // TODO: should we allow this, e.g. to declare a global
7310 // function?
7311 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 goto erret;
7313
7314 case CMD_return:
7315 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007316 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 break;
7318
7319 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007320 if (get_vim_var_nr(VV_DISALLOW_LET))
7321 {
7322 emsg(_(e_cannot_use_let_in_vim9_script));
7323 break;
7324 }
7325 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007326 case CMD_var:
7327 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 case CMD_const:
7329 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007330 if (line == p)
7331 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 break;
7333
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007334 case CMD_unlet:
7335 case CMD_unlockvar:
7336 case CMD_lockvar:
7337 line = compile_unletlock(p, &ea, &cctx);
7338 break;
7339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 case CMD_import:
7341 line = compile_import(p, &cctx);
7342 break;
7343
7344 case CMD_if:
7345 line = compile_if(p, &cctx);
7346 break;
7347 case CMD_elseif:
7348 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007349 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 break;
7351 case CMD_else:
7352 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007353 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 break;
7355 case CMD_endif:
7356 line = compile_endif(p, &cctx);
7357 break;
7358
7359 case CMD_while:
7360 line = compile_while(p, &cctx);
7361 break;
7362 case CMD_endwhile:
7363 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007364 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 break;
7366
7367 case CMD_for:
7368 line = compile_for(p, &cctx);
7369 break;
7370 case CMD_endfor:
7371 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007372 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 break;
7374 case CMD_continue:
7375 line = compile_continue(p, &cctx);
7376 break;
7377 case CMD_break:
7378 line = compile_break(p, &cctx);
7379 break;
7380
7381 case CMD_try:
7382 line = compile_try(p, &cctx);
7383 break;
7384 case CMD_catch:
7385 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007386 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 break;
7388 case CMD_finally:
7389 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007390 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 break;
7392 case CMD_endtry:
7393 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007394 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 break;
7396 case CMD_throw:
7397 line = compile_throw(p, &cctx);
7398 break;
7399
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007400 case CMD_eval:
7401 if (compile_expr0(&p, &cctx) == FAIL)
7402 goto erret;
7403
Bram Moolenaar3988f642020-08-27 22:43:03 +02007404 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007405 generate_instr_drop(&cctx, ISN_DROP, 1);
7406
7407 line = skipwhite(p);
7408 break;
7409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007412 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007413 case CMD_echomsg:
7414 case CMD_echoerr:
7415 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007416 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007418 case CMD_put:
7419 ea.cmd = cmd;
7420 line = compile_put(p, &ea, &cctx);
7421 break;
7422
Bram Moolenaar3988f642020-08-27 22:43:03 +02007423 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007424
Bram Moolenaarae616492020-07-28 20:07:27 +02007425 case CMD_append:
7426 case CMD_change:
7427 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007428 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007429 case CMD_xit:
7430 not_in_vim9(&ea);
7431 goto erret;
7432
Bram Moolenaar002262f2020-07-08 17:47:57 +02007433 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007434 if (cctx.ctx_skip != SKIP_YES)
7435 {
7436 semsg(_(e_invalid_command_str), ea.cmd);
7437 goto erret;
7438 }
7439 // We don't check for a next command here.
7440 line = (char_u *)"";
7441 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007444 if (cctx.ctx_skip == SKIP_YES)
7445 {
7446 // We don't check for a next command here.
7447 line = (char_u *)"";
7448 }
7449 else
7450 {
7451 // Not recognized, execute with do_cmdline_cmd().
7452 ea.arg = p;
7453 line = compile_exec(line, &ea, &cctx);
7454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 break;
7456 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007457nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 if (line == NULL)
7459 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007460 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461
7462 if (cctx.ctx_type_stack.ga_len < 0)
7463 {
7464 iemsg("Type stack underflow");
7465 goto erret;
7466 }
7467 }
7468
7469 if (cctx.ctx_scope != NULL)
7470 {
7471 if (cctx.ctx_scope->se_type == IF_SCOPE)
7472 emsg(_(e_endif));
7473 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7474 emsg(_(e_endwhile));
7475 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7476 emsg(_(e_endfor));
7477 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007478 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 goto erret;
7480 }
7481
Bram Moolenaarefd88552020-06-18 20:50:10 +02007482 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 {
7484 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7485 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007486 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 goto erret;
7488 }
7489
7490 // Return zero if there is no return at the end.
7491 generate_PUSHNR(&cctx, 0);
7492 generate_instr(&cctx, ISN_RETURN);
7493 }
7494
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007495 {
7496 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7497 + ufunc->uf_dfunc_idx;
7498 dfunc->df_deleted = FALSE;
7499 dfunc->df_instr = instr->ga_data;
7500 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007501 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007502 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007503 if (cctx.ctx_outer_used)
7504 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007505 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007507
7508 ret = OK;
7509
7510erret:
7511 if (ret == FAIL)
7512 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007513 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007514 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7515 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007516
7517 for (idx = 0; idx < instr->ga_len; ++idx)
7518 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007520
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007521 // If using the last entry in the table and it was added above, we
7522 // might as well remove it.
7523 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007524 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007525 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007526 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007527 ufunc->uf_dfunc_idx = 0;
7528 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007529 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007530
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007531 while (cctx.ctx_scope != NULL)
7532 drop_scope(&cctx);
7533
Bram Moolenaar20431c92020-03-20 18:39:46 +01007534 // Don't execute this function body.
7535 ga_clear_strings(&ufunc->uf_lines);
7536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 if (errormsg != NULL)
7538 emsg(errormsg);
7539 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007540 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 }
7542
7543 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007544 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007545 if (do_estack_push)
7546 estack_pop();
7547
Bram Moolenaar20431c92020-03-20 18:39:46 +01007548 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007549 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007551 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552}
7553
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007554 void
7555set_function_type(ufunc_T *ufunc)
7556{
7557 int varargs = ufunc->uf_va_name != NULL;
7558 int argcount = ufunc->uf_args.ga_len;
7559
7560 // Create a type for the function, with the return type and any
7561 // argument types.
7562 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7563 // The type is included in "tt_args".
7564 if (argcount > 0 || varargs)
7565 {
7566 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7567 argcount, &ufunc->uf_type_list);
7568 // Add argument types to the function type.
7569 if (func_type_add_arg_types(ufunc->uf_func_type,
7570 argcount + varargs,
7571 &ufunc->uf_type_list) == FAIL)
7572 return;
7573 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7574 ufunc->uf_func_type->tt_min_argcount =
7575 argcount - ufunc->uf_def_args.ga_len;
7576 if (ufunc->uf_arg_types == NULL)
7577 {
7578 int i;
7579
7580 // lambda does not have argument types.
7581 for (i = 0; i < argcount; ++i)
7582 ufunc->uf_func_type->tt_args[i] = &t_any;
7583 }
7584 else
7585 mch_memmove(ufunc->uf_func_type->tt_args,
7586 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7587 if (varargs)
7588 {
7589 ufunc->uf_func_type->tt_args[argcount] =
7590 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7591 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7592 }
7593 }
7594 else
7595 // No arguments, can use a predefined type.
7596 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7597 argcount, &ufunc->uf_type_list);
7598}
7599
7600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601/*
7602 * Delete an instruction, free what it contains.
7603 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007604 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605delete_instr(isn_T *isn)
7606{
7607 switch (isn->isn_type)
7608 {
7609 case ISN_EXEC:
7610 case ISN_LOADENV:
7611 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007612 case ISN_LOADB:
7613 case ISN_LOADW:
7614 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007616 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 case ISN_PUSHEXC:
7618 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007619 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007621 case ISN_STOREB:
7622 case ISN_STOREW:
7623 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007624 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007625 vim_free(isn->isn_arg.string);
7626 break;
7627
7628 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007629 case ISN_STORES:
7630 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 break;
7632
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007633 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007634 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007635 vim_free(isn->isn_arg.unlet.ul_name);
7636 break;
7637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007638 case ISN_STOREOPT:
7639 vim_free(isn->isn_arg.storeopt.so_name);
7640 break;
7641
7642 case ISN_PUSHBLOB: // push blob isn_arg.blob
7643 blob_unref(isn->isn_arg.blob);
7644 break;
7645
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007646 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007647#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007648 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007649#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007650 break;
7651
7652 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007653#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007654 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007655#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007656 break;
7657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 case ISN_UCALL:
7659 vim_free(isn->isn_arg.ufunc.cuf_name);
7660 break;
7661
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007662 case ISN_FUNCREF:
7663 {
7664 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7665 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007666
7667 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7668 func_ptr_unref(dfunc->df_ufunc);
7669 }
7670 break;
7671
7672 case ISN_DCALL:
7673 {
7674 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7675 + isn->isn_arg.dfunc.cdf_idx;
7676
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007677 if (dfunc->df_ufunc != NULL
7678 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007679 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007680 }
7681 break;
7682
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007683 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007684 {
7685 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7686 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7687
7688 if (ufunc != NULL)
7689 {
7690 // Clear uf_dfunc_idx so that the function is deleted.
7691 clear_def_function(ufunc);
7692 ufunc->uf_dfunc_idx = 0;
7693 func_ptr_unref(ufunc);
7694 }
7695
7696 vim_free(lambda);
7697 vim_free(isn->isn_arg.newfunc.nf_global);
7698 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007699 break;
7700
Bram Moolenaar5e654232020-09-16 15:22:00 +02007701 case ISN_CHECKTYPE:
7702 free_type(isn->isn_arg.type.ct_type);
7703 break;
7704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 case ISN_2BOOL:
7706 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007707 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 case ISN_ADDBLOB:
7709 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007710 case ISN_ANYINDEX:
7711 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007713 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007715 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717 case ISN_COMPAREANY:
7718 case ISN_COMPAREBLOB:
7719 case ISN_COMPAREBOOL:
7720 case ISN_COMPAREDICT:
7721 case ISN_COMPAREFLOAT:
7722 case ISN_COMPAREFUNC:
7723 case ISN_COMPARELIST:
7724 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 case ISN_COMPARESPECIAL:
7726 case ISN_COMPARESTRING:
7727 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007728 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 case ISN_DROP:
7730 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007731 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007732 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007733 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007734 case ISN_EXECCONCAT:
7735 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007737 case ISN_GETITEM:
7738 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007739 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007740 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007741 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007743 case ISN_LOADBDICT:
7744 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007745 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007747 case ISN_LOADSCRIPT:
7748 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007750 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007751 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007752 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 case ISN_NEGATENR:
7754 case ISN_NEWDICT:
7755 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007757 case ISN_OPFLOAT:
7758 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007760 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007761 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762 case ISN_PUSHF:
7763 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007765 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007767 case ISN_SHUFFLE:
7768 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007770 case ISN_STOREDICT:
7771 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007772 case ISN_STORENR:
7773 case ISN_STOREOUTER:
7774 case ISN_STOREREG:
7775 case ISN_STORESCRIPT:
7776 case ISN_STOREV:
7777 case ISN_STRINDEX:
7778 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 case ISN_THROW:
7780 case ISN_TRY:
7781 // nothing allocated
7782 break;
7783 }
7784}
7785
7786/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007787 * Free all instructions for "dfunc".
7788 */
7789 static void
7790delete_def_function_contents(dfunc_T *dfunc)
7791{
7792 int idx;
7793
7794 ga_clear(&dfunc->df_def_args_isn);
7795
7796 if (dfunc->df_instr != NULL)
7797 {
7798 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7799 delete_instr(dfunc->df_instr + idx);
7800 VIM_CLEAR(dfunc->df_instr);
7801 }
7802
7803 dfunc->df_deleted = TRUE;
7804}
7805
7806/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007807 * When a user function is deleted, clear the contents of any associated def
7808 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 */
7810 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007811clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007813 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 {
7815 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7816 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817
Bram Moolenaar20431c92020-03-20 18:39:46 +01007818 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007819 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 }
7821}
7822
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007823/*
7824 * Used when a user function is about to be deleted: remove the pointer to it.
7825 * The entry in def_functions is then unused.
7826 */
7827 void
7828unlink_def_function(ufunc_T *ufunc)
7829{
7830 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7831
7832 dfunc->df_ufunc = NULL;
7833}
7834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007836/*
7837 * Free all functions defined with ":def".
7838 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839 void
7840free_def_functions(void)
7841{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007842 int idx;
7843
7844 for (idx = 0; idx < def_functions.ga_len; ++idx)
7845 {
7846 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7847
7848 delete_def_function_contents(dfunc);
7849 }
7850
7851 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852}
7853#endif
7854
7855
7856#endif // FEAT_EVAL