blob: cd5e45bc68a69cf601fd8c3c8e170e8ab78ecd6d [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 Moolenaarfbdd08e2020-03-01 14:04:46 +01001463 type_T *argtypes[MAX_FUNC_ARGS];
1464 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465
Bram Moolenaar080457c2020-03-03 21:53:32 +01001466 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001467 argoff = check_internal_func(func_idx, argcount);
1468 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 return FAIL;
1470
Bram Moolenaar389df252020-07-09 21:20:47 +02001471 if (method_call && argoff > 1)
1472 {
1473 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.shuffle.shfl_item = argcount;
1476 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1477 }
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1480 return FAIL;
1481 isn->isn_arg.bfunc.cbf_idx = func_idx;
1482 isn->isn_arg.bfunc.cbf_argcount = argcount;
1483
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001484 for (i = 0; i < argcount; ++i)
1485 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 stack->ga_len -= argcount; // drop the arguments
1488 if (ga_grow(stack, 1) == FAIL)
1489 return FAIL;
1490 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001491 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 ++stack->ga_len; // add return value
1493
1494 return OK;
1495}
1496
1497/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001498 * Generate an ISN_LISTAPPEND instruction. Works like add().
1499 * Argument count is already checked.
1500 */
1501 static int
1502generate_LISTAPPEND(cctx_T *cctx)
1503{
1504 garray_T *stack = &cctx->ctx_type_stack;
1505 type_T *list_type;
1506 type_T *item_type;
1507 type_T *expected;
1508
1509 // Caller already checked that list_type is a list.
1510 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1511 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1512 expected = list_type->tt_member;
1513 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1514 return FAIL;
1515
1516 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1517 return FAIL;
1518
1519 --stack->ga_len; // drop the argument
1520 return OK;
1521}
1522
1523/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001524 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1525 * Argument count is already checked.
1526 */
1527 static int
1528generate_BLOBAPPEND(cctx_T *cctx)
1529{
1530 garray_T *stack = &cctx->ctx_type_stack;
1531 type_T *item_type;
1532
1533 // Caller already checked that blob_type is a blob.
1534 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1535 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1536 return FAIL;
1537
1538 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1539 return FAIL;
1540
1541 --stack->ga_len; // drop the argument
1542 return OK;
1543}
1544
1545/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 * Generate an ISN_DCALL or ISN_UCALL instruction.
1547 * Return FAIL if the number of arguments is wrong.
1548 */
1549 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001550generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551{
1552 isn_T *isn;
1553 garray_T *stack = &cctx->ctx_type_stack;
1554 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001555 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556
Bram Moolenaar080457c2020-03-03 21:53:32 +01001557 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 if (argcount > regular_args && !has_varargs(ufunc))
1559 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001560 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 return FAIL;
1562 }
1563 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1564 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001565 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 return FAIL;
1567 }
1568
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001569 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001570 {
1571 int i;
1572
1573 for (i = 0; i < argcount; ++i)
1574 {
1575 type_T *expected;
1576 type_T *actual;
1577
1578 if (i < regular_args)
1579 {
1580 if (ufunc->uf_arg_types == NULL)
1581 continue;
1582 expected = ufunc->uf_arg_types[i];
1583 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001584 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1585 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001586 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001587 else
1588 expected = ufunc->uf_va_type->tt_member;
1589 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001590 if (need_type(actual, expected, -argcount + i, cctx,
1591 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001592 {
1593 arg_type_mismatch(expected, actual, i + 1);
1594 return FAIL;
1595 }
1596 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001597 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001598 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1599 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001600 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001601 }
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001604 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001605 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001607 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 {
1609 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1610 isn->isn_arg.dfunc.cdf_argcount = argcount;
1611 }
1612 else
1613 {
1614 // A user function may be deleted and redefined later, can't use the
1615 // ufunc pointer, need to look it up again at runtime.
1616 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1617 isn->isn_arg.ufunc.cuf_argcount = argcount;
1618 }
1619
1620 stack->ga_len -= argcount; // drop the arguments
1621 if (ga_grow(stack, 1) == FAIL)
1622 return FAIL;
1623 // add return value
1624 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1625 ++stack->ga_len;
1626
1627 return OK;
1628}
1629
1630/*
1631 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1632 */
1633 static int
1634generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1635{
1636 isn_T *isn;
1637 garray_T *stack = &cctx->ctx_type_stack;
1638
Bram Moolenaar080457c2020-03-03 21:53:32 +01001639 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1641 return FAIL;
1642 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1643 isn->isn_arg.ufunc.cuf_argcount = argcount;
1644
1645 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001646 if (ga_grow(stack, 1) == FAIL)
1647 return FAIL;
1648 // add return value
1649 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1650 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651
1652 return OK;
1653}
1654
1655/*
1656 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001657 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 */
1659 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001660generate_PCALL(
1661 cctx_T *cctx,
1662 int argcount,
1663 char_u *name,
1664 type_T *type,
1665 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666{
1667 isn_T *isn;
1668 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001669 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaar080457c2020-03-03 21:53:32 +01001671 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001672
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001673 if (type->tt_type == VAR_ANY)
1674 ret_type = &t_any;
1675 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001676 {
1677 if (type->tt_argcount != -1)
1678 {
1679 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1680
1681 if (argcount < type->tt_min_argcount - varargs)
1682 {
1683 semsg(_(e_toofewarg), "[reference]");
1684 return FAIL;
1685 }
1686 if (!varargs && argcount > type->tt_argcount)
1687 {
1688 semsg(_(e_toomanyarg), "[reference]");
1689 return FAIL;
1690 }
1691 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001692 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001693 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001694 else
1695 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001696 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001697 return FAIL;
1698 }
1699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1701 return FAIL;
1702 isn->isn_arg.pfunc.cpf_top = at_top;
1703 isn->isn_arg.pfunc.cpf_argcount = argcount;
1704
1705 stack->ga_len -= argcount; // drop the arguments
1706
1707 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001708 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001710 // If partial is above the arguments it must be cleared and replaced with
1711 // the return value.
1712 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1713 return FAIL;
1714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 return OK;
1716}
1717
1718/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001719 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 */
1721 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001722generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723{
1724 isn_T *isn;
1725 garray_T *stack = &cctx->ctx_type_stack;
1726 type_T *type;
1727
Bram Moolenaar080457c2020-03-03 21:53:32 +01001728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001729 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001731 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001733 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001735 if (type->tt_type != VAR_DICT && type != &t_any)
1736 {
1737 emsg(_(e_dictreq));
1738 return FAIL;
1739 }
1740 // change dict type to dict member type
1741 if (type->tt_type == VAR_DICT)
1742 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
1744 return OK;
1745}
1746
1747/*
1748 * Generate an ISN_ECHO instruction.
1749 */
1750 static int
1751generate_ECHO(cctx_T *cctx, int with_white, int count)
1752{
1753 isn_T *isn;
1754
Bram Moolenaar080457c2020-03-03 21:53:32 +01001755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1757 return FAIL;
1758 isn->isn_arg.echo.echo_with_white = with_white;
1759 isn->isn_arg.echo.echo_count = count;
1760
1761 return OK;
1762}
1763
Bram Moolenaarad39c092020-02-26 18:23:43 +01001764/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001765 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001766 */
1767 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001768generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001769{
1770 isn_T *isn;
1771
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001772 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001773 return FAIL;
1774 isn->isn_arg.number = count;
1775
1776 return OK;
1777}
1778
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001779/*
1780 * Generate an ISN_PUT instruction.
1781 */
1782 static int
1783generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1784{
1785 isn_T *isn;
1786
1787 RETURN_OK_IF_SKIP(cctx);
1788 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1789 return FAIL;
1790 isn->isn_arg.put.put_regname = regname;
1791 isn->isn_arg.put.put_lnum = lnum;
1792 return OK;
1793}
1794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 static int
1796generate_EXEC(cctx_T *cctx, char_u *line)
1797{
1798 isn_T *isn;
1799
Bram Moolenaar080457c2020-03-03 21:53:32 +01001800 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1802 return FAIL;
1803 isn->isn_arg.string = vim_strsave(line);
1804 return OK;
1805}
1806
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001807 static int
1808generate_EXECCONCAT(cctx_T *cctx, int count)
1809{
1810 isn_T *isn;
1811
1812 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1813 return FAIL;
1814 isn->isn_arg.number = count;
1815 return OK;
1816}
1817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818/*
1819 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001820 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001822 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001823reserve_local(
1824 cctx_T *cctx,
1825 char_u *name,
1826 size_t len,
1827 int isConst,
1828 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 lvar_T *lvar;
1831
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001832 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001834 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001835 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 }
1837
1838 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001839 return NULL;
1840 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001841 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001843 // Every local variable uses the next entry on the stack. We could re-use
1844 // the last ones when leaving a scope, but then variables used in a closure
1845 // might get overwritten. To keep things simple do not re-use stack
1846 // entries. This is less efficient, but memory is cheap these days.
1847 lvar->lv_idx = cctx->ctx_locals_count++;
1848
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001849 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 lvar->lv_const = isConst;
1851 lvar->lv_type = type;
1852
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001853 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854}
1855
1856/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001857 * Remove local variables above "new_top".
1858 */
1859 static void
1860unwind_locals(cctx_T *cctx, int new_top)
1861{
1862 if (cctx->ctx_locals.ga_len > new_top)
1863 {
1864 int idx;
1865 lvar_T *lvar;
1866
1867 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1868 {
1869 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1870 vim_free(lvar->lv_name);
1871 }
1872 }
1873 cctx->ctx_locals.ga_len = new_top;
1874}
1875
1876/*
1877 * Free all local variables.
1878 */
1879 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001880free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001881{
1882 unwind_locals(cctx, 0);
1883 ga_clear(&cctx->ctx_locals);
1884}
1885
1886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 * Find "name" in script-local items of script "sid".
1888 * Returns the index in "sn_var_vals" if found.
1889 * If found but not in "sn_var_vals" returns -1.
1890 * If not found returns -2.
1891 */
1892 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001893get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894{
1895 hashtab_T *ht;
1896 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001897 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001898 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 int idx;
1900
Bram Moolenaare3d46852020-08-29 13:39:17 +02001901 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001903 if (sid == current_sctx.sc_sid)
1904 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001905 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001906
1907 if (sav == NULL)
1908 return -2;
1909 idx = sav->sav_var_vals_idx;
1910 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1911 if (check_writable && sv->sv_const)
1912 semsg(_(e_readonlyvar), name);
1913 return idx;
1914 }
1915
1916 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 ht = &SCRIPT_VARS(sid);
1918 di = find_var_in_ht(ht, 0, name, TRUE);
1919 if (di == NULL)
1920 return -2;
1921
1922 // Now find the svar_T index in sn_var_vals.
1923 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1924 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001925 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 if (sv->sv_tv == &di->di_tv)
1927 {
1928 if (check_writable && sv->sv_const)
1929 semsg(_(e_readonlyvar), name);
1930 return idx;
1931 }
1932 }
1933 return -1;
1934}
1935
1936/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001937 * Find "name" in imported items of the current script or in "cctx" if not
1938 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 */
1940 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001941find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 int idx;
1944
Bram Moolenaare3d46852020-08-29 13:39:17 +02001945 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001946 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 if (cctx != NULL)
1948 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1949 {
1950 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1951 + idx;
1952
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001953 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1954 : STRLEN(import->imp_name) == len
1955 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 return import;
1957 }
1958
Bram Moolenaarefa94442020-08-08 22:16:00 +02001959 return find_imported_in_script(name, len, current_sctx.sc_sid);
1960}
1961
1962 imported_T *
1963find_imported_in_script(char_u *name, size_t len, int sid)
1964{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001965 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001966 int idx;
1967
Bram Moolenaare3d46852020-08-29 13:39:17 +02001968 if (!SCRIPT_ID_VALID(sid))
1969 return NULL;
1970 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1972 {
1973 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1974
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001975 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1976 : STRLEN(import->imp_name) == len
1977 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 return import;
1979 }
1980 return NULL;
1981}
1982
1983/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001984 * Free all imported variables.
1985 */
1986 static void
1987free_imported(cctx_T *cctx)
1988{
1989 int idx;
1990
1991 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1992 {
1993 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1994
1995 vim_free(import->imp_name);
1996 }
1997 ga_clear(&cctx->ctx_imports);
1998}
1999
2000/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002001 * Return TRUE if "p" points at a "#" but not at "#{".
2002 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002003 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002004vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002005{
2006 return p[0] == '#' && p[1] != '{';
2007}
2008
2009/*
2010 * Return a pointer to the next line that isn't empty or only contains a
2011 * comment. Skips over white space.
2012 * Returns NULL if there is none.
2013 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002014 char_u *
2015peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002016{
2017 int lnum = cctx->ctx_lnum;
2018
2019 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2020 {
2021 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002022 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002023
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002024 // ignore NULLs inserted for continuation lines
2025 if (line != NULL)
2026 {
2027 p = skipwhite(line);
2028 if (*p != NUL && !vim9_comment_start(p))
2029 return p;
2030 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002031 }
2032 return NULL;
2033}
2034
2035/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002036 * Called when checking for a following operator at "arg". When the rest of
2037 * the line is empty or only a comment, peek the next line. If there is a next
2038 * line return a pointer to it and set "nextp".
2039 * Otherwise skip over white space.
2040 */
2041 static char_u *
2042may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2043{
2044 char_u *p = skipwhite(arg);
2045
2046 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002047 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002048 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002049 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002050 if (*nextp != NULL)
2051 return *nextp;
2052 }
2053 return p;
2054}
2055
2056/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002057 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002058 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002059 * Returns NULL when at the end.
2060 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002061 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002062next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002063{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002064 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002065
2066 do
2067 {
2068 ++cctx->ctx_lnum;
2069 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002070 {
2071 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002072 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002073 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002074 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002075 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002076 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002077 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002078 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002079 return line;
2080}
2081
2082/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002083 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002084 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002085 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2086 */
2087 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002088may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002089{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002090 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002091 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002092 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002093
2094 if (next == NULL)
2095 return FAIL;
2096 *arg = skipwhite(next);
2097 }
2098 return OK;
2099}
2100
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002101/*
2102 * Idem, and give an error when failed.
2103 */
2104 static int
2105may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2106{
2107 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2108 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002109 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002110 return FAIL;
2111 }
2112 return OK;
2113}
2114
2115
Bram Moolenaara5565e42020-05-09 15:44:01 +02002116// Structure passed between the compile_expr* functions to keep track of
2117// constants that have been parsed but for which no code was produced yet. If
2118// possible expressions on these constants are applied at compile time. If
2119// that is not possible, the code to push the constants needs to be generated
2120// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002121// Using 50 should be more than enough of 5 levels of ().
2122#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002123typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002124 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002125 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002126 int pp_is_const; // all generated code was constants, used for a
2127 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002128} ppconst_T;
2129
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002130static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002131static int compile_expr0(char_u **arg, cctx_T *cctx);
2132static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2133
Bram Moolenaara5565e42020-05-09 15:44:01 +02002134/*
2135 * Generate a PUSH instruction for "tv".
2136 * "tv" will be consumed or cleared.
2137 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2138 */
2139 static int
2140generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2141{
2142 if (tv != NULL)
2143 {
2144 switch (tv->v_type)
2145 {
2146 case VAR_UNKNOWN:
2147 break;
2148 case VAR_BOOL:
2149 generate_PUSHBOOL(cctx, tv->vval.v_number);
2150 break;
2151 case VAR_SPECIAL:
2152 generate_PUSHSPEC(cctx, tv->vval.v_number);
2153 break;
2154 case VAR_NUMBER:
2155 generate_PUSHNR(cctx, tv->vval.v_number);
2156 break;
2157#ifdef FEAT_FLOAT
2158 case VAR_FLOAT:
2159 generate_PUSHF(cctx, tv->vval.v_float);
2160 break;
2161#endif
2162 case VAR_BLOB:
2163 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2164 tv->vval.v_blob = NULL;
2165 break;
2166 case VAR_STRING:
2167 generate_PUSHS(cctx, tv->vval.v_string);
2168 tv->vval.v_string = NULL;
2169 break;
2170 default:
2171 iemsg("constant type not supported");
2172 clear_tv(tv);
2173 return FAIL;
2174 }
2175 tv->v_type = VAR_UNKNOWN;
2176 }
2177 return OK;
2178}
2179
2180/*
2181 * Generate code for any ppconst entries.
2182 */
2183 static int
2184generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2185{
2186 int i;
2187 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002188 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002189
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002190 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002191 for (i = 0; i < ppconst->pp_used; ++i)
2192 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2193 ret = FAIL;
2194 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002195 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002196 return ret;
2197}
2198
2199/*
2200 * Clear ppconst constants. Used when failing.
2201 */
2202 static void
2203clear_ppconst(ppconst_T *ppconst)
2204{
2205 int i;
2206
2207 for (i = 0; i < ppconst->pp_used; ++i)
2208 clear_tv(&ppconst->pp_tv[i]);
2209 ppconst->pp_used = 0;
2210}
2211
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002212/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002213 * Generate an instruction to load script-local variable "name", without the
2214 * leading "s:".
2215 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 */
2217 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002218compile_load_scriptvar(
2219 cctx_T *cctx,
2220 char_u *name, // variable NUL terminated
2221 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002222 char_u **end, // end of variable
2223 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002225 scriptitem_T *si;
2226 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 imported_T *import;
2228
Bram Moolenaare3d46852020-08-29 13:39:17 +02002229 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2230 return FAIL;
2231 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002232 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002233 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002235 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002236 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2237 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 }
2239 if (idx >= 0)
2240 {
2241 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2242
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002243 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 current_sctx.sc_sid, idx, sv->sv_type);
2245 return OK;
2246 }
2247
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002248 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 if (import != NULL)
2250 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002251 if (import->imp_all)
2252 {
2253 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002254 char_u *exp_name;
2255 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002256 ufunc_T *ufunc;
2257 type_T *type;
2258
2259 // Used "import * as Name", need to lookup the member.
2260 if (*p != '.')
2261 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002262 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002263 return FAIL;
2264 }
2265 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002266 if (VIM_ISWHITE(*p))
2267 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002268 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002269 return FAIL;
2270 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002271
Bram Moolenaar1c991142020-07-04 13:15:31 +02002272 // isolate one name
2273 exp_name = p;
2274 while (eval_isnamec(*p))
2275 ++p;
2276 cc = *p;
2277 *p = NUL;
2278
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002279 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002280 *p = cc;
2281 p = skipwhite(p);
2282
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002283 // TODO: what if it is a function?
2284 if (idx < 0)
2285 return FAIL;
2286 *end = p;
2287
2288 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2289 import->imp_sid,
2290 idx,
2291 type);
2292 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002293 else if (import->imp_funcname != NULL)
2294 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002295 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002296 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2297 import->imp_sid,
2298 import->imp_var_vals_idx,
2299 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 return OK;
2301 }
2302
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002303 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002304 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 return FAIL;
2306}
2307
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002308 static int
2309generate_funcref(cctx_T *cctx, char_u *name)
2310{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002311 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002312
2313 if (ufunc == NULL)
2314 return FAIL;
2315
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002316 // Need to compile any default values to get the argument types.
2317 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2318 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2319 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002320 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002321}
2322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323/*
2324 * Compile a variable name into a load instruction.
2325 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002326 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 * When "error" is FALSE do not give an error when not found.
2328 */
2329 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002330compile_load(
2331 char_u **arg,
2332 char_u *end_arg,
2333 cctx_T *cctx,
2334 int is_expr,
2335 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336{
2337 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002338 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002339 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002341 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342
2343 if (*(*arg + 1) == ':')
2344 {
2345 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002346 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002347 {
2348 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002350 switch (**arg)
2351 {
2352 case 'g': isn_type = ISN_LOADGDICT; break;
2353 case 'w': isn_type = ISN_LOADWDICT; break;
2354 case 't': isn_type = ISN_LOADTDICT; break;
2355 case 'b': isn_type = ISN_LOADBDICT; break;
2356 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002357 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002358 goto theend;
2359 }
2360 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2361 goto theend;
2362 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 else
2365 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002366 isntype_T isn_type = ISN_DROP;
2367
2368 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2369 if (name == NULL)
2370 return FAIL;
2371
2372 switch (**arg)
2373 {
2374 case 'v': res = generate_LOADV(cctx, name, error);
2375 break;
2376 case 's': res = compile_load_scriptvar(cctx, name,
2377 NULL, NULL, error);
2378 break;
2379 case 'g': isn_type = ISN_LOADG; break;
2380 case 'w': isn_type = ISN_LOADW; break;
2381 case 't': isn_type = ISN_LOADT; break;
2382 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002383 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002384 goto theend;
2385 }
2386 if (isn_type != ISN_DROP)
2387 {
2388 // Global, Buffer-local, Window-local and Tabpage-local
2389 // variables can be defined later, thus we don't check if it
2390 // exists, give error at runtime.
2391 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 }
2394 }
2395 else
2396 {
2397 size_t len = end - *arg;
2398 int idx;
2399 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002400 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
2402 name = vim_strnsave(*arg, end - *arg);
2403 if (name == NULL)
2404 return FAIL;
2405
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002406 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002408 if (!gen_load_outer)
2409 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
2411 else
2412 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002413 lvar_T *lvar = lookup_local(*arg, len, cctx);
2414
2415 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002417 type = lvar->lv_type;
2418 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002419 if (lvar->lv_from_outer)
2420 gen_load_outer = TRUE;
2421 else
2422 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 }
2424 else
2425 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002426 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002427 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002428 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002429 || find_imported(name, 0, cctx) != NULL)
2430 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002431
Bram Moolenaar0f769812020-09-12 18:32:34 +02002432 // When evaluating an expression and the name starts with an
2433 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002434 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002435 if (res == FAIL && is_expr
2436 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002437 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 }
2439 }
2440 if (gen_load)
2441 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002442 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002443 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002444 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002445 cctx->ctx_outer_used = TRUE;
2446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 }
2448
2449 *arg = end;
2450
2451theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002452 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002453 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 vim_free(name);
2455 return res;
2456}
2457
2458/*
2459 * Compile the argument expressions.
2460 * "arg" points to just after the "(" and is advanced to after the ")"
2461 */
2462 static int
2463compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2464{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002465 char_u *p = *arg;
2466 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002467 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468
Bram Moolenaare6085c52020-04-12 20:19:16 +02002469 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002471 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2472 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002473 if (*p == ')')
2474 {
2475 *arg = p + 1;
2476 return OK;
2477 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002478 if (must_end)
2479 {
2480 semsg(_(e_missing_comma_before_argument_str), p);
2481 return FAIL;
2482 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002483
Bram Moolenaara5565e42020-05-09 15:44:01 +02002484 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 return FAIL;
2486 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002487
2488 if (*p != ',' && *skipwhite(p) == ',')
2489 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002490 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002491 p = skipwhite(p);
2492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002494 {
2495 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002497 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002498 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002499 else
2500 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002501 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002502 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002504failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002505 emsg(_(e_missing_close));
2506 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507}
2508
2509/*
2510 * Compile a function call: name(arg1, arg2)
2511 * "arg" points to "name", "arg + varlen" to the "(".
2512 * "argcount_init" is 1 for "value->method()"
2513 * Instructions:
2514 * EVAL arg1
2515 * EVAL arg2
2516 * BCALL / DCALL / UCALL
2517 */
2518 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002519compile_call(
2520 char_u **arg,
2521 size_t varlen,
2522 cctx_T *cctx,
2523 ppconst_T *ppconst,
2524 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525{
2526 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002527 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 int argcount = argcount_init;
2529 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002530 char_u fname_buf[FLEN_FIXED + 1];
2531 char_u *tofree = NULL;
2532 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002534 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002535 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536
Bram Moolenaara5565e42020-05-09 15:44:01 +02002537 // we can evaluate "has('name')" at compile time
2538 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2539 {
2540 char_u *s = skipwhite(*arg + varlen + 1);
2541 typval_T argvars[2];
2542
2543 argvars[0].v_type = VAR_UNKNOWN;
2544 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002545 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002546 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002547 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002548 s = skipwhite(s);
2549 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2550 {
2551 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2552
2553 *arg = s + 1;
2554 argvars[1].v_type = VAR_UNKNOWN;
2555 tv->v_type = VAR_NUMBER;
2556 tv->vval.v_number = 0;
2557 f_has(argvars, tv);
2558 clear_tv(&argvars[0]);
2559 ++ppconst->pp_used;
2560 return OK;
2561 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002562 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002563 }
2564
2565 if (generate_ppconst(cctx, ppconst) == FAIL)
2566 return FAIL;
2567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (varlen >= sizeof(namebuf))
2569 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002570 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 return FAIL;
2572 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002573 vim_strncpy(namebuf, *arg, varlen);
2574 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
2576 *arg = skipwhite(*arg + varlen + 1);
2577 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002578 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579
Bram Moolenaara1773442020-08-12 15:21:22 +02002580 is_autoload = vim_strchr(name, '#') != NULL;
2581 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 {
2583 int idx;
2584
2585 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002586 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002588 {
2589 if (STRCMP(name, "add") == 0 && argcount == 2)
2590 {
2591 garray_T *stack = &cctx->ctx_type_stack;
2592 type_T *type = ((type_T **)stack->ga_data)[
2593 stack->ga_len - 2];
2594
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002595 if (type->tt_type == VAR_LIST)
2596 {
2597 // inline "add(list, item)" so that the type can be checked
2598 res = generate_LISTAPPEND(cctx);
2599 idx = -1;
2600 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002601 else if (type->tt_type == VAR_BLOB)
2602 {
2603 // inline "add(blob, nr)" so that the type can be checked
2604 res = generate_BLOBAPPEND(cctx);
2605 idx = -1;
2606 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002607 }
2608
2609 if (idx >= 0)
2610 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2611 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002612 else
2613 semsg(_(e_unknownfunc), namebuf);
2614 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 }
2616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002618 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002619 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002620 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002621 {
2622 res = generate_CALL(cctx, ufunc, argcount);
2623 goto theend;
2624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625
2626 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002627 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002628 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002630 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002631 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002632 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002633 garray_T *stack = &cctx->ctx_type_stack;
2634 type_T *type;
2635
2636 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2637 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002638 goto theend;
2639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640
Bram Moolenaar0f769812020-09-12 18:32:34 +02002641 // If we can find a global function by name generate the right call.
2642 if (ufunc != NULL)
2643 {
2644 res = generate_CALL(cctx, ufunc, argcount);
2645 goto theend;
2646 }
2647
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002648 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002649 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002650 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002651 res = generate_UCALL(cctx, name, argcount);
2652 else
2653 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002654
2655theend:
2656 vim_free(tofree);
2657 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658}
2659
2660// like NAMESPACE_CHAR but with 'a' and 'l'.
2661#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2662
2663/*
2664 * Find the end of a variable or function name. Unlike find_name_end() this
2665 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002666 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 * Return a pointer to just after the name. Equal to "arg" if there is no
2668 * valid name.
2669 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002670 static char_u *
2671to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672{
2673 char_u *p;
2674
2675 // Quick check for valid starting character.
2676 if (!eval_isnamec1(*arg))
2677 return arg;
2678
2679 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2680 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2681 // and can be used in slice "[n:]".
2682 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002683 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2685 break;
2686 return p;
2687}
2688
2689/*
2690 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002691 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 */
2693 char_u *
2694to_name_const_end(char_u *arg)
2695{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002696 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 typval_T rettv;
2698
2699 if (p == arg && *arg == '[')
2700 {
2701
2702 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002703 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 p = arg;
2705 }
2706 else if (p == arg && *arg == '#' && arg[1] == '{')
2707 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002708 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002710 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 p = arg;
2712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
2714 return p;
2715}
2716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717/*
2718 * parse a list: [expr, expr]
2719 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002720 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 */
2722 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002723compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724{
2725 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002726 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002728 int is_const;
2729 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002731 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002733 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002734 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002735 semsg(_(e_list_end), *arg);
2736 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002737 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002738 if (*p == ',')
2739 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002740 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002741 return FAIL;
2742 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002743 if (*p == ']')
2744 {
2745 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002746 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002747 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002748 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002749 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002750 if (!is_const)
2751 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 ++count;
2753 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002754 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002756 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2757 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002758 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002759 return FAIL;
2760 }
2761 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002762 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 p = skipwhite(p);
2764 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002765 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002767 ppconst->pp_is_const = is_all_const;
2768 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769}
2770
2771/*
2772 * parse a lambda: {arg, arg -> expr}
2773 * "*arg" points to the '{'.
2774 */
2775 static int
2776compile_lambda(char_u **arg, cctx_T *cctx)
2777{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 typval_T rettv;
2779 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002780 evalarg_T evalarg;
2781
2782 CLEAR_FIELD(evalarg);
2783 evalarg.eval_flags = EVAL_EVALUATE;
2784 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785
2786 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002787 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002788 {
2789 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002791 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002794 ++ufunc->uf_refcount;
2795 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002796 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797
2798 // The function will have one line: "return {expr}".
2799 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002800 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002802 clear_evalarg(&evalarg, NULL);
2803
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002804 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002805 {
2806 // The return type will now be known.
2807 set_function_type(ufunc);
2808
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002809 // The function reference count will be 1. When the ISN_FUNCREF
2810 // instruction is deleted the reference count is decremented and the
2811 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002812 return generate_FUNCREF(cctx, ufunc);
2813 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002814
2815 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 return FAIL;
2817}
2818
2819/*
2820 * Compile a lamda call: expr->{lambda}(args)
2821 * "arg" points to the "{".
2822 */
2823 static int
2824compile_lambda_call(char_u **arg, cctx_T *cctx)
2825{
2826 ufunc_T *ufunc;
2827 typval_T rettv;
2828 int argcount = 1;
2829 int ret = FAIL;
2830
2831 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002832 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 return FAIL;
2834
2835 if (**arg != '(')
2836 {
2837 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002838 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 else
2840 semsg(_(e_missing_paren), "lambda");
2841 clear_tv(&rettv);
2842 return FAIL;
2843 }
2844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 ufunc = rettv.vval.v_partial->pt_func;
2846 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002847 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002848 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002849
Bram Moolenaara05e5242020-09-19 18:19:19 +02002850 // The function will have one line: "return {expr}". Compile it into
2851 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002852 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
2854 // compile the arguments
2855 *arg = skipwhite(*arg + 1);
2856 if (compile_arguments(arg, cctx, &argcount) == OK)
2857 // call the compiled function
2858 ret = generate_CALL(cctx, ufunc, argcount);
2859
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002860 if (ret == FAIL)
2861 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 return ret;
2863}
2864
2865/*
2866 * parse a dict: {'key': val} or #{key: val}
2867 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002868 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 */
2870 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872{
2873 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002874 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 int count = 0;
2876 dict_T *d = dict_alloc();
2877 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002878 char_u *whitep = *arg;
2879 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002880 int is_const;
2881 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882
2883 if (d == NULL)
2884 return FAIL;
2885 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002886 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 {
2888 char_u *key = NULL;
2889
Bram Moolenaar23c55272020-06-21 16:58:13 +02002890 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002891 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002892 *arg = NULL;
2893 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002894 }
2895
2896 if (**arg == '}')
2897 break;
2898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 if (literal)
2900 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002901 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902
Bram Moolenaar2c330432020-04-13 14:41:35 +02002903 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002905 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 return FAIL;
2907 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002908 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 if (generate_PUSHS(cctx, key) == FAIL)
2910 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002911 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 }
2913 else
2914 {
2915 isn_T *isn;
2916
Bram Moolenaara5565e42020-05-09 15:44:01 +02002917 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2920 if (isn->isn_type == ISN_PUSHS)
2921 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002922 else
2923 {
2924 type_T *keytype = ((type_T **)stack->ga_data)
2925 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002926 if (need_type(keytype, &t_string, -1, cctx,
2927 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002928 return FAIL;
2929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 }
2931
2932 // Check for duplicate keys, if using string keys.
2933 if (key != NULL)
2934 {
2935 item = dict_find(d, key, -1);
2936 if (item != NULL)
2937 {
2938 semsg(_(e_duplicate_key), key);
2939 goto failret;
2940 }
2941 item = dictitem_alloc(key);
2942 if (item != NULL)
2943 {
2944 item->di_tv.v_type = VAR_UNKNOWN;
2945 item->di_tv.v_lock = 0;
2946 if (dict_add(d, item) == FAIL)
2947 dictitem_free(item);
2948 }
2949 }
2950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 if (**arg != ':')
2952 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002953 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002954 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002955 else
2956 semsg(_(e_missing_dict_colon), *arg);
2957 return FAIL;
2958 }
2959 whitep = *arg + 1;
2960 if (!IS_WHITE_OR_NUL(*whitep))
2961 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002962 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 return FAIL;
2964 }
2965
2966 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002967 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002968 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002969 *arg = NULL;
2970 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002971 }
2972
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002973 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002975 if (!is_const)
2976 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 ++count;
2978
Bram Moolenaar2c330432020-04-13 14:41:35 +02002979 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002980 *arg = skipwhite(*arg);
2981 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002982 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002983 *arg = NULL;
2984 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 if (**arg == '}')
2987 break;
2988 if (**arg != ',')
2989 {
2990 semsg(_(e_missing_dict_comma), *arg);
2991 goto failret;
2992 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002993 if (IS_WHITE_OR_NUL(*whitep))
2994 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002995 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002996 return FAIL;
2997 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002998 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02002999 if (!IS_WHITE_OR_NUL(*whitep))
3000 {
3001 semsg(_(e_white_space_required_after_str), ",");
3002 return FAIL;
3003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 *arg = skipwhite(*arg + 1);
3005 }
3006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 *arg = *arg + 1;
3008
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003009 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003010 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003011 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003012 *arg += STRLEN(*arg);
3013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003015 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 return generate_NEWDICT(cctx, count);
3017
3018failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003019 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003020 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003021 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003022 *arg = (char_u *)"";
3023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 dict_unref(d);
3025 return FAIL;
3026}
3027
3028/*
3029 * Compile "&option".
3030 */
3031 static int
3032compile_get_option(char_u **arg, cctx_T *cctx)
3033{
3034 typval_T rettv;
3035 char_u *start = *arg;
3036 int ret;
3037
3038 // parse the option and get the current value to get the type.
3039 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003040 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 if (ret == OK)
3042 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003043 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 char_u *name = vim_strnsave(start, *arg - start);
3045 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3046
3047 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3048 vim_free(name);
3049 }
3050 clear_tv(&rettv);
3051
3052 return ret;
3053}
3054
3055/*
3056 * Compile "$VAR".
3057 */
3058 static int
3059compile_get_env(char_u **arg, cctx_T *cctx)
3060{
3061 char_u *start = *arg;
3062 int len;
3063 int ret;
3064 char_u *name;
3065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 ++*arg;
3067 len = get_env_len(arg);
3068 if (len == 0)
3069 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003070 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 return FAIL;
3072 }
3073
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003074 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 name = vim_strnsave(start, len + 1);
3076 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3077 vim_free(name);
3078 return ret;
3079}
3080
3081/*
3082 * Compile "@r".
3083 */
3084 static int
3085compile_get_register(char_u **arg, cctx_T *cctx)
3086{
3087 int ret;
3088
3089 ++*arg;
3090 if (**arg == NUL)
3091 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003092 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 return FAIL;
3094 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003095 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 {
3097 emsg_invreg(**arg);
3098 return FAIL;
3099 }
3100 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3101 ++*arg;
3102 return ret;
3103}
3104
3105/*
3106 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003107 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 */
3109 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003110apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003112 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
3114 // this works from end to start
3115 while (p > start)
3116 {
3117 --p;
3118 if (*p == '-' || *p == '+')
3119 {
3120 // only '-' has an effect, for '+' we only check the type
3121#ifdef FEAT_FLOAT
3122 if (rettv->v_type == VAR_FLOAT)
3123 {
3124 if (*p == '-')
3125 rettv->vval.v_float = -rettv->vval.v_float;
3126 }
3127 else
3128#endif
3129 {
3130 varnumber_T val;
3131 int error = FALSE;
3132
3133 // tv_get_number_chk() accepts a string, but we don't want that
3134 // here
3135 if (check_not_string(rettv) == FAIL)
3136 return FAIL;
3137 val = tv_get_number_chk(rettv, &error);
3138 clear_tv(rettv);
3139 if (error)
3140 return FAIL;
3141 if (*p == '-')
3142 val = -val;
3143 rettv->v_type = VAR_NUMBER;
3144 rettv->vval.v_number = val;
3145 }
3146 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003147 else if (numeric_only)
3148 {
3149 ++p;
3150 break;
3151 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003152 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 {
3154 int v = tv2bool(rettv);
3155
3156 // '!' is permissive in the type.
3157 clear_tv(rettv);
3158 rettv->v_type = VAR_BOOL;
3159 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3160 }
3161 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003162 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 return OK;
3164}
3165
3166/*
3167 * Recognize v: variables that are constants and set "rettv".
3168 */
3169 static void
3170get_vim_constant(char_u **arg, typval_T *rettv)
3171{
3172 if (STRNCMP(*arg, "v:true", 6) == 0)
3173 {
3174 rettv->v_type = VAR_BOOL;
3175 rettv->vval.v_number = VVAL_TRUE;
3176 *arg += 6;
3177 }
3178 else if (STRNCMP(*arg, "v:false", 7) == 0)
3179 {
3180 rettv->v_type = VAR_BOOL;
3181 rettv->vval.v_number = VVAL_FALSE;
3182 *arg += 7;
3183 }
3184 else if (STRNCMP(*arg, "v:null", 6) == 0)
3185 {
3186 rettv->v_type = VAR_SPECIAL;
3187 rettv->vval.v_number = VVAL_NULL;
3188 *arg += 6;
3189 }
3190 else if (STRNCMP(*arg, "v:none", 6) == 0)
3191 {
3192 rettv->v_type = VAR_SPECIAL;
3193 rettv->vval.v_number = VVAL_NONE;
3194 *arg += 6;
3195 }
3196}
3197
Bram Moolenaar696ba232020-07-29 21:20:41 +02003198 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003199get_compare_type(char_u *p, int *len, int *type_is)
3200{
3201 exptype_T type = EXPR_UNKNOWN;
3202 int i;
3203
3204 switch (p[0])
3205 {
3206 case '=': if (p[1] == '=')
3207 type = EXPR_EQUAL;
3208 else if (p[1] == '~')
3209 type = EXPR_MATCH;
3210 break;
3211 case '!': if (p[1] == '=')
3212 type = EXPR_NEQUAL;
3213 else if (p[1] == '~')
3214 type = EXPR_NOMATCH;
3215 break;
3216 case '>': if (p[1] != '=')
3217 {
3218 type = EXPR_GREATER;
3219 *len = 1;
3220 }
3221 else
3222 type = EXPR_GEQUAL;
3223 break;
3224 case '<': if (p[1] != '=')
3225 {
3226 type = EXPR_SMALLER;
3227 *len = 1;
3228 }
3229 else
3230 type = EXPR_SEQUAL;
3231 break;
3232 case 'i': if (p[1] == 's')
3233 {
3234 // "is" and "isnot"; but not a prefix of a name
3235 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3236 *len = 5;
3237 i = p[*len];
3238 if (!isalnum(i) && i != '_')
3239 {
3240 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3241 *type_is = TRUE;
3242 }
3243 }
3244 break;
3245 }
3246 return type;
3247}
3248
3249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003251 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 */
3253 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003254compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003256 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257
3258 // this works from end to start
3259 while (p > start)
3260 {
3261 --p;
3262 if (*p == '-' || *p == '+')
3263 {
3264 int negate = *p == '-';
3265 isn_T *isn;
3266
3267 // TODO: check type
3268 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3269 {
3270 --p;
3271 if (*p == '-')
3272 negate = !negate;
3273 }
3274 // only '-' has an effect, for '+' we only check the type
3275 if (negate)
3276 isn = generate_instr(cctx, ISN_NEGATENR);
3277 else
3278 isn = generate_instr(cctx, ISN_CHECKNR);
3279 if (isn == NULL)
3280 return FAIL;
3281 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003282 else if (numeric_only)
3283 {
3284 ++p;
3285 break;
3286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 else
3288 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003289 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003291 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003293 if (p[-1] == '!')
3294 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 }
3297 if (generate_2BOOL(cctx, invert) == FAIL)
3298 return FAIL;
3299 }
3300 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003301 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 return OK;
3303}
3304
3305/*
3306 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003307 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 */
3309 static int
3310compile_subscript(
3311 char_u **arg,
3312 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003313 char_u *start_leader,
3314 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003315 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003317 char_u *name_start = *end_leader;
3318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 for (;;)
3320 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003321 char_u *p = skipwhite(*arg);
3322
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003323 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003324 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003325 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003326
3327 // If a following line starts with "->{" or "->X" advance to that
3328 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003329 // Also if a following line starts with ".x".
3330 if (next != NULL &&
3331 ((next[0] == '-' && next[1] == '>'
3332 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003333 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003334 {
3335 next = next_line_from_context(cctx, TRUE);
3336 if (next == NULL)
3337 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003338 *arg = next;
3339 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003340 }
3341 }
3342
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003343 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3344 // not a function call.
3345 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003347 garray_T *stack = &cctx->ctx_type_stack;
3348 type_T *type;
3349 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003351 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003352 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003353 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003356 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3357
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003358 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3360 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003361 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 return FAIL;
3363 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003364 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003366 char_u *pstart = p;
3367
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003368 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003369 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003370 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 // something->method()
3373 // Apply the '!', '-' and '+' first:
3374 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003375 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003378 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003379 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003380 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 if (**arg == '{')
3382 {
3383 // lambda call: list->{lambda}
3384 if (compile_lambda_call(arg, cctx) == FAIL)
3385 return FAIL;
3386 }
3387 else
3388 {
3389 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003390 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003391 if (!eval_isnamec1(*p))
3392 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003393 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003394 return FAIL;
3395 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003396 if (ASCII_ISALPHA(*p) && p[1] == ':')
3397 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003398 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 ;
3400 if (*p != '(')
3401 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003402 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 return FAIL;
3404 }
3405 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003406 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 return FAIL;
3408 }
3409 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003410 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003412 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003413 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003414 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003415 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003416 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003417
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003418 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003419 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003420 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003421 // TODO: blob index
3422 // TODO: more arguments
3423 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003424 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003425 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003426 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003427
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003428 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003429 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003430 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003431 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003432 if (**arg == ':')
3433 // missing first index is equal to zero
3434 generate_PUSHNR(cctx, 0);
3435 else
3436 {
3437 if (compile_expr0(arg, cctx) == FAIL)
3438 return FAIL;
3439 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3440 return FAIL;
3441 *arg = skipwhite(*arg);
3442 }
3443 if (**arg == ':')
3444 {
3445 *arg = skipwhite(*arg + 1);
3446 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3447 return FAIL;
3448 if (**arg == ']')
3449 // missing second index is equal to end of string
3450 generate_PUSHNR(cctx, -1);
3451 else
3452 {
3453 if (compile_expr0(arg, cctx) == FAIL)
3454 return FAIL;
3455 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3456 return FAIL;
3457 *arg = skipwhite(*arg);
3458 }
3459 is_slice = TRUE;
3460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461
3462 if (**arg != ']')
3463 {
3464 emsg(_(e_missbrac));
3465 return FAIL;
3466 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003467 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003469 // We can index a list and a dict. If we don't know the type
3470 // we can use the index value type.
3471 // TODO: If we don't know use an instruction to figure it out at
3472 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003473 typep = ((type_T **)stack->ga_data) + stack->ga_len
3474 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003475 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003476 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3477 // If the index is a string, the variable must be a Dict.
3478 if (*typep == &t_any && valtype == &t_string)
3479 vtype = VAR_DICT;
3480 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003481 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003482 if (need_type(valtype, &t_number, -1, cctx,
3483 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003484 return FAIL;
3485 if (is_slice)
3486 {
3487 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003488 if (need_type(valtype, &t_number, -2, cctx,
3489 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003490 return FAIL;
3491 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003492 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003493
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003494 if (vtype == VAR_DICT)
3495 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003496 if (is_slice)
3497 {
3498 emsg(_(e_cannot_slice_dictionary));
3499 return FAIL;
3500 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003501 if ((*typep)->tt_type == VAR_DICT)
3502 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003503 else
3504 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003505 if (need_type(*typep, &t_dict_any, -2, cctx,
3506 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003507 return FAIL;
3508 *typep = &t_any;
3509 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003510 if (may_generate_2STRING(-1, cctx) == FAIL)
3511 return FAIL;
3512 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3513 return FAIL;
3514 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003515 else if (vtype == VAR_STRING)
3516 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003517 *typep = &t_string;
3518 if ((is_slice
3519 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3520 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003521 return FAIL;
3522 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003523 else if (vtype == VAR_BLOB)
3524 {
3525 emsg("Sorry, blob index and slice not implemented yet");
3526 return FAIL;
3527 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003528 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003529 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003530 if (is_slice)
3531 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003532 if (generate_instr_drop(cctx,
3533 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3534 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003535 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003536 }
Bram Moolenaared591872020-08-15 22:14:53 +02003537 else
3538 {
3539 if ((*typep)->tt_type == VAR_LIST)
3540 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003541 if (generate_instr_drop(cctx,
3542 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3543 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003544 return FAIL;
3545 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003546 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003547 else
3548 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003549 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003550 return FAIL;
3551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003553 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003555 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003556 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003557 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003558 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003559
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003560 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003561 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003562 {
3563 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003564 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003565 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003566 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003567 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 while (eval_isnamec(*p))
3569 MB_PTR_ADV(p);
3570 if (p == *arg)
3571 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003572 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 return FAIL;
3574 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003575 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 return FAIL;
3577 *arg = p;
3578 }
3579 else
3580 break;
3581 }
3582
3583 // TODO - see handle_subscript():
3584 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3585 // Don't do this when "Func" is already a partial that was bound
3586 // explicitly (pt_auto is FALSE).
3587
3588 return OK;
3589}
3590
3591/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003592 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3593 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003595 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003596 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003597 *
3598 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 */
3600
3601/*
3602 * number number constant
3603 * 0zFFFFFFFF Blob constant
3604 * "string" string constant
3605 * 'string' literal string constant
3606 * &option-name option value
3607 * @r register contents
3608 * identifier variable value
3609 * function() function call
3610 * $VAR environment variable
3611 * (expression) nested expression
3612 * [expr, expr] List
3613 * {key: val, key: val} Dictionary
3614 * #{key: val, key: val} Dictionary with literal keys
3615 *
3616 * Also handle:
3617 * ! in front logical NOT
3618 * - in front unary minus
3619 * + in front unary plus (ignored)
3620 * trailing (arg) funcref/partial call
3621 * trailing [] subscript in String or List
3622 * trailing .name entry in Dictionary
3623 * trailing ->name() method call
3624 */
3625 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003626compile_expr7(
3627 char_u **arg,
3628 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003629 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 char_u *start_leader, *end_leader;
3632 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003633 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003634 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003636 ppconst->pp_is_const = FALSE;
3637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 /*
3639 * Skip '!', '-' and '+' characters. They are handled later.
3640 */
3641 start_leader = *arg;
3642 while (**arg == '!' || **arg == '-' || **arg == '+')
3643 *arg = skipwhite(*arg + 1);
3644 end_leader = *arg;
3645
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003646 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 switch (**arg)
3648 {
3649 /*
3650 * Number constant.
3651 */
3652 case '0': // also for blob starting with 0z
3653 case '1':
3654 case '2':
3655 case '3':
3656 case '4':
3657 case '5':
3658 case '6':
3659 case '7':
3660 case '8':
3661 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003662 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003664 // Apply "-" and "+" just before the number now, right to
3665 // left. Matters especially when "->" follows. Stops at
3666 // '!'.
3667 if (apply_leader(rettv, TRUE,
3668 start_leader, &end_leader) == FAIL)
3669 {
3670 clear_tv(rettv);
3671 return FAIL;
3672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 break;
3674
3675 /*
3676 * String constant: "string".
3677 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003678 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 return FAIL;
3680 break;
3681
3682 /*
3683 * Literal string constant: 'str''ing'.
3684 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003685 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 return FAIL;
3687 break;
3688
3689 /*
3690 * Constant Vim variable.
3691 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003692 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 ret = NOTDONE;
3694 break;
3695
3696 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003697 * "true" constant
3698 */
3699 case 't': if (STRNCMP(*arg, "true", 4) == 0
3700 && !eval_isnamec((*arg)[4]))
3701 {
3702 *arg += 4;
3703 rettv->v_type = VAR_BOOL;
3704 rettv->vval.v_number = VVAL_TRUE;
3705 }
3706 else
3707 ret = NOTDONE;
3708 break;
3709
3710 /*
3711 * "false" constant
3712 */
3713 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3714 && !eval_isnamec((*arg)[5]))
3715 {
3716 *arg += 5;
3717 rettv->v_type = VAR_BOOL;
3718 rettv->vval.v_number = VVAL_FALSE;
3719 }
3720 else
3721 ret = NOTDONE;
3722 break;
3723
3724 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 * List: [expr, expr]
3726 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003727 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 break;
3729
3730 /*
3731 * Dictionary: #{key: val, key: val}
3732 */
3733 case '#': if ((*arg)[1] == '{')
3734 {
3735 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003736 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 }
3738 else
3739 ret = NOTDONE;
3740 break;
3741
3742 /*
3743 * Lambda: {arg, arg -> expr}
3744 * Dictionary: {'key': val, 'key': val}
3745 */
3746 case '{': {
3747 char_u *start = skipwhite(*arg + 1);
3748
3749 // Find out what comes after the arguments.
3750 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003751 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 if (ret != FAIL && *start == '>')
3753 ret = compile_lambda(arg, cctx);
3754 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003755 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 }
3757 break;
3758
3759 /*
3760 * Option value: &name
3761 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003762 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3763 return FAIL;
3764 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 break;
3766
3767 /*
3768 * Environment variable: $VAR.
3769 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003770 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3771 return FAIL;
3772 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 break;
3774
3775 /*
3776 * Register contents: @r.
3777 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003778 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3779 return FAIL;
3780 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 break;
3782 /*
3783 * nested expression: (expression).
3784 */
3785 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003786
3787 // recursive!
3788 if (ppconst->pp_used <= PPSIZE - 10)
3789 {
3790 ret = compile_expr1(arg, cctx, ppconst);
3791 }
3792 else
3793 {
3794 // Not enough space in ppconst, flush constants.
3795 if (generate_ppconst(cctx, ppconst) == FAIL)
3796 return FAIL;
3797 ret = compile_expr0(arg, cctx);
3798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 *arg = skipwhite(*arg);
3800 if (**arg == ')')
3801 ++*arg;
3802 else if (ret == OK)
3803 {
3804 emsg(_(e_missing_close));
3805 ret = FAIL;
3806 }
3807 break;
3808
3809 default: ret = NOTDONE;
3810 break;
3811 }
3812 if (ret == FAIL)
3813 return FAIL;
3814
Bram Moolenaar1c747212020-05-09 18:28:34 +02003815 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003817 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003818 clear_tv(rettv);
3819 else
3820 // A constant expression can possibly be handled compile time,
3821 // return the value instead of generating code.
3822 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
3824 else if (ret == NOTDONE)
3825 {
3826 char_u *p;
3827 int r;
3828
3829 if (!eval_isnamec1(**arg))
3830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003831 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 return FAIL;
3833 }
3834
3835 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003836 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003838 {
3839 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003842 {
3843 if (generate_ppconst(cctx, ppconst) == FAIL)
3844 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003845 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 if (r == FAIL)
3848 return FAIL;
3849 }
3850
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003851 // Handle following "[]", ".member", etc.
3852 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003853 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854 ppconst) == FAIL)
3855 return FAIL;
3856 if (ppconst->pp_used > 0)
3857 {
3858 // apply the '!', '-' and '+' before the constant
3859 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003860 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003861 return FAIL;
3862 return OK;
3863 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003864 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003866 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867}
3868
3869/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003870 * Give the "white on both sides" error, taking the operator from "p[len]".
3871 */
3872 void
3873error_white_both(char_u *op, int len)
3874{
3875 char_u buf[10];
3876
3877 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003878 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003879}
3880
3881/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003882 * <type>expr7: runtime type check / conversion
3883 */
3884 static int
3885compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3886{
3887 type_T *want_type = NULL;
3888
3889 // Recognize <type>
3890 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3891 {
3892 int called_emsg_before = called_emsg;
3893
3894 ++*arg;
3895 want_type = parse_type(arg, cctx->ctx_type_list);
3896 if (called_emsg != called_emsg_before)
3897 return FAIL;
3898
3899 if (**arg != '>')
3900 {
3901 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003902 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003903 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003904 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003905 return FAIL;
3906 }
3907 ++*arg;
3908 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3909 return FAIL;
3910 }
3911
3912 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3913 return FAIL;
3914
3915 if (want_type != NULL)
3916 {
3917 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003918 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003919
Bram Moolenaard1103582020-08-14 22:44:25 +02003920 generate_ppconst(cctx, ppconst);
3921 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003922 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003923 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003924 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003925 return FAIL;
3926 }
3927 }
3928
3929 return OK;
3930}
3931
3932/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 * * number multiplication
3934 * / number division
3935 * % number modulo
3936 */
3937 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003938compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939{
3940 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003941 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003942 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003944 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003945 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 return FAIL;
3947
3948 /*
3949 * Repeat computing, until no "*", "/" or "%" is following.
3950 */
3951 for (;;)
3952 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003953 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 if (*op != '*' && *op != '/' && *op != '%')
3955 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003956 if (next != NULL)
3957 {
3958 *arg = next_line_from_context(cctx, TRUE);
3959 op = skipwhite(*arg);
3960 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003961
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003962 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003964 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003965 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 }
3967 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003968 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003969 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003971 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003972 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003974
3975 if (ppconst->pp_used == ppconst_used + 2
3976 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3977 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003978 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003979 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3980 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003981 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003983 // both are numbers: compute the result
3984 switch (*op)
3985 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003986 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003987 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003988 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003989 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003990 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003991 break;
3992 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003993 tv1->vval.v_number = res;
3994 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003995 }
3996 else
3997 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003998 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003999 generate_two_op(cctx, op);
4000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 }
4002
4003 return OK;
4004}
4005
4006/*
4007 * + number addition
4008 * - number subtraction
4009 * .. string concatenation
4010 */
4011 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004012compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013{
4014 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004015 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004017 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
4019 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004020 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 return FAIL;
4022
4023 /*
4024 * Repeat computing, until no "+", "-" or ".." is following.
4025 */
4026 for (;;)
4027 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004028 op = may_peek_next_line(cctx, *arg, &next);
4029 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 break;
4031 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004032 if (next != NULL)
4033 {
4034 *arg = next_line_from_context(cctx, TRUE);
4035 op = skipwhite(*arg);
4036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004038 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004040 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004041 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
4043
4044 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004045 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004046 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004048 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004049 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 return FAIL;
4051
Bram Moolenaara5565e42020-05-09 15:44:01 +02004052 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004053 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4055 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4056 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4057 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4060 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004061
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004062 // concat/subtract/add constant numbers
4063 if (*op == '+')
4064 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4065 else if (*op == '-')
4066 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4067 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004068 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004069 // concatenate constant strings
4070 char_u *s1 = tv1->vval.v_string;
4071 char_u *s2 = tv2->vval.v_string;
4072 size_t len1 = STRLEN(s1);
4073
4074 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4075 if (tv1->vval.v_string == NULL)
4076 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004077 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004078 return FAIL;
4079 }
4080 mch_memmove(tv1->vval.v_string, s1, len1);
4081 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004082 vim_free(s1);
4083 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004084 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004085 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 }
4087 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004088 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004089 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004090 if (*op == '.')
4091 {
4092 if (may_generate_2STRING(-2, cctx) == FAIL
4093 || may_generate_2STRING(-1, cctx) == FAIL)
4094 return FAIL;
4095 generate_instr_drop(cctx, ISN_CONCAT, 1);
4096 }
4097 else
4098 generate_two_op(cctx, op);
4099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 }
4101
4102 return OK;
4103}
4104
4105/*
4106 * expr5a == expr5b
4107 * expr5a =~ expr5b
4108 * expr5a != expr5b
4109 * expr5a !~ expr5b
4110 * expr5a > expr5b
4111 * expr5a >= expr5b
4112 * expr5a < expr5b
4113 * expr5a <= expr5b
4114 * expr5a is expr5b
4115 * expr5a isnot expr5b
4116 *
4117 * Produces instructions:
4118 * EVAL expr5a Push result of "expr5a"
4119 * EVAL expr5b Push result of "expr5b"
4120 * COMPARE one of the compare instructions
4121 */
4122 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124{
4125 exptype_T type = EXPR_UNKNOWN;
4126 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004127 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004130 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131
4132 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004133 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 return FAIL;
4135
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004136 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004137 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138
4139 /*
4140 * If there is a comparative operator, use it.
4141 */
4142 if (type != EXPR_UNKNOWN)
4143 {
4144 int ic = FALSE; // Default: do not ignore case
4145
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004146 if (next != NULL)
4147 {
4148 *arg = next_line_from_context(cctx, TRUE);
4149 p = skipwhite(*arg);
4150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 if (type_is && (p[len] == '?' || p[len] == '#'))
4152 {
4153 semsg(_(e_invexpr2), *arg);
4154 return FAIL;
4155 }
4156 // extra question mark appended: ignore case
4157 if (p[len] == '?')
4158 {
4159 ic = TRUE;
4160 ++len;
4161 }
4162 // extra '#' appended: match case (ignored)
4163 else if (p[len] == '#')
4164 ++len;
4165 // nothing appended: match case
4166
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004167 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004169 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004170 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 }
4172
4173 // get the second variable
4174 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004175 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004176 return FAIL;
4177
Bram Moolenaara5565e42020-05-09 15:44:01 +02004178 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 return FAIL;
4180
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 if (ppconst->pp_used == ppconst_used + 2)
4182 {
4183 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4184 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4185 int ret;
4186
4187 // Both sides are a constant, compute the result now.
4188 // First check for a valid combination of types, this is more
4189 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004190 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004191 ret = FAIL;
4192 else
4193 {
4194 ret = typval_compare(tv1, tv2, type, ic);
4195 tv1->v_type = VAR_BOOL;
4196 tv1->vval.v_number = tv1->vval.v_number
4197 ? VVAL_TRUE : VVAL_FALSE;
4198 clear_tv(tv2);
4199 --ppconst->pp_used;
4200 }
4201 return ret;
4202 }
4203
4204 generate_ppconst(cctx, ppconst);
4205 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 }
4207
4208 return OK;
4209}
4210
Bram Moolenaar7f141552020-05-09 17:35:53 +02004211static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213/*
4214 * Compile || or &&.
4215 */
4216 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004217compile_and_or(
4218 char_u **arg,
4219 cctx_T *cctx,
4220 char *op,
4221 ppconst_T *ppconst,
4222 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004224 char_u *next;
4225 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 int opchar = *op;
4227
4228 if (p[0] == opchar && p[1] == opchar)
4229 {
4230 garray_T *instr = &cctx->ctx_instr;
4231 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004232 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004233 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234
4235 /*
4236 * Repeat until there is no following "||" or "&&"
4237 */
4238 ga_init2(&end_ga, sizeof(int), 10);
4239 while (p[0] == opchar && p[1] == opchar)
4240 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004241 if (next != NULL)
4242 {
4243 *arg = next_line_from_context(cctx, TRUE);
4244 p = skipwhite(*arg);
4245 }
4246
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004247 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4248 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004249 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004250 return FAIL;
4251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004253 // TODO: use ppconst if the value is a constant and check
4254 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 generate_ppconst(cctx, ppconst);
4256
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004257 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4258 all_bool_values = FALSE;
4259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 if (ga_grow(&end_ga, 1) == FAIL)
4261 {
4262 ga_clear(&end_ga);
4263 return FAIL;
4264 }
4265 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4266 ++end_ga.ga_len;
4267 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004268 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269
4270 // eval the next expression
4271 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004272 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004273 return FAIL;
4274
Bram Moolenaara5565e42020-05-09 15:44:01 +02004275 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4276 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 {
4278 ga_clear(&end_ga);
4279 return FAIL;
4280 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004281
4282 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004284 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
4286 // Fill in the end label in all jumps.
4287 while (end_ga.ga_len > 0)
4288 {
4289 isn_T *isn;
4290
4291 --end_ga.ga_len;
4292 isn = ((isn_T *)instr->ga_data)
4293 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4294 isn->isn_arg.jump.jump_where = instr->ga_len;
4295 }
4296 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004297
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004298 // The resulting type is converted to bool if needed.
4299 if (!all_bool_values)
4300 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 }
4302
4303 return OK;
4304}
4305
4306/*
4307 * expr4a && expr4a && expr4a logical AND
4308 *
4309 * Produces instructions:
4310 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004311 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004313 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004315 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 * end:
4317 */
4318 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004321 int ppconst_used = ppconst->pp_used;
4322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004324 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 return FAIL;
4326
4327 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004328 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329}
4330
4331/*
4332 * expr3a || expr3b || expr3c logical OR
4333 *
4334 * Produces instructions:
4335 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004336 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004338 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004340 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 * end:
4342 */
4343 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 int ppconst_used = ppconst->pp_used;
4347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 return FAIL;
4351
4352 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004353 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354}
4355
4356/*
4357 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004359 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 * JUMP_IF_FALSE alt jump if false
4361 * EVAL expr1a
4362 * JUMP_ALWAYS end
4363 * alt: EVAL expr1b
4364 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004365 *
4366 * Toplevel expression: expr2 ?? expr1
4367 * Produces instructions:
4368 * EVAL expr2 Push result of "expr2"
4369 * JUMP_AND_KEEP_IF_TRUE end jump if true
4370 * EVAL expr1
4371 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 */
4373 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375{
4376 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004377 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004378 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379
Bram Moolenaar3988f642020-08-27 22:43:03 +02004380 // Ignore all kinds of errors when not producing code.
4381 if (cctx->ctx_skip == SKIP_YES)
4382 {
4383 skip_expr(arg);
4384 return OK;
4385 }
4386
Bram Moolenaar61a89812020-05-07 16:58:17 +02004387 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 return FAIL;
4390
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004391 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 if (*p == '?')
4393 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004394 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 garray_T *instr = &cctx->ctx_instr;
4396 garray_T *stack = &cctx->ctx_type_stack;
4397 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004398 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004400 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 int has_const_expr = FALSE;
4402 int const_value = FALSE;
4403 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004405 if (next != NULL)
4406 {
4407 *arg = next_line_from_context(cctx, TRUE);
4408 p = skipwhite(*arg);
4409 }
4410
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004411 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004412 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004413 semsg(_(e_white_space_required_before_and_after_str),
4414 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004415 return FAIL;
4416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 if (ppconst->pp_used == ppconst_used + 1)
4419 {
4420 // the condition is a constant, we know whether the ? or the :
4421 // expression is to be evaluated.
4422 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004423 if (op_falsy)
4424 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4425 else
4426 {
4427 int error = FALSE;
4428
4429 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4430 &error);
4431 if (error)
4432 return FAIL;
4433 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004434 cctx->ctx_skip = save_skip == SKIP_YES ||
4435 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4436
4437 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4438 // "left ?? right" and "left" is truthy: produce "left"
4439 generate_ppconst(cctx, ppconst);
4440 else
4441 {
4442 clear_tv(&ppconst->pp_tv[ppconst_used]);
4443 --ppconst->pp_used;
4444 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004445 }
4446 else
4447 {
4448 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004449 if (op_falsy)
4450 end_idx = instr->ga_len;
4451 generate_JUMP(cctx, op_falsy
4452 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4453 if (op_falsy)
4454 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456
4457 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004458 *arg = skipwhite(p + 1 + op_falsy);
4459 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004460 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004462 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 if (!has_const_expr)
4465 {
4466 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004468 if (!op_falsy)
4469 {
4470 // remember the type and drop it
4471 --stack->ga_len;
4472 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004474 end_idx = instr->ga_len;
4475 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004476
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004477 // jump here from JUMP_IF_FALSE
4478 isn = ((isn_T *)instr->ga_data) + alt_idx;
4479 isn->isn_arg.jump.jump_where = instr->ga_len;
4480 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004483 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004485 // Check for the ":".
4486 p = may_peek_next_line(cctx, *arg, &next);
4487 if (*p != ':')
4488 {
4489 emsg(_(e_missing_colon));
4490 return FAIL;
4491 }
4492 if (next != NULL)
4493 {
4494 *arg = next_line_from_context(cctx, TRUE);
4495 p = skipwhite(*arg);
4496 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004497
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004498 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4499 {
4500 semsg(_(e_white_space_required_before_and_after_str), ":");
4501 return FAIL;
4502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004504 // evaluate the third expression
4505 if (has_const_expr)
4506 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004507 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004508 *arg = skipwhite(p + 1);
4509 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4510 return FAIL;
4511 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4512 return FAIL;
4513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 if (!has_const_expr)
4516 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004517 type_T **typep;
4518
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004522 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4523 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004524
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004525 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 isn = ((isn_T *)instr->ga_data) + end_idx;
4527 isn->isn_arg.jump.jump_where = instr->ga_len;
4528 }
4529
4530 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 }
4532 return OK;
4533}
4534
4535/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004537 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4538 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004539 */
4540 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004541compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004542{
4543 ppconst_T ppconst;
4544
4545 CLEAR_FIELD(ppconst);
4546 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4547 {
4548 clear_ppconst(&ppconst);
4549 return FAIL;
4550 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004551 if (is_const != NULL)
4552 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004553 if (generate_ppconst(cctx, &ppconst) == FAIL)
4554 return FAIL;
4555 return OK;
4556}
4557
4558/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004559 * Toplevel expression.
4560 */
4561 static int
4562compile_expr0(char_u **arg, cctx_T *cctx)
4563{
4564 return compile_expr0_ext(arg, cctx, NULL);
4565}
4566
4567/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 * compile "return [expr]"
4569 */
4570 static char_u *
4571compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4572{
4573 char_u *p = arg;
4574 garray_T *stack = &cctx->ctx_type_stack;
4575 type_T *stack_type;
4576
4577 if (*p != NUL && *p != '|' && *p != '\n')
4578 {
4579 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004580 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 return NULL;
4582
4583 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4584 if (set_return_type)
4585 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004586 else
4587 {
4588 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4589 && stack_type->tt_type != VAR_VOID
4590 && stack_type->tt_type != VAR_UNKNOWN)
4591 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004592 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004593 return NULL;
4594 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004595 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004596 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004597 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 }
4600 else
4601 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004602 // "set_return_type" cannot be TRUE, only used for a lambda which
4603 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004604 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4605 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004607 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 return NULL;
4609 }
4610
4611 // No argument, return zero.
4612 generate_PUSHNR(cctx, 0);
4613 }
4614
4615 if (generate_instr(cctx, ISN_RETURN) == NULL)
4616 return NULL;
4617
4618 // "return val | endif" is possible
4619 return skipwhite(p);
4620}
4621
4622/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004623 * Get a line from the compilation context, compatible with exarg_T getline().
4624 * Return a pointer to the line in allocated memory.
4625 * Return NULL for end-of-file or some error.
4626 */
4627 static char_u *
4628exarg_getline(
4629 int c UNUSED,
4630 void *cookie,
4631 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004632 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004633{
4634 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004635 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004636
Bram Moolenaar66250c92020-08-20 15:02:42 +02004637 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004638 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004639 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004640 return NULL;
4641 ++cctx->ctx_lnum;
4642 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4643 // Comment lines result in NULL pointers, skip them.
4644 if (p != NULL)
4645 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004646 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004647}
4648
4649/*
4650 * Compile a nested :def command.
4651 */
4652 static char_u *
4653compile_nested_function(exarg_T *eap, cctx_T *cctx)
4654{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004655 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004656 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004657 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004658 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004659 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004660 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004661
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004662 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004663 {
4664 emsg(_(e_cannot_use_bang_with_nested_def));
4665 return NULL;
4666 }
4667
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004668 // Only g:Func() can use a namespace.
4669 if (name_start[1] == ':' && !is_global)
4670 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004671 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004672 return NULL;
4673 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004674 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4675 return NULL;
4676
Bram Moolenaar04b12692020-05-04 23:24:44 +02004677 eap->arg = name_end;
4678 eap->getline = exarg_getline;
4679 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004680 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004681 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004682 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004683 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004684
Bram Moolenaar822ba242020-05-24 23:00:18 +02004685 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004686 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004687 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004688 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004689 {
4690 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004691 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004692 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004693
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004694 if (is_global)
4695 {
4696 char_u *func_name = vim_strnsave(name_start + 2,
4697 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004698
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004699 if (func_name == NULL)
4700 r = FAIL;
4701 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004702 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004703 }
4704 else
4705 {
4706 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004707 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004708 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004709 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004710
Bram Moolenaareef21022020-08-01 22:16:43 +02004711 if (lvar == NULL)
4712 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004713 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004714 return NULL;
4715 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004716
4717 // copy over the block scope IDs
4718 if (block_depth > 0)
4719 {
4720 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4721 if (ufunc->uf_block_ids != NULL)
4722 {
4723 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4724 sizeof(int) * block_depth);
4725 ufunc->uf_block_depth = block_depth;
4726 }
4727 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004728 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004729
Bram Moolenaar61a89812020-05-07 16:58:17 +02004730 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004731 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004732}
4733
4734/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 * Return the length of an assignment operator, or zero if there isn't one.
4736 */
4737 int
4738assignment_len(char_u *p, int *heredoc)
4739{
4740 if (*p == '=')
4741 {
4742 if (p[1] == '<' && p[2] == '<')
4743 {
4744 *heredoc = TRUE;
4745 return 3;
4746 }
4747 return 1;
4748 }
4749 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4750 return 2;
4751 if (STRNCMP(p, "..=", 3) == 0)
4752 return 3;
4753 return 0;
4754}
4755
4756// words that cannot be used as a variable
4757static char *reserved[] = {
4758 "true",
4759 "false",
4760 NULL
4761};
4762
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004763typedef enum {
4764 dest_local,
4765 dest_option,
4766 dest_env,
4767 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004768 dest_buffer,
4769 dest_window,
4770 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004771 dest_vimvar,
4772 dest_script,
4773 dest_reg,
4774} assign_dest_T;
4775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004777 * Generate the load instruction for "name".
4778 */
4779 static void
4780generate_loadvar(
4781 cctx_T *cctx,
4782 assign_dest_T dest,
4783 char_u *name,
4784 lvar_T *lvar,
4785 type_T *type)
4786{
4787 switch (dest)
4788 {
4789 case dest_option:
4790 // TODO: check the option exists
4791 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4792 break;
4793 case dest_global:
4794 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4795 break;
4796 case dest_buffer:
4797 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4798 break;
4799 case dest_window:
4800 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4801 break;
4802 case dest_tab:
4803 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4804 break;
4805 case dest_script:
4806 compile_load_scriptvar(cctx,
4807 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4808 break;
4809 case dest_env:
4810 // Include $ in the name here
4811 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4812 break;
4813 case dest_reg:
4814 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4815 break;
4816 case dest_vimvar:
4817 generate_LOADV(cctx, name + 2, TRUE);
4818 break;
4819 case dest_local:
4820 if (lvar->lv_from_outer)
4821 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4822 NULL, type);
4823 else
4824 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4825 break;
4826 }
4827}
4828
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004829 void
4830vim9_declare_error(char_u *name)
4831{
4832 char *scope = "";
4833
4834 switch (*name)
4835 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004836 case 'g': scope = _("global"); break;
4837 case 'b': scope = _("buffer"); break;
4838 case 'w': scope = _("window"); break;
4839 case 't': scope = _("tab"); break;
4840 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004841 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004842 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004843 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004844 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004845 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004846 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004847 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004848 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004849 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004850}
4851
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004852/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004853 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004854 * "let name"
4855 * "var name = expr"
4856 * "final name = expr"
4857 * "const name = expr"
4858 * "name = expr"
4859 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 * Return NULL for an error.
4861 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 */
4863 static char_u *
4864compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4865{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004866 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004868 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 char_u *ret = NULL;
4870 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004871 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004872 int scriptvar_sid = 0;
4873 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004876 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 int oplen = 0;
4879 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004880 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004881 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004882 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004884 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4885 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004887 // Skip over the "var" or "[var, var]" to get to any "=".
4888 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4889 if (p == NULL)
4890 return *arg == '[' ? arg : NULL;
4891
4892 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004894 // TODO: should we allow this, and figure out type inference from list
4895 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004896 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 return NULL;
4898 }
4899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 sp = p;
4901 p = skipwhite(p);
4902 op = p;
4903 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904
4905 if (var_count > 0 && oplen == 0)
4906 // can be something like "[1, 2]->func()"
4907 return arg;
4908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4910 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004911 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004912 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004913 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 if (heredoc)
4916 {
4917 list_T *l;
4918 listitem_T *li;
4919
4920 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004921 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004923 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004924 if (l == NULL)
4925 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926
Bram Moolenaar078269b2020-09-21 20:35:55 +02004927 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004929 // Push each line and the create the list.
4930 FOR_ALL_LIST_ITEMS(l, li)
4931 {
4932 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4933 li->li_tv.vval.v_string = NULL;
4934 }
4935 generate_NEWLIST(cctx, l->lv_len);
4936 type = &t_list_string;
4937 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 list_free(l);
4940 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004943 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004945 // for "[var, var] = expr" evaluate the expression here, loop over the
4946 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004947
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 p = skipwhite(op + oplen);
4949 if (compile_expr0(&p, cctx) == FAIL)
4950 return NULL;
4951 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004953 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004955 type_T *stacktype;
4956
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004957 stacktype = stack->ga_len == 0 ? &t_void
4958 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004961 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 goto theend;
4963 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004964 if (need_type(stacktype, &t_list_any, -1, cctx,
4965 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004966 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004967 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004968 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4969 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004970 }
4971 }
4972
4973 /*
4974 * Loop over variables in "[var, var] = expr".
4975 * For "var = expr" and "let var: type" this is done only once.
4976 */
4977 if (var_count > 0)
4978 var_start = skipwhite(arg + 1); // skip over the "["
4979 else
4980 var_start = arg;
4981 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4982 {
4983 char_u *var_end = skip_var_one(var_start, FALSE);
4984 size_t varlen;
4985 int new_local = FALSE;
4986 int opt_type;
4987 int opt_flags = 0;
4988 assign_dest_T dest = dest_local;
4989 int vimvaridx = -1;
4990 lvar_T *lvar = NULL;
4991 lvar_T arg_lvar;
4992 int has_type = FALSE;
4993 int has_index = FALSE;
4994 int instr_count = -1;
4995
Bram Moolenaar65821722020-08-02 18:58:54 +02004996 if (*var_start == '@')
4997 p = var_start + 2;
4998 else
4999 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005000 // skip over the leading "&", "&l:", "&g:" and "$"
5001 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005002 p = to_name_end(p, TRUE);
5003 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004
5005 // "a: type" is declaring variable "a" with a type, not "a:".
5006 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5007 --var_end;
5008 if (is_decl && p == var_start + 2 && p[-1] == ':')
5009 --p;
5010
5011 varlen = p - var_start;
5012 vim_free(name);
5013 name = vim_strnsave(var_start, varlen);
5014 if (name == NULL)
5015 return NULL;
5016 if (!heredoc)
5017 type = &t_any;
5018
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005019 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005021 int declare_error = FALSE;
5022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 if (*var_start == '&')
5024 {
5025 int cc;
5026 long numval;
5027
5028 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005029 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 emsg(_(e_const_option));
5032 goto theend;
5033 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005034 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005035 p = var_start;
5036 p = find_option_end(&p, &opt_flags);
5037 if (p == NULL)
5038 {
5039 // cannot happen?
5040 emsg(_(e_letunexp));
5041 goto theend;
5042 }
5043 cc = *p;
5044 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005045 opt_type = get_option_value(skip_option_env_lead(var_start),
5046 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 *p = cc;
5048 if (opt_type == -3)
5049 {
5050 semsg(_(e_unknown_option), var_start);
5051 goto theend;
5052 }
5053 if (opt_type == -2 || opt_type == 0)
5054 type = &t_string;
5055 else
5056 type = &t_number; // both number and boolean option
5057 }
5058 else if (*var_start == '$')
5059 {
5060 dest = dest_env;
5061 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005062 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 }
5064 else if (*var_start == '@')
5065 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005066 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005067 {
5068 emsg_invreg(var_start[1]);
5069 goto theend;
5070 }
5071 dest = dest_reg;
5072 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005073 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005075 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 {
5077 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005078 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005080 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 {
5082 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005083 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005085 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 {
5087 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005088 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005090 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 {
5092 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005093 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005095 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 {
5097 typval_T *vtv;
5098 int di_flags;
5099
5100 vimvaridx = find_vim_var(name + 2, &di_flags);
5101 if (vimvaridx < 0)
5102 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005103 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 goto theend;
5105 }
5106 // We use the current value of "sandbox" here, is that OK?
5107 if (var_check_ro(di_flags, name, FALSE))
5108 goto theend;
5109 dest = dest_vimvar;
5110 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005111 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005112 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005113 }
5114 else
5115 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005116 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117
5118 for (idx = 0; reserved[idx] != NULL; ++idx)
5119 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005120 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005121 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005122 goto theend;
5123 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005124
5125 lvar = lookup_local(var_start, varlen, cctx);
5126 if (lvar == NULL)
5127 {
5128 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005129 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5131 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005132 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005133 if (is_decl)
5134 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005135 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 goto theend;
5137 }
5138 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005139 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005140 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 if (lvar != NULL)
5142 {
5143 if (is_decl)
5144 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005145 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 goto theend;
5147 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005149 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005151 int script_namespace = varlen > 1
5152 && STRNCMP(var_start, "s:", 2) == 0;
5153 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005154 ? script_var_exists(var_start + 2, varlen - 2,
5155 FALSE, cctx)
5156 : script_var_exists(var_start, varlen,
5157 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005158 imported_T *import =
5159 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005160
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005161 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005162 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005163 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5164
5165 if (is_decl)
5166 {
5167 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005168 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005169 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005170 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005171 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005172 name);
5173 goto theend;
5174 }
5175 else if (cctx->ctx_ufunc->uf_script_ctx_version
5176 == SCRIPT_VERSION_VIM9
5177 && script_namespace
5178 && !script_var && import == NULL)
5179 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005180 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005181 goto theend;
5182 }
5183
5184 dest = dest_script;
5185
5186 // existing script-local variables should have a type
5187 scriptvar_sid = current_sctx.sc_sid;
5188 if (import != NULL)
5189 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005190 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005191 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005192 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005193 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005194 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005195 {
5196 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5197 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005198 ((svar_T *)si->sn_var_vals.ga_data)
5199 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005200 type = sv->sv_type;
5201 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005202 }
5203 }
5204 else if (name[1] == ':' && name[2] != NUL)
5205 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005206 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 goto theend;
5208 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005209 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005210 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005211 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005212 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005213 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005214 else if (check_defined(var_start, varlen, cctx) == FAIL)
5215 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005218
5219 if (declare_error)
5220 {
5221 vim9_declare_error(name);
5222 goto theend;
5223 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005224 }
5225
5226 // handle "a:name" as a name, not index "name" on "a"
5227 if (varlen > 1 || var_start[varlen] != ':')
5228 p = var_end;
5229
5230 if (dest != dest_option)
5231 {
5232 if (is_decl && *p == ':')
5233 {
5234 // parse optional type: "let var: type = expr"
5235 if (!VIM_ISWHITE(p[1]))
5236 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005237 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238 goto theend;
5239 }
5240 p = skipwhite(p + 1);
5241 type = parse_type(&p, cctx->ctx_type_list);
5242 has_type = TRUE;
5243 }
5244 else if (lvar != NULL)
5245 type = lvar->lv_type;
5246 }
5247
5248 if (oplen == 3 && !heredoc && dest != dest_global
5249 && type->tt_type != VAR_STRING
5250 && type->tt_type != VAR_ANY)
5251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005252 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253 goto theend;
5254 }
5255
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005256 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005257 {
5258 if (oplen > 1 && !heredoc)
5259 {
5260 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005261 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 goto theend;
5263 }
5264
5265 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005266 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5267 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005268 goto theend;
5269 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005270 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 if (lvar == NULL)
5272 goto theend;
5273 new_local = TRUE;
5274 }
5275
5276 member_type = type;
5277 if (var_end > var_start + varlen)
5278 {
5279 // Something follows after the variable: "var[idx]".
5280 if (is_decl)
5281 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005282 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 goto theend;
5284 }
5285
5286 if (var_start[varlen] == '[')
5287 {
5288 has_index = TRUE;
5289 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005290 member_type = &t_any;
5291 else
5292 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005293 }
5294 else
5295 {
5296 semsg("Not supported yet: %s", var_start);
5297 goto theend;
5298 }
5299 }
5300 else if (lvar == &arg_lvar)
5301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005302 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 goto theend;
5304 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005305 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5306 {
5307 semsg(_(e_cannot_assign_to_constant), name);
5308 goto theend;
5309 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005310
5311 if (!heredoc)
5312 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005313 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005314 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005315 if (oplen > 0 && var_count == 0)
5316 {
5317 // skip over the "=" and the expression
5318 p = skipwhite(op + oplen);
5319 compile_expr0(&p, cctx);
5320 }
5321 }
5322 else if (oplen > 0)
5323 {
5324 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005325 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005326
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327 // For "var = expr" evaluate the expression.
5328 if (var_count == 0)
5329 {
5330 int r;
5331
5332 // for "+=", "*=", "..=" etc. first load the current value
5333 if (*op != '=')
5334 {
5335 generate_loadvar(cctx, dest, name, lvar, type);
5336
5337 if (has_index)
5338 {
5339 // TODO: get member from list or dict
5340 emsg("Index with operation not supported yet");
5341 goto theend;
5342 }
5343 }
5344
5345 // Compile the expression. Temporarily hide the new local
5346 // variable here, it is not available to this expression.
5347 if (new_local)
5348 --cctx->ctx_locals.ga_len;
5349 instr_count = instr->ga_len;
5350 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005351 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005352 if (new_local)
5353 ++cctx->ctx_locals.ga_len;
5354 if (r == FAIL)
5355 goto theend;
5356 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005357 else if (semicolon && var_idx == var_count - 1)
5358 {
5359 // For "[var; var] = expr" get the rest of the list
5360 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5361 goto theend;
5362 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005363 else
5364 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365 // For "[var, var] = expr" get the "var_idx" item from the
5366 // list.
5367 if (generate_GETITEM(cctx, var_idx) == FAIL)
5368 return FAIL;
5369 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005370
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005371 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005372 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005373 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005375 if ((stacktype->tt_type == VAR_FUNC
5376 || stacktype->tt_type == VAR_PARTIAL)
5377 && var_wrong_func_name(name, TRUE))
5378 goto theend;
5379
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005380 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005381 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005382 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005383 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005384 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005385 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 }
5387 else
5388 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005389 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005390 // for a variable that implies &t_any.
5391 if (stacktype == &t_list_empty)
5392 lvar->lv_type = &t_list_any;
5393 else if (stacktype == &t_dict_empty)
5394 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005395 else if (stacktype == &t_unknown)
5396 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005397 else
5398 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005399 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005400 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005401 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005402 {
5403 type_T *use_type = lvar->lv_type;
5404
Bram Moolenaar71abe482020-09-14 22:28:30 +02005405 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005406 if (has_index)
5407 {
5408 use_type = use_type->tt_member;
5409 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005410 // could be indexing "any"
5411 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005412 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005413 if (need_type(stacktype, use_type, -1, cctx,
5414 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005415 goto theend;
5416 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005417 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005418 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005419 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005420 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005422 else if (cmdidx == CMD_final)
5423 {
5424 emsg(_(e_final_requires_a_value));
5425 goto theend;
5426 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005427 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005429 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 goto theend;
5431 }
5432 else if (!has_type || dest == dest_option)
5433 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005434 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005435 goto theend;
5436 }
5437 else
5438 {
5439 // variables are always initialized
5440 if (ga_grow(instr, 1) == FAIL)
5441 goto theend;
5442 switch (member_type->tt_type)
5443 {
5444 case VAR_BOOL:
5445 generate_PUSHBOOL(cctx, VVAL_FALSE);
5446 break;
5447 case VAR_FLOAT:
5448#ifdef FEAT_FLOAT
5449 generate_PUSHF(cctx, 0.0);
5450#endif
5451 break;
5452 case VAR_STRING:
5453 generate_PUSHS(cctx, NULL);
5454 break;
5455 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005456 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005457 break;
5458 case VAR_FUNC:
5459 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5460 break;
5461 case VAR_LIST:
5462 generate_NEWLIST(cctx, 0);
5463 break;
5464 case VAR_DICT:
5465 generate_NEWDICT(cctx, 0);
5466 break;
5467 case VAR_JOB:
5468 generate_PUSHJOB(cctx, NULL);
5469 break;
5470 case VAR_CHANNEL:
5471 generate_PUSHCHANNEL(cctx, NULL);
5472 break;
5473 case VAR_NUMBER:
5474 case VAR_UNKNOWN:
5475 case VAR_ANY:
5476 case VAR_PARTIAL:
5477 case VAR_VOID:
5478 case VAR_SPECIAL: // cannot happen
5479 generate_PUSHNR(cctx, 0);
5480 break;
5481 }
5482 }
5483 if (var_count == 0)
5484 end = p;
5485 }
5486
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005487 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005488 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005489 break;
5490
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005491 if (oplen > 0 && *op != '=')
5492 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005493 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 type_T *stacktype;
5495
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005496 if (*op == '.')
5497 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005498 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005499 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005500 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005501 if (
5502#ifdef FEAT_FLOAT
5503 // If variable is float operation with number is OK.
5504 !(expected == &t_float && stacktype == &t_number) &&
5505#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005506 need_type(stacktype, expected, -1, cctx,
5507 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 goto theend;
5509
5510 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005511 {
5512 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5513 goto theend;
5514 }
5515 else if (*op == '+')
5516 {
5517 if (generate_add_instr(cctx,
5518 operator_type(member_type, stacktype),
5519 member_type, stacktype) == FAIL)
5520 goto theend;
5521 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005522 else if (generate_two_op(cctx, op) == FAIL)
5523 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005526 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005527 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 int r;
5529
5530 // Compile the "idx" in "var[idx]".
5531 if (new_local)
5532 --cctx->ctx_locals.ga_len;
5533 p = skipwhite(var_start + varlen + 1);
5534 r = compile_expr0(&p, cctx);
5535 if (new_local)
5536 ++cctx->ctx_locals.ga_len;
5537 if (r == FAIL)
5538 goto theend;
5539 if (*skipwhite(p) != ']')
5540 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005541 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005542 emsg(_(e_missbrac));
5543 goto theend;
5544 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005545 if (type == &t_any)
5546 {
5547 type_T *idx_type = ((type_T **)stack->ga_data)[
5548 stack->ga_len - 1];
5549 // Index on variable of unknown type: guess the type from the
5550 // index type: number is dict, otherwise dict.
5551 // TODO: should do the assignment at runtime
5552 if (idx_type->tt_type == VAR_NUMBER)
5553 type = &t_list_any;
5554 else
5555 type = &t_dict_any;
5556 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005557 if (type->tt_type == VAR_DICT
5558 && may_generate_2STRING(-1, cctx) == FAIL)
5559 goto theend;
5560 if (type->tt_type == VAR_LIST
5561 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005562 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563 {
5564 emsg(_(e_number_exp));
5565 goto theend;
5566 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005567
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005568 // Load the dict or list. On the stack we then have:
5569 // - value
5570 // - index
5571 // - variable
5572 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005573
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005574 if (type->tt_type == VAR_LIST)
5575 {
5576 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5577 return FAIL;
5578 }
5579 else if (type->tt_type == VAR_DICT)
5580 {
5581 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5582 return FAIL;
5583 }
5584 else
5585 {
5586 emsg(_(e_listreq));
5587 goto theend;
5588 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005589 }
5590 else
5591 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005592 if (is_decl && cmdidx == CMD_const
5593 && (dest == dest_script || dest == dest_local))
5594 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005595 generate_LOCKCONST(cctx);
5596
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005597 switch (dest)
5598 {
5599 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005600 generate_STOREOPT(cctx, skip_option_env_lead(name),
5601 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005602 break;
5603 case dest_global:
5604 // include g: with the name, easier to execute that way
5605 generate_STORE(cctx, ISN_STOREG, 0, name);
5606 break;
5607 case dest_buffer:
5608 // include b: with the name, easier to execute that way
5609 generate_STORE(cctx, ISN_STOREB, 0, name);
5610 break;
5611 case dest_window:
5612 // include w: with the name, easier to execute that way
5613 generate_STORE(cctx, ISN_STOREW, 0, name);
5614 break;
5615 case dest_tab:
5616 // include t: with the name, easier to execute that way
5617 generate_STORE(cctx, ISN_STORET, 0, name);
5618 break;
5619 case dest_env:
5620 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5621 break;
5622 case dest_reg:
5623 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5624 break;
5625 case dest_vimvar:
5626 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5627 break;
5628 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005629 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005630 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005631 {
5632 char_u *name_s = name;
5633
5634 // Include s: in the name for store_var()
5635 if (name[1] != ':')
5636 {
5637 int len = (int)STRLEN(name) + 3;
5638
5639 name_s = alloc(len);
5640 if (name_s == NULL)
5641 name_s = name;
5642 else
5643 vim_snprintf((char *)name_s, len,
5644 "s:%s", name);
5645 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005646 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5647 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005648 if (name_s != name)
5649 vim_free(name_s);
5650 }
5651 else
5652 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005653 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005654 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005655 break;
5656 case dest_local:
5657 if (lvar != NULL)
5658 {
5659 isn_T *isn = ((isn_T *)instr->ga_data)
5660 + instr->ga_len - 1;
5661
5662 // optimization: turn "var = 123" from ISN_PUSHNR +
5663 // ISN_STORE into ISN_STORENR
5664 if (!lvar->lv_from_outer
5665 && instr->ga_len == instr_count + 1
5666 && isn->isn_type == ISN_PUSHNR)
5667 {
5668 varnumber_T val = isn->isn_arg.number;
5669
5670 isn->isn_type = ISN_STORENR;
5671 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5672 isn->isn_arg.storenr.stnr_val = val;
5673 if (stack->ga_len > 0)
5674 --stack->ga_len;
5675 }
5676 else if (lvar->lv_from_outer)
5677 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005678 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005679 else
5680 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5681 }
5682 break;
5683 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005684 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685
5686 if (var_idx + 1 < var_count)
5687 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005689
5690 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005691 if (var_count > 0 && !semicolon)
5692 {
5693 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5694 goto theend;
5695 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005696
Bram Moolenaarb2097502020-07-19 17:17:02 +02005697 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698
5699theend:
5700 vim_free(name);
5701 return ret;
5702}
5703
5704/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005705 * Check if "name" can be "unlet".
5706 */
5707 int
5708check_vim9_unlet(char_u *name)
5709{
5710 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5711 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005712 // "unlet s:var" is allowed in legacy script.
5713 if (*name == 's' && !script_is_vim9())
5714 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005715 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005716 return FAIL;
5717 }
5718 return OK;
5719}
5720
5721/*
5722 * Callback passed to ex_unletlock().
5723 */
5724 static int
5725compile_unlet(
5726 lval_T *lvp,
5727 char_u *name_end,
5728 exarg_T *eap,
5729 int deep UNUSED,
5730 void *coookie)
5731{
5732 cctx_T *cctx = coookie;
5733
5734 if (lvp->ll_tv == NULL)
5735 {
5736 char_u *p = lvp->ll_name;
5737 int cc = *name_end;
5738 int ret = OK;
5739
5740 // Normal name. Only supports g:, w:, t: and b: namespaces.
5741 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005742 if (*p == '$')
5743 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5744 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005745 ret = FAIL;
5746 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005747 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005748
5749 *name_end = cc;
5750 return ret;
5751 }
5752
5753 // TODO: unlet {list}[idx]
5754 // TODO: unlet {dict}[key]
5755 emsg("Sorry, :unlet not fully implemented yet");
5756 return FAIL;
5757}
5758
5759/*
5760 * compile "unlet var", "lock var" and "unlock var"
5761 * "arg" points to "var".
5762 */
5763 static char_u *
5764compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5765{
5766 char_u *p = arg;
5767
5768 if (eap->cmdidx != CMD_unlet)
5769 {
5770 emsg("Sorry, :lock and unlock not implemented yet");
5771 return NULL;
5772 }
5773
5774 if (*p == '!')
5775 {
5776 p = skipwhite(p + 1);
5777 eap->forceit = TRUE;
5778 }
5779
5780 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5781 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5782}
5783
5784/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785 * Compile an :import command.
5786 */
5787 static char_u *
5788compile_import(char_u *arg, cctx_T *cctx)
5789{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005790 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005791}
5792
5793/*
5794 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5795 */
5796 static int
5797compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5798{
5799 garray_T *instr = &cctx->ctx_instr;
5800 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5801
5802 if (endlabel == NULL)
5803 return FAIL;
5804 endlabel->el_next = *el;
5805 *el = endlabel;
5806 endlabel->el_end_label = instr->ga_len;
5807
5808 generate_JUMP(cctx, when, 0);
5809 return OK;
5810}
5811
5812 static void
5813compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5814{
5815 garray_T *instr = &cctx->ctx_instr;
5816
5817 while (*el != NULL)
5818 {
5819 endlabel_T *cur = (*el);
5820 isn_T *isn;
5821
5822 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5823 isn->isn_arg.jump.jump_where = instr->ga_len;
5824 *el = cur->el_next;
5825 vim_free(cur);
5826 }
5827}
5828
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005829 static void
5830compile_free_jump_to_end(endlabel_T **el)
5831{
5832 while (*el != NULL)
5833 {
5834 endlabel_T *cur = (*el);
5835
5836 *el = cur->el_next;
5837 vim_free(cur);
5838 }
5839}
5840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841/*
5842 * Create a new scope and set up the generic items.
5843 */
5844 static scope_T *
5845new_scope(cctx_T *cctx, scopetype_T type)
5846{
5847 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5848
5849 if (scope == NULL)
5850 return NULL;
5851 scope->se_outer = cctx->ctx_scope;
5852 cctx->ctx_scope = scope;
5853 scope->se_type = type;
5854 scope->se_local_count = cctx->ctx_locals.ga_len;
5855 return scope;
5856}
5857
5858/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005859 * Free the current scope and go back to the outer scope.
5860 */
5861 static void
5862drop_scope(cctx_T *cctx)
5863{
5864 scope_T *scope = cctx->ctx_scope;
5865
5866 if (scope == NULL)
5867 {
5868 iemsg("calling drop_scope() without a scope");
5869 return;
5870 }
5871 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005872 switch (scope->se_type)
5873 {
5874 case IF_SCOPE:
5875 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5876 case FOR_SCOPE:
5877 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5878 case WHILE_SCOPE:
5879 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5880 case TRY_SCOPE:
5881 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5882 case NO_SCOPE:
5883 case BLOCK_SCOPE:
5884 break;
5885 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005886 vim_free(scope);
5887}
5888
5889/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005890 * Check that the top of the type stack has a type that can be used as a
5891 * condition. Give an error and return FAIL if not.
5892 */
5893 static int
5894bool_on_stack(cctx_T *cctx)
5895{
5896 garray_T *stack = &cctx->ctx_type_stack;
5897 type_T *type;
5898
5899 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5900 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005901 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005902 return FAIL;
5903 return OK;
5904}
5905
5906/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907 * compile "if expr"
5908 *
5909 * "if expr" Produces instructions:
5910 * EVAL expr Push result of "expr"
5911 * JUMP_IF_FALSE end
5912 * ... body ...
5913 * end:
5914 *
5915 * "if expr | else" Produces instructions:
5916 * EVAL expr Push result of "expr"
5917 * JUMP_IF_FALSE else
5918 * ... body ...
5919 * JUMP_ALWAYS end
5920 * else:
5921 * ... body ...
5922 * end:
5923 *
5924 * "if expr1 | elseif expr2 | else" Produces instructions:
5925 * EVAL expr Push result of "expr"
5926 * JUMP_IF_FALSE elseif
5927 * ... body ...
5928 * JUMP_ALWAYS end
5929 * elseif:
5930 * EVAL expr Push result of "expr"
5931 * JUMP_IF_FALSE else
5932 * ... body ...
5933 * JUMP_ALWAYS end
5934 * else:
5935 * ... body ...
5936 * end:
5937 */
5938 static char_u *
5939compile_if(char_u *arg, cctx_T *cctx)
5940{
5941 char_u *p = arg;
5942 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005943 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005945 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005946 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947
Bram Moolenaara5565e42020-05-09 15:44:01 +02005948 CLEAR_FIELD(ppconst);
5949 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005950 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005951 clear_ppconst(&ppconst);
5952 return NULL;
5953 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005954 if (cctx->ctx_skip == SKIP_YES)
5955 clear_ppconst(&ppconst);
5956 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005957 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005958 int error = FALSE;
5959 int v;
5960
Bram Moolenaara5565e42020-05-09 15:44:01 +02005961 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005962 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005963 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005964 if (error)
5965 return NULL;
5966 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005967 }
5968 else
5969 {
5970 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005971 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005972 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005973 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005974 if (bool_on_stack(cctx) == FAIL)
5975 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977
5978 scope = new_scope(cctx, IF_SCOPE);
5979 if (scope == NULL)
5980 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005981 scope->se_skip_save = skip_save;
5982 // "is_had_return" will be reset if any block does not end in :return
5983 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005985 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005986 {
5987 // "where" is set when ":elseif", "else" or ":endif" is found
5988 scope->se_u.se_if.is_if_label = instr->ga_len;
5989 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5990 }
5991 else
5992 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
5994 return p;
5995}
5996
5997 static char_u *
5998compile_elseif(char_u *arg, cctx_T *cctx)
5999{
6000 char_u *p = arg;
6001 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006002 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 isn_T *isn;
6004 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006005 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006006 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007
6008 if (scope == NULL || scope->se_type != IF_SCOPE)
6009 {
6010 emsg(_(e_elseif_without_if));
6011 return NULL;
6012 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006013 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006014 if (!cctx->ctx_had_return)
6015 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006017 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006018 {
6019 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006021 return NULL;
6022 // previous "if" or "elseif" jumps here
6023 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6024 isn->isn_arg.jump.jump_where = instr->ga_len;
6025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006027 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006028 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006029 if (cctx->ctx_skip == SKIP_YES)
6030 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006031 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006032 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006033 clear_ppconst(&ppconst);
6034 return NULL;
6035 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006036 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006037 if (scope->se_skip_save == SKIP_YES)
6038 clear_ppconst(&ppconst);
6039 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006040 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006041 int error = FALSE;
6042 int v;
6043
Bram Moolenaar7f141552020-05-09 17:35:53 +02006044 // The expression results in a constant.
6045 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006046 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6047 if (error)
6048 return NULL;
6049 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006050 clear_ppconst(&ppconst);
6051 scope->se_u.se_if.is_if_label = -1;
6052 }
6053 else
6054 {
6055 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006056 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006057 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006058 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006059 if (bool_on_stack(cctx) == FAIL)
6060 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006062 // "where" is set when ":elseif", "else" or ":endif" is found
6063 scope->se_u.se_if.is_if_label = instr->ga_len;
6064 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066
6067 return p;
6068}
6069
6070 static char_u *
6071compile_else(char_u *arg, cctx_T *cctx)
6072{
6073 char_u *p = arg;
6074 garray_T *instr = &cctx->ctx_instr;
6075 isn_T *isn;
6076 scope_T *scope = cctx->ctx_scope;
6077
6078 if (scope == NULL || scope->se_type != IF_SCOPE)
6079 {
6080 emsg(_(e_else_without_if));
6081 return NULL;
6082 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006083 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006084 if (!cctx->ctx_had_return)
6085 scope->se_u.se_if.is_had_return = FALSE;
6086 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087
Bram Moolenaarefd88552020-06-18 20:50:10 +02006088 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006089 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006090 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006091 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006092 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006093 if (!cctx->ctx_had_return
6094 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6095 JUMP_ALWAYS, cctx) == FAIL)
6096 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006097 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006098
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006099 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006100 {
6101 if (scope->se_u.se_if.is_if_label >= 0)
6102 {
6103 // previous "if" or "elseif" jumps here
6104 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6105 isn->isn_arg.jump.jump_where = instr->ga_len;
6106 scope->se_u.se_if.is_if_label = -1;
6107 }
6108 }
6109
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006110 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006111 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113
6114 return p;
6115}
6116
6117 static char_u *
6118compile_endif(char_u *arg, cctx_T *cctx)
6119{
6120 scope_T *scope = cctx->ctx_scope;
6121 ifscope_T *ifscope;
6122 garray_T *instr = &cctx->ctx_instr;
6123 isn_T *isn;
6124
6125 if (scope == NULL || scope->se_type != IF_SCOPE)
6126 {
6127 emsg(_(e_endif_without_if));
6128 return NULL;
6129 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006130 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006131 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006132 if (!cctx->ctx_had_return)
6133 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006135 if (scope->se_u.se_if.is_if_label >= 0)
6136 {
6137 // previous "if" or "elseif" jumps here
6138 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6139 isn->isn_arg.jump.jump_where = instr->ga_len;
6140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141 // Fill in the "end" label in jumps at the end of the blocks.
6142 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006143 cctx->ctx_skip = scope->se_skip_save;
6144
6145 // If all the blocks end in :return and there is an :else then the
6146 // had_return flag is set.
6147 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006149 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 return arg;
6151}
6152
6153/*
6154 * compile "for var in expr"
6155 *
6156 * Produces instructions:
6157 * PUSHNR -1
6158 * STORE loop-idx Set index to -1
6159 * EVAL expr Push result of "expr"
6160 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6161 * - if beyond end, jump to "end"
6162 * - otherwise get item from list and push it
6163 * STORE var Store item in "var"
6164 * ... body ...
6165 * JUMP top Jump back to repeat
6166 * end: DROP Drop the result of "expr"
6167 *
6168 */
6169 static char_u *
6170compile_for(char_u *arg, cctx_T *cctx)
6171{
6172 char_u *p;
6173 size_t varlen;
6174 garray_T *instr = &cctx->ctx_instr;
6175 garray_T *stack = &cctx->ctx_type_stack;
6176 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006177 lvar_T *loop_lvar; // loop iteration variable
6178 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179 type_T *vartype;
6180
6181 // TODO: list of variables: "for [key, value] in dict"
6182 // parse "var"
6183 for (p = arg; eval_isnamec1(*p); ++p)
6184 ;
6185 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006186 var_lvar = lookup_local(arg, varlen, cctx);
6187 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006189 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 return NULL;
6191 }
6192
6193 // consume "in"
6194 p = skipwhite(p);
6195 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6196 {
6197 emsg(_(e_missing_in));
6198 return NULL;
6199 }
6200 p = skipwhite(p + 2);
6201
6202
6203 scope = new_scope(cctx, FOR_SCOPE);
6204 if (scope == NULL)
6205 return NULL;
6206
6207 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006208 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6209 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006210 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006211 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006212 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215
6216 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006217 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6218 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006219 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006220 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006221 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006225 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226
6227 // compile "expr", it remains on the stack until "endfor"
6228 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006229 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006230 {
6231 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006235 // Now that we know the type of "var", check that it is a list, now or at
6236 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006238 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006240 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 return NULL;
6242 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006243 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006244 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245
6246 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006247 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006249 generate_FOR(cctx, loop_lvar->lv_idx);
6250 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251
6252 return arg;
6253}
6254
6255/*
6256 * compile "endfor"
6257 */
6258 static char_u *
6259compile_endfor(char_u *arg, cctx_T *cctx)
6260{
6261 garray_T *instr = &cctx->ctx_instr;
6262 scope_T *scope = cctx->ctx_scope;
6263 forscope_T *forscope;
6264 isn_T *isn;
6265
6266 if (scope == NULL || scope->se_type != FOR_SCOPE)
6267 {
6268 emsg(_(e_for));
6269 return NULL;
6270 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006271 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006273 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274
6275 // At end of ":for" scope jump back to the FOR instruction.
6276 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6277
6278 // Fill in the "end" label in the FOR statement so it can jump here
6279 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6280 isn->isn_arg.forloop.for_end = instr->ga_len;
6281
6282 // Fill in the "end" label any BREAK statements
6283 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6284
6285 // Below the ":for" scope drop the "expr" list from the stack.
6286 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6287 return NULL;
6288
6289 vim_free(scope);
6290
6291 return arg;
6292}
6293
6294/*
6295 * compile "while expr"
6296 *
6297 * Produces instructions:
6298 * top: EVAL expr Push result of "expr"
6299 * JUMP_IF_FALSE end jump if false
6300 * ... body ...
6301 * JUMP top Jump back to repeat
6302 * end:
6303 *
6304 */
6305 static char_u *
6306compile_while(char_u *arg, cctx_T *cctx)
6307{
6308 char_u *p = arg;
6309 garray_T *instr = &cctx->ctx_instr;
6310 scope_T *scope;
6311
6312 scope = new_scope(cctx, WHILE_SCOPE);
6313 if (scope == NULL)
6314 return NULL;
6315
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006316 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317
6318 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006319 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 return NULL;
6321
Bram Moolenaar13106602020-10-04 16:06:05 +02006322 if (bool_on_stack(cctx) == FAIL)
6323 return FAIL;
6324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006326 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 JUMP_IF_FALSE, cctx) == FAIL)
6328 return FAIL;
6329
6330 return p;
6331}
6332
6333/*
6334 * compile "endwhile"
6335 */
6336 static char_u *
6337compile_endwhile(char_u *arg, cctx_T *cctx)
6338{
6339 scope_T *scope = cctx->ctx_scope;
6340
6341 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6342 {
6343 emsg(_(e_while));
6344 return NULL;
6345 }
6346 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006347 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348
6349 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006350 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351
6352 // Fill in the "end" label in the WHILE statement so it can jump here.
6353 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006354 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355
6356 vim_free(scope);
6357
6358 return arg;
6359}
6360
6361/*
6362 * compile "continue"
6363 */
6364 static char_u *
6365compile_continue(char_u *arg, cctx_T *cctx)
6366{
6367 scope_T *scope = cctx->ctx_scope;
6368
6369 for (;;)
6370 {
6371 if (scope == NULL)
6372 {
6373 emsg(_(e_continue));
6374 return NULL;
6375 }
6376 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6377 break;
6378 scope = scope->se_outer;
6379 }
6380
6381 // Jump back to the FOR or WHILE instruction.
6382 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006383 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6384 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 return arg;
6386}
6387
6388/*
6389 * compile "break"
6390 */
6391 static char_u *
6392compile_break(char_u *arg, cctx_T *cctx)
6393{
6394 scope_T *scope = cctx->ctx_scope;
6395 endlabel_T **el;
6396
6397 for (;;)
6398 {
6399 if (scope == NULL)
6400 {
6401 emsg(_(e_break));
6402 return NULL;
6403 }
6404 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6405 break;
6406 scope = scope->se_outer;
6407 }
6408
6409 // Jump to the end of the FOR or WHILE loop.
6410 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006411 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006413 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6415 return FAIL;
6416
6417 return arg;
6418}
6419
6420/*
6421 * compile "{" start of block
6422 */
6423 static char_u *
6424compile_block(char_u *arg, cctx_T *cctx)
6425{
6426 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6427 return NULL;
6428 return skipwhite(arg + 1);
6429}
6430
6431/*
6432 * compile end of block: drop one scope
6433 */
6434 static void
6435compile_endblock(cctx_T *cctx)
6436{
6437 scope_T *scope = cctx->ctx_scope;
6438
6439 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006440 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 vim_free(scope);
6442}
6443
6444/*
6445 * compile "try"
6446 * Creates a new scope for the try-endtry, pointing to the first catch and
6447 * finally.
6448 * Creates another scope for the "try" block itself.
6449 * TRY instruction sets up exception handling at runtime.
6450 *
6451 * "try"
6452 * TRY -> catch1, -> finally push trystack entry
6453 * ... try block
6454 * "throw {exception}"
6455 * EVAL {exception}
6456 * THROW create exception
6457 * ... try block
6458 * " catch {expr}"
6459 * JUMP -> finally
6460 * catch1: PUSH exeception
6461 * EVAL {expr}
6462 * MATCH
6463 * JUMP nomatch -> catch2
6464 * CATCH remove exception
6465 * ... catch block
6466 * " catch"
6467 * JUMP -> finally
6468 * catch2: CATCH remove exception
6469 * ... catch block
6470 * " finally"
6471 * finally:
6472 * ... finally block
6473 * " endtry"
6474 * ENDTRY pop trystack entry, may rethrow
6475 */
6476 static char_u *
6477compile_try(char_u *arg, cctx_T *cctx)
6478{
6479 garray_T *instr = &cctx->ctx_instr;
6480 scope_T *try_scope;
6481 scope_T *scope;
6482
6483 // scope that holds the jumps that go to catch/finally/endtry
6484 try_scope = new_scope(cctx, TRY_SCOPE);
6485 if (try_scope == NULL)
6486 return NULL;
6487
6488 // "catch" is set when the first ":catch" is found.
6489 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006490 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491 if (generate_instr(cctx, ISN_TRY) == NULL)
6492 return NULL;
6493
6494 // scope for the try block itself
6495 scope = new_scope(cctx, BLOCK_SCOPE);
6496 if (scope == NULL)
6497 return NULL;
6498
6499 return arg;
6500}
6501
6502/*
6503 * compile "catch {expr}"
6504 */
6505 static char_u *
6506compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6507{
6508 scope_T *scope = cctx->ctx_scope;
6509 garray_T *instr = &cctx->ctx_instr;
6510 char_u *p;
6511 isn_T *isn;
6512
6513 // end block scope from :try or :catch
6514 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6515 compile_endblock(cctx);
6516 scope = cctx->ctx_scope;
6517
6518 // Error if not in a :try scope
6519 if (scope == NULL || scope->se_type != TRY_SCOPE)
6520 {
6521 emsg(_(e_catch));
6522 return NULL;
6523 }
6524
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006525 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006527 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 return NULL;
6529 }
6530
6531 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006532 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 JUMP_ALWAYS, cctx) == FAIL)
6534 return NULL;
6535
6536 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006537 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 if (isn->isn_arg.try.try_catch == 0)
6539 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006540 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006541 {
6542 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006543 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 isn->isn_arg.jump.jump_where = instr->ga_len;
6545 }
6546
6547 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006548 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006550 scope->se_u.se_try.ts_caught_all = TRUE;
6551 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 }
6553 else
6554 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006555 char_u *end;
6556 char_u *pat;
6557 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006558 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006559 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 // Push v:exception, push {expr} and MATCH
6562 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6563
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006564 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006565 if (*end != *p)
6566 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006567 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006568 vim_free(tofree);
6569 return FAIL;
6570 }
6571 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006572 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006573 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006574 len = (int)(end - tofree);
6575 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006576 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006577 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006578 if (pat == NULL)
6579 return FAIL;
6580 if (generate_PUSHS(cctx, pat) == FAIL)
6581 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6584 return NULL;
6585
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006586 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6588 return NULL;
6589 }
6590
6591 if (generate_instr(cctx, ISN_CATCH) == NULL)
6592 return NULL;
6593
6594 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6595 return NULL;
6596 return p;
6597}
6598
6599 static char_u *
6600compile_finally(char_u *arg, cctx_T *cctx)
6601{
6602 scope_T *scope = cctx->ctx_scope;
6603 garray_T *instr = &cctx->ctx_instr;
6604 isn_T *isn;
6605
6606 // end block scope from :try or :catch
6607 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6608 compile_endblock(cctx);
6609 scope = cctx->ctx_scope;
6610
6611 // Error if not in a :try scope
6612 if (scope == NULL || scope->se_type != TRY_SCOPE)
6613 {
6614 emsg(_(e_finally));
6615 return NULL;
6616 }
6617
6618 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006619 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 if (isn->isn_arg.try.try_finally != 0)
6621 {
6622 emsg(_(e_finally_dup));
6623 return NULL;
6624 }
6625
6626 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006627 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628
Bram Moolenaar585fea72020-04-02 22:33:21 +02006629 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006630 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 {
6632 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006633 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006635 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 }
6637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638 // TODO: set index in ts_finally_label jumps
6639
6640 return arg;
6641}
6642
6643 static char_u *
6644compile_endtry(char_u *arg, cctx_T *cctx)
6645{
6646 scope_T *scope = cctx->ctx_scope;
6647 garray_T *instr = &cctx->ctx_instr;
6648 isn_T *isn;
6649
6650 // end block scope from :catch or :finally
6651 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6652 compile_endblock(cctx);
6653 scope = cctx->ctx_scope;
6654
6655 // Error if not in a :try scope
6656 if (scope == NULL || scope->se_type != TRY_SCOPE)
6657 {
6658 if (scope == NULL)
6659 emsg(_(e_no_endtry));
6660 else if (scope->se_type == WHILE_SCOPE)
6661 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006662 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 emsg(_(e_endfor));
6664 else
6665 emsg(_(e_endif));
6666 return NULL;
6667 }
6668
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006669 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6671 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006672 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673 return NULL;
6674 }
6675
6676 // Fill in the "end" label in jumps at the end of the blocks, if not done
6677 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006678 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679
6680 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006681 if (isn->isn_arg.try.try_catch == 0)
6682 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 if (isn->isn_arg.try.try_finally == 0)
6684 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006685
6686 if (scope->se_u.se_try.ts_catch_label != 0)
6687 {
6688 // Last catch without match jumps here
6689 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6690 isn->isn_arg.jump.jump_where = instr->ga_len;
6691 }
6692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 compile_endblock(cctx);
6694
6695 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6696 return NULL;
6697 return arg;
6698}
6699
6700/*
6701 * compile "throw {expr}"
6702 */
6703 static char_u *
6704compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6705{
6706 char_u *p = skipwhite(arg);
6707
Bram Moolenaara5565e42020-05-09 15:44:01 +02006708 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 return NULL;
6710 if (may_generate_2STRING(-1, cctx) == FAIL)
6711 return NULL;
6712 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6713 return NULL;
6714
6715 return p;
6716}
6717
6718/*
6719 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006720 * compile "echomsg expr"
6721 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006722 * compile "execute expr"
6723 */
6724 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006725compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006726{
6727 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006728 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006729 int count = 0;
6730
6731 for (;;)
6732 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006733 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006734 return NULL;
6735 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006736 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006737 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006738 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006739 break;
6740 }
6741
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006742 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6743 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6744 else if (cmdidx == CMD_execute)
6745 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6746 else if (cmdidx == CMD_echomsg)
6747 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6748 else
6749 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 return p;
6751}
6752
6753/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006754 * :put r
6755 * :put ={expr}
6756 */
6757 static char_u *
6758compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6759{
6760 char_u *line = arg;
6761 linenr_T lnum;
6762 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006763 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006764
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006765 eap->regname = *line;
6766
6767 if (eap->regname == '=')
6768 {
6769 char_u *p = line + 1;
6770
6771 if (compile_expr0(&p, cctx) == FAIL)
6772 return NULL;
6773 line = p;
6774 }
6775 else if (eap->regname != NUL)
6776 ++line;
6777
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006778 // "errormsg" will not be set because the range is ADDR_LINES.
6779 // TODO: if the range contains something like "$" or "." need to evaluate
6780 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006781 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6782 return NULL;
6783 if (eap->addr_count == 0)
6784 lnum = -1;
6785 else
6786 lnum = eap->line2;
6787 if (above)
6788 --lnum;
6789
6790 generate_PUT(cctx, eap->regname, lnum);
6791 return line;
6792}
6793
6794/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006795 * A command that is not compiled, execute with legacy code.
6796 */
6797 static char_u *
6798compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6799{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006800 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006801 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006802 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006803
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006804 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006805 goto theend;
6806
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006807 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006808 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006809 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006810 int usefilter = FALSE;
6811
6812 has_expr = argt & (EX_XFILE | EX_EXPAND);
6813
6814 // If the command can be followed by a bar, find the bar and truncate
6815 // it, so that the following command can be compiled.
6816 // The '|' is overwritten with a NUL, it is put back below.
6817 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6818 && *eap->arg == '!')
6819 // :w !filter or :r !filter or :r! filter
6820 usefilter = TRUE;
6821 if ((argt & EX_TRLBAR) && !usefilter)
6822 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006823 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006824 separate_nextcmd(eap);
6825 if (eap->nextcmd != NULL)
6826 nextcmd = eap->nextcmd;
6827 }
6828 }
6829
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006830 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6831 {
6832 // expand filename in "syntax include [@group] filename"
6833 has_expr = TRUE;
6834 eap->arg = skipwhite(eap->arg + 7);
6835 if (*eap->arg == '@')
6836 eap->arg = skiptowhite(eap->arg);
6837 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006838
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006839 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006840 {
6841 int count = 0;
6842 char_u *start = skipwhite(line);
6843
6844 // :cmd xxx`=expr1`yyy`=expr2`zzz
6845 // PUSHS ":cmd xxx"
6846 // eval expr1
6847 // PUSHS "yyy"
6848 // eval expr2
6849 // PUSHS "zzz"
6850 // EXECCONCAT 5
6851 for (;;)
6852 {
6853 if (p > start)
6854 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006855 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006856 ++count;
6857 }
6858 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006859 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006860 return NULL;
6861 may_generate_2STRING(-1, cctx);
6862 ++count;
6863 p = skipwhite(p);
6864 if (*p != '`')
6865 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006866 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006867 return NULL;
6868 }
6869 start = p + 1;
6870
6871 p = (char_u *)strstr((char *)start, "`=");
6872 if (p == NULL)
6873 {
6874 if (*skipwhite(start) != NUL)
6875 {
6876 generate_PUSHS(cctx, vim_strsave(start));
6877 ++count;
6878 }
6879 break;
6880 }
6881 }
6882 generate_EXECCONCAT(cctx, count);
6883 }
6884 else
6885 generate_EXEC(cctx, line);
6886
6887theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006888 if (*nextcmd != NUL)
6889 {
6890 // the parser expects a pointer to the bar, put it back
6891 --nextcmd;
6892 *nextcmd = '|';
6893 }
6894
6895 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006896}
6897
6898/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006899 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006900 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006901 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006902 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006903add_def_function(ufunc_T *ufunc)
6904{
6905 dfunc_T *dfunc;
6906
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006907 if (def_functions.ga_len == 0)
6908 {
6909 // The first position is not used, so that a zero uf_dfunc_idx means it
6910 // wasn't set.
6911 if (ga_grow(&def_functions, 1) == FAIL)
6912 return FAIL;
6913 ++def_functions.ga_len;
6914 }
6915
Bram Moolenaar09689a02020-05-09 22:50:08 +02006916 // Add the function to "def_functions".
6917 if (ga_grow(&def_functions, 1) == FAIL)
6918 return FAIL;
6919 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6920 CLEAR_POINTER(dfunc);
6921 dfunc->df_idx = def_functions.ga_len;
6922 ufunc->uf_dfunc_idx = dfunc->df_idx;
6923 dfunc->df_ufunc = ufunc;
6924 ++def_functions.ga_len;
6925 return OK;
6926}
6927
6928/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 * After ex_function() has collected all the function lines: parse and compile
6930 * the lines into instructions.
6931 * Adds the function to "def_functions".
6932 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6933 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006934 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006935 * This can be used recursively through compile_lambda(), which may reallocate
6936 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006937 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006939 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006940compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 char_u *line = NULL;
6943 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 cctx_T cctx;
6946 garray_T *instr;
6947 int called_emsg_before = called_emsg;
6948 int ret = FAIL;
6949 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006950 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006951 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006952 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006953 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006955 // When using a function that was compiled before: Free old instructions.
6956 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006957 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006959 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6960 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006961 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006963 else
6964 {
6965 if (add_def_function(ufunc) == FAIL)
6966 return FAIL;
6967 new_def_function = TRUE;
6968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969
Bram Moolenaar985116a2020-07-12 17:31:09 +02006970 ufunc->uf_def_status = UF_COMPILING;
6971
Bram Moolenaara80faa82020-04-12 19:37:17 +02006972 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 cctx.ctx_ufunc = ufunc;
6974 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006975 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6977 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6978 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6979 cctx.ctx_type_list = &ufunc->uf_type_list;
6980 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6981 instr = &cctx.ctx_instr;
6982
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006983 // Set the context to the function, it may be compiled when called from
6984 // another script. Set the script version to the most modern one.
6985 // The line number will be set in next_line_from_context().
6986 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6988
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006989 // Make sure error messages are OK.
6990 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6991 if (do_estack_push)
6992 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006993 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006994
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006995 if (ufunc->uf_def_args.ga_len > 0)
6996 {
6997 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006998 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006999 int i;
7000 char_u *arg;
7001 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007002 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007003
7004 // Produce instructions for the default values of optional arguments.
7005 // Store the instruction index in uf_def_arg_idx[] so that we know
7006 // where to start when the function is called, depending on the number
7007 // of arguments.
7008 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7009 if (ufunc->uf_def_arg_idx == NULL)
7010 goto erret;
7011 for (i = 0; i < count; ++i)
7012 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007013 garray_T *stack = &cctx.ctx_type_stack;
7014 type_T *val_type;
7015 int arg_idx = first_def_arg + i;
7016
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007017 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7018 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007019 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007020 goto erret;
7021
7022 // If no type specified use the type of the default value.
7023 // Otherwise check that the default value type matches the
7024 // specified type.
7025 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7026 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007027 {
7028 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007029 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007030 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007031 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7032 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007033 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007034
7035 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007036 goto erret;
7037 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007038 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007039
7040 if (did_set_arg_type)
7041 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007042 }
7043
7044 /*
7045 * Loop over all the lines of the function and generate instructions.
7046 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 for (;;)
7048 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007049 exarg_T ea;
7050 cmdmod_T save_cmdmod;
7051 int starts_with_colon = FALSE;
7052 char_u *cmd;
7053 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007054
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007055 // Bail out on the first error to avoid a flood of errors and report
7056 // the right line number when inside try/catch.
7057 if (emsg_before != called_emsg)
7058 goto erret;
7059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 if (line != NULL && *line == '|')
7061 // the line continues after a '|'
7062 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007063 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007064 && !(*line == '#' && (line == cctx.ctx_line_start
7065 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007067 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 goto erret;
7069 }
7070 else
7071 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007072 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007073 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007074 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007077 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078
Bram Moolenaara80faa82020-04-12 19:37:17 +02007079 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 ea.cmdlinep = &line;
7081 ea.cmd = skipwhite(line);
7082
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007083 // Some things can be recognized by the first character.
7084 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007086 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007087 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007088 if (ea.cmd[1] != '{')
7089 {
7090 line = (char_u *)"";
7091 continue;
7092 }
7093 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007095 case '}':
7096 {
7097 // "}" ends a block scope
7098 scopetype_T stype = cctx.ctx_scope == NULL
7099 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007101 if (stype == BLOCK_SCOPE)
7102 {
7103 compile_endblock(&cctx);
7104 line = ea.cmd;
7105 }
7106 else
7107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007108 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007109 goto erret;
7110 }
7111 if (line != NULL)
7112 line = skipwhite(ea.cmd + 1);
7113 continue;
7114 }
7115
7116 case '{':
7117 // "{" starts a block scope
7118 // "{'a': 1}->func() is something else
7119 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7120 {
7121 line = compile_block(ea.cmd, &cctx);
7122 continue;
7123 }
7124 break;
7125
7126 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007127 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007128 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 }
7130
7131 /*
7132 * COMMAND MODIFIERS
7133 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007134 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7136 {
7137 if (errormsg != NULL)
7138 goto erret;
7139 // empty line or comment
7140 line = (char_u *)"";
7141 continue;
7142 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007143 // TODO: use modifiers in the command
7144 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007145 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146
7147 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007148 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007150 {
7151 if (*ea.cmd == '(')
7152 // not for "call()"
7153 ea.cmd = p;
7154 else
7155 ea.cmd = skipwhite(ea.cmd);
7156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007158 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007160 char_u *pskip;
7161
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007162 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007163 // find what follows.
7164 // Skip over "var.member", "var[idx]" and the like.
7165 // Also "&opt = val", "$ENV = val" and "@r = val".
7166 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007167 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007168 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007169 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007171 char_u *var_end;
7172 int oplen;
7173 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174
Bram Moolenaar65821722020-08-02 18:58:54 +02007175 if (ea.cmd[0] == '@')
7176 var_end = ea.cmd + 2;
7177 else
7178 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007179 FNE_CHECK_START | FNE_INCL_BR);
7180 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007181 if (oplen > 0)
7182 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007183 size_t len = p - ea.cmd;
7184
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007185 // Recognize an assignment if we recognize the variable
7186 // name:
7187 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007188 // "local = expr" where "local" is a local var.
7189 // "script = expr" where "script" is a script-local var.
7190 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007191 // "&opt = expr"
7192 // "$ENV = expr"
7193 // "@r = expr"
7194 if (*ea.cmd == '&'
7195 || *ea.cmd == '$'
7196 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007197 || ((len) > 2 && ea.cmd[1] == ':')
7198 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007199 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007200 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007201 || script_var_exists(ea.cmd, len,
7202 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007203 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007204 {
7205 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007206 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007207 goto erret;
7208 continue;
7209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210 }
7211 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007212
7213 if (*ea.cmd == '[')
7214 {
7215 // [var, var] = expr
7216 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7217 if (line == NULL)
7218 goto erret;
7219 if (line != ea.cmd)
7220 continue;
7221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 }
7223
7224 /*
7225 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007226 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007228 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007229 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007230 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007231 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007232 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007233 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007234 if (!starts_with_colon)
7235 {
7236 emsg(_(e_colon_required_before_a_range));
7237 goto erret;
7238 }
7239 if (ends_excmd2(line, ea.cmd))
7240 {
7241 // A range without a command: jump to the line.
7242 // TODO: compile to a more efficient command, possibly
7243 // calling parse_cmd_address().
7244 ea.cmdidx = CMD_SIZE;
7245 line = compile_exec(line, &ea, &cctx);
7246 goto nextline;
7247 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007248 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007249 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007250 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007251 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007252 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253
7254 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7255 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007256 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007257 {
7258 line += STRLEN(line);
7259 continue;
7260 }
7261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007263 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007265 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007266 iemsg("Command from find_ex_command() not handled");
7267 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 }
7270
Bram Moolenaar3988f642020-08-27 22:43:03 +02007271 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007272 && ea.cmdidx != CMD_elseif
7273 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007274 && ea.cmdidx != CMD_endif
7275 && ea.cmdidx != CMD_endfor
7276 && ea.cmdidx != CMD_endwhile
7277 && ea.cmdidx != CMD_catch
7278 && ea.cmdidx != CMD_finally
7279 && ea.cmdidx != CMD_endtry)
7280 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007281 emsg(_(e_unreachable_code_after_return));
7282 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007283 }
7284
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007285 p = skipwhite(p);
7286 if (ea.cmdidx != CMD_SIZE
7287 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7288 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007289 if (ea.cmdidx >= 0)
7290 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007291 if ((ea.argt & EX_BANG) && *p == '!')
7292 {
7293 ea.forceit = TRUE;
7294 p = skipwhite(p + 1);
7295 }
7296 }
7297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 switch (ea.cmdidx)
7299 {
7300 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007301 ea.arg = p;
7302 line = compile_nested_function(&ea, &cctx);
7303 break;
7304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007306 // TODO: should we allow this, e.g. to declare a global
7307 // function?
7308 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309 goto erret;
7310
7311 case CMD_return:
7312 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007313 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 break;
7315
7316 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007317 if (get_vim_var_nr(VV_DISALLOW_LET))
7318 {
7319 emsg(_(e_cannot_use_let_in_vim9_script));
7320 break;
7321 }
7322 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007323 case CMD_var:
7324 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 case CMD_const:
7326 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007327 if (line == p)
7328 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 break;
7330
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007331 case CMD_unlet:
7332 case CMD_unlockvar:
7333 case CMD_lockvar:
7334 line = compile_unletlock(p, &ea, &cctx);
7335 break;
7336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 case CMD_import:
7338 line = compile_import(p, &cctx);
7339 break;
7340
7341 case CMD_if:
7342 line = compile_if(p, &cctx);
7343 break;
7344 case CMD_elseif:
7345 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007346 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 break;
7348 case CMD_else:
7349 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007350 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 break;
7352 case CMD_endif:
7353 line = compile_endif(p, &cctx);
7354 break;
7355
7356 case CMD_while:
7357 line = compile_while(p, &cctx);
7358 break;
7359 case CMD_endwhile:
7360 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007361 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 break;
7363
7364 case CMD_for:
7365 line = compile_for(p, &cctx);
7366 break;
7367 case CMD_endfor:
7368 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007369 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 break;
7371 case CMD_continue:
7372 line = compile_continue(p, &cctx);
7373 break;
7374 case CMD_break:
7375 line = compile_break(p, &cctx);
7376 break;
7377
7378 case CMD_try:
7379 line = compile_try(p, &cctx);
7380 break;
7381 case CMD_catch:
7382 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007383 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 break;
7385 case CMD_finally:
7386 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007387 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 break;
7389 case CMD_endtry:
7390 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007391 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 break;
7393 case CMD_throw:
7394 line = compile_throw(p, &cctx);
7395 break;
7396
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007397 case CMD_eval:
7398 if (compile_expr0(&p, &cctx) == FAIL)
7399 goto erret;
7400
Bram Moolenaar3988f642020-08-27 22:43:03 +02007401 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007402 generate_instr_drop(&cctx, ISN_DROP, 1);
7403
7404 line = skipwhite(p);
7405 break;
7406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007409 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007410 case CMD_echomsg:
7411 case CMD_echoerr:
7412 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007413 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007415 case CMD_put:
7416 ea.cmd = cmd;
7417 line = compile_put(p, &ea, &cctx);
7418 break;
7419
Bram Moolenaar3988f642020-08-27 22:43:03 +02007420 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007421
Bram Moolenaarae616492020-07-28 20:07:27 +02007422 case CMD_append:
7423 case CMD_change:
7424 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007425 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007426 case CMD_xit:
7427 not_in_vim9(&ea);
7428 goto erret;
7429
Bram Moolenaar002262f2020-07-08 17:47:57 +02007430 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007431 if (cctx.ctx_skip != SKIP_YES)
7432 {
7433 semsg(_(e_invalid_command_str), ea.cmd);
7434 goto erret;
7435 }
7436 // We don't check for a next command here.
7437 line = (char_u *)"";
7438 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007441 if (cctx.ctx_skip == SKIP_YES)
7442 {
7443 // We don't check for a next command here.
7444 line = (char_u *)"";
7445 }
7446 else
7447 {
7448 // Not recognized, execute with do_cmdline_cmd().
7449 ea.arg = p;
7450 line = compile_exec(line, &ea, &cctx);
7451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 break;
7453 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007454nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 if (line == NULL)
7456 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007457 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458
7459 if (cctx.ctx_type_stack.ga_len < 0)
7460 {
7461 iemsg("Type stack underflow");
7462 goto erret;
7463 }
7464 }
7465
7466 if (cctx.ctx_scope != NULL)
7467 {
7468 if (cctx.ctx_scope->se_type == IF_SCOPE)
7469 emsg(_(e_endif));
7470 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7471 emsg(_(e_endwhile));
7472 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7473 emsg(_(e_endfor));
7474 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007475 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 goto erret;
7477 }
7478
Bram Moolenaarefd88552020-06-18 20:50:10 +02007479 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 {
7481 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7482 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007483 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 goto erret;
7485 }
7486
7487 // Return zero if there is no return at the end.
7488 generate_PUSHNR(&cctx, 0);
7489 generate_instr(&cctx, ISN_RETURN);
7490 }
7491
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007492 {
7493 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7494 + ufunc->uf_dfunc_idx;
7495 dfunc->df_deleted = FALSE;
7496 dfunc->df_instr = instr->ga_data;
7497 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007498 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007499 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007500 if (cctx.ctx_outer_used)
7501 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007502 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504
7505 ret = OK;
7506
7507erret:
7508 if (ret == FAIL)
7509 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007510 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007511 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7512 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007513
7514 for (idx = 0; idx < instr->ga_len; ++idx)
7515 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007517
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007518 // If using the last entry in the table and it was added above, we
7519 // might as well remove it.
7520 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007521 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007522 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007523 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007524 ufunc->uf_dfunc_idx = 0;
7525 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007526 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007527
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007528 while (cctx.ctx_scope != NULL)
7529 drop_scope(&cctx);
7530
Bram Moolenaar20431c92020-03-20 18:39:46 +01007531 // Don't execute this function body.
7532 ga_clear_strings(&ufunc->uf_lines);
7533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534 if (errormsg != NULL)
7535 emsg(errormsg);
7536 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007537 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007538 }
7539
7540 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007541 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007542 if (do_estack_push)
7543 estack_pop();
7544
Bram Moolenaar20431c92020-03-20 18:39:46 +01007545 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007546 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007547 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007548 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549}
7550
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007551 void
7552set_function_type(ufunc_T *ufunc)
7553{
7554 int varargs = ufunc->uf_va_name != NULL;
7555 int argcount = ufunc->uf_args.ga_len;
7556
7557 // Create a type for the function, with the return type and any
7558 // argument types.
7559 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7560 // The type is included in "tt_args".
7561 if (argcount > 0 || varargs)
7562 {
7563 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7564 argcount, &ufunc->uf_type_list);
7565 // Add argument types to the function type.
7566 if (func_type_add_arg_types(ufunc->uf_func_type,
7567 argcount + varargs,
7568 &ufunc->uf_type_list) == FAIL)
7569 return;
7570 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7571 ufunc->uf_func_type->tt_min_argcount =
7572 argcount - ufunc->uf_def_args.ga_len;
7573 if (ufunc->uf_arg_types == NULL)
7574 {
7575 int i;
7576
7577 // lambda does not have argument types.
7578 for (i = 0; i < argcount; ++i)
7579 ufunc->uf_func_type->tt_args[i] = &t_any;
7580 }
7581 else
7582 mch_memmove(ufunc->uf_func_type->tt_args,
7583 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7584 if (varargs)
7585 {
7586 ufunc->uf_func_type->tt_args[argcount] =
7587 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7588 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7589 }
7590 }
7591 else
7592 // No arguments, can use a predefined type.
7593 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7594 argcount, &ufunc->uf_type_list);
7595}
7596
7597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598/*
7599 * Delete an instruction, free what it contains.
7600 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007601 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602delete_instr(isn_T *isn)
7603{
7604 switch (isn->isn_type)
7605 {
7606 case ISN_EXEC:
7607 case ISN_LOADENV:
7608 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007609 case ISN_LOADB:
7610 case ISN_LOADW:
7611 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007613 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007614 case ISN_PUSHEXC:
7615 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007616 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007618 case ISN_STOREB:
7619 case ISN_STOREW:
7620 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007621 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007622 vim_free(isn->isn_arg.string);
7623 break;
7624
7625 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007626 case ISN_STORES:
7627 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628 break;
7629
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007630 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007631 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007632 vim_free(isn->isn_arg.unlet.ul_name);
7633 break;
7634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 case ISN_STOREOPT:
7636 vim_free(isn->isn_arg.storeopt.so_name);
7637 break;
7638
7639 case ISN_PUSHBLOB: // push blob isn_arg.blob
7640 blob_unref(isn->isn_arg.blob);
7641 break;
7642
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007643 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007645 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007646#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007647 break;
7648
7649 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007650#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007651 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007652#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007653 break;
7654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 case ISN_UCALL:
7656 vim_free(isn->isn_arg.ufunc.cuf_name);
7657 break;
7658
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007659 case ISN_FUNCREF:
7660 {
7661 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7662 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007663
7664 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7665 func_ptr_unref(dfunc->df_ufunc);
7666 }
7667 break;
7668
7669 case ISN_DCALL:
7670 {
7671 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7672 + isn->isn_arg.dfunc.cdf_idx;
7673
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007674 if (dfunc->df_ufunc != NULL
7675 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007676 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007677 }
7678 break;
7679
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007680 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007681 {
7682 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7683 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7684
7685 if (ufunc != NULL)
7686 {
7687 // Clear uf_dfunc_idx so that the function is deleted.
7688 clear_def_function(ufunc);
7689 ufunc->uf_dfunc_idx = 0;
7690 func_ptr_unref(ufunc);
7691 }
7692
7693 vim_free(lambda);
7694 vim_free(isn->isn_arg.newfunc.nf_global);
7695 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007696 break;
7697
Bram Moolenaar5e654232020-09-16 15:22:00 +02007698 case ISN_CHECKTYPE:
7699 free_type(isn->isn_arg.type.ct_type);
7700 break;
7701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007702 case ISN_2BOOL:
7703 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007704 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 case ISN_ADDBLOB:
7706 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007707 case ISN_ANYINDEX:
7708 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007710 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007711 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007712 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 case ISN_COMPAREANY:
7715 case ISN_COMPAREBLOB:
7716 case ISN_COMPAREBOOL:
7717 case ISN_COMPAREDICT:
7718 case ISN_COMPAREFLOAT:
7719 case ISN_COMPAREFUNC:
7720 case ISN_COMPARELIST:
7721 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722 case ISN_COMPARESPECIAL:
7723 case ISN_COMPARESTRING:
7724 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007725 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007726 case ISN_DROP:
7727 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007728 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007729 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007731 case ISN_EXECCONCAT:
7732 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007733 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007734 case ISN_GETITEM:
7735 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007736 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007737 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007738 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007740 case ISN_LOADBDICT:
7741 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007742 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007744 case ISN_LOADSCRIPT:
7745 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007747 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007748 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007749 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007750 case ISN_NEGATENR:
7751 case ISN_NEWDICT:
7752 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007754 case ISN_OPFLOAT:
7755 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007757 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007758 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 case ISN_PUSHF:
7760 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007762 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007764 case ISN_SHUFFLE:
7765 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007767 case ISN_STOREDICT:
7768 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007769 case ISN_STORENR:
7770 case ISN_STOREOUTER:
7771 case ISN_STOREREG:
7772 case ISN_STORESCRIPT:
7773 case ISN_STOREV:
7774 case ISN_STRINDEX:
7775 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 case ISN_THROW:
7777 case ISN_TRY:
7778 // nothing allocated
7779 break;
7780 }
7781}
7782
7783/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007784 * Free all instructions for "dfunc".
7785 */
7786 static void
7787delete_def_function_contents(dfunc_T *dfunc)
7788{
7789 int idx;
7790
7791 ga_clear(&dfunc->df_def_args_isn);
7792
7793 if (dfunc->df_instr != NULL)
7794 {
7795 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7796 delete_instr(dfunc->df_instr + idx);
7797 VIM_CLEAR(dfunc->df_instr);
7798 }
7799
7800 dfunc->df_deleted = TRUE;
7801}
7802
7803/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007804 * When a user function is deleted, clear the contents of any associated def
7805 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 */
7807 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007808clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007810 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811 {
7812 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7813 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814
Bram Moolenaar20431c92020-03-20 18:39:46 +01007815 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007816 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817 }
7818}
7819
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007820/*
7821 * Used when a user function is about to be deleted: remove the pointer to it.
7822 * The entry in def_functions is then unused.
7823 */
7824 void
7825unlink_def_function(ufunc_T *ufunc)
7826{
7827 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7828
7829 dfunc->df_ufunc = NULL;
7830}
7831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007833/*
7834 * Free all functions defined with ":def".
7835 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 void
7837free_def_functions(void)
7838{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007839 int idx;
7840
7841 for (idx = 0; idx < def_functions.ga_len; ++idx)
7842 {
7843 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7844
7845 delete_def_function_contents(dfunc);
7846 }
7847
7848 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849}
7850#endif
7851
7852
7853#endif // FEAT_EVAL