blob: bb4807eeb7e63a8a5f7ae2458a99e674e982126b [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 Moolenaar8a7d6542020-01-26 15:56:19 +01001524 * Generate an ISN_DCALL or ISN_UCALL instruction.
1525 * Return FAIL if the number of arguments is wrong.
1526 */
1527 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001528generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529{
1530 isn_T *isn;
1531 garray_T *stack = &cctx->ctx_type_stack;
1532 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001533 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534
Bram Moolenaar080457c2020-03-03 21:53:32 +01001535 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 if (argcount > regular_args && !has_varargs(ufunc))
1537 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001538 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 return FAIL;
1540 }
1541 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1542 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001543 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 return FAIL;
1545 }
1546
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001547 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001548 {
1549 int i;
1550
1551 for (i = 0; i < argcount; ++i)
1552 {
1553 type_T *expected;
1554 type_T *actual;
1555
1556 if (i < regular_args)
1557 {
1558 if (ufunc->uf_arg_types == NULL)
1559 continue;
1560 expected = ufunc->uf_arg_types[i];
1561 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001562 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1563 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001564 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001565 else
1566 expected = ufunc->uf_va_type->tt_member;
1567 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001568 if (need_type(actual, expected, -argcount + i, cctx,
1569 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001570 {
1571 arg_type_mismatch(expected, actual, i + 1);
1572 return FAIL;
1573 }
1574 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001575 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001576 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1577 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001578 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001579 }
1580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001582 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001583 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001585 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 {
1587 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1588 isn->isn_arg.dfunc.cdf_argcount = argcount;
1589 }
1590 else
1591 {
1592 // A user function may be deleted and redefined later, can't use the
1593 // ufunc pointer, need to look it up again at runtime.
1594 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1595 isn->isn_arg.ufunc.cuf_argcount = argcount;
1596 }
1597
1598 stack->ga_len -= argcount; // drop the arguments
1599 if (ga_grow(stack, 1) == FAIL)
1600 return FAIL;
1601 // add return value
1602 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1603 ++stack->ga_len;
1604
1605 return OK;
1606}
1607
1608/*
1609 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1610 */
1611 static int
1612generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1613{
1614 isn_T *isn;
1615 garray_T *stack = &cctx->ctx_type_stack;
1616
Bram Moolenaar080457c2020-03-03 21:53:32 +01001617 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1619 return FAIL;
1620 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1621 isn->isn_arg.ufunc.cuf_argcount = argcount;
1622
1623 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001624 if (ga_grow(stack, 1) == FAIL)
1625 return FAIL;
1626 // add return value
1627 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1628 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629
1630 return OK;
1631}
1632
1633/*
1634 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001635 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 */
1637 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001638generate_PCALL(
1639 cctx_T *cctx,
1640 int argcount,
1641 char_u *name,
1642 type_T *type,
1643 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644{
1645 isn_T *isn;
1646 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001647 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648
Bram Moolenaar080457c2020-03-03 21:53:32 +01001649 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001650
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001651 if (type->tt_type == VAR_ANY)
1652 ret_type = &t_any;
1653 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001654 {
1655 if (type->tt_argcount != -1)
1656 {
1657 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1658
1659 if (argcount < type->tt_min_argcount - varargs)
1660 {
1661 semsg(_(e_toofewarg), "[reference]");
1662 return FAIL;
1663 }
1664 if (!varargs && argcount > type->tt_argcount)
1665 {
1666 semsg(_(e_toomanyarg), "[reference]");
1667 return FAIL;
1668 }
1669 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001670 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001671 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001672 else
1673 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001674 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001675 return FAIL;
1676 }
1677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1679 return FAIL;
1680 isn->isn_arg.pfunc.cpf_top = at_top;
1681 isn->isn_arg.pfunc.cpf_argcount = argcount;
1682
1683 stack->ga_len -= argcount; // drop the arguments
1684
1685 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001686 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001688 // If partial is above the arguments it must be cleared and replaced with
1689 // the return value.
1690 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1691 return FAIL;
1692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 return OK;
1694}
1695
1696/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001697 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 */
1699 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001700generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701{
1702 isn_T *isn;
1703 garray_T *stack = &cctx->ctx_type_stack;
1704 type_T *type;
1705
Bram Moolenaar080457c2020-03-03 21:53:32 +01001706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001707 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001709 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001711 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001713 if (type->tt_type != VAR_DICT && type != &t_any)
1714 {
1715 emsg(_(e_dictreq));
1716 return FAIL;
1717 }
1718 // change dict type to dict member type
1719 if (type->tt_type == VAR_DICT)
1720 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721
1722 return OK;
1723}
1724
1725/*
1726 * Generate an ISN_ECHO instruction.
1727 */
1728 static int
1729generate_ECHO(cctx_T *cctx, int with_white, int count)
1730{
1731 isn_T *isn;
1732
Bram Moolenaar080457c2020-03-03 21:53:32 +01001733 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1735 return FAIL;
1736 isn->isn_arg.echo.echo_with_white = with_white;
1737 isn->isn_arg.echo.echo_count = count;
1738
1739 return OK;
1740}
1741
Bram Moolenaarad39c092020-02-26 18:23:43 +01001742/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001743 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001744 */
1745 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001746generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001747{
1748 isn_T *isn;
1749
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001750 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001751 return FAIL;
1752 isn->isn_arg.number = count;
1753
1754 return OK;
1755}
1756
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001757/*
1758 * Generate an ISN_PUT instruction.
1759 */
1760 static int
1761generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1762{
1763 isn_T *isn;
1764
1765 RETURN_OK_IF_SKIP(cctx);
1766 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1767 return FAIL;
1768 isn->isn_arg.put.put_regname = regname;
1769 isn->isn_arg.put.put_lnum = lnum;
1770 return OK;
1771}
1772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 static int
1774generate_EXEC(cctx_T *cctx, char_u *line)
1775{
1776 isn_T *isn;
1777
Bram Moolenaar080457c2020-03-03 21:53:32 +01001778 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1780 return FAIL;
1781 isn->isn_arg.string = vim_strsave(line);
1782 return OK;
1783}
1784
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001785 static int
1786generate_EXECCONCAT(cctx_T *cctx, int count)
1787{
1788 isn_T *isn;
1789
1790 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1791 return FAIL;
1792 isn->isn_arg.number = count;
1793 return OK;
1794}
1795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796/*
1797 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001798 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001800 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001801reserve_local(
1802 cctx_T *cctx,
1803 char_u *name,
1804 size_t len,
1805 int isConst,
1806 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 lvar_T *lvar;
1809
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001810 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001812 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001813 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 }
1815
1816 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001817 return NULL;
1818 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001819 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001821 // Every local variable uses the next entry on the stack. We could re-use
1822 // the last ones when leaving a scope, but then variables used in a closure
1823 // might get overwritten. To keep things simple do not re-use stack
1824 // entries. This is less efficient, but memory is cheap these days.
1825 lvar->lv_idx = cctx->ctx_locals_count++;
1826
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001827 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 lvar->lv_const = isConst;
1829 lvar->lv_type = type;
1830
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001831 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832}
1833
1834/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001835 * Remove local variables above "new_top".
1836 */
1837 static void
1838unwind_locals(cctx_T *cctx, int new_top)
1839{
1840 if (cctx->ctx_locals.ga_len > new_top)
1841 {
1842 int idx;
1843 lvar_T *lvar;
1844
1845 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1846 {
1847 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1848 vim_free(lvar->lv_name);
1849 }
1850 }
1851 cctx->ctx_locals.ga_len = new_top;
1852}
1853
1854/*
1855 * Free all local variables.
1856 */
1857 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001858free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001859{
1860 unwind_locals(cctx, 0);
1861 ga_clear(&cctx->ctx_locals);
1862}
1863
1864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 * Find "name" in script-local items of script "sid".
1866 * Returns the index in "sn_var_vals" if found.
1867 * If found but not in "sn_var_vals" returns -1.
1868 * If not found returns -2.
1869 */
1870 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001871get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872{
1873 hashtab_T *ht;
1874 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001875 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001876 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 int idx;
1878
Bram Moolenaare3d46852020-08-29 13:39:17 +02001879 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001881 if (sid == current_sctx.sc_sid)
1882 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001883 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001884
1885 if (sav == NULL)
1886 return -2;
1887 idx = sav->sav_var_vals_idx;
1888 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1889 if (check_writable && sv->sv_const)
1890 semsg(_(e_readonlyvar), name);
1891 return idx;
1892 }
1893
1894 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 ht = &SCRIPT_VARS(sid);
1896 di = find_var_in_ht(ht, 0, name, TRUE);
1897 if (di == NULL)
1898 return -2;
1899
1900 // Now find the svar_T index in sn_var_vals.
1901 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1902 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001903 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 if (sv->sv_tv == &di->di_tv)
1905 {
1906 if (check_writable && sv->sv_const)
1907 semsg(_(e_readonlyvar), name);
1908 return idx;
1909 }
1910 }
1911 return -1;
1912}
1913
1914/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001915 * Find "name" in imported items of the current script or in "cctx" if not
1916 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 */
1918 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001919find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 int idx;
1922
Bram Moolenaare3d46852020-08-29 13:39:17 +02001923 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001924 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if (cctx != NULL)
1926 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1927 {
1928 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1929 + idx;
1930
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001931 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1932 : STRLEN(import->imp_name) == len
1933 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 return import;
1935 }
1936
Bram Moolenaarefa94442020-08-08 22:16:00 +02001937 return find_imported_in_script(name, len, current_sctx.sc_sid);
1938}
1939
1940 imported_T *
1941find_imported_in_script(char_u *name, size_t len, int sid)
1942{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001943 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001944 int idx;
1945
Bram Moolenaare3d46852020-08-29 13:39:17 +02001946 if (!SCRIPT_ID_VALID(sid))
1947 return NULL;
1948 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1950 {
1951 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + 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 return NULL;
1959}
1960
1961/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001962 * Free all imported variables.
1963 */
1964 static void
1965free_imported(cctx_T *cctx)
1966{
1967 int idx;
1968
1969 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1970 {
1971 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1972
1973 vim_free(import->imp_name);
1974 }
1975 ga_clear(&cctx->ctx_imports);
1976}
1977
1978/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001979 * Return TRUE if "p" points at a "#" but not at "#{".
1980 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001981 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001982vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001983{
1984 return p[0] == '#' && p[1] != '{';
1985}
1986
1987/*
1988 * Return a pointer to the next line that isn't empty or only contains a
1989 * comment. Skips over white space.
1990 * Returns NULL if there is none.
1991 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001992 char_u *
1993peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001994{
1995 int lnum = cctx->ctx_lnum;
1996
1997 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1998 {
1999 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002000 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002001
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002002 // ignore NULLs inserted for continuation lines
2003 if (line != NULL)
2004 {
2005 p = skipwhite(line);
2006 if (*p != NUL && !vim9_comment_start(p))
2007 return p;
2008 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002009 }
2010 return NULL;
2011}
2012
2013/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002014 * Called when checking for a following operator at "arg". When the rest of
2015 * the line is empty or only a comment, peek the next line. If there is a next
2016 * line return a pointer to it and set "nextp".
2017 * Otherwise skip over white space.
2018 */
2019 static char_u *
2020may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2021{
2022 char_u *p = skipwhite(arg);
2023
2024 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002025 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002026 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002027 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002028 if (*nextp != NULL)
2029 return *nextp;
2030 }
2031 return p;
2032}
2033
2034/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002035 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002036 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002037 * Returns NULL when at the end.
2038 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002039 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002040next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002041{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002042 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002043
2044 do
2045 {
2046 ++cctx->ctx_lnum;
2047 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002048 {
2049 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002050 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002051 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002052 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002053 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002054 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002055 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002056 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002057 return line;
2058}
2059
2060/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002061 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002062 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002063 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2064 */
2065 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002066may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002067{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002068 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002069 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002070 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002071
2072 if (next == NULL)
2073 return FAIL;
2074 *arg = skipwhite(next);
2075 }
2076 return OK;
2077}
2078
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002079/*
2080 * Idem, and give an error when failed.
2081 */
2082 static int
2083may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2084{
2085 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2086 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002087 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002088 return FAIL;
2089 }
2090 return OK;
2091}
2092
2093
Bram Moolenaara5565e42020-05-09 15:44:01 +02002094// Structure passed between the compile_expr* functions to keep track of
2095// constants that have been parsed but for which no code was produced yet. If
2096// possible expressions on these constants are applied at compile time. If
2097// that is not possible, the code to push the constants needs to be generated
2098// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002099// Using 50 should be more than enough of 5 levels of ().
2100#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002101typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002102 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002103 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002104 int pp_is_const; // all generated code was constants, used for a
2105 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002106} ppconst_T;
2107
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002108static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002109static int compile_expr0(char_u **arg, cctx_T *cctx);
2110static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2111
Bram Moolenaara5565e42020-05-09 15:44:01 +02002112/*
2113 * Generate a PUSH instruction for "tv".
2114 * "tv" will be consumed or cleared.
2115 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2116 */
2117 static int
2118generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2119{
2120 if (tv != NULL)
2121 {
2122 switch (tv->v_type)
2123 {
2124 case VAR_UNKNOWN:
2125 break;
2126 case VAR_BOOL:
2127 generate_PUSHBOOL(cctx, tv->vval.v_number);
2128 break;
2129 case VAR_SPECIAL:
2130 generate_PUSHSPEC(cctx, tv->vval.v_number);
2131 break;
2132 case VAR_NUMBER:
2133 generate_PUSHNR(cctx, tv->vval.v_number);
2134 break;
2135#ifdef FEAT_FLOAT
2136 case VAR_FLOAT:
2137 generate_PUSHF(cctx, tv->vval.v_float);
2138 break;
2139#endif
2140 case VAR_BLOB:
2141 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2142 tv->vval.v_blob = NULL;
2143 break;
2144 case VAR_STRING:
2145 generate_PUSHS(cctx, tv->vval.v_string);
2146 tv->vval.v_string = NULL;
2147 break;
2148 default:
2149 iemsg("constant type not supported");
2150 clear_tv(tv);
2151 return FAIL;
2152 }
2153 tv->v_type = VAR_UNKNOWN;
2154 }
2155 return OK;
2156}
2157
2158/*
2159 * Generate code for any ppconst entries.
2160 */
2161 static int
2162generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2163{
2164 int i;
2165 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002166 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002167
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002168 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002169 for (i = 0; i < ppconst->pp_used; ++i)
2170 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2171 ret = FAIL;
2172 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002173 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002174 return ret;
2175}
2176
2177/*
2178 * Clear ppconst constants. Used when failing.
2179 */
2180 static void
2181clear_ppconst(ppconst_T *ppconst)
2182{
2183 int i;
2184
2185 for (i = 0; i < ppconst->pp_used; ++i)
2186 clear_tv(&ppconst->pp_tv[i]);
2187 ppconst->pp_used = 0;
2188}
2189
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002190/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002191 * Generate an instruction to load script-local variable "name", without the
2192 * leading "s:".
2193 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 */
2195 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002196compile_load_scriptvar(
2197 cctx_T *cctx,
2198 char_u *name, // variable NUL terminated
2199 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002200 char_u **end, // end of variable
2201 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002203 scriptitem_T *si;
2204 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 imported_T *import;
2206
Bram Moolenaare3d46852020-08-29 13:39:17 +02002207 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2208 return FAIL;
2209 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002210 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002211 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002213 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002214 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2215 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 }
2217 if (idx >= 0)
2218 {
2219 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2220
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002221 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 current_sctx.sc_sid, idx, sv->sv_type);
2223 return OK;
2224 }
2225
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002226 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 if (import != NULL)
2228 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002229 if (import->imp_all)
2230 {
2231 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002232 char_u *exp_name;
2233 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002234 ufunc_T *ufunc;
2235 type_T *type;
2236
2237 // Used "import * as Name", need to lookup the member.
2238 if (*p != '.')
2239 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002240 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002241 return FAIL;
2242 }
2243 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002244 if (VIM_ISWHITE(*p))
2245 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002246 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002247 return FAIL;
2248 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002249
Bram Moolenaar1c991142020-07-04 13:15:31 +02002250 // isolate one name
2251 exp_name = p;
2252 while (eval_isnamec(*p))
2253 ++p;
2254 cc = *p;
2255 *p = NUL;
2256
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002257 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002258 *p = cc;
2259 p = skipwhite(p);
2260
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002261 // TODO: what if it is a function?
2262 if (idx < 0)
2263 return FAIL;
2264 *end = p;
2265
2266 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2267 import->imp_sid,
2268 idx,
2269 type);
2270 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002271 else if (import->imp_funcname != NULL)
2272 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002273 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002274 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2275 import->imp_sid,
2276 import->imp_var_vals_idx,
2277 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 return OK;
2279 }
2280
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002281 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002282 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 return FAIL;
2284}
2285
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002286 static int
2287generate_funcref(cctx_T *cctx, char_u *name)
2288{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002289 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002290
2291 if (ufunc == NULL)
2292 return FAIL;
2293
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002294 // Need to compile any default values to get the argument types.
2295 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2296 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2297 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002298 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002299}
2300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301/*
2302 * Compile a variable name into a load instruction.
2303 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002304 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 * When "error" is FALSE do not give an error when not found.
2306 */
2307 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002308compile_load(
2309 char_u **arg,
2310 char_u *end_arg,
2311 cctx_T *cctx,
2312 int is_expr,
2313 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314{
2315 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002316 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002317 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002319 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320
2321 if (*(*arg + 1) == ':')
2322 {
2323 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002324 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002325 {
2326 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002328 switch (**arg)
2329 {
2330 case 'g': isn_type = ISN_LOADGDICT; break;
2331 case 'w': isn_type = ISN_LOADWDICT; break;
2332 case 't': isn_type = ISN_LOADTDICT; break;
2333 case 'b': isn_type = ISN_LOADBDICT; break;
2334 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002335 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002336 goto theend;
2337 }
2338 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2339 goto theend;
2340 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 else
2343 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002344 isntype_T isn_type = ISN_DROP;
2345
2346 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2347 if (name == NULL)
2348 return FAIL;
2349
2350 switch (**arg)
2351 {
2352 case 'v': res = generate_LOADV(cctx, name, error);
2353 break;
2354 case 's': res = compile_load_scriptvar(cctx, name,
2355 NULL, NULL, error);
2356 break;
2357 case 'g': isn_type = ISN_LOADG; break;
2358 case 'w': isn_type = ISN_LOADW; break;
2359 case 't': isn_type = ISN_LOADT; break;
2360 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002361 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002362 goto theend;
2363 }
2364 if (isn_type != ISN_DROP)
2365 {
2366 // Global, Buffer-local, Window-local and Tabpage-local
2367 // variables can be defined later, thus we don't check if it
2368 // exists, give error at runtime.
2369 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 }
2372 }
2373 else
2374 {
2375 size_t len = end - *arg;
2376 int idx;
2377 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002378 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379
2380 name = vim_strnsave(*arg, end - *arg);
2381 if (name == NULL)
2382 return FAIL;
2383
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002384 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002386 if (!gen_load_outer)
2387 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 }
2389 else
2390 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002391 lvar_T *lvar = lookup_local(*arg, len, cctx);
2392
2393 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002395 type = lvar->lv_type;
2396 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002397 if (lvar->lv_from_outer)
2398 gen_load_outer = TRUE;
2399 else
2400 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 }
2402 else
2403 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002404 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002405 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002406 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002407 || find_imported(name, 0, cctx) != NULL)
2408 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002409
Bram Moolenaar0f769812020-09-12 18:32:34 +02002410 // When evaluating an expression and the name starts with an
2411 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002412 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002413 if (res == FAIL && is_expr
2414 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002415 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 }
2417 }
2418 if (gen_load)
2419 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002420 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002421 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002422 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002423 cctx->ctx_outer_used = TRUE;
2424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 }
2426
2427 *arg = end;
2428
2429theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002430 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002431 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 vim_free(name);
2433 return res;
2434}
2435
2436/*
2437 * Compile the argument expressions.
2438 * "arg" points to just after the "(" and is advanced to after the ")"
2439 */
2440 static int
2441compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2442{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002443 char_u *p = *arg;
2444 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002445 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446
Bram Moolenaare6085c52020-04-12 20:19:16 +02002447 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002449 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2450 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002451 if (*p == ')')
2452 {
2453 *arg = p + 1;
2454 return OK;
2455 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002456 if (must_end)
2457 {
2458 semsg(_(e_missing_comma_before_argument_str), p);
2459 return FAIL;
2460 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002461
Bram Moolenaara5565e42020-05-09 15:44:01 +02002462 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 return FAIL;
2464 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002465
2466 if (*p != ',' && *skipwhite(p) == ',')
2467 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002468 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002469 p = skipwhite(p);
2470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002472 {
2473 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002474 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002475 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002476 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002477 else
2478 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002479 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002480 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002482failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002483 emsg(_(e_missing_close));
2484 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485}
2486
2487/*
2488 * Compile a function call: name(arg1, arg2)
2489 * "arg" points to "name", "arg + varlen" to the "(".
2490 * "argcount_init" is 1 for "value->method()"
2491 * Instructions:
2492 * EVAL arg1
2493 * EVAL arg2
2494 * BCALL / DCALL / UCALL
2495 */
2496 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002497compile_call(
2498 char_u **arg,
2499 size_t varlen,
2500 cctx_T *cctx,
2501 ppconst_T *ppconst,
2502 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503{
2504 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002505 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 int argcount = argcount_init;
2507 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002508 char_u fname_buf[FLEN_FIXED + 1];
2509 char_u *tofree = NULL;
2510 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002512 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002513 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514
Bram Moolenaara5565e42020-05-09 15:44:01 +02002515 // we can evaluate "has('name')" at compile time
2516 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2517 {
2518 char_u *s = skipwhite(*arg + varlen + 1);
2519 typval_T argvars[2];
2520
2521 argvars[0].v_type = VAR_UNKNOWN;
2522 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002523 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002524 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002525 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002526 s = skipwhite(s);
2527 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2528 {
2529 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2530
2531 *arg = s + 1;
2532 argvars[1].v_type = VAR_UNKNOWN;
2533 tv->v_type = VAR_NUMBER;
2534 tv->vval.v_number = 0;
2535 f_has(argvars, tv);
2536 clear_tv(&argvars[0]);
2537 ++ppconst->pp_used;
2538 return OK;
2539 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002540 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002541 }
2542
2543 if (generate_ppconst(cctx, ppconst) == FAIL)
2544 return FAIL;
2545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 if (varlen >= sizeof(namebuf))
2547 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002548 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 return FAIL;
2550 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002551 vim_strncpy(namebuf, *arg, varlen);
2552 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553
2554 *arg = skipwhite(*arg + varlen + 1);
2555 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002556 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557
Bram Moolenaara1773442020-08-12 15:21:22 +02002558 is_autoload = vim_strchr(name, '#') != NULL;
2559 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 {
2561 int idx;
2562
2563 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002564 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002566 {
2567 if (STRCMP(name, "add") == 0 && argcount == 2)
2568 {
2569 garray_T *stack = &cctx->ctx_type_stack;
2570 type_T *type = ((type_T **)stack->ga_data)[
2571 stack->ga_len - 2];
2572
2573 // TODO: also check for VAR_BLOB
2574 if (type->tt_type == VAR_LIST)
2575 {
2576 // inline "add(list, item)" so that the type can be checked
2577 res = generate_LISTAPPEND(cctx);
2578 idx = -1;
2579 }
2580 }
2581
2582 if (idx >= 0)
2583 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2584 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002585 else
2586 semsg(_(e_unknownfunc), namebuf);
2587 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 }
2589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002591 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002592 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002593 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002594 {
2595 res = generate_CALL(cctx, ufunc, argcount);
2596 goto theend;
2597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598
2599 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002600 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002601 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002603 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002604 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002605 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002606 garray_T *stack = &cctx->ctx_type_stack;
2607 type_T *type;
2608
2609 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2610 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002611 goto theend;
2612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613
Bram Moolenaar0f769812020-09-12 18:32:34 +02002614 // If we can find a global function by name generate the right call.
2615 if (ufunc != NULL)
2616 {
2617 res = generate_CALL(cctx, ufunc, argcount);
2618 goto theend;
2619 }
2620
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002621 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002622 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002623 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002624 res = generate_UCALL(cctx, name, argcount);
2625 else
2626 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002627
2628theend:
2629 vim_free(tofree);
2630 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631}
2632
2633// like NAMESPACE_CHAR but with 'a' and 'l'.
2634#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2635
2636/*
2637 * Find the end of a variable or function name. Unlike find_name_end() this
2638 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002639 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 * Return a pointer to just after the name. Equal to "arg" if there is no
2641 * valid name.
2642 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002643 static char_u *
2644to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645{
2646 char_u *p;
2647
2648 // Quick check for valid starting character.
2649 if (!eval_isnamec1(*arg))
2650 return arg;
2651
2652 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2653 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2654 // and can be used in slice "[n:]".
2655 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002656 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2658 break;
2659 return p;
2660}
2661
2662/*
2663 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002664 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 */
2666 char_u *
2667to_name_const_end(char_u *arg)
2668{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002669 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 typval_T rettv;
2671
2672 if (p == arg && *arg == '[')
2673 {
2674
2675 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002676 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 p = arg;
2678 }
2679 else if (p == arg && *arg == '#' && arg[1] == '{')
2680 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002681 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002683 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 p = arg;
2685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686
2687 return p;
2688}
2689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690/*
2691 * parse a list: [expr, expr]
2692 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002693 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 */
2695 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002696compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697{
2698 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002699 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002701 int is_const;
2702 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002704 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002706 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002707 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002708 semsg(_(e_list_end), *arg);
2709 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002710 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002711 if (*p == ',')
2712 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002713 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002714 return FAIL;
2715 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002716 if (*p == ']')
2717 {
2718 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002719 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002720 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002721 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002722 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002723 if (!is_const)
2724 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 ++count;
2726 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002727 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002729 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2730 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002731 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002732 return FAIL;
2733 }
2734 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002735 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 p = skipwhite(p);
2737 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002738 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002740 ppconst->pp_is_const = is_all_const;
2741 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742}
2743
2744/*
2745 * parse a lambda: {arg, arg -> expr}
2746 * "*arg" points to the '{'.
2747 */
2748 static int
2749compile_lambda(char_u **arg, cctx_T *cctx)
2750{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 typval_T rettv;
2752 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002753 evalarg_T evalarg;
2754
2755 CLEAR_FIELD(evalarg);
2756 evalarg.eval_flags = EVAL_EVALUATE;
2757 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758
2759 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002760 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002761 {
2762 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002764 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002767 ++ufunc->uf_refcount;
2768 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002769 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770
2771 // The function will have one line: "return {expr}".
2772 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002773 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002775 clear_evalarg(&evalarg, NULL);
2776
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002777 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002778 {
2779 // The return type will now be known.
2780 set_function_type(ufunc);
2781
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002782 // The function reference count will be 1. When the ISN_FUNCREF
2783 // instruction is deleted the reference count is decremented and the
2784 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002785 return generate_FUNCREF(cctx, ufunc);
2786 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002787
2788 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 return FAIL;
2790}
2791
2792/*
2793 * Compile a lamda call: expr->{lambda}(args)
2794 * "arg" points to the "{".
2795 */
2796 static int
2797compile_lambda_call(char_u **arg, cctx_T *cctx)
2798{
2799 ufunc_T *ufunc;
2800 typval_T rettv;
2801 int argcount = 1;
2802 int ret = FAIL;
2803
2804 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002805 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 return FAIL;
2807
2808 if (**arg != '(')
2809 {
2810 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002811 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 else
2813 semsg(_(e_missing_paren), "lambda");
2814 clear_tv(&rettv);
2815 return FAIL;
2816 }
2817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 ufunc = rettv.vval.v_partial->pt_func;
2819 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002820 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002821 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002822
Bram Moolenaara05e5242020-09-19 18:19:19 +02002823 // The function will have one line: "return {expr}". Compile it into
2824 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002825 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826
2827 // compile the arguments
2828 *arg = skipwhite(*arg + 1);
2829 if (compile_arguments(arg, cctx, &argcount) == OK)
2830 // call the compiled function
2831 ret = generate_CALL(cctx, ufunc, argcount);
2832
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002833 if (ret == FAIL)
2834 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 return ret;
2836}
2837
2838/*
2839 * parse a dict: {'key': val} or #{key: val}
2840 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002841 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 */
2843 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002844compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845{
2846 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002847 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 int count = 0;
2849 dict_T *d = dict_alloc();
2850 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002851 char_u *whitep = *arg;
2852 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002853 int is_const;
2854 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855
2856 if (d == NULL)
2857 return FAIL;
2858 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002859 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 {
2861 char_u *key = NULL;
2862
Bram Moolenaar23c55272020-06-21 16:58:13 +02002863 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002864 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002865 *arg = NULL;
2866 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002867 }
2868
2869 if (**arg == '}')
2870 break;
2871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 if (literal)
2873 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002874 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875
Bram Moolenaar2c330432020-04-13 14:41:35 +02002876 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002878 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 return FAIL;
2880 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002881 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (generate_PUSHS(cctx, key) == FAIL)
2883 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002884 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 }
2886 else
2887 {
2888 isn_T *isn;
2889
Bram Moolenaara5565e42020-05-09 15:44:01 +02002890 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2893 if (isn->isn_type == ISN_PUSHS)
2894 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002895 else
2896 {
2897 type_T *keytype = ((type_T **)stack->ga_data)
2898 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002899 if (need_type(keytype, &t_string, -1, cctx,
2900 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002901 return FAIL;
2902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 }
2904
2905 // Check for duplicate keys, if using string keys.
2906 if (key != NULL)
2907 {
2908 item = dict_find(d, key, -1);
2909 if (item != NULL)
2910 {
2911 semsg(_(e_duplicate_key), key);
2912 goto failret;
2913 }
2914 item = dictitem_alloc(key);
2915 if (item != NULL)
2916 {
2917 item->di_tv.v_type = VAR_UNKNOWN;
2918 item->di_tv.v_lock = 0;
2919 if (dict_add(d, item) == FAIL)
2920 dictitem_free(item);
2921 }
2922 }
2923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 if (**arg != ':')
2925 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002926 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002927 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002928 else
2929 semsg(_(e_missing_dict_colon), *arg);
2930 return FAIL;
2931 }
2932 whitep = *arg + 1;
2933 if (!IS_WHITE_OR_NUL(*whitep))
2934 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002935 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 return FAIL;
2937 }
2938
2939 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002940 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002941 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002942 *arg = NULL;
2943 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002944 }
2945
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002946 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002948 if (!is_const)
2949 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 ++count;
2951
Bram Moolenaar2c330432020-04-13 14:41:35 +02002952 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002953 *arg = skipwhite(*arg);
2954 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002955 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002956 *arg = NULL;
2957 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 if (**arg == '}')
2960 break;
2961 if (**arg != ',')
2962 {
2963 semsg(_(e_missing_dict_comma), *arg);
2964 goto failret;
2965 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002966 if (IS_WHITE_OR_NUL(*whitep))
2967 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002968 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002969 return FAIL;
2970 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002971 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 *arg = skipwhite(*arg + 1);
2973 }
2974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 *arg = *arg + 1;
2976
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002977 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002978 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002979 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002980 *arg += STRLEN(*arg);
2981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002983 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 return generate_NEWDICT(cctx, count);
2985
2986failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002987 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002988 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002989 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002990 *arg = (char_u *)"";
2991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 dict_unref(d);
2993 return FAIL;
2994}
2995
2996/*
2997 * Compile "&option".
2998 */
2999 static int
3000compile_get_option(char_u **arg, cctx_T *cctx)
3001{
3002 typval_T rettv;
3003 char_u *start = *arg;
3004 int ret;
3005
3006 // parse the option and get the current value to get the type.
3007 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003008 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 if (ret == OK)
3010 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003011 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 char_u *name = vim_strnsave(start, *arg - start);
3013 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3014
3015 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3016 vim_free(name);
3017 }
3018 clear_tv(&rettv);
3019
3020 return ret;
3021}
3022
3023/*
3024 * Compile "$VAR".
3025 */
3026 static int
3027compile_get_env(char_u **arg, cctx_T *cctx)
3028{
3029 char_u *start = *arg;
3030 int len;
3031 int ret;
3032 char_u *name;
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 ++*arg;
3035 len = get_env_len(arg);
3036 if (len == 0)
3037 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003038 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 return FAIL;
3040 }
3041
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003042 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 name = vim_strnsave(start, len + 1);
3044 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3045 vim_free(name);
3046 return ret;
3047}
3048
3049/*
3050 * Compile "@r".
3051 */
3052 static int
3053compile_get_register(char_u **arg, cctx_T *cctx)
3054{
3055 int ret;
3056
3057 ++*arg;
3058 if (**arg == NUL)
3059 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003060 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 return FAIL;
3062 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003063 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 {
3065 emsg_invreg(**arg);
3066 return FAIL;
3067 }
3068 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3069 ++*arg;
3070 return ret;
3071}
3072
3073/*
3074 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003075 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 */
3077 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003078apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003080 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081
3082 // this works from end to start
3083 while (p > start)
3084 {
3085 --p;
3086 if (*p == '-' || *p == '+')
3087 {
3088 // only '-' has an effect, for '+' we only check the type
3089#ifdef FEAT_FLOAT
3090 if (rettv->v_type == VAR_FLOAT)
3091 {
3092 if (*p == '-')
3093 rettv->vval.v_float = -rettv->vval.v_float;
3094 }
3095 else
3096#endif
3097 {
3098 varnumber_T val;
3099 int error = FALSE;
3100
3101 // tv_get_number_chk() accepts a string, but we don't want that
3102 // here
3103 if (check_not_string(rettv) == FAIL)
3104 return FAIL;
3105 val = tv_get_number_chk(rettv, &error);
3106 clear_tv(rettv);
3107 if (error)
3108 return FAIL;
3109 if (*p == '-')
3110 val = -val;
3111 rettv->v_type = VAR_NUMBER;
3112 rettv->vval.v_number = val;
3113 }
3114 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003115 else if (numeric_only)
3116 {
3117 ++p;
3118 break;
3119 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003120 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 {
3122 int v = tv2bool(rettv);
3123
3124 // '!' is permissive in the type.
3125 clear_tv(rettv);
3126 rettv->v_type = VAR_BOOL;
3127 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3128 }
3129 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003130 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 return OK;
3132}
3133
3134/*
3135 * Recognize v: variables that are constants and set "rettv".
3136 */
3137 static void
3138get_vim_constant(char_u **arg, typval_T *rettv)
3139{
3140 if (STRNCMP(*arg, "v:true", 6) == 0)
3141 {
3142 rettv->v_type = VAR_BOOL;
3143 rettv->vval.v_number = VVAL_TRUE;
3144 *arg += 6;
3145 }
3146 else if (STRNCMP(*arg, "v:false", 7) == 0)
3147 {
3148 rettv->v_type = VAR_BOOL;
3149 rettv->vval.v_number = VVAL_FALSE;
3150 *arg += 7;
3151 }
3152 else if (STRNCMP(*arg, "v:null", 6) == 0)
3153 {
3154 rettv->v_type = VAR_SPECIAL;
3155 rettv->vval.v_number = VVAL_NULL;
3156 *arg += 6;
3157 }
3158 else if (STRNCMP(*arg, "v:none", 6) == 0)
3159 {
3160 rettv->v_type = VAR_SPECIAL;
3161 rettv->vval.v_number = VVAL_NONE;
3162 *arg += 6;
3163 }
3164}
3165
Bram Moolenaar696ba232020-07-29 21:20:41 +02003166 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003167get_compare_type(char_u *p, int *len, int *type_is)
3168{
3169 exptype_T type = EXPR_UNKNOWN;
3170 int i;
3171
3172 switch (p[0])
3173 {
3174 case '=': if (p[1] == '=')
3175 type = EXPR_EQUAL;
3176 else if (p[1] == '~')
3177 type = EXPR_MATCH;
3178 break;
3179 case '!': if (p[1] == '=')
3180 type = EXPR_NEQUAL;
3181 else if (p[1] == '~')
3182 type = EXPR_NOMATCH;
3183 break;
3184 case '>': if (p[1] != '=')
3185 {
3186 type = EXPR_GREATER;
3187 *len = 1;
3188 }
3189 else
3190 type = EXPR_GEQUAL;
3191 break;
3192 case '<': if (p[1] != '=')
3193 {
3194 type = EXPR_SMALLER;
3195 *len = 1;
3196 }
3197 else
3198 type = EXPR_SEQUAL;
3199 break;
3200 case 'i': if (p[1] == 's')
3201 {
3202 // "is" and "isnot"; but not a prefix of a name
3203 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3204 *len = 5;
3205 i = p[*len];
3206 if (!isalnum(i) && i != '_')
3207 {
3208 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3209 *type_is = TRUE;
3210 }
3211 }
3212 break;
3213 }
3214 return type;
3215}
3216
3217/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003219 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 */
3221 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003222compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003224 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225
3226 // this works from end to start
3227 while (p > start)
3228 {
3229 --p;
3230 if (*p == '-' || *p == '+')
3231 {
3232 int negate = *p == '-';
3233 isn_T *isn;
3234
3235 // TODO: check type
3236 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3237 {
3238 --p;
3239 if (*p == '-')
3240 negate = !negate;
3241 }
3242 // only '-' has an effect, for '+' we only check the type
3243 if (negate)
3244 isn = generate_instr(cctx, ISN_NEGATENR);
3245 else
3246 isn = generate_instr(cctx, ISN_CHECKNR);
3247 if (isn == NULL)
3248 return FAIL;
3249 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003250 else if (numeric_only)
3251 {
3252 ++p;
3253 break;
3254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 else
3256 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003257 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003259 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003261 if (p[-1] == '!')
3262 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 }
3265 if (generate_2BOOL(cctx, invert) == FAIL)
3266 return FAIL;
3267 }
3268 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003269 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 return OK;
3271}
3272
3273/*
3274 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003275 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 */
3277 static int
3278compile_subscript(
3279 char_u **arg,
3280 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003281 char_u *start_leader,
3282 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003283 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003285 char_u *name_start = *end_leader;
3286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 for (;;)
3288 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003289 char_u *p = skipwhite(*arg);
3290
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003291 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003292 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003293 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003294
3295 // If a following line starts with "->{" or "->X" advance to that
3296 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003297 // Also if a following line starts with ".x".
3298 if (next != NULL &&
3299 ((next[0] == '-' && next[1] == '>'
3300 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003301 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003302 {
3303 next = next_line_from_context(cctx, TRUE);
3304 if (next == NULL)
3305 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003306 *arg = next;
3307 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003308 }
3309 }
3310
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003311 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3312 // not a function call.
3313 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003315 garray_T *stack = &cctx->ctx_type_stack;
3316 type_T *type;
3317 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003319 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003320 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003321 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003324 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3325
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003326 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3328 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003329 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 return FAIL;
3331 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003332 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003334 char_u *pstart = p;
3335
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003336 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003337 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003338 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 // something->method()
3341 // Apply the '!', '-' and '+' first:
3342 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003343 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003346 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003347 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003348 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 if (**arg == '{')
3350 {
3351 // lambda call: list->{lambda}
3352 if (compile_lambda_call(arg, cctx) == FAIL)
3353 return FAIL;
3354 }
3355 else
3356 {
3357 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003358 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003359 if (!eval_isnamec1(*p))
3360 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003361 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003362 return FAIL;
3363 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003364 if (ASCII_ISALPHA(*p) && p[1] == ':')
3365 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003366 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 ;
3368 if (*p != '(')
3369 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003370 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 return FAIL;
3372 }
3373 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003374 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 return FAIL;
3376 }
3377 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003378 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003380 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003381 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003382 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003383 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003384 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003385
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003386 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003387 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003388 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003389 // TODO: blob index
3390 // TODO: more arguments
3391 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003392 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003393 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003394 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003395
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003396 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003397 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003398 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003399 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003400 if (**arg == ':')
3401 // missing first index is equal to zero
3402 generate_PUSHNR(cctx, 0);
3403 else
3404 {
3405 if (compile_expr0(arg, cctx) == FAIL)
3406 return FAIL;
3407 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3408 return FAIL;
3409 *arg = skipwhite(*arg);
3410 }
3411 if (**arg == ':')
3412 {
3413 *arg = skipwhite(*arg + 1);
3414 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3415 return FAIL;
3416 if (**arg == ']')
3417 // missing second index is equal to end of string
3418 generate_PUSHNR(cctx, -1);
3419 else
3420 {
3421 if (compile_expr0(arg, cctx) == FAIL)
3422 return FAIL;
3423 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3424 return FAIL;
3425 *arg = skipwhite(*arg);
3426 }
3427 is_slice = TRUE;
3428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429
3430 if (**arg != ']')
3431 {
3432 emsg(_(e_missbrac));
3433 return FAIL;
3434 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003435 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003437 // We can index a list and a dict. If we don't know the type
3438 // we can use the index value type.
3439 // TODO: If we don't know use an instruction to figure it out at
3440 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003441 typep = ((type_T **)stack->ga_data) + stack->ga_len
3442 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003443 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003444 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3445 // If the index is a string, the variable must be a Dict.
3446 if (*typep == &t_any && valtype == &t_string)
3447 vtype = VAR_DICT;
3448 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003449 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003450 if (need_type(valtype, &t_number, -1, cctx,
3451 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003452 return FAIL;
3453 if (is_slice)
3454 {
3455 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003456 if (need_type(valtype, &t_number, -2, cctx,
3457 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003458 return FAIL;
3459 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003460 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003461
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003462 if (vtype == VAR_DICT)
3463 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003464 if (is_slice)
3465 {
3466 emsg(_(e_cannot_slice_dictionary));
3467 return FAIL;
3468 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003469 if ((*typep)->tt_type == VAR_DICT)
3470 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003471 else
3472 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003473 if (need_type(*typep, &t_dict_any, -2, cctx,
3474 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003475 return FAIL;
3476 *typep = &t_any;
3477 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003478 if (may_generate_2STRING(-1, cctx) == FAIL)
3479 return FAIL;
3480 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3481 return FAIL;
3482 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003483 else if (vtype == VAR_STRING)
3484 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003485 *typep = &t_string;
3486 if ((is_slice
3487 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3488 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003489 return FAIL;
3490 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003491 else if (vtype == VAR_BLOB)
3492 {
3493 emsg("Sorry, blob index and slice not implemented yet");
3494 return FAIL;
3495 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003496 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003497 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003498 if (is_slice)
3499 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003500 if (generate_instr_drop(cctx,
3501 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3502 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003503 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003504 }
Bram Moolenaared591872020-08-15 22:14:53 +02003505 else
3506 {
3507 if ((*typep)->tt_type == VAR_LIST)
3508 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003509 if (generate_instr_drop(cctx,
3510 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3511 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003512 return FAIL;
3513 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003514 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515 else
3516 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003517 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003518 return FAIL;
3519 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003521 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003523 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003524 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003525 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003526 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003527
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003528 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003529 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003530 {
3531 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003532 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003533 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003534 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003535 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 while (eval_isnamec(*p))
3537 MB_PTR_ADV(p);
3538 if (p == *arg)
3539 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003540 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 return FAIL;
3542 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003543 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 return FAIL;
3545 *arg = p;
3546 }
3547 else
3548 break;
3549 }
3550
3551 // TODO - see handle_subscript():
3552 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3553 // Don't do this when "Func" is already a partial that was bound
3554 // explicitly (pt_auto is FALSE).
3555
3556 return OK;
3557}
3558
3559/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003560 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3561 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003563 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003564 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003565 *
3566 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 */
3568
3569/*
3570 * number number constant
3571 * 0zFFFFFFFF Blob constant
3572 * "string" string constant
3573 * 'string' literal string constant
3574 * &option-name option value
3575 * @r register contents
3576 * identifier variable value
3577 * function() function call
3578 * $VAR environment variable
3579 * (expression) nested expression
3580 * [expr, expr] List
3581 * {key: val, key: val} Dictionary
3582 * #{key: val, key: val} Dictionary with literal keys
3583 *
3584 * Also handle:
3585 * ! in front logical NOT
3586 * - in front unary minus
3587 * + in front unary plus (ignored)
3588 * trailing (arg) funcref/partial call
3589 * trailing [] subscript in String or List
3590 * trailing .name entry in Dictionary
3591 * trailing ->name() method call
3592 */
3593 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003594compile_expr7(
3595 char_u **arg,
3596 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003597 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 char_u *start_leader, *end_leader;
3600 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003601 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003602 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003604 ppconst->pp_is_const = FALSE;
3605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 /*
3607 * Skip '!', '-' and '+' characters. They are handled later.
3608 */
3609 start_leader = *arg;
3610 while (**arg == '!' || **arg == '-' || **arg == '+')
3611 *arg = skipwhite(*arg + 1);
3612 end_leader = *arg;
3613
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003614 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 switch (**arg)
3616 {
3617 /*
3618 * Number constant.
3619 */
3620 case '0': // also for blob starting with 0z
3621 case '1':
3622 case '2':
3623 case '3':
3624 case '4':
3625 case '5':
3626 case '6':
3627 case '7':
3628 case '8':
3629 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003630 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003632 // Apply "-" and "+" just before the number now, right to
3633 // left. Matters especially when "->" follows. Stops at
3634 // '!'.
3635 if (apply_leader(rettv, TRUE,
3636 start_leader, &end_leader) == FAIL)
3637 {
3638 clear_tv(rettv);
3639 return FAIL;
3640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 break;
3642
3643 /*
3644 * String constant: "string".
3645 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003646 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 return FAIL;
3648 break;
3649
3650 /*
3651 * Literal string constant: 'str''ing'.
3652 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003653 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 return FAIL;
3655 break;
3656
3657 /*
3658 * Constant Vim variable.
3659 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003660 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 ret = NOTDONE;
3662 break;
3663
3664 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003665 * "true" constant
3666 */
3667 case 't': if (STRNCMP(*arg, "true", 4) == 0
3668 && !eval_isnamec((*arg)[4]))
3669 {
3670 *arg += 4;
3671 rettv->v_type = VAR_BOOL;
3672 rettv->vval.v_number = VVAL_TRUE;
3673 }
3674 else
3675 ret = NOTDONE;
3676 break;
3677
3678 /*
3679 * "false" constant
3680 */
3681 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3682 && !eval_isnamec((*arg)[5]))
3683 {
3684 *arg += 5;
3685 rettv->v_type = VAR_BOOL;
3686 rettv->vval.v_number = VVAL_FALSE;
3687 }
3688 else
3689 ret = NOTDONE;
3690 break;
3691
3692 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 * List: [expr, expr]
3694 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003695 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 break;
3697
3698 /*
3699 * Dictionary: #{key: val, key: val}
3700 */
3701 case '#': if ((*arg)[1] == '{')
3702 {
3703 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003704 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 }
3706 else
3707 ret = NOTDONE;
3708 break;
3709
3710 /*
3711 * Lambda: {arg, arg -> expr}
3712 * Dictionary: {'key': val, 'key': val}
3713 */
3714 case '{': {
3715 char_u *start = skipwhite(*arg + 1);
3716
3717 // Find out what comes after the arguments.
3718 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003719 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 if (ret != FAIL && *start == '>')
3721 ret = compile_lambda(arg, cctx);
3722 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003723 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 }
3725 break;
3726
3727 /*
3728 * Option value: &name
3729 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003730 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3731 return FAIL;
3732 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 break;
3734
3735 /*
3736 * Environment variable: $VAR.
3737 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003738 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3739 return FAIL;
3740 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 break;
3742
3743 /*
3744 * Register contents: @r.
3745 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003746 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3747 return FAIL;
3748 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 break;
3750 /*
3751 * nested expression: (expression).
3752 */
3753 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003754
3755 // recursive!
3756 if (ppconst->pp_used <= PPSIZE - 10)
3757 {
3758 ret = compile_expr1(arg, cctx, ppconst);
3759 }
3760 else
3761 {
3762 // Not enough space in ppconst, flush constants.
3763 if (generate_ppconst(cctx, ppconst) == FAIL)
3764 return FAIL;
3765 ret = compile_expr0(arg, cctx);
3766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 *arg = skipwhite(*arg);
3768 if (**arg == ')')
3769 ++*arg;
3770 else if (ret == OK)
3771 {
3772 emsg(_(e_missing_close));
3773 ret = FAIL;
3774 }
3775 break;
3776
3777 default: ret = NOTDONE;
3778 break;
3779 }
3780 if (ret == FAIL)
3781 return FAIL;
3782
Bram Moolenaar1c747212020-05-09 18:28:34 +02003783 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003785 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003786 clear_tv(rettv);
3787 else
3788 // A constant expression can possibly be handled compile time,
3789 // return the value instead of generating code.
3790 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 }
3792 else if (ret == NOTDONE)
3793 {
3794 char_u *p;
3795 int r;
3796
3797 if (!eval_isnamec1(**arg))
3798 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003799 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 return FAIL;
3801 }
3802
3803 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003804 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003806 {
3807 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003810 {
3811 if (generate_ppconst(cctx, ppconst) == FAIL)
3812 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003813 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003814 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 if (r == FAIL)
3816 return FAIL;
3817 }
3818
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003819 // Handle following "[]", ".member", etc.
3820 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003821 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003822 ppconst) == FAIL)
3823 return FAIL;
3824 if (ppconst->pp_used > 0)
3825 {
3826 // apply the '!', '-' and '+' before the constant
3827 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003828 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003829 return FAIL;
3830 return OK;
3831 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003832 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003834 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835}
3836
3837/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003838 * Give the "white on both sides" error, taking the operator from "p[len]".
3839 */
3840 void
3841error_white_both(char_u *op, int len)
3842{
3843 char_u buf[10];
3844
3845 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003846 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003847}
3848
3849/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003850 * <type>expr7: runtime type check / conversion
3851 */
3852 static int
3853compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3854{
3855 type_T *want_type = NULL;
3856
3857 // Recognize <type>
3858 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3859 {
3860 int called_emsg_before = called_emsg;
3861
3862 ++*arg;
3863 want_type = parse_type(arg, cctx->ctx_type_list);
3864 if (called_emsg != called_emsg_before)
3865 return FAIL;
3866
3867 if (**arg != '>')
3868 {
3869 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003870 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003871 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003872 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003873 return FAIL;
3874 }
3875 ++*arg;
3876 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3877 return FAIL;
3878 }
3879
3880 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3881 return FAIL;
3882
3883 if (want_type != NULL)
3884 {
3885 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003886 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003887
Bram Moolenaard1103582020-08-14 22:44:25 +02003888 generate_ppconst(cctx, ppconst);
3889 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003890 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003891 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003892 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003893 return FAIL;
3894 }
3895 }
3896
3897 return OK;
3898}
3899
3900/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 * * number multiplication
3902 * / number division
3903 * % number modulo
3904 */
3905 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003906compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907{
3908 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003909 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003910 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003912 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003913 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 return FAIL;
3915
3916 /*
3917 * Repeat computing, until no "*", "/" or "%" is following.
3918 */
3919 for (;;)
3920 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003921 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 if (*op != '*' && *op != '/' && *op != '%')
3923 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003924 if (next != NULL)
3925 {
3926 *arg = next_line_from_context(cctx, TRUE);
3927 op = skipwhite(*arg);
3928 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003929
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003930 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003932 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003933 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 }
3935 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003936 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003937 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003939 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003940 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003942
3943 if (ppconst->pp_used == ppconst_used + 2
3944 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3945 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003946 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003947 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3948 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003951 // both are numbers: compute the result
3952 switch (*op)
3953 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003954 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003955 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003956 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003957 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003958 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003959 break;
3960 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003961 tv1->vval.v_number = res;
3962 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003963 }
3964 else
3965 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003966 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003967 generate_two_op(cctx, op);
3968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 }
3970
3971 return OK;
3972}
3973
3974/*
3975 * + number addition
3976 * - number subtraction
3977 * .. string concatenation
3978 */
3979 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003980compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981{
3982 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003983 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003985 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
3987 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003988 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 return FAIL;
3990
3991 /*
3992 * Repeat computing, until no "+", "-" or ".." is following.
3993 */
3994 for (;;)
3995 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003996 op = may_peek_next_line(cctx, *arg, &next);
3997 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 break;
3999 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004000 if (next != NULL)
4001 {
4002 *arg = next_line_from_context(cctx, TRUE);
4003 op = skipwhite(*arg);
4004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004006 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004008 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004009 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 }
4011
4012 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004013 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004014 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004016 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004017 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 return FAIL;
4019
Bram Moolenaara5565e42020-05-09 15:44:01 +02004020 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004021 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004022 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4023 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4024 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4025 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004027 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4028 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004029
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004030 // concat/subtract/add constant numbers
4031 if (*op == '+')
4032 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4033 else if (*op == '-')
4034 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4035 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004036 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004037 // concatenate constant strings
4038 char_u *s1 = tv1->vval.v_string;
4039 char_u *s2 = tv2->vval.v_string;
4040 size_t len1 = STRLEN(s1);
4041
4042 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4043 if (tv1->vval.v_string == NULL)
4044 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004045 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004046 return FAIL;
4047 }
4048 mch_memmove(tv1->vval.v_string, s1, len1);
4049 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004050 vim_free(s1);
4051 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004052 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004053 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 }
4055 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004056 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004058 if (*op == '.')
4059 {
4060 if (may_generate_2STRING(-2, cctx) == FAIL
4061 || may_generate_2STRING(-1, cctx) == FAIL)
4062 return FAIL;
4063 generate_instr_drop(cctx, ISN_CONCAT, 1);
4064 }
4065 else
4066 generate_two_op(cctx, op);
4067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 }
4069
4070 return OK;
4071}
4072
4073/*
4074 * expr5a == expr5b
4075 * expr5a =~ expr5b
4076 * expr5a != expr5b
4077 * expr5a !~ expr5b
4078 * expr5a > expr5b
4079 * expr5a >= expr5b
4080 * expr5a < expr5b
4081 * expr5a <= expr5b
4082 * expr5a is expr5b
4083 * expr5a isnot expr5b
4084 *
4085 * Produces instructions:
4086 * EVAL expr5a Push result of "expr5a"
4087 * EVAL expr5b Push result of "expr5b"
4088 * COMPARE one of the compare instructions
4089 */
4090 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092{
4093 exptype_T type = EXPR_UNKNOWN;
4094 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004095 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004098 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099
4100 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004101 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 return FAIL;
4103
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004104 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004105 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106
4107 /*
4108 * If there is a comparative operator, use it.
4109 */
4110 if (type != EXPR_UNKNOWN)
4111 {
4112 int ic = FALSE; // Default: do not ignore case
4113
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004114 if (next != NULL)
4115 {
4116 *arg = next_line_from_context(cctx, TRUE);
4117 p = skipwhite(*arg);
4118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 if (type_is && (p[len] == '?' || p[len] == '#'))
4120 {
4121 semsg(_(e_invexpr2), *arg);
4122 return FAIL;
4123 }
4124 // extra question mark appended: ignore case
4125 if (p[len] == '?')
4126 {
4127 ic = TRUE;
4128 ++len;
4129 }
4130 // extra '#' appended: match case (ignored)
4131 else if (p[len] == '#')
4132 ++len;
4133 // nothing appended: match case
4134
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004135 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004137 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004138 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 }
4140
4141 // get the second variable
4142 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004143 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004144 return FAIL;
4145
Bram Moolenaara5565e42020-05-09 15:44:01 +02004146 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 return FAIL;
4148
Bram Moolenaara5565e42020-05-09 15:44:01 +02004149 if (ppconst->pp_used == ppconst_used + 2)
4150 {
4151 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4152 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4153 int ret;
4154
4155 // Both sides are a constant, compute the result now.
4156 // First check for a valid combination of types, this is more
4157 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004158 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004159 ret = FAIL;
4160 else
4161 {
4162 ret = typval_compare(tv1, tv2, type, ic);
4163 tv1->v_type = VAR_BOOL;
4164 tv1->vval.v_number = tv1->vval.v_number
4165 ? VVAL_TRUE : VVAL_FALSE;
4166 clear_tv(tv2);
4167 --ppconst->pp_used;
4168 }
4169 return ret;
4170 }
4171
4172 generate_ppconst(cctx, ppconst);
4173 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 }
4175
4176 return OK;
4177}
4178
Bram Moolenaar7f141552020-05-09 17:35:53 +02004179static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181/*
4182 * Compile || or &&.
4183 */
4184 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004185compile_and_or(
4186 char_u **arg,
4187 cctx_T *cctx,
4188 char *op,
4189 ppconst_T *ppconst,
4190 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004192 char_u *next;
4193 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 int opchar = *op;
4195
4196 if (p[0] == opchar && p[1] == opchar)
4197 {
4198 garray_T *instr = &cctx->ctx_instr;
4199 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004200 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004201 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202
4203 /*
4204 * Repeat until there is no following "||" or "&&"
4205 */
4206 ga_init2(&end_ga, sizeof(int), 10);
4207 while (p[0] == opchar && p[1] == opchar)
4208 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004209 if (next != NULL)
4210 {
4211 *arg = next_line_from_context(cctx, TRUE);
4212 p = skipwhite(*arg);
4213 }
4214
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004215 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4216 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004217 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004218 return FAIL;
4219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004221 // TODO: use ppconst if the value is a constant and check
4222 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 generate_ppconst(cctx, ppconst);
4224
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004225 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4226 all_bool_values = FALSE;
4227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 if (ga_grow(&end_ga, 1) == FAIL)
4229 {
4230 ga_clear(&end_ga);
4231 return FAIL;
4232 }
4233 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4234 ++end_ga.ga_len;
4235 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004236 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237
4238 // eval the next expression
4239 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004240 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004241 return FAIL;
4242
Bram Moolenaara5565e42020-05-09 15:44:01 +02004243 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4244 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 {
4246 ga_clear(&end_ga);
4247 return FAIL;
4248 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004249
4250 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004252 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253
4254 // Fill in the end label in all jumps.
4255 while (end_ga.ga_len > 0)
4256 {
4257 isn_T *isn;
4258
4259 --end_ga.ga_len;
4260 isn = ((isn_T *)instr->ga_data)
4261 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4262 isn->isn_arg.jump.jump_where = instr->ga_len;
4263 }
4264 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004265
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004266 // The resulting type is converted to bool if needed.
4267 if (!all_bool_values)
4268 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * expr4a && expr4a && expr4a logical AND
4276 *
4277 * Produces instructions:
4278 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004279 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004281 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004283 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 * end:
4285 */
4286 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004289 int ppconst_used = ppconst->pp_used;
4290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004292 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 return FAIL;
4294
4295 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004296 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297}
4298
4299/*
4300 * expr3a || expr3b || expr3c logical OR
4301 *
4302 * Produces instructions:
4303 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004304 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004306 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004308 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 * end:
4310 */
4311 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004312compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004314 int ppconst_used = ppconst->pp_used;
4315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004317 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 return FAIL;
4319
4320 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004321 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322}
4323
4324/*
4325 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004327 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 * JUMP_IF_FALSE alt jump if false
4329 * EVAL expr1a
4330 * JUMP_ALWAYS end
4331 * alt: EVAL expr1b
4332 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004333 *
4334 * Toplevel expression: expr2 ?? expr1
4335 * Produces instructions:
4336 * EVAL expr2 Push result of "expr2"
4337 * JUMP_AND_KEEP_IF_TRUE end jump if true
4338 * EVAL expr1
4339 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 */
4341 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343{
4344 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004345 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004346 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
Bram Moolenaar3988f642020-08-27 22:43:03 +02004348 // Ignore all kinds of errors when not producing code.
4349 if (cctx->ctx_skip == SKIP_YES)
4350 {
4351 skip_expr(arg);
4352 return OK;
4353 }
4354
Bram Moolenaar61a89812020-05-07 16:58:17 +02004355 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004356 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 return FAIL;
4358
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004359 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 if (*p == '?')
4361 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004362 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 garray_T *instr = &cctx->ctx_instr;
4364 garray_T *stack = &cctx->ctx_type_stack;
4365 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004366 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004368 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 int has_const_expr = FALSE;
4370 int const_value = FALSE;
4371 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004373 if (next != NULL)
4374 {
4375 *arg = next_line_from_context(cctx, TRUE);
4376 p = skipwhite(*arg);
4377 }
4378
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004379 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004380 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004381 semsg(_(e_white_space_required_before_and_after_str),
4382 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004383 return FAIL;
4384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386 if (ppconst->pp_used == ppconst_used + 1)
4387 {
4388 // the condition is a constant, we know whether the ? or the :
4389 // expression is to be evaluated.
4390 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004391 if (op_falsy)
4392 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4393 else
4394 {
4395 int error = FALSE;
4396
4397 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4398 &error);
4399 if (error)
4400 return FAIL;
4401 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004402 cctx->ctx_skip = save_skip == SKIP_YES ||
4403 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4404
4405 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4406 // "left ?? right" and "left" is truthy: produce "left"
4407 generate_ppconst(cctx, ppconst);
4408 else
4409 {
4410 clear_tv(&ppconst->pp_tv[ppconst_used]);
4411 --ppconst->pp_used;
4412 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413 }
4414 else
4415 {
4416 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004417 if (op_falsy)
4418 end_idx = instr->ga_len;
4419 generate_JUMP(cctx, op_falsy
4420 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4421 if (op_falsy)
4422 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424
4425 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004426 *arg = skipwhite(p + 1 + op_falsy);
4427 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004428 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004429 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004430 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431
Bram Moolenaara5565e42020-05-09 15:44:01 +02004432 if (!has_const_expr)
4433 {
4434 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004436 if (!op_falsy)
4437 {
4438 // remember the type and drop it
4439 --stack->ga_len;
4440 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004442 end_idx = instr->ga_len;
4443 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004445 // jump here from JUMP_IF_FALSE
4446 isn = ((isn_T *)instr->ga_data) + alt_idx;
4447 isn->isn_arg.jump.jump_where = instr->ga_len;
4448 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004451 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004453 // Check for the ":".
4454 p = may_peek_next_line(cctx, *arg, &next);
4455 if (*p != ':')
4456 {
4457 emsg(_(e_missing_colon));
4458 return FAIL;
4459 }
4460 if (next != NULL)
4461 {
4462 *arg = next_line_from_context(cctx, TRUE);
4463 p = skipwhite(*arg);
4464 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004465
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004466 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4467 {
4468 semsg(_(e_white_space_required_before_and_after_str), ":");
4469 return FAIL;
4470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004472 // evaluate the third expression
4473 if (has_const_expr)
4474 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004475 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004476 *arg = skipwhite(p + 1);
4477 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4478 return FAIL;
4479 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4480 return FAIL;
4481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482
Bram Moolenaara5565e42020-05-09 15:44:01 +02004483 if (!has_const_expr)
4484 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004485 type_T **typep;
4486
Bram Moolenaara5565e42020-05-09 15:44:01 +02004487 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488
Bram Moolenaara5565e42020-05-09 15:44:01 +02004489 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004490 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4491 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004492
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004493 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494 isn = ((isn_T *)instr->ga_data) + end_idx;
4495 isn->isn_arg.jump.jump_where = instr->ga_len;
4496 }
4497
4498 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 }
4500 return OK;
4501}
4502
4503/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004505 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4506 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 */
4508 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004509compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510{
4511 ppconst_T ppconst;
4512
4513 CLEAR_FIELD(ppconst);
4514 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4515 {
4516 clear_ppconst(&ppconst);
4517 return FAIL;
4518 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004519 if (is_const != NULL)
4520 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521 if (generate_ppconst(cctx, &ppconst) == FAIL)
4522 return FAIL;
4523 return OK;
4524}
4525
4526/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004527 * Toplevel expression.
4528 */
4529 static int
4530compile_expr0(char_u **arg, cctx_T *cctx)
4531{
4532 return compile_expr0_ext(arg, cctx, NULL);
4533}
4534
4535/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 * compile "return [expr]"
4537 */
4538 static char_u *
4539compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4540{
4541 char_u *p = arg;
4542 garray_T *stack = &cctx->ctx_type_stack;
4543 type_T *stack_type;
4544
4545 if (*p != NUL && *p != '|' && *p != '\n')
4546 {
4547 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004548 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 return NULL;
4550
4551 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4552 if (set_return_type)
4553 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004554 else
4555 {
4556 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4557 && stack_type->tt_type != VAR_VOID
4558 && stack_type->tt_type != VAR_UNKNOWN)
4559 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004560 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004561 return NULL;
4562 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004563 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004564 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004565 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 }
4568 else
4569 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004570 // "set_return_type" cannot be TRUE, only used for a lambda which
4571 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004572 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4573 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004575 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 return NULL;
4577 }
4578
4579 // No argument, return zero.
4580 generate_PUSHNR(cctx, 0);
4581 }
4582
4583 if (generate_instr(cctx, ISN_RETURN) == NULL)
4584 return NULL;
4585
4586 // "return val | endif" is possible
4587 return skipwhite(p);
4588}
4589
4590/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004591 * Get a line from the compilation context, compatible with exarg_T getline().
4592 * Return a pointer to the line in allocated memory.
4593 * Return NULL for end-of-file or some error.
4594 */
4595 static char_u *
4596exarg_getline(
4597 int c UNUSED,
4598 void *cookie,
4599 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004600 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004601{
4602 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004603 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004604
Bram Moolenaar66250c92020-08-20 15:02:42 +02004605 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004606 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004607 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004608 return NULL;
4609 ++cctx->ctx_lnum;
4610 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4611 // Comment lines result in NULL pointers, skip them.
4612 if (p != NULL)
4613 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004614 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004615}
4616
4617/*
4618 * Compile a nested :def command.
4619 */
4620 static char_u *
4621compile_nested_function(exarg_T *eap, cctx_T *cctx)
4622{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004623 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004624 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004625 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004626 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004627 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004628 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004629
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004630 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004631 {
4632 emsg(_(e_cannot_use_bang_with_nested_def));
4633 return NULL;
4634 }
4635
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004636 // Only g:Func() can use a namespace.
4637 if (name_start[1] == ':' && !is_global)
4638 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004639 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004640 return NULL;
4641 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004642 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4643 return NULL;
4644
Bram Moolenaar04b12692020-05-04 23:24:44 +02004645 eap->arg = name_end;
4646 eap->getline = exarg_getline;
4647 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004648 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004649 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004650 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004651 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004652
Bram Moolenaar822ba242020-05-24 23:00:18 +02004653 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004654 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004655 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004656 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004657 {
4658 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004659 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004660 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004661
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004662 if (is_global)
4663 {
4664 char_u *func_name = vim_strnsave(name_start + 2,
4665 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004666
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004667 if (func_name == NULL)
4668 r = FAIL;
4669 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004670 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004671 }
4672 else
4673 {
4674 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004675 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004676 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004677 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004678
Bram Moolenaareef21022020-08-01 22:16:43 +02004679 if (lvar == NULL)
4680 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004681 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004682 return NULL;
4683 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004684
4685 // copy over the block scope IDs
4686 if (block_depth > 0)
4687 {
4688 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4689 if (ufunc->uf_block_ids != NULL)
4690 {
4691 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4692 sizeof(int) * block_depth);
4693 ufunc->uf_block_depth = block_depth;
4694 }
4695 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004696 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004697
Bram Moolenaar61a89812020-05-07 16:58:17 +02004698 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004699 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004700}
4701
4702/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 * Return the length of an assignment operator, or zero if there isn't one.
4704 */
4705 int
4706assignment_len(char_u *p, int *heredoc)
4707{
4708 if (*p == '=')
4709 {
4710 if (p[1] == '<' && p[2] == '<')
4711 {
4712 *heredoc = TRUE;
4713 return 3;
4714 }
4715 return 1;
4716 }
4717 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4718 return 2;
4719 if (STRNCMP(p, "..=", 3) == 0)
4720 return 3;
4721 return 0;
4722}
4723
4724// words that cannot be used as a variable
4725static char *reserved[] = {
4726 "true",
4727 "false",
4728 NULL
4729};
4730
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004731typedef enum {
4732 dest_local,
4733 dest_option,
4734 dest_env,
4735 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004736 dest_buffer,
4737 dest_window,
4738 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004739 dest_vimvar,
4740 dest_script,
4741 dest_reg,
4742} assign_dest_T;
4743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004745 * Generate the load instruction for "name".
4746 */
4747 static void
4748generate_loadvar(
4749 cctx_T *cctx,
4750 assign_dest_T dest,
4751 char_u *name,
4752 lvar_T *lvar,
4753 type_T *type)
4754{
4755 switch (dest)
4756 {
4757 case dest_option:
4758 // TODO: check the option exists
4759 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4760 break;
4761 case dest_global:
4762 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4763 break;
4764 case dest_buffer:
4765 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4766 break;
4767 case dest_window:
4768 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4769 break;
4770 case dest_tab:
4771 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4772 break;
4773 case dest_script:
4774 compile_load_scriptvar(cctx,
4775 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4776 break;
4777 case dest_env:
4778 // Include $ in the name here
4779 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4780 break;
4781 case dest_reg:
4782 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4783 break;
4784 case dest_vimvar:
4785 generate_LOADV(cctx, name + 2, TRUE);
4786 break;
4787 case dest_local:
4788 if (lvar->lv_from_outer)
4789 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4790 NULL, type);
4791 else
4792 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4793 break;
4794 }
4795}
4796
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004797 void
4798vim9_declare_error(char_u *name)
4799{
4800 char *scope = "";
4801
4802 switch (*name)
4803 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004804 case 'g': scope = _("global"); break;
4805 case 'b': scope = _("buffer"); break;
4806 case 'w': scope = _("window"); break;
4807 case 't': scope = _("tab"); break;
4808 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004809 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004810 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004811 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004812 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004813 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004814 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004815 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004816 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004817 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004818}
4819
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004820/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004821 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004822 * "let name"
4823 * "var name = expr"
4824 * "final name = expr"
4825 * "const name = expr"
4826 * "name = expr"
4827 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 * Return NULL for an error.
4829 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 */
4831 static char_u *
4832compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4833{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004834 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004836 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 char_u *ret = NULL;
4838 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004839 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004840 int scriptvar_sid = 0;
4841 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004844 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 int oplen = 0;
4847 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004848 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004849 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004850 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004852 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4853 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004855 // Skip over the "var" or "[var, var]" to get to any "=".
4856 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4857 if (p == NULL)
4858 return *arg == '[' ? arg : NULL;
4859
4860 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004862 // TODO: should we allow this, and figure out type inference from list
4863 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004864 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 return NULL;
4866 }
4867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 sp = p;
4869 p = skipwhite(p);
4870 op = p;
4871 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872
4873 if (var_count > 0 && oplen == 0)
4874 // can be something like "[1, 2]->func()"
4875 return arg;
4876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4878 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004879 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004880 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004881 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 if (heredoc)
4884 {
4885 list_T *l;
4886 listitem_T *li;
4887
4888 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004889 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004891 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004892 if (l == NULL)
4893 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894
Bram Moolenaar078269b2020-09-21 20:35:55 +02004895 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004897 // Push each line and the create the list.
4898 FOR_ALL_LIST_ITEMS(l, li)
4899 {
4900 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4901 li->li_tv.vval.v_string = NULL;
4902 }
4903 generate_NEWLIST(cctx, l->lv_len);
4904 type = &t_list_string;
4905 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 list_free(l);
4908 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004909 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004911 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004913 // for "[var, var] = expr" evaluate the expression here, loop over the
4914 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004915
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004916 p = skipwhite(op + oplen);
4917 if (compile_expr0(&p, cctx) == FAIL)
4918 return NULL;
4919 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004921 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004923 type_T *stacktype;
4924
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004925 stacktype = stack->ga_len == 0 ? &t_void
4926 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004927 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004929 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004930 goto theend;
4931 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004932 if (need_type(stacktype, &t_list_any, -1, cctx,
4933 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004934 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004935 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004936 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4937 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 }
4939 }
4940
4941 /*
4942 * Loop over variables in "[var, var] = expr".
4943 * For "var = expr" and "let var: type" this is done only once.
4944 */
4945 if (var_count > 0)
4946 var_start = skipwhite(arg + 1); // skip over the "["
4947 else
4948 var_start = arg;
4949 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4950 {
4951 char_u *var_end = skip_var_one(var_start, FALSE);
4952 size_t varlen;
4953 int new_local = FALSE;
4954 int opt_type;
4955 int opt_flags = 0;
4956 assign_dest_T dest = dest_local;
4957 int vimvaridx = -1;
4958 lvar_T *lvar = NULL;
4959 lvar_T arg_lvar;
4960 int has_type = FALSE;
4961 int has_index = FALSE;
4962 int instr_count = -1;
4963
Bram Moolenaar65821722020-08-02 18:58:54 +02004964 if (*var_start == '@')
4965 p = var_start + 2;
4966 else
4967 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004968 // skip over the leading "&", "&l:", "&g:" and "$"
4969 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004970 p = to_name_end(p, TRUE);
4971 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004972
4973 // "a: type" is declaring variable "a" with a type, not "a:".
4974 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4975 --var_end;
4976 if (is_decl && p == var_start + 2 && p[-1] == ':')
4977 --p;
4978
4979 varlen = p - var_start;
4980 vim_free(name);
4981 name = vim_strnsave(var_start, varlen);
4982 if (name == NULL)
4983 return NULL;
4984 if (!heredoc)
4985 type = &t_any;
4986
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004987 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004989 int declare_error = FALSE;
4990
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991 if (*var_start == '&')
4992 {
4993 int cc;
4994 long numval;
4995
4996 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004997 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004999 emsg(_(e_const_option));
5000 goto theend;
5001 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005002 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 p = var_start;
5004 p = find_option_end(&p, &opt_flags);
5005 if (p == NULL)
5006 {
5007 // cannot happen?
5008 emsg(_(e_letunexp));
5009 goto theend;
5010 }
5011 cc = *p;
5012 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005013 opt_type = get_option_value(skip_option_env_lead(var_start),
5014 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 *p = cc;
5016 if (opt_type == -3)
5017 {
5018 semsg(_(e_unknown_option), var_start);
5019 goto theend;
5020 }
5021 if (opt_type == -2 || opt_type == 0)
5022 type = &t_string;
5023 else
5024 type = &t_number; // both number and boolean option
5025 }
5026 else if (*var_start == '$')
5027 {
5028 dest = dest_env;
5029 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005030 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 }
5032 else if (*var_start == '@')
5033 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005034 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005035 {
5036 emsg_invreg(var_start[1]);
5037 goto theend;
5038 }
5039 dest = dest_reg;
5040 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005041 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005043 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005044 {
5045 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005046 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005048 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049 {
5050 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005051 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005053 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005054 {
5055 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005056 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005058 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 {
5060 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005061 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005062 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005063 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005064 {
5065 typval_T *vtv;
5066 int di_flags;
5067
5068 vimvaridx = find_vim_var(name + 2, &di_flags);
5069 if (vimvaridx < 0)
5070 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005071 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 goto theend;
5073 }
5074 // We use the current value of "sandbox" here, is that OK?
5075 if (var_check_ro(di_flags, name, FALSE))
5076 goto theend;
5077 dest = dest_vimvar;
5078 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005079 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005080 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 }
5082 else
5083 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005084 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085
5086 for (idx = 0; reserved[idx] != NULL; ++idx)
5087 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005088 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005089 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005090 goto theend;
5091 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092
5093 lvar = lookup_local(var_start, varlen, cctx);
5094 if (lvar == NULL)
5095 {
5096 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005097 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5099 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005100 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 if (is_decl)
5102 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005103 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 goto theend;
5105 }
5106 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005107 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005108 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005109 if (lvar != NULL)
5110 {
5111 if (is_decl)
5112 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005113 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005114 goto theend;
5115 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005116 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005117 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005118 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005119 int script_namespace = varlen > 1
5120 && STRNCMP(var_start, "s:", 2) == 0;
5121 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005122 ? script_var_exists(var_start + 2, varlen - 2,
5123 FALSE, cctx)
5124 : script_var_exists(var_start, varlen,
5125 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005126 imported_T *import =
5127 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005128
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005129 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005131 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5132
5133 if (is_decl)
5134 {
5135 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005136 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005137 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005138 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005139 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005140 name);
5141 goto theend;
5142 }
5143 else if (cctx->ctx_ufunc->uf_script_ctx_version
5144 == SCRIPT_VERSION_VIM9
5145 && script_namespace
5146 && !script_var && import == NULL)
5147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005148 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005149 goto theend;
5150 }
5151
5152 dest = dest_script;
5153
5154 // existing script-local variables should have a type
5155 scriptvar_sid = current_sctx.sc_sid;
5156 if (import != NULL)
5157 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005158 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005159 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005160 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005161 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005162 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005163 {
5164 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5165 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005166 ((svar_T *)si->sn_var_vals.ga_data)
5167 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005168 type = sv->sv_type;
5169 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005170 }
5171 }
5172 else if (name[1] == ':' && name[2] != NUL)
5173 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005174 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 goto theend;
5176 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005177 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005178 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005179 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005180 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005181 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005182 else if (check_defined(var_start, varlen, cctx) == FAIL)
5183 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005184 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005186
5187 if (declare_error)
5188 {
5189 vim9_declare_error(name);
5190 goto theend;
5191 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 }
5193
5194 // handle "a:name" as a name, not index "name" on "a"
5195 if (varlen > 1 || var_start[varlen] != ':')
5196 p = var_end;
5197
5198 if (dest != dest_option)
5199 {
5200 if (is_decl && *p == ':')
5201 {
5202 // parse optional type: "let var: type = expr"
5203 if (!VIM_ISWHITE(p[1]))
5204 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005205 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 goto theend;
5207 }
5208 p = skipwhite(p + 1);
5209 type = parse_type(&p, cctx->ctx_type_list);
5210 has_type = TRUE;
5211 }
5212 else if (lvar != NULL)
5213 type = lvar->lv_type;
5214 }
5215
5216 if (oplen == 3 && !heredoc && dest != dest_global
5217 && type->tt_type != VAR_STRING
5218 && type->tt_type != VAR_ANY)
5219 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005220 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 goto theend;
5222 }
5223
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005224 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005225 {
5226 if (oplen > 1 && !heredoc)
5227 {
5228 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005229 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005230 goto theend;
5231 }
5232
5233 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005234 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5235 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005236 goto theend;
5237 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005238 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005239 if (lvar == NULL)
5240 goto theend;
5241 new_local = TRUE;
5242 }
5243
5244 member_type = type;
5245 if (var_end > var_start + varlen)
5246 {
5247 // Something follows after the variable: "var[idx]".
5248 if (is_decl)
5249 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005250 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 goto theend;
5252 }
5253
5254 if (var_start[varlen] == '[')
5255 {
5256 has_index = TRUE;
5257 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005258 member_type = &t_any;
5259 else
5260 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005261 }
5262 else
5263 {
5264 semsg("Not supported yet: %s", var_start);
5265 goto theend;
5266 }
5267 }
5268 else if (lvar == &arg_lvar)
5269 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005270 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 goto theend;
5272 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005273 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5274 {
5275 semsg(_(e_cannot_assign_to_constant), name);
5276 goto theend;
5277 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278
5279 if (!heredoc)
5280 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005281 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005282 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005283 if (oplen > 0 && var_count == 0)
5284 {
5285 // skip over the "=" and the expression
5286 p = skipwhite(op + oplen);
5287 compile_expr0(&p, cctx);
5288 }
5289 }
5290 else if (oplen > 0)
5291 {
5292 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005293 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005294
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 // For "var = expr" evaluate the expression.
5296 if (var_count == 0)
5297 {
5298 int r;
5299
5300 // for "+=", "*=", "..=" etc. first load the current value
5301 if (*op != '=')
5302 {
5303 generate_loadvar(cctx, dest, name, lvar, type);
5304
5305 if (has_index)
5306 {
5307 // TODO: get member from list or dict
5308 emsg("Index with operation not supported yet");
5309 goto theend;
5310 }
5311 }
5312
5313 // Compile the expression. Temporarily hide the new local
5314 // variable here, it is not available to this expression.
5315 if (new_local)
5316 --cctx->ctx_locals.ga_len;
5317 instr_count = instr->ga_len;
5318 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005319 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005320 if (new_local)
5321 ++cctx->ctx_locals.ga_len;
5322 if (r == FAIL)
5323 goto theend;
5324 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005325 else if (semicolon && var_idx == var_count - 1)
5326 {
5327 // For "[var; var] = expr" get the rest of the list
5328 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5329 goto theend;
5330 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005331 else
5332 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 // For "[var, var] = expr" get the "var_idx" item from the
5334 // list.
5335 if (generate_GETITEM(cctx, var_idx) == FAIL)
5336 return FAIL;
5337 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005338
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005339 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005340 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005341 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005343 if ((stacktype->tt_type == VAR_FUNC
5344 || stacktype->tt_type == VAR_PARTIAL)
5345 && var_wrong_func_name(name, TRUE))
5346 goto theend;
5347
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005348 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005349 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005350 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005352 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005353 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005354 }
5355 else
5356 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005357 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005358 // for a variable that implies &t_any.
5359 if (stacktype == &t_list_empty)
5360 lvar->lv_type = &t_list_any;
5361 else if (stacktype == &t_dict_empty)
5362 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005363 else if (stacktype == &t_unknown)
5364 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005365 else
5366 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005367 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005368 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005369 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005370 {
5371 type_T *use_type = lvar->lv_type;
5372
Bram Moolenaar71abe482020-09-14 22:28:30 +02005373 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005374 if (has_index)
5375 {
5376 use_type = use_type->tt_member;
5377 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005378 // could be indexing "any"
5379 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005380 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005381 if (need_type(stacktype, use_type, -1, cctx,
5382 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005383 goto theend;
5384 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005385 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005386 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005387 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005388 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005390 else if (cmdidx == CMD_final)
5391 {
5392 emsg(_(e_final_requires_a_value));
5393 goto theend;
5394 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005397 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398 goto theend;
5399 }
5400 else if (!has_type || dest == dest_option)
5401 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005402 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005403 goto theend;
5404 }
5405 else
5406 {
5407 // variables are always initialized
5408 if (ga_grow(instr, 1) == FAIL)
5409 goto theend;
5410 switch (member_type->tt_type)
5411 {
5412 case VAR_BOOL:
5413 generate_PUSHBOOL(cctx, VVAL_FALSE);
5414 break;
5415 case VAR_FLOAT:
5416#ifdef FEAT_FLOAT
5417 generate_PUSHF(cctx, 0.0);
5418#endif
5419 break;
5420 case VAR_STRING:
5421 generate_PUSHS(cctx, NULL);
5422 break;
5423 case VAR_BLOB:
5424 generate_PUSHBLOB(cctx, NULL);
5425 break;
5426 case VAR_FUNC:
5427 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5428 break;
5429 case VAR_LIST:
5430 generate_NEWLIST(cctx, 0);
5431 break;
5432 case VAR_DICT:
5433 generate_NEWDICT(cctx, 0);
5434 break;
5435 case VAR_JOB:
5436 generate_PUSHJOB(cctx, NULL);
5437 break;
5438 case VAR_CHANNEL:
5439 generate_PUSHCHANNEL(cctx, NULL);
5440 break;
5441 case VAR_NUMBER:
5442 case VAR_UNKNOWN:
5443 case VAR_ANY:
5444 case VAR_PARTIAL:
5445 case VAR_VOID:
5446 case VAR_SPECIAL: // cannot happen
5447 generate_PUSHNR(cctx, 0);
5448 break;
5449 }
5450 }
5451 if (var_count == 0)
5452 end = p;
5453 }
5454
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005455 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005456 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005457 break;
5458
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005459 if (oplen > 0 && *op != '=')
5460 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005461 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005462 type_T *stacktype;
5463
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 if (*op == '.')
5465 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005466 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005467 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005468 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005469 if (
5470#ifdef FEAT_FLOAT
5471 // If variable is float operation with number is OK.
5472 !(expected == &t_float && stacktype == &t_number) &&
5473#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005474 need_type(stacktype, expected, -1, cctx,
5475 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005476 goto theend;
5477
5478 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005479 {
5480 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5481 goto theend;
5482 }
5483 else if (*op == '+')
5484 {
5485 if (generate_add_instr(cctx,
5486 operator_type(member_type, stacktype),
5487 member_type, stacktype) == FAIL)
5488 goto theend;
5489 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005490 else if (generate_two_op(cctx, op) == FAIL)
5491 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005495 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005496 int r;
5497
5498 // Compile the "idx" in "var[idx]".
5499 if (new_local)
5500 --cctx->ctx_locals.ga_len;
5501 p = skipwhite(var_start + varlen + 1);
5502 r = compile_expr0(&p, cctx);
5503 if (new_local)
5504 ++cctx->ctx_locals.ga_len;
5505 if (r == FAIL)
5506 goto theend;
5507 if (*skipwhite(p) != ']')
5508 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005509 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005510 emsg(_(e_missbrac));
5511 goto theend;
5512 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005513 if (type == &t_any)
5514 {
5515 type_T *idx_type = ((type_T **)stack->ga_data)[
5516 stack->ga_len - 1];
5517 // Index on variable of unknown type: guess the type from the
5518 // index type: number is dict, otherwise dict.
5519 // TODO: should do the assignment at runtime
5520 if (idx_type->tt_type == VAR_NUMBER)
5521 type = &t_list_any;
5522 else
5523 type = &t_dict_any;
5524 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005525 if (type->tt_type == VAR_DICT
5526 && may_generate_2STRING(-1, cctx) == FAIL)
5527 goto theend;
5528 if (type->tt_type == VAR_LIST
5529 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005530 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005531 {
5532 emsg(_(e_number_exp));
5533 goto theend;
5534 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005535
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005536 // Load the dict or list. On the stack we then have:
5537 // - value
5538 // - index
5539 // - variable
5540 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005541
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005542 if (type->tt_type == VAR_LIST)
5543 {
5544 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5545 return FAIL;
5546 }
5547 else if (type->tt_type == VAR_DICT)
5548 {
5549 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5550 return FAIL;
5551 }
5552 else
5553 {
5554 emsg(_(e_listreq));
5555 goto theend;
5556 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005557 }
5558 else
5559 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005560 if (is_decl && cmdidx == CMD_const
5561 && (dest == dest_script || dest == dest_local))
5562 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005563 generate_LOCKCONST(cctx);
5564
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005565 switch (dest)
5566 {
5567 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005568 generate_STOREOPT(cctx, skip_option_env_lead(name),
5569 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005570 break;
5571 case dest_global:
5572 // include g: with the name, easier to execute that way
5573 generate_STORE(cctx, ISN_STOREG, 0, name);
5574 break;
5575 case dest_buffer:
5576 // include b: with the name, easier to execute that way
5577 generate_STORE(cctx, ISN_STOREB, 0, name);
5578 break;
5579 case dest_window:
5580 // include w: with the name, easier to execute that way
5581 generate_STORE(cctx, ISN_STOREW, 0, name);
5582 break;
5583 case dest_tab:
5584 // include t: with the name, easier to execute that way
5585 generate_STORE(cctx, ISN_STORET, 0, name);
5586 break;
5587 case dest_env:
5588 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5589 break;
5590 case dest_reg:
5591 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5592 break;
5593 case dest_vimvar:
5594 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5595 break;
5596 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005597 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005598 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005599 {
5600 char_u *name_s = name;
5601
5602 // Include s: in the name for store_var()
5603 if (name[1] != ':')
5604 {
5605 int len = (int)STRLEN(name) + 3;
5606
5607 name_s = alloc(len);
5608 if (name_s == NULL)
5609 name_s = name;
5610 else
5611 vim_snprintf((char *)name_s, len,
5612 "s:%s", name);
5613 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005614 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5615 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005616 if (name_s != name)
5617 vim_free(name_s);
5618 }
5619 else
5620 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005621 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005622 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005623 break;
5624 case dest_local:
5625 if (lvar != NULL)
5626 {
5627 isn_T *isn = ((isn_T *)instr->ga_data)
5628 + instr->ga_len - 1;
5629
5630 // optimization: turn "var = 123" from ISN_PUSHNR +
5631 // ISN_STORE into ISN_STORENR
5632 if (!lvar->lv_from_outer
5633 && instr->ga_len == instr_count + 1
5634 && isn->isn_type == ISN_PUSHNR)
5635 {
5636 varnumber_T val = isn->isn_arg.number;
5637
5638 isn->isn_type = ISN_STORENR;
5639 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5640 isn->isn_arg.storenr.stnr_val = val;
5641 if (stack->ga_len > 0)
5642 --stack->ga_len;
5643 }
5644 else if (lvar->lv_from_outer)
5645 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005646 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005647 else
5648 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5649 }
5650 break;
5651 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005652 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005653
5654 if (var_idx + 1 < var_count)
5655 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005657
5658 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005659 if (var_count > 0 && !semicolon)
5660 {
5661 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5662 goto theend;
5663 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005664
Bram Moolenaarb2097502020-07-19 17:17:02 +02005665 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
5667theend:
5668 vim_free(name);
5669 return ret;
5670}
5671
5672/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005673 * Check if "name" can be "unlet".
5674 */
5675 int
5676check_vim9_unlet(char_u *name)
5677{
5678 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5679 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005680 // "unlet s:var" is allowed in legacy script.
5681 if (*name == 's' && !script_is_vim9())
5682 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005683 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005684 return FAIL;
5685 }
5686 return OK;
5687}
5688
5689/*
5690 * Callback passed to ex_unletlock().
5691 */
5692 static int
5693compile_unlet(
5694 lval_T *lvp,
5695 char_u *name_end,
5696 exarg_T *eap,
5697 int deep UNUSED,
5698 void *coookie)
5699{
5700 cctx_T *cctx = coookie;
5701
5702 if (lvp->ll_tv == NULL)
5703 {
5704 char_u *p = lvp->ll_name;
5705 int cc = *name_end;
5706 int ret = OK;
5707
5708 // Normal name. Only supports g:, w:, t: and b: namespaces.
5709 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005710 if (*p == '$')
5711 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5712 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005713 ret = FAIL;
5714 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005715 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005716
5717 *name_end = cc;
5718 return ret;
5719 }
5720
5721 // TODO: unlet {list}[idx]
5722 // TODO: unlet {dict}[key]
5723 emsg("Sorry, :unlet not fully implemented yet");
5724 return FAIL;
5725}
5726
5727/*
5728 * compile "unlet var", "lock var" and "unlock var"
5729 * "arg" points to "var".
5730 */
5731 static char_u *
5732compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5733{
5734 char_u *p = arg;
5735
5736 if (eap->cmdidx != CMD_unlet)
5737 {
5738 emsg("Sorry, :lock and unlock not implemented yet");
5739 return NULL;
5740 }
5741
5742 if (*p == '!')
5743 {
5744 p = skipwhite(p + 1);
5745 eap->forceit = TRUE;
5746 }
5747
5748 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5749 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5750}
5751
5752/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005753 * Compile an :import command.
5754 */
5755 static char_u *
5756compile_import(char_u *arg, cctx_T *cctx)
5757{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005758 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759}
5760
5761/*
5762 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5763 */
5764 static int
5765compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5766{
5767 garray_T *instr = &cctx->ctx_instr;
5768 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5769
5770 if (endlabel == NULL)
5771 return FAIL;
5772 endlabel->el_next = *el;
5773 *el = endlabel;
5774 endlabel->el_end_label = instr->ga_len;
5775
5776 generate_JUMP(cctx, when, 0);
5777 return OK;
5778}
5779
5780 static void
5781compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5782{
5783 garray_T *instr = &cctx->ctx_instr;
5784
5785 while (*el != NULL)
5786 {
5787 endlabel_T *cur = (*el);
5788 isn_T *isn;
5789
5790 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5791 isn->isn_arg.jump.jump_where = instr->ga_len;
5792 *el = cur->el_next;
5793 vim_free(cur);
5794 }
5795}
5796
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005797 static void
5798compile_free_jump_to_end(endlabel_T **el)
5799{
5800 while (*el != NULL)
5801 {
5802 endlabel_T *cur = (*el);
5803
5804 *el = cur->el_next;
5805 vim_free(cur);
5806 }
5807}
5808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005809/*
5810 * Create a new scope and set up the generic items.
5811 */
5812 static scope_T *
5813new_scope(cctx_T *cctx, scopetype_T type)
5814{
5815 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5816
5817 if (scope == NULL)
5818 return NULL;
5819 scope->se_outer = cctx->ctx_scope;
5820 cctx->ctx_scope = scope;
5821 scope->se_type = type;
5822 scope->se_local_count = cctx->ctx_locals.ga_len;
5823 return scope;
5824}
5825
5826/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005827 * Free the current scope and go back to the outer scope.
5828 */
5829 static void
5830drop_scope(cctx_T *cctx)
5831{
5832 scope_T *scope = cctx->ctx_scope;
5833
5834 if (scope == NULL)
5835 {
5836 iemsg("calling drop_scope() without a scope");
5837 return;
5838 }
5839 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005840 switch (scope->se_type)
5841 {
5842 case IF_SCOPE:
5843 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5844 case FOR_SCOPE:
5845 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5846 case WHILE_SCOPE:
5847 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5848 case TRY_SCOPE:
5849 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5850 case NO_SCOPE:
5851 case BLOCK_SCOPE:
5852 break;
5853 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005854 vim_free(scope);
5855}
5856
5857/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005858 * Check that the top of the type stack has a type that can be used as a
5859 * condition. Give an error and return FAIL if not.
5860 */
5861 static int
5862bool_on_stack(cctx_T *cctx)
5863{
5864 garray_T *stack = &cctx->ctx_type_stack;
5865 type_T *type;
5866
5867 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5868 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005869 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005870 return FAIL;
5871 return OK;
5872}
5873
5874/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875 * compile "if expr"
5876 *
5877 * "if expr" Produces instructions:
5878 * EVAL expr Push result of "expr"
5879 * JUMP_IF_FALSE end
5880 * ... body ...
5881 * end:
5882 *
5883 * "if expr | else" Produces instructions:
5884 * EVAL expr Push result of "expr"
5885 * JUMP_IF_FALSE else
5886 * ... body ...
5887 * JUMP_ALWAYS end
5888 * else:
5889 * ... body ...
5890 * end:
5891 *
5892 * "if expr1 | elseif expr2 | else" Produces instructions:
5893 * EVAL expr Push result of "expr"
5894 * JUMP_IF_FALSE elseif
5895 * ... body ...
5896 * JUMP_ALWAYS end
5897 * elseif:
5898 * EVAL expr Push result of "expr"
5899 * JUMP_IF_FALSE else
5900 * ... body ...
5901 * JUMP_ALWAYS end
5902 * else:
5903 * ... body ...
5904 * end:
5905 */
5906 static char_u *
5907compile_if(char_u *arg, cctx_T *cctx)
5908{
5909 char_u *p = arg;
5910 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005911 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005913 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005914 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915
Bram Moolenaara5565e42020-05-09 15:44:01 +02005916 CLEAR_FIELD(ppconst);
5917 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005918 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005919 clear_ppconst(&ppconst);
5920 return NULL;
5921 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005922 if (cctx->ctx_skip == SKIP_YES)
5923 clear_ppconst(&ppconst);
5924 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005925 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005926 int error = FALSE;
5927 int v;
5928
Bram Moolenaara5565e42020-05-09 15:44:01 +02005929 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005930 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005931 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005932 if (error)
5933 return NULL;
5934 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005935 }
5936 else
5937 {
5938 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005939 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005940 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005941 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005942 if (bool_on_stack(cctx) == FAIL)
5943 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005945
5946 scope = new_scope(cctx, IF_SCOPE);
5947 if (scope == NULL)
5948 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005949 scope->se_skip_save = skip_save;
5950 // "is_had_return" will be reset if any block does not end in :return
5951 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005953 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005954 {
5955 // "where" is set when ":elseif", "else" or ":endif" is found
5956 scope->se_u.se_if.is_if_label = instr->ga_len;
5957 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5958 }
5959 else
5960 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961
5962 return p;
5963}
5964
5965 static char_u *
5966compile_elseif(char_u *arg, cctx_T *cctx)
5967{
5968 char_u *p = arg;
5969 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005970 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 isn_T *isn;
5972 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005973 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005974 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975
5976 if (scope == NULL || scope->se_type != IF_SCOPE)
5977 {
5978 emsg(_(e_elseif_without_if));
5979 return NULL;
5980 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005981 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005982 if (!cctx->ctx_had_return)
5983 scope->se_u.se_if.is_had_return = FALSE;
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 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005989 return NULL;
5990 // previous "if" or "elseif" jumps here
5991 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5992 isn->isn_arg.jump.jump_where = instr->ga_len;
5993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005995 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005996 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005997 if (cctx->ctx_skip == SKIP_YES)
5998 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005999 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006000 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006001 clear_ppconst(&ppconst);
6002 return NULL;
6003 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006004 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006005 if (scope->se_skip_save == SKIP_YES)
6006 clear_ppconst(&ppconst);
6007 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006008 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006009 int error = FALSE;
6010 int v;
6011
Bram Moolenaar7f141552020-05-09 17:35:53 +02006012 // The expression results in a constant.
6013 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006014 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6015 if (error)
6016 return NULL;
6017 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006018 clear_ppconst(&ppconst);
6019 scope->se_u.se_if.is_if_label = -1;
6020 }
6021 else
6022 {
6023 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006024 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006025 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006026 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006027 if (bool_on_stack(cctx) == FAIL)
6028 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 // "where" is set when ":elseif", "else" or ":endif" is found
6031 scope->se_u.se_if.is_if_label = instr->ga_len;
6032 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034
6035 return p;
6036}
6037
6038 static char_u *
6039compile_else(char_u *arg, cctx_T *cctx)
6040{
6041 char_u *p = arg;
6042 garray_T *instr = &cctx->ctx_instr;
6043 isn_T *isn;
6044 scope_T *scope = cctx->ctx_scope;
6045
6046 if (scope == NULL || scope->se_type != IF_SCOPE)
6047 {
6048 emsg(_(e_else_without_if));
6049 return NULL;
6050 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006051 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006052 if (!cctx->ctx_had_return)
6053 scope->se_u.se_if.is_had_return = FALSE;
6054 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055
Bram Moolenaarefd88552020-06-18 20:50:10 +02006056 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006057 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006058 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006059 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006060 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006061 if (!cctx->ctx_had_return
6062 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6063 JUMP_ALWAYS, cctx) == FAIL)
6064 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006065 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006066
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006067 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006068 {
6069 if (scope->se_u.se_if.is_if_label >= 0)
6070 {
6071 // previous "if" or "elseif" jumps here
6072 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6073 isn->isn_arg.jump.jump_where = instr->ga_len;
6074 scope->se_u.se_if.is_if_label = -1;
6075 }
6076 }
6077
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006078 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006079 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081
6082 return p;
6083}
6084
6085 static char_u *
6086compile_endif(char_u *arg, cctx_T *cctx)
6087{
6088 scope_T *scope = cctx->ctx_scope;
6089 ifscope_T *ifscope;
6090 garray_T *instr = &cctx->ctx_instr;
6091 isn_T *isn;
6092
6093 if (scope == NULL || scope->se_type != IF_SCOPE)
6094 {
6095 emsg(_(e_endif_without_if));
6096 return NULL;
6097 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006098 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006099 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006100 if (!cctx->ctx_had_return)
6101 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006103 if (scope->se_u.se_if.is_if_label >= 0)
6104 {
6105 // previous "if" or "elseif" jumps here
6106 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6107 isn->isn_arg.jump.jump_where = instr->ga_len;
6108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 // Fill in the "end" label in jumps at the end of the blocks.
6110 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006111 cctx->ctx_skip = scope->se_skip_save;
6112
6113 // If all the blocks end in :return and there is an :else then the
6114 // had_return flag is set.
6115 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006117 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 return arg;
6119}
6120
6121/*
6122 * compile "for var in expr"
6123 *
6124 * Produces instructions:
6125 * PUSHNR -1
6126 * STORE loop-idx Set index to -1
6127 * EVAL expr Push result of "expr"
6128 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6129 * - if beyond end, jump to "end"
6130 * - otherwise get item from list and push it
6131 * STORE var Store item in "var"
6132 * ... body ...
6133 * JUMP top Jump back to repeat
6134 * end: DROP Drop the result of "expr"
6135 *
6136 */
6137 static char_u *
6138compile_for(char_u *arg, cctx_T *cctx)
6139{
6140 char_u *p;
6141 size_t varlen;
6142 garray_T *instr = &cctx->ctx_instr;
6143 garray_T *stack = &cctx->ctx_type_stack;
6144 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006145 lvar_T *loop_lvar; // loop iteration variable
6146 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147 type_T *vartype;
6148
6149 // TODO: list of variables: "for [key, value] in dict"
6150 // parse "var"
6151 for (p = arg; eval_isnamec1(*p); ++p)
6152 ;
6153 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006154 var_lvar = lookup_local(arg, varlen, cctx);
6155 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006157 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158 return NULL;
6159 }
6160
6161 // consume "in"
6162 p = skipwhite(p);
6163 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6164 {
6165 emsg(_(e_missing_in));
6166 return NULL;
6167 }
6168 p = skipwhite(p + 2);
6169
6170
6171 scope = new_scope(cctx, FOR_SCOPE);
6172 if (scope == NULL)
6173 return NULL;
6174
6175 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006176 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6177 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006178 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006179 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006180 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183
6184 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006185 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6186 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006187 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006188 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006189 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006193 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194
6195 // compile "expr", it remains on the stack until "endfor"
6196 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006197 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006198 {
6199 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006203 // Now that we know the type of "var", check that it is a list, now or at
6204 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006206 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006207 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006208 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 return NULL;
6210 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006211 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006212 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213
6214 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006215 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006217 generate_FOR(cctx, loop_lvar->lv_idx);
6218 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219
6220 return arg;
6221}
6222
6223/*
6224 * compile "endfor"
6225 */
6226 static char_u *
6227compile_endfor(char_u *arg, cctx_T *cctx)
6228{
6229 garray_T *instr = &cctx->ctx_instr;
6230 scope_T *scope = cctx->ctx_scope;
6231 forscope_T *forscope;
6232 isn_T *isn;
6233
6234 if (scope == NULL || scope->se_type != FOR_SCOPE)
6235 {
6236 emsg(_(e_for));
6237 return NULL;
6238 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006239 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006241 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242
6243 // At end of ":for" scope jump back to the FOR instruction.
6244 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6245
6246 // Fill in the "end" label in the FOR statement so it can jump here
6247 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6248 isn->isn_arg.forloop.for_end = instr->ga_len;
6249
6250 // Fill in the "end" label any BREAK statements
6251 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6252
6253 // Below the ":for" scope drop the "expr" list from the stack.
6254 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6255 return NULL;
6256
6257 vim_free(scope);
6258
6259 return arg;
6260}
6261
6262/*
6263 * compile "while expr"
6264 *
6265 * Produces instructions:
6266 * top: EVAL expr Push result of "expr"
6267 * JUMP_IF_FALSE end jump if false
6268 * ... body ...
6269 * JUMP top Jump back to repeat
6270 * end:
6271 *
6272 */
6273 static char_u *
6274compile_while(char_u *arg, cctx_T *cctx)
6275{
6276 char_u *p = arg;
6277 garray_T *instr = &cctx->ctx_instr;
6278 scope_T *scope;
6279
6280 scope = new_scope(cctx, WHILE_SCOPE);
6281 if (scope == NULL)
6282 return NULL;
6283
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006284 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285
6286 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006287 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 return NULL;
6289
Bram Moolenaar13106602020-10-04 16:06:05 +02006290 if (bool_on_stack(cctx) == FAIL)
6291 return FAIL;
6292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006294 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 JUMP_IF_FALSE, cctx) == FAIL)
6296 return FAIL;
6297
6298 return p;
6299}
6300
6301/*
6302 * compile "endwhile"
6303 */
6304 static char_u *
6305compile_endwhile(char_u *arg, cctx_T *cctx)
6306{
6307 scope_T *scope = cctx->ctx_scope;
6308
6309 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6310 {
6311 emsg(_(e_while));
6312 return NULL;
6313 }
6314 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006315 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316
6317 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006318 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319
6320 // Fill in the "end" label in the WHILE statement so it can jump here.
6321 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006322 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323
6324 vim_free(scope);
6325
6326 return arg;
6327}
6328
6329/*
6330 * compile "continue"
6331 */
6332 static char_u *
6333compile_continue(char_u *arg, cctx_T *cctx)
6334{
6335 scope_T *scope = cctx->ctx_scope;
6336
6337 for (;;)
6338 {
6339 if (scope == NULL)
6340 {
6341 emsg(_(e_continue));
6342 return NULL;
6343 }
6344 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6345 break;
6346 scope = scope->se_outer;
6347 }
6348
6349 // Jump back to the FOR or WHILE instruction.
6350 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006351 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6352 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 return arg;
6354}
6355
6356/*
6357 * compile "break"
6358 */
6359 static char_u *
6360compile_break(char_u *arg, cctx_T *cctx)
6361{
6362 scope_T *scope = cctx->ctx_scope;
6363 endlabel_T **el;
6364
6365 for (;;)
6366 {
6367 if (scope == NULL)
6368 {
6369 emsg(_(e_break));
6370 return NULL;
6371 }
6372 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6373 break;
6374 scope = scope->se_outer;
6375 }
6376
6377 // Jump to the end of the FOR or WHILE loop.
6378 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006379 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006381 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6383 return FAIL;
6384
6385 return arg;
6386}
6387
6388/*
6389 * compile "{" start of block
6390 */
6391 static char_u *
6392compile_block(char_u *arg, cctx_T *cctx)
6393{
6394 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6395 return NULL;
6396 return skipwhite(arg + 1);
6397}
6398
6399/*
6400 * compile end of block: drop one scope
6401 */
6402 static void
6403compile_endblock(cctx_T *cctx)
6404{
6405 scope_T *scope = cctx->ctx_scope;
6406
6407 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006408 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 vim_free(scope);
6410}
6411
6412/*
6413 * compile "try"
6414 * Creates a new scope for the try-endtry, pointing to the first catch and
6415 * finally.
6416 * Creates another scope for the "try" block itself.
6417 * TRY instruction sets up exception handling at runtime.
6418 *
6419 * "try"
6420 * TRY -> catch1, -> finally push trystack entry
6421 * ... try block
6422 * "throw {exception}"
6423 * EVAL {exception}
6424 * THROW create exception
6425 * ... try block
6426 * " catch {expr}"
6427 * JUMP -> finally
6428 * catch1: PUSH exeception
6429 * EVAL {expr}
6430 * MATCH
6431 * JUMP nomatch -> catch2
6432 * CATCH remove exception
6433 * ... catch block
6434 * " catch"
6435 * JUMP -> finally
6436 * catch2: CATCH remove exception
6437 * ... catch block
6438 * " finally"
6439 * finally:
6440 * ... finally block
6441 * " endtry"
6442 * ENDTRY pop trystack entry, may rethrow
6443 */
6444 static char_u *
6445compile_try(char_u *arg, cctx_T *cctx)
6446{
6447 garray_T *instr = &cctx->ctx_instr;
6448 scope_T *try_scope;
6449 scope_T *scope;
6450
6451 // scope that holds the jumps that go to catch/finally/endtry
6452 try_scope = new_scope(cctx, TRY_SCOPE);
6453 if (try_scope == NULL)
6454 return NULL;
6455
6456 // "catch" is set when the first ":catch" is found.
6457 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006458 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459 if (generate_instr(cctx, ISN_TRY) == NULL)
6460 return NULL;
6461
6462 // scope for the try block itself
6463 scope = new_scope(cctx, BLOCK_SCOPE);
6464 if (scope == NULL)
6465 return NULL;
6466
6467 return arg;
6468}
6469
6470/*
6471 * compile "catch {expr}"
6472 */
6473 static char_u *
6474compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6475{
6476 scope_T *scope = cctx->ctx_scope;
6477 garray_T *instr = &cctx->ctx_instr;
6478 char_u *p;
6479 isn_T *isn;
6480
6481 // end block scope from :try or :catch
6482 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6483 compile_endblock(cctx);
6484 scope = cctx->ctx_scope;
6485
6486 // Error if not in a :try scope
6487 if (scope == NULL || scope->se_type != TRY_SCOPE)
6488 {
6489 emsg(_(e_catch));
6490 return NULL;
6491 }
6492
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006493 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006495 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 return NULL;
6497 }
6498
6499 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006500 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 JUMP_ALWAYS, cctx) == FAIL)
6502 return NULL;
6503
6504 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006505 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006506 if (isn->isn_arg.try.try_catch == 0)
6507 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006508 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 {
6510 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006511 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 isn->isn_arg.jump.jump_where = instr->ga_len;
6513 }
6514
6515 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006516 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006517 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006518 scope->se_u.se_try.ts_caught_all = TRUE;
6519 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 }
6521 else
6522 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006523 char_u *end;
6524 char_u *pat;
6525 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006526 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006527 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 // Push v:exception, push {expr} and MATCH
6530 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6531
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006532 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006533 if (*end != *p)
6534 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006535 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006536 vim_free(tofree);
6537 return FAIL;
6538 }
6539 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006540 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006541 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006542 len = (int)(end - tofree);
6543 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006544 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006545 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006546 if (pat == NULL)
6547 return FAIL;
6548 if (generate_PUSHS(cctx, pat) == FAIL)
6549 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6552 return NULL;
6553
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006554 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6556 return NULL;
6557 }
6558
6559 if (generate_instr(cctx, ISN_CATCH) == NULL)
6560 return NULL;
6561
6562 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6563 return NULL;
6564 return p;
6565}
6566
6567 static char_u *
6568compile_finally(char_u *arg, cctx_T *cctx)
6569{
6570 scope_T *scope = cctx->ctx_scope;
6571 garray_T *instr = &cctx->ctx_instr;
6572 isn_T *isn;
6573
6574 // end block scope from :try or :catch
6575 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6576 compile_endblock(cctx);
6577 scope = cctx->ctx_scope;
6578
6579 // Error if not in a :try scope
6580 if (scope == NULL || scope->se_type != TRY_SCOPE)
6581 {
6582 emsg(_(e_finally));
6583 return NULL;
6584 }
6585
6586 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006587 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 if (isn->isn_arg.try.try_finally != 0)
6589 {
6590 emsg(_(e_finally_dup));
6591 return NULL;
6592 }
6593
6594 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006595 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596
Bram Moolenaar585fea72020-04-02 22:33:21 +02006597 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006598 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 {
6600 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006601 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006603 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 }
6605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 // TODO: set index in ts_finally_label jumps
6607
6608 return arg;
6609}
6610
6611 static char_u *
6612compile_endtry(char_u *arg, cctx_T *cctx)
6613{
6614 scope_T *scope = cctx->ctx_scope;
6615 garray_T *instr = &cctx->ctx_instr;
6616 isn_T *isn;
6617
6618 // end block scope from :catch or :finally
6619 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6620 compile_endblock(cctx);
6621 scope = cctx->ctx_scope;
6622
6623 // Error if not in a :try scope
6624 if (scope == NULL || scope->se_type != TRY_SCOPE)
6625 {
6626 if (scope == NULL)
6627 emsg(_(e_no_endtry));
6628 else if (scope->se_type == WHILE_SCOPE)
6629 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006630 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 emsg(_(e_endfor));
6632 else
6633 emsg(_(e_endif));
6634 return NULL;
6635 }
6636
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006637 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006640 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 return NULL;
6642 }
6643
6644 // Fill in the "end" label in jumps at the end of the blocks, if not done
6645 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006646 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647
6648 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006649 if (isn->isn_arg.try.try_catch == 0)
6650 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 if (isn->isn_arg.try.try_finally == 0)
6652 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006653
6654 if (scope->se_u.se_try.ts_catch_label != 0)
6655 {
6656 // Last catch without match jumps here
6657 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6658 isn->isn_arg.jump.jump_where = instr->ga_len;
6659 }
6660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 compile_endblock(cctx);
6662
6663 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6664 return NULL;
6665 return arg;
6666}
6667
6668/*
6669 * compile "throw {expr}"
6670 */
6671 static char_u *
6672compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6673{
6674 char_u *p = skipwhite(arg);
6675
Bram Moolenaara5565e42020-05-09 15:44:01 +02006676 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 return NULL;
6678 if (may_generate_2STRING(-1, cctx) == FAIL)
6679 return NULL;
6680 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6681 return NULL;
6682
6683 return p;
6684}
6685
6686/*
6687 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006688 * compile "echomsg expr"
6689 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006690 * compile "execute expr"
6691 */
6692 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006693compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006694{
6695 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006696 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006697 int count = 0;
6698
6699 for (;;)
6700 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006701 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006702 return NULL;
6703 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006704 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006705 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006706 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006707 break;
6708 }
6709
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006710 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6711 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6712 else if (cmdidx == CMD_execute)
6713 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6714 else if (cmdidx == CMD_echomsg)
6715 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6716 else
6717 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 return p;
6719}
6720
6721/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006722 * :put r
6723 * :put ={expr}
6724 */
6725 static char_u *
6726compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6727{
6728 char_u *line = arg;
6729 linenr_T lnum;
6730 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006731 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006732
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006733 eap->regname = *line;
6734
6735 if (eap->regname == '=')
6736 {
6737 char_u *p = line + 1;
6738
6739 if (compile_expr0(&p, cctx) == FAIL)
6740 return NULL;
6741 line = p;
6742 }
6743 else if (eap->regname != NUL)
6744 ++line;
6745
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006746 // "errormsg" will not be set because the range is ADDR_LINES.
6747 // TODO: if the range contains something like "$" or "." need to evaluate
6748 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006749 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6750 return NULL;
6751 if (eap->addr_count == 0)
6752 lnum = -1;
6753 else
6754 lnum = eap->line2;
6755 if (above)
6756 --lnum;
6757
6758 generate_PUT(cctx, eap->regname, lnum);
6759 return line;
6760}
6761
6762/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006763 * A command that is not compiled, execute with legacy code.
6764 */
6765 static char_u *
6766compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6767{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006768 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006769 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006770 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006771
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006772 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006773 goto theend;
6774
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006775 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006776 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006777 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006778 int usefilter = FALSE;
6779
6780 has_expr = argt & (EX_XFILE | EX_EXPAND);
6781
6782 // If the command can be followed by a bar, find the bar and truncate
6783 // it, so that the following command can be compiled.
6784 // The '|' is overwritten with a NUL, it is put back below.
6785 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6786 && *eap->arg == '!')
6787 // :w !filter or :r !filter or :r! filter
6788 usefilter = TRUE;
6789 if ((argt & EX_TRLBAR) && !usefilter)
6790 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006791 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006792 separate_nextcmd(eap);
6793 if (eap->nextcmd != NULL)
6794 nextcmd = eap->nextcmd;
6795 }
6796 }
6797
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006798 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6799 {
6800 // expand filename in "syntax include [@group] filename"
6801 has_expr = TRUE;
6802 eap->arg = skipwhite(eap->arg + 7);
6803 if (*eap->arg == '@')
6804 eap->arg = skiptowhite(eap->arg);
6805 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006806
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006807 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006808 {
6809 int count = 0;
6810 char_u *start = skipwhite(line);
6811
6812 // :cmd xxx`=expr1`yyy`=expr2`zzz
6813 // PUSHS ":cmd xxx"
6814 // eval expr1
6815 // PUSHS "yyy"
6816 // eval expr2
6817 // PUSHS "zzz"
6818 // EXECCONCAT 5
6819 for (;;)
6820 {
6821 if (p > start)
6822 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006823 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006824 ++count;
6825 }
6826 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006827 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006828 return NULL;
6829 may_generate_2STRING(-1, cctx);
6830 ++count;
6831 p = skipwhite(p);
6832 if (*p != '`')
6833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006834 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006835 return NULL;
6836 }
6837 start = p + 1;
6838
6839 p = (char_u *)strstr((char *)start, "`=");
6840 if (p == NULL)
6841 {
6842 if (*skipwhite(start) != NUL)
6843 {
6844 generate_PUSHS(cctx, vim_strsave(start));
6845 ++count;
6846 }
6847 break;
6848 }
6849 }
6850 generate_EXECCONCAT(cctx, count);
6851 }
6852 else
6853 generate_EXEC(cctx, line);
6854
6855theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006856 if (*nextcmd != NUL)
6857 {
6858 // the parser expects a pointer to the bar, put it back
6859 --nextcmd;
6860 *nextcmd = '|';
6861 }
6862
6863 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006864}
6865
6866/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006867 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006868 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006869 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006870 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006871add_def_function(ufunc_T *ufunc)
6872{
6873 dfunc_T *dfunc;
6874
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006875 if (def_functions.ga_len == 0)
6876 {
6877 // The first position is not used, so that a zero uf_dfunc_idx means it
6878 // wasn't set.
6879 if (ga_grow(&def_functions, 1) == FAIL)
6880 return FAIL;
6881 ++def_functions.ga_len;
6882 }
6883
Bram Moolenaar09689a02020-05-09 22:50:08 +02006884 // Add the function to "def_functions".
6885 if (ga_grow(&def_functions, 1) == FAIL)
6886 return FAIL;
6887 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6888 CLEAR_POINTER(dfunc);
6889 dfunc->df_idx = def_functions.ga_len;
6890 ufunc->uf_dfunc_idx = dfunc->df_idx;
6891 dfunc->df_ufunc = ufunc;
6892 ++def_functions.ga_len;
6893 return OK;
6894}
6895
6896/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 * After ex_function() has collected all the function lines: parse and compile
6898 * the lines into instructions.
6899 * Adds the function to "def_functions".
6900 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6901 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006902 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006903 * This can be used recursively through compile_lambda(), which may reallocate
6904 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006905 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006907 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006908compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 char_u *line = NULL;
6911 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 cctx_T cctx;
6914 garray_T *instr;
6915 int called_emsg_before = called_emsg;
6916 int ret = FAIL;
6917 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006918 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006919 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006920 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006921 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006923 // When using a function that was compiled before: Free old instructions.
6924 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006925 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006927 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6928 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006929 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006931 else
6932 {
6933 if (add_def_function(ufunc) == FAIL)
6934 return FAIL;
6935 new_def_function = TRUE;
6936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937
Bram Moolenaar985116a2020-07-12 17:31:09 +02006938 ufunc->uf_def_status = UF_COMPILING;
6939
Bram Moolenaara80faa82020-04-12 19:37:17 +02006940 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 cctx.ctx_ufunc = ufunc;
6942 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006943 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6945 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6946 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6947 cctx.ctx_type_list = &ufunc->uf_type_list;
6948 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6949 instr = &cctx.ctx_instr;
6950
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006951 // Set the context to the function, it may be compiled when called from
6952 // another script. Set the script version to the most modern one.
6953 // The line number will be set in next_line_from_context().
6954 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6956
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006957 // Make sure error messages are OK.
6958 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6959 if (do_estack_push)
6960 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006961 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006962
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006963 if (ufunc->uf_def_args.ga_len > 0)
6964 {
6965 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006966 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006967 int i;
6968 char_u *arg;
6969 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006970 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006971
6972 // Produce instructions for the default values of optional arguments.
6973 // Store the instruction index in uf_def_arg_idx[] so that we know
6974 // where to start when the function is called, depending on the number
6975 // of arguments.
6976 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6977 if (ufunc->uf_def_arg_idx == NULL)
6978 goto erret;
6979 for (i = 0; i < count; ++i)
6980 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006981 garray_T *stack = &cctx.ctx_type_stack;
6982 type_T *val_type;
6983 int arg_idx = first_def_arg + i;
6984
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006985 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6986 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006987 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006988 goto erret;
6989
6990 // If no type specified use the type of the default value.
6991 // Otherwise check that the default value type matches the
6992 // specified type.
6993 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6994 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006995 {
6996 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006997 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006998 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006999 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7000 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007001 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007002
7003 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007004 goto erret;
7005 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007006 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007007
7008 if (did_set_arg_type)
7009 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007010 }
7011
7012 /*
7013 * Loop over all the lines of the function and generate instructions.
7014 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 for (;;)
7016 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007017 exarg_T ea;
7018 cmdmod_T save_cmdmod;
7019 int starts_with_colon = FALSE;
7020 char_u *cmd;
7021 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007022
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007023 // Bail out on the first error to avoid a flood of errors and report
7024 // the right line number when inside try/catch.
7025 if (emsg_before != called_emsg)
7026 goto erret;
7027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 if (line != NULL && *line == '|')
7029 // the line continues after a '|'
7030 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007031 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007032 && !(*line == '#' && (line == cctx.ctx_line_start
7033 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007035 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 goto erret;
7037 }
7038 else
7039 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007040 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007041 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007042 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007045 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046
Bram Moolenaara80faa82020-04-12 19:37:17 +02007047 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 ea.cmdlinep = &line;
7049 ea.cmd = skipwhite(line);
7050
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007051 // Some things can be recognized by the first character.
7052 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007054 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007055 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007056 if (ea.cmd[1] != '{')
7057 {
7058 line = (char_u *)"";
7059 continue;
7060 }
7061 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007063 case '}':
7064 {
7065 // "}" ends a block scope
7066 scopetype_T stype = cctx.ctx_scope == NULL
7067 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007069 if (stype == BLOCK_SCOPE)
7070 {
7071 compile_endblock(&cctx);
7072 line = ea.cmd;
7073 }
7074 else
7075 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007076 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007077 goto erret;
7078 }
7079 if (line != NULL)
7080 line = skipwhite(ea.cmd + 1);
7081 continue;
7082 }
7083
7084 case '{':
7085 // "{" starts a block scope
7086 // "{'a': 1}->func() is something else
7087 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7088 {
7089 line = compile_block(ea.cmd, &cctx);
7090 continue;
7091 }
7092 break;
7093
7094 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007095 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007096 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 }
7098
7099 /*
7100 * COMMAND MODIFIERS
7101 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007102 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7104 {
7105 if (errormsg != NULL)
7106 goto erret;
7107 // empty line or comment
7108 line = (char_u *)"";
7109 continue;
7110 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007111 // TODO: use modifiers in the command
7112 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007113 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114
7115 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007116 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007118 {
7119 if (*ea.cmd == '(')
7120 // not for "call()"
7121 ea.cmd = p;
7122 else
7123 ea.cmd = skipwhite(ea.cmd);
7124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007126 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007128 char_u *pskip;
7129
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007130 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007131 // find what follows.
7132 // Skip over "var.member", "var[idx]" and the like.
7133 // Also "&opt = val", "$ENV = val" and "@r = val".
7134 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007135 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007136 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007137 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007139 char_u *var_end;
7140 int oplen;
7141 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142
Bram Moolenaar65821722020-08-02 18:58:54 +02007143 if (ea.cmd[0] == '@')
7144 var_end = ea.cmd + 2;
7145 else
7146 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007147 FNE_CHECK_START | FNE_INCL_BR);
7148 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007149 if (oplen > 0)
7150 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007151 size_t len = p - ea.cmd;
7152
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007153 // Recognize an assignment if we recognize the variable
7154 // name:
7155 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007156 // "local = expr" where "local" is a local var.
7157 // "script = expr" where "script" is a script-local var.
7158 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007159 // "&opt = expr"
7160 // "$ENV = expr"
7161 // "@r = expr"
7162 if (*ea.cmd == '&'
7163 || *ea.cmd == '$'
7164 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007165 || ((len) > 2 && ea.cmd[1] == ':')
7166 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007167 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007168 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007169 || script_var_exists(ea.cmd, len,
7170 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007171 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007172 {
7173 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007174 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007175 goto erret;
7176 continue;
7177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 }
7179 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007180
7181 if (*ea.cmd == '[')
7182 {
7183 // [var, var] = expr
7184 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7185 if (line == NULL)
7186 goto erret;
7187 if (line != ea.cmd)
7188 continue;
7189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 }
7191
7192 /*
7193 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007194 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007196 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007197 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007198 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007199 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007200 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007201 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007202 if (!starts_with_colon)
7203 {
7204 emsg(_(e_colon_required_before_a_range));
7205 goto erret;
7206 }
7207 if (ends_excmd2(line, ea.cmd))
7208 {
7209 // A range without a command: jump to the line.
7210 // TODO: compile to a more efficient command, possibly
7211 // calling parse_cmd_address().
7212 ea.cmdidx = CMD_SIZE;
7213 line = compile_exec(line, &ea, &cctx);
7214 goto nextline;
7215 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007216 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007217 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007218 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007219 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007220 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221
7222 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7223 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007224 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007225 {
7226 line += STRLEN(line);
7227 continue;
7228 }
7229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007231 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007233 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007234 iemsg("Command from find_ex_command() not handled");
7235 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 }
7238
Bram Moolenaar3988f642020-08-27 22:43:03 +02007239 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007240 && ea.cmdidx != CMD_elseif
7241 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007242 && ea.cmdidx != CMD_endif
7243 && ea.cmdidx != CMD_endfor
7244 && ea.cmdidx != CMD_endwhile
7245 && ea.cmdidx != CMD_catch
7246 && ea.cmdidx != CMD_finally
7247 && ea.cmdidx != CMD_endtry)
7248 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007249 emsg(_(e_unreachable_code_after_return));
7250 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007251 }
7252
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007253 p = skipwhite(p);
7254 if (ea.cmdidx != CMD_SIZE
7255 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7256 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007257 if (ea.cmdidx >= 0)
7258 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007259 if ((ea.argt & EX_BANG) && *p == '!')
7260 {
7261 ea.forceit = TRUE;
7262 p = skipwhite(p + 1);
7263 }
7264 }
7265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 switch (ea.cmdidx)
7267 {
7268 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007269 ea.arg = p;
7270 line = compile_nested_function(&ea, &cctx);
7271 break;
7272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007274 // TODO: should we allow this, e.g. to declare a global
7275 // function?
7276 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 goto erret;
7278
7279 case CMD_return:
7280 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007281 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 break;
7283
7284 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007285 if (get_vim_var_nr(VV_DISALLOW_LET))
7286 {
7287 emsg(_(e_cannot_use_let_in_vim9_script));
7288 break;
7289 }
7290 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007291 case CMD_var:
7292 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 case CMD_const:
7294 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007295 if (line == p)
7296 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 break;
7298
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007299 case CMD_unlet:
7300 case CMD_unlockvar:
7301 case CMD_lockvar:
7302 line = compile_unletlock(p, &ea, &cctx);
7303 break;
7304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 case CMD_import:
7306 line = compile_import(p, &cctx);
7307 break;
7308
7309 case CMD_if:
7310 line = compile_if(p, &cctx);
7311 break;
7312 case CMD_elseif:
7313 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007314 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 break;
7316 case CMD_else:
7317 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007318 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 break;
7320 case CMD_endif:
7321 line = compile_endif(p, &cctx);
7322 break;
7323
7324 case CMD_while:
7325 line = compile_while(p, &cctx);
7326 break;
7327 case CMD_endwhile:
7328 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007329 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 break;
7331
7332 case CMD_for:
7333 line = compile_for(p, &cctx);
7334 break;
7335 case CMD_endfor:
7336 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007337 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 break;
7339 case CMD_continue:
7340 line = compile_continue(p, &cctx);
7341 break;
7342 case CMD_break:
7343 line = compile_break(p, &cctx);
7344 break;
7345
7346 case CMD_try:
7347 line = compile_try(p, &cctx);
7348 break;
7349 case CMD_catch:
7350 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007351 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 break;
7353 case CMD_finally:
7354 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007355 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 break;
7357 case CMD_endtry:
7358 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007359 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 break;
7361 case CMD_throw:
7362 line = compile_throw(p, &cctx);
7363 break;
7364
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007365 case CMD_eval:
7366 if (compile_expr0(&p, &cctx) == FAIL)
7367 goto erret;
7368
Bram Moolenaar3988f642020-08-27 22:43:03 +02007369 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007370 generate_instr_drop(&cctx, ISN_DROP, 1);
7371
7372 line = skipwhite(p);
7373 break;
7374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007377 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007378 case CMD_echomsg:
7379 case CMD_echoerr:
7380 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007381 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007383 case CMD_put:
7384 ea.cmd = cmd;
7385 line = compile_put(p, &ea, &cctx);
7386 break;
7387
Bram Moolenaar3988f642020-08-27 22:43:03 +02007388 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007389
Bram Moolenaarae616492020-07-28 20:07:27 +02007390 case CMD_append:
7391 case CMD_change:
7392 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007393 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007394 case CMD_xit:
7395 not_in_vim9(&ea);
7396 goto erret;
7397
Bram Moolenaar002262f2020-07-08 17:47:57 +02007398 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007399 if (cctx.ctx_skip != SKIP_YES)
7400 {
7401 semsg(_(e_invalid_command_str), ea.cmd);
7402 goto erret;
7403 }
7404 // We don't check for a next command here.
7405 line = (char_u *)"";
7406 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007409 if (cctx.ctx_skip == SKIP_YES)
7410 {
7411 // We don't check for a next command here.
7412 line = (char_u *)"";
7413 }
7414 else
7415 {
7416 // Not recognized, execute with do_cmdline_cmd().
7417 ea.arg = p;
7418 line = compile_exec(line, &ea, &cctx);
7419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 break;
7421 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007422nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 if (line == NULL)
7424 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007425 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426
7427 if (cctx.ctx_type_stack.ga_len < 0)
7428 {
7429 iemsg("Type stack underflow");
7430 goto erret;
7431 }
7432 }
7433
7434 if (cctx.ctx_scope != NULL)
7435 {
7436 if (cctx.ctx_scope->se_type == IF_SCOPE)
7437 emsg(_(e_endif));
7438 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7439 emsg(_(e_endwhile));
7440 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7441 emsg(_(e_endfor));
7442 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007443 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 goto erret;
7445 }
7446
Bram Moolenaarefd88552020-06-18 20:50:10 +02007447 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 {
7449 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7450 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007451 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 goto erret;
7453 }
7454
7455 // Return zero if there is no return at the end.
7456 generate_PUSHNR(&cctx, 0);
7457 generate_instr(&cctx, ISN_RETURN);
7458 }
7459
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007460 {
7461 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7462 + ufunc->uf_dfunc_idx;
7463 dfunc->df_deleted = FALSE;
7464 dfunc->df_instr = instr->ga_data;
7465 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007466 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007467 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007468 if (cctx.ctx_outer_used)
7469 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007470 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472
7473 ret = OK;
7474
7475erret:
7476 if (ret == FAIL)
7477 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007478 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007479 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7480 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007481
7482 for (idx = 0; idx < instr->ga_len; ++idx)
7483 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007485
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007486 // If using the last entry in the table and it was added above, we
7487 // might as well remove it.
7488 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007489 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007490 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007491 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007492 ufunc->uf_dfunc_idx = 0;
7493 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007494 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007495
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007496 while (cctx.ctx_scope != NULL)
7497 drop_scope(&cctx);
7498
Bram Moolenaar20431c92020-03-20 18:39:46 +01007499 // Don't execute this function body.
7500 ga_clear_strings(&ufunc->uf_lines);
7501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 if (errormsg != NULL)
7503 emsg(errormsg);
7504 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007505 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 }
7507
7508 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007509 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007510 if (do_estack_push)
7511 estack_pop();
7512
Bram Moolenaar20431c92020-03-20 18:39:46 +01007513 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007514 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007516 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517}
7518
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007519 void
7520set_function_type(ufunc_T *ufunc)
7521{
7522 int varargs = ufunc->uf_va_name != NULL;
7523 int argcount = ufunc->uf_args.ga_len;
7524
7525 // Create a type for the function, with the return type and any
7526 // argument types.
7527 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7528 // The type is included in "tt_args".
7529 if (argcount > 0 || varargs)
7530 {
7531 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7532 argcount, &ufunc->uf_type_list);
7533 // Add argument types to the function type.
7534 if (func_type_add_arg_types(ufunc->uf_func_type,
7535 argcount + varargs,
7536 &ufunc->uf_type_list) == FAIL)
7537 return;
7538 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7539 ufunc->uf_func_type->tt_min_argcount =
7540 argcount - ufunc->uf_def_args.ga_len;
7541 if (ufunc->uf_arg_types == NULL)
7542 {
7543 int i;
7544
7545 // lambda does not have argument types.
7546 for (i = 0; i < argcount; ++i)
7547 ufunc->uf_func_type->tt_args[i] = &t_any;
7548 }
7549 else
7550 mch_memmove(ufunc->uf_func_type->tt_args,
7551 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7552 if (varargs)
7553 {
7554 ufunc->uf_func_type->tt_args[argcount] =
7555 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7556 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7557 }
7558 }
7559 else
7560 // No arguments, can use a predefined type.
7561 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7562 argcount, &ufunc->uf_type_list);
7563}
7564
7565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007566/*
7567 * Delete an instruction, free what it contains.
7568 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007569 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570delete_instr(isn_T *isn)
7571{
7572 switch (isn->isn_type)
7573 {
7574 case ISN_EXEC:
7575 case ISN_LOADENV:
7576 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007577 case ISN_LOADB:
7578 case ISN_LOADW:
7579 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007581 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 case ISN_PUSHEXC:
7583 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007584 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007586 case ISN_STOREB:
7587 case ISN_STOREW:
7588 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007589 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 vim_free(isn->isn_arg.string);
7591 break;
7592
7593 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007594 case ISN_STORES:
7595 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596 break;
7597
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007598 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007599 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007600 vim_free(isn->isn_arg.unlet.ul_name);
7601 break;
7602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603 case ISN_STOREOPT:
7604 vim_free(isn->isn_arg.storeopt.so_name);
7605 break;
7606
7607 case ISN_PUSHBLOB: // push blob isn_arg.blob
7608 blob_unref(isn->isn_arg.blob);
7609 break;
7610
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007611 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007612#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007613 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007614#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007615 break;
7616
7617 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007618#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007619 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007620#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007621 break;
7622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 case ISN_UCALL:
7624 vim_free(isn->isn_arg.ufunc.cuf_name);
7625 break;
7626
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007627 case ISN_FUNCREF:
7628 {
7629 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7630 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007631
7632 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7633 func_ptr_unref(dfunc->df_ufunc);
7634 }
7635 break;
7636
7637 case ISN_DCALL:
7638 {
7639 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7640 + isn->isn_arg.dfunc.cdf_idx;
7641
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007642 if (dfunc->df_ufunc != NULL
7643 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007644 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007645 }
7646 break;
7647
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007648 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007649 {
7650 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7651 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7652
7653 if (ufunc != NULL)
7654 {
7655 // Clear uf_dfunc_idx so that the function is deleted.
7656 clear_def_function(ufunc);
7657 ufunc->uf_dfunc_idx = 0;
7658 func_ptr_unref(ufunc);
7659 }
7660
7661 vim_free(lambda);
7662 vim_free(isn->isn_arg.newfunc.nf_global);
7663 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007664 break;
7665
Bram Moolenaar5e654232020-09-16 15:22:00 +02007666 case ISN_CHECKTYPE:
7667 free_type(isn->isn_arg.type.ct_type);
7668 break;
7669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670 case ISN_2BOOL:
7671 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007672 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 case ISN_ADDBLOB:
7674 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007675 case ISN_ANYINDEX:
7676 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677 case ISN_BCALL:
7678 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007679 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 case ISN_COMPAREANY:
7682 case ISN_COMPAREBLOB:
7683 case ISN_COMPAREBOOL:
7684 case ISN_COMPAREDICT:
7685 case ISN_COMPAREFLOAT:
7686 case ISN_COMPAREFUNC:
7687 case ISN_COMPARELIST:
7688 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689 case ISN_COMPARESPECIAL:
7690 case ISN_COMPARESTRING:
7691 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007692 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 case ISN_DROP:
7694 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007695 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007696 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007698 case ISN_EXECCONCAT:
7699 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007701 case ISN_GETITEM:
7702 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007703 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007704 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007705 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007707 case ISN_LOADBDICT:
7708 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007709 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007711 case ISN_LOADSCRIPT:
7712 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007714 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007715 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007716 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717 case ISN_NEGATENR:
7718 case ISN_NEWDICT:
7719 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007720 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007721 case ISN_OPFLOAT:
7722 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007723 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007724 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007725 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007726 case ISN_PUSHF:
7727 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007729 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007731 case ISN_SHUFFLE:
7732 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007733 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007734 case ISN_STOREDICT:
7735 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007736 case ISN_STORENR:
7737 case ISN_STOREOUTER:
7738 case ISN_STOREREG:
7739 case ISN_STORESCRIPT:
7740 case ISN_STOREV:
7741 case ISN_STRINDEX:
7742 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 case ISN_THROW:
7744 case ISN_TRY:
7745 // nothing allocated
7746 break;
7747 }
7748}
7749
7750/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007751 * Free all instructions for "dfunc".
7752 */
7753 static void
7754delete_def_function_contents(dfunc_T *dfunc)
7755{
7756 int idx;
7757
7758 ga_clear(&dfunc->df_def_args_isn);
7759
7760 if (dfunc->df_instr != NULL)
7761 {
7762 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7763 delete_instr(dfunc->df_instr + idx);
7764 VIM_CLEAR(dfunc->df_instr);
7765 }
7766
7767 dfunc->df_deleted = TRUE;
7768}
7769
7770/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007771 * When a user function is deleted, clear the contents of any associated def
7772 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773 */
7774 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007775clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007777 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 {
7779 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7780 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781
Bram Moolenaar20431c92020-03-20 18:39:46 +01007782 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007783 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 }
7785}
7786
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007787/*
7788 * Used when a user function is about to be deleted: remove the pointer to it.
7789 * The entry in def_functions is then unused.
7790 */
7791 void
7792unlink_def_function(ufunc_T *ufunc)
7793{
7794 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7795
7796 dfunc->df_ufunc = NULL;
7797}
7798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007800/*
7801 * Free all functions defined with ":def".
7802 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 void
7804free_def_functions(void)
7805{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007806 int idx;
7807
7808 for (idx = 0; idx < def_functions.ga_len; ++idx)
7809 {
7810 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7811
7812 delete_def_function_contents(dfunc);
7813 }
7814
7815 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816}
7817#endif
7818
7819
7820#endif // FEAT_EVAL