blob: 55368d1b5cb2fbd5e4086e0510fda0556d3c1fb2 [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 Moolenaarc8cd2b32020-05-01 19:29:08 +0200818 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200819 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200820 * - "actual" is a type that can be "expected" type: add a runtime check; or
821 * - return FAIL.
822 */
823 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200824need_type(
825 type_T *actual,
826 type_T *expected,
827 int offset,
828 cctx_T *cctx,
829 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200830{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200831 if (expected == &t_bool && actual != &t_bool
832 && (actual->tt_flags & TTFLAG_BOOL_OK))
833 {
834 // Using "0", "1" or the result of an expression with "&&" or "||" as a
835 // boolean is OK but requires a conversion.
836 generate_2BOOL(cctx, FALSE);
837 return OK;
838 }
839
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200840 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200842
843 // If the actual type can be the expected type add a runtime check.
844 // TODO: if it's a constant a runtime check makes no sense.
845 if (actual->tt_type == VAR_ANY
846 || actual->tt_type == VAR_UNKNOWN
847 || (actual->tt_type == VAR_FUNC
848 && (expected->tt_type == VAR_FUNC
849 || expected->tt_type == VAR_PARTIAL)
850 && (actual->tt_member == &t_any || actual->tt_argcount < 0))
851 || (actual->tt_type == VAR_LIST
852 && expected->tt_type == VAR_LIST
853 && actual->tt_member == &t_any)
854 || (actual->tt_type == VAR_DICT
855 && expected->tt_type == VAR_DICT
856 && actual->tt_member == &t_any))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200857 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200858 generate_TYPECHECK(cctx, expected, offset);
859 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200860 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200861
862 if (!silent)
863 type_mismatch(expected, actual);
864 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200865}
866
867/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 * Generate an ISN_PUSHNR instruction.
869 */
870 static int
871generate_PUSHNR(cctx_T *cctx, varnumber_T number)
872{
873 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200874 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875
Bram Moolenaar080457c2020-03-03 21:53:32 +0100876 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
878 return FAIL;
879 isn->isn_arg.number = number;
880
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200881 if (number == 0 || number == 1)
882 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200883 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200884
885 // A 0 or 1 number can also be used as a bool.
886 if (type != NULL)
887 {
888 type->tt_type = VAR_NUMBER;
889 type->tt_flags = TTFLAG_BOOL_OK;
890 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
891 }
892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 return OK;
894}
895
896/*
897 * Generate an ISN_PUSHBOOL instruction.
898 */
899 static int
900generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
901{
902 isn_T *isn;
903
Bram Moolenaar080457c2020-03-03 21:53:32 +0100904 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
906 return FAIL;
907 isn->isn_arg.number = number;
908
909 return OK;
910}
911
912/*
913 * Generate an ISN_PUSHSPEC instruction.
914 */
915 static int
916generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
917{
918 isn_T *isn;
919
Bram Moolenaar080457c2020-03-03 21:53:32 +0100920 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
922 return FAIL;
923 isn->isn_arg.number = number;
924
925 return OK;
926}
927
928#ifdef FEAT_FLOAT
929/*
930 * Generate an ISN_PUSHF instruction.
931 */
932 static int
933generate_PUSHF(cctx_T *cctx, float_T fnumber)
934{
935 isn_T *isn;
936
Bram Moolenaar080457c2020-03-03 21:53:32 +0100937 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
939 return FAIL;
940 isn->isn_arg.fnumber = fnumber;
941
942 return OK;
943}
944#endif
945
946/*
947 * Generate an ISN_PUSHS instruction.
948 * Consumes "str".
949 */
950 static int
951generate_PUSHS(cctx_T *cctx, char_u *str)
952{
953 isn_T *isn;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
957 return FAIL;
958 isn->isn_arg.string = str;
959
960 return OK;
961}
962
963/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100964 * Generate an ISN_PUSHCHANNEL instruction.
965 * Consumes "channel".
966 */
967 static int
968generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
969{
970 isn_T *isn;
971
Bram Moolenaar080457c2020-03-03 21:53:32 +0100972 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100973 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
974 return FAIL;
975 isn->isn_arg.channel = channel;
976
977 return OK;
978}
979
980/*
981 * Generate an ISN_PUSHJOB instruction.
982 * Consumes "job".
983 */
984 static int
985generate_PUSHJOB(cctx_T *cctx, job_T *job)
986{
987 isn_T *isn;
988
Bram Moolenaar080457c2020-03-03 21:53:32 +0100989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100990 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100991 return FAIL;
992 isn->isn_arg.job = job;
993
994 return OK;
995}
996
997/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 * Generate an ISN_PUSHBLOB instruction.
999 * Consumes "blob".
1000 */
1001 static int
1002generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1003{
1004 isn_T *isn;
1005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.blob = blob;
1010
1011 return OK;
1012}
1013
1014/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001015 * Generate an ISN_PUSHFUNC instruction with name "name".
1016 * Consumes "name".
1017 */
1018 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001019generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001020{
1021 isn_T *isn;
1022
Bram Moolenaar080457c2020-03-03 21:53:32 +01001023 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001024 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001025 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001026 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001027
1028 return OK;
1029}
1030
1031/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001032 * Generate an ISN_GETITEM instruction with "index".
1033 */
1034 static int
1035generate_GETITEM(cctx_T *cctx, int index)
1036{
1037 isn_T *isn;
1038 garray_T *stack = &cctx->ctx_type_stack;
1039 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1040 type_T *item_type = &t_any;
1041
1042 RETURN_OK_IF_SKIP(cctx);
1043
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001044 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001046 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001047 emsg(_(e_listreq));
1048 return FAIL;
1049 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001050 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001051 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1052 return FAIL;
1053 isn->isn_arg.number = index;
1054
1055 // add the item type to the type stack
1056 if (ga_grow(stack, 1) == FAIL)
1057 return FAIL;
1058 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1059 ++stack->ga_len;
1060 return OK;
1061}
1062
1063/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001064 * Generate an ISN_SLICE instruction with "count".
1065 */
1066 static int
1067generate_SLICE(cctx_T *cctx, int count)
1068{
1069 isn_T *isn;
1070
1071 RETURN_OK_IF_SKIP(cctx);
1072 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1073 return FAIL;
1074 isn->isn_arg.number = count;
1075 return OK;
1076}
1077
1078/*
1079 * Generate an ISN_CHECKLEN instruction with "min_len".
1080 */
1081 static int
1082generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1083{
1084 isn_T *isn;
1085
1086 RETURN_OK_IF_SKIP(cctx);
1087
1088 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.checklen.cl_min_len = min_len;
1091 isn->isn_arg.checklen.cl_more_OK = more_OK;
1092
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 * Generate an ISN_STORE instruction.
1098 */
1099 static int
1100generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1101{
1102 isn_T *isn;
1103
Bram Moolenaar080457c2020-03-03 21:53:32 +01001104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1106 return FAIL;
1107 if (name != NULL)
1108 isn->isn_arg.string = vim_strsave(name);
1109 else
1110 isn->isn_arg.number = idx;
1111
1112 return OK;
1113}
1114
1115/*
1116 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1117 */
1118 static int
1119generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1120{
1121 isn_T *isn;
1122
Bram Moolenaar080457c2020-03-03 21:53:32 +01001123 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1125 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001126 isn->isn_arg.storenr.stnr_idx = idx;
1127 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128
1129 return OK;
1130}
1131
1132/*
1133 * Generate an ISN_STOREOPT instruction
1134 */
1135 static int
1136generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1137{
1138 isn_T *isn;
1139
Bram Moolenaar080457c2020-03-03 21:53:32 +01001140 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001141 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 return FAIL;
1143 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1144 isn->isn_arg.storeopt.so_flags = opt_flags;
1145
1146 return OK;
1147}
1148
1149/*
1150 * Generate an ISN_LOAD or similar instruction.
1151 */
1152 static int
1153generate_LOAD(
1154 cctx_T *cctx,
1155 isntype_T isn_type,
1156 int idx,
1157 char_u *name,
1158 type_T *type)
1159{
1160 isn_T *isn;
1161
Bram Moolenaar080457c2020-03-03 21:53:32 +01001162 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1164 return FAIL;
1165 if (name != NULL)
1166 isn->isn_arg.string = vim_strsave(name);
1167 else
1168 isn->isn_arg.number = idx;
1169
1170 return OK;
1171}
1172
1173/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001174 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001175 */
1176 static int
1177generate_LOADV(
1178 cctx_T *cctx,
1179 char_u *name,
1180 int error)
1181{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001182 int di_flags;
1183 int vidx = find_vim_var(name, &di_flags);
1184 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001185
Bram Moolenaar080457c2020-03-03 21:53:32 +01001186 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001187 if (vidx < 0)
1188 {
1189 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001190 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001191 return FAIL;
1192 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001193 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001194
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001195 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001196}
1197
1198/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001199 * Generate an ISN_UNLET instruction.
1200 */
1201 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001202generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001203{
1204 isn_T *isn;
1205
1206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001207 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001208 return FAIL;
1209 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1210 isn->isn_arg.unlet.ul_forceit = forceit;
1211
1212 return OK;
1213}
1214
1215/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001216 * Generate an ISN_LOCKCONST instruction.
1217 */
1218 static int
1219generate_LOCKCONST(cctx_T *cctx)
1220{
1221 isn_T *isn;
1222
1223 RETURN_OK_IF_SKIP(cctx);
1224 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1225 return FAIL;
1226 return OK;
1227}
1228
1229/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 * Generate an ISN_LOADS instruction.
1231 */
1232 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001233generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001235 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237 int sid,
1238 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239{
1240 isn_T *isn;
1241
Bram Moolenaar080457c2020-03-03 21:53:32 +01001242 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001243 if (isn_type == ISN_LOADS)
1244 isn = generate_instr_type(cctx, isn_type, type);
1245 else
1246 isn = generate_instr_drop(cctx, isn_type, 1);
1247 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001249 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1250 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251
1252 return OK;
1253}
1254
1255/*
1256 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1257 */
1258 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001259generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 cctx_T *cctx,
1261 isntype_T isn_type,
1262 int sid,
1263 int idx,
1264 type_T *type)
1265{
1266 isn_T *isn;
1267
Bram Moolenaar080457c2020-03-03 21:53:32 +01001268 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if (isn_type == ISN_LOADSCRIPT)
1270 isn = generate_instr_type(cctx, isn_type, type);
1271 else
1272 isn = generate_instr_drop(cctx, isn_type, 1);
1273 if (isn == NULL)
1274 return FAIL;
1275 isn->isn_arg.script.script_sid = sid;
1276 isn->isn_arg.script.script_idx = idx;
1277 return OK;
1278}
1279
1280/*
1281 * Generate an ISN_NEWLIST instruction.
1282 */
1283 static int
1284generate_NEWLIST(cctx_T *cctx, int count)
1285{
1286 isn_T *isn;
1287 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 type_T *type;
1289 type_T *member;
1290
Bram Moolenaar080457c2020-03-03 21:53:32 +01001291 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1293 return FAIL;
1294 isn->isn_arg.number = count;
1295
Bram Moolenaar127542b2020-08-09 17:22:04 +02001296 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001297 if (count == 0)
1298 member = &t_void;
1299 else
1300 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001301 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1302 cctx->ctx_type_list);
1303 type = get_list_type(member, cctx->ctx_type_list);
1304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 // drop the value types
1306 stack->ga_len -= count;
1307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 // add the list type to the type stack
1309 if (ga_grow(stack, 1) == FAIL)
1310 return FAIL;
1311 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1312 ++stack->ga_len;
1313
1314 return OK;
1315}
1316
1317/*
1318 * Generate an ISN_NEWDICT instruction.
1319 */
1320 static int
1321generate_NEWDICT(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 type_T *type;
1326 type_T *member;
1327
Bram Moolenaar080457c2020-03-03 21:53:32 +01001328 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1330 return FAIL;
1331 isn->isn_arg.number = count;
1332
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001333 if (count == 0)
1334 member = &t_void;
1335 else
1336 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001337 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1338 cctx->ctx_type_list);
1339 type = get_dict_type(member, cctx->ctx_type_list);
1340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 // drop the key and value types
1342 stack->ga_len -= 2 * count;
1343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 // add the dict type to the type stack
1345 if (ga_grow(stack, 1) == FAIL)
1346 return FAIL;
1347 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1348 ++stack->ga_len;
1349
1350 return OK;
1351}
1352
1353/*
1354 * Generate an ISN_FUNCREF instruction.
1355 */
1356 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001357generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358{
1359 isn_T *isn;
1360 garray_T *stack = &cctx->ctx_type_stack;
1361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1364 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001365 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001366 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367
1368 if (ga_grow(stack, 1) == FAIL)
1369 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001370 ((type_T **)stack->ga_data)[stack->ga_len] =
1371 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 ++stack->ga_len;
1373
1374 return OK;
1375}
1376
1377/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001378 * Generate an ISN_NEWFUNC instruction.
1379 */
1380 static int
1381generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1382{
1383 isn_T *isn;
1384 char_u *name;
1385
1386 RETURN_OK_IF_SKIP(cctx);
1387 name = vim_strsave(lambda_name);
1388 if (name == NULL)
1389 return FAIL;
1390 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1391 return FAIL;
1392 isn->isn_arg.newfunc.nf_lambda = name;
1393 isn->isn_arg.newfunc.nf_global = func_name;
1394
1395 return OK;
1396}
1397
1398/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 * Generate an ISN_JUMP instruction.
1400 */
1401 static int
1402generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1403{
1404 isn_T *isn;
1405 garray_T *stack = &cctx->ctx_type_stack;
1406
Bram Moolenaar080457c2020-03-03 21:53:32 +01001407 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1409 return FAIL;
1410 isn->isn_arg.jump.jump_when = when;
1411 isn->isn_arg.jump.jump_where = where;
1412
1413 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1414 --stack->ga_len;
1415
1416 return OK;
1417}
1418
1419 static int
1420generate_FOR(cctx_T *cctx, int loop_idx)
1421{
1422 isn_T *isn;
1423 garray_T *stack = &cctx->ctx_type_stack;
1424
Bram Moolenaar080457c2020-03-03 21:53:32 +01001425 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1427 return FAIL;
1428 isn->isn_arg.forloop.for_idx = loop_idx;
1429
1430 if (ga_grow(stack, 1) == FAIL)
1431 return FAIL;
1432 // type doesn't matter, will be stored next
1433 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1434 ++stack->ga_len;
1435
1436 return OK;
1437}
1438
1439/*
1440 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001441 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 * Return FAIL if the number of arguments is wrong.
1443 */
1444 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001445generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446{
1447 isn_T *isn;
1448 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001449 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001450 type_T *argtypes[MAX_FUNC_ARGS];
1451 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452
Bram Moolenaar080457c2020-03-03 21:53:32 +01001453 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001454 argoff = check_internal_func(func_idx, argcount);
1455 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 return FAIL;
1457
Bram Moolenaar389df252020-07-09 21:20:47 +02001458 if (method_call && argoff > 1)
1459 {
1460 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1461 return FAIL;
1462 isn->isn_arg.shuffle.shfl_item = argcount;
1463 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1464 }
1465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1467 return FAIL;
1468 isn->isn_arg.bfunc.cbf_idx = func_idx;
1469 isn->isn_arg.bfunc.cbf_argcount = argcount;
1470
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001471 for (i = 0; i < argcount; ++i)
1472 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 stack->ga_len -= argcount; // drop the arguments
1475 if (ga_grow(stack, 1) == FAIL)
1476 return FAIL;
1477 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001478 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 ++stack->ga_len; // add return value
1480
1481 return OK;
1482}
1483
1484/*
1485 * Generate an ISN_DCALL or ISN_UCALL instruction.
1486 * Return FAIL if the number of arguments is wrong.
1487 */
1488 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001489generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490{
1491 isn_T *isn;
1492 garray_T *stack = &cctx->ctx_type_stack;
1493 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001494 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495
Bram Moolenaar080457c2020-03-03 21:53:32 +01001496 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 if (argcount > regular_args && !has_varargs(ufunc))
1498 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001499 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 return FAIL;
1501 }
1502 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1503 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001504 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 return FAIL;
1506 }
1507
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001508 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001509 {
1510 int i;
1511
1512 for (i = 0; i < argcount; ++i)
1513 {
1514 type_T *expected;
1515 type_T *actual;
1516
1517 if (i < regular_args)
1518 {
1519 if (ufunc->uf_arg_types == NULL)
1520 continue;
1521 expected = ufunc->uf_arg_types[i];
1522 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001523 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1524 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001525 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001526 else
1527 expected = ufunc->uf_va_type->tt_member;
1528 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001529 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001530 {
1531 arg_type_mismatch(expected, actual, i + 1);
1532 return FAIL;
1533 }
1534 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001535 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001536 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1537 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001538 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001539 }
1540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001542 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001543 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001545 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 {
1547 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1548 isn->isn_arg.dfunc.cdf_argcount = argcount;
1549 }
1550 else
1551 {
1552 // A user function may be deleted and redefined later, can't use the
1553 // ufunc pointer, need to look it up again at runtime.
1554 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1555 isn->isn_arg.ufunc.cuf_argcount = argcount;
1556 }
1557
1558 stack->ga_len -= argcount; // drop the arguments
1559 if (ga_grow(stack, 1) == FAIL)
1560 return FAIL;
1561 // add return value
1562 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1563 ++stack->ga_len;
1564
1565 return OK;
1566}
1567
1568/*
1569 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1570 */
1571 static int
1572generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1573{
1574 isn_T *isn;
1575 garray_T *stack = &cctx->ctx_type_stack;
1576
Bram Moolenaar080457c2020-03-03 21:53:32 +01001577 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1579 return FAIL;
1580 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1581 isn->isn_arg.ufunc.cuf_argcount = argcount;
1582
1583 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001584 if (ga_grow(stack, 1) == FAIL)
1585 return FAIL;
1586 // add return value
1587 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1588 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589
1590 return OK;
1591}
1592
1593/*
1594 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001595 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 */
1597 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001598generate_PCALL(
1599 cctx_T *cctx,
1600 int argcount,
1601 char_u *name,
1602 type_T *type,
1603 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604{
1605 isn_T *isn;
1606 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001607 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608
Bram Moolenaar080457c2020-03-03 21:53:32 +01001609 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001610
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001611 if (type->tt_type == VAR_ANY)
1612 ret_type = &t_any;
1613 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001614 {
1615 if (type->tt_argcount != -1)
1616 {
1617 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1618
1619 if (argcount < type->tt_min_argcount - varargs)
1620 {
1621 semsg(_(e_toofewarg), "[reference]");
1622 return FAIL;
1623 }
1624 if (!varargs && argcount > type->tt_argcount)
1625 {
1626 semsg(_(e_toomanyarg), "[reference]");
1627 return FAIL;
1628 }
1629 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001630 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001631 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001632 else
1633 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001634 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001635 return FAIL;
1636 }
1637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1639 return FAIL;
1640 isn->isn_arg.pfunc.cpf_top = at_top;
1641 isn->isn_arg.pfunc.cpf_argcount = argcount;
1642
1643 stack->ga_len -= argcount; // drop the arguments
1644
1645 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001646 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001648 // If partial is above the arguments it must be cleared and replaced with
1649 // the return value.
1650 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1651 return FAIL;
1652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 return OK;
1654}
1655
1656/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001657 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 */
1659 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001660generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661{
1662 isn_T *isn;
1663 garray_T *stack = &cctx->ctx_type_stack;
1664 type_T *type;
1665
Bram Moolenaar080457c2020-03-03 21:53:32 +01001666 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001667 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001669 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001671 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001673 if (type->tt_type != VAR_DICT && type != &t_any)
1674 {
1675 emsg(_(e_dictreq));
1676 return FAIL;
1677 }
1678 // change dict type to dict member type
1679 if (type->tt_type == VAR_DICT)
1680 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681
1682 return OK;
1683}
1684
1685/*
1686 * Generate an ISN_ECHO instruction.
1687 */
1688 static int
1689generate_ECHO(cctx_T *cctx, int with_white, int count)
1690{
1691 isn_T *isn;
1692
Bram Moolenaar080457c2020-03-03 21:53:32 +01001693 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1695 return FAIL;
1696 isn->isn_arg.echo.echo_with_white = with_white;
1697 isn->isn_arg.echo.echo_count = count;
1698
1699 return OK;
1700}
1701
Bram Moolenaarad39c092020-02-26 18:23:43 +01001702/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001703 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001704 */
1705 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001706generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001707{
1708 isn_T *isn;
1709
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001710 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001711 return FAIL;
1712 isn->isn_arg.number = count;
1713
1714 return OK;
1715}
1716
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001717/*
1718 * Generate an ISN_PUT instruction.
1719 */
1720 static int
1721generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1722{
1723 isn_T *isn;
1724
1725 RETURN_OK_IF_SKIP(cctx);
1726 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1727 return FAIL;
1728 isn->isn_arg.put.put_regname = regname;
1729 isn->isn_arg.put.put_lnum = lnum;
1730 return OK;
1731}
1732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 static int
1734generate_EXEC(cctx_T *cctx, char_u *line)
1735{
1736 isn_T *isn;
1737
Bram Moolenaar080457c2020-03-03 21:53:32 +01001738 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1740 return FAIL;
1741 isn->isn_arg.string = vim_strsave(line);
1742 return OK;
1743}
1744
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001745 static int
1746generate_EXECCONCAT(cctx_T *cctx, int count)
1747{
1748 isn_T *isn;
1749
1750 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1751 return FAIL;
1752 isn->isn_arg.number = count;
1753 return OK;
1754}
1755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756/*
1757 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001758 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001760 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001761reserve_local(
1762 cctx_T *cctx,
1763 char_u *name,
1764 size_t len,
1765 int isConst,
1766 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 lvar_T *lvar;
1769
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001770 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001772 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001773 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 }
1775
1776 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001777 return NULL;
1778 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001779 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001781 // Every local variable uses the next entry on the stack. We could re-use
1782 // the last ones when leaving a scope, but then variables used in a closure
1783 // might get overwritten. To keep things simple do not re-use stack
1784 // entries. This is less efficient, but memory is cheap these days.
1785 lvar->lv_idx = cctx->ctx_locals_count++;
1786
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001787 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 lvar->lv_const = isConst;
1789 lvar->lv_type = type;
1790
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001791 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792}
1793
1794/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001795 * Remove local variables above "new_top".
1796 */
1797 static void
1798unwind_locals(cctx_T *cctx, int new_top)
1799{
1800 if (cctx->ctx_locals.ga_len > new_top)
1801 {
1802 int idx;
1803 lvar_T *lvar;
1804
1805 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1806 {
1807 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1808 vim_free(lvar->lv_name);
1809 }
1810 }
1811 cctx->ctx_locals.ga_len = new_top;
1812}
1813
1814/*
1815 * Free all local variables.
1816 */
1817 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001818free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001819{
1820 unwind_locals(cctx, 0);
1821 ga_clear(&cctx->ctx_locals);
1822}
1823
1824/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 * Find "name" in script-local items of script "sid".
1826 * Returns the index in "sn_var_vals" if found.
1827 * If found but not in "sn_var_vals" returns -1.
1828 * If not found returns -2.
1829 */
1830 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001831get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832{
1833 hashtab_T *ht;
1834 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001835 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001836 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 int idx;
1838
Bram Moolenaare3d46852020-08-29 13:39:17 +02001839 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001841 if (sid == current_sctx.sc_sid)
1842 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001843 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001844
1845 if (sav == NULL)
1846 return -2;
1847 idx = sav->sav_var_vals_idx;
1848 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1849 if (check_writable && sv->sv_const)
1850 semsg(_(e_readonlyvar), name);
1851 return idx;
1852 }
1853
1854 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 ht = &SCRIPT_VARS(sid);
1856 di = find_var_in_ht(ht, 0, name, TRUE);
1857 if (di == NULL)
1858 return -2;
1859
1860 // Now find the svar_T index in sn_var_vals.
1861 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1862 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001863 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 if (sv->sv_tv == &di->di_tv)
1865 {
1866 if (check_writable && sv->sv_const)
1867 semsg(_(e_readonlyvar), name);
1868 return idx;
1869 }
1870 }
1871 return -1;
1872}
1873
1874/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001875 * Find "name" in imported items of the current script or in "cctx" if not
1876 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 */
1878 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001879find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 int idx;
1882
Bram Moolenaare3d46852020-08-29 13:39:17 +02001883 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001884 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 if (cctx != NULL)
1886 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1887 {
1888 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1889 + idx;
1890
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001891 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1892 : STRLEN(import->imp_name) == len
1893 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 return import;
1895 }
1896
Bram Moolenaarefa94442020-08-08 22:16:00 +02001897 return find_imported_in_script(name, len, current_sctx.sc_sid);
1898}
1899
1900 imported_T *
1901find_imported_in_script(char_u *name, size_t len, int sid)
1902{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001903 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001904 int idx;
1905
Bram Moolenaare3d46852020-08-29 13:39:17 +02001906 if (!SCRIPT_ID_VALID(sid))
1907 return NULL;
1908 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1910 {
1911 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1912
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001913 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1914 : STRLEN(import->imp_name) == len
1915 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 return import;
1917 }
1918 return NULL;
1919}
1920
1921/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001922 * Free all imported variables.
1923 */
1924 static void
1925free_imported(cctx_T *cctx)
1926{
1927 int idx;
1928
1929 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1930 {
1931 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1932
1933 vim_free(import->imp_name);
1934 }
1935 ga_clear(&cctx->ctx_imports);
1936}
1937
1938/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001939 * Return TRUE if "p" points at a "#" but not at "#{".
1940 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001941 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001942vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001943{
1944 return p[0] == '#' && p[1] != '{';
1945}
1946
1947/*
1948 * Return a pointer to the next line that isn't empty or only contains a
1949 * comment. Skips over white space.
1950 * Returns NULL if there is none.
1951 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001952 char_u *
1953peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001954{
1955 int lnum = cctx->ctx_lnum;
1956
1957 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1958 {
1959 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001960 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001961
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001962 // ignore NULLs inserted for continuation lines
1963 if (line != NULL)
1964 {
1965 p = skipwhite(line);
1966 if (*p != NUL && !vim9_comment_start(p))
1967 return p;
1968 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001969 }
1970 return NULL;
1971}
1972
1973/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001974 * Called when checking for a following operator at "arg". When the rest of
1975 * the line is empty or only a comment, peek the next line. If there is a next
1976 * line return a pointer to it and set "nextp".
1977 * Otherwise skip over white space.
1978 */
1979 static char_u *
1980may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1981{
1982 char_u *p = skipwhite(arg);
1983
1984 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001985 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001986 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001987 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001988 if (*nextp != NULL)
1989 return *nextp;
1990 }
1991 return p;
1992}
1993
1994/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001995 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001996 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001997 * Returns NULL when at the end.
1998 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001999 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002000next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002001{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002002 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002003
2004 do
2005 {
2006 ++cctx->ctx_lnum;
2007 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002008 {
2009 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002010 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002011 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002012 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002013 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002014 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002015 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002016 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002017 return line;
2018}
2019
2020/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002021 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002022 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002023 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2024 */
2025 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002026may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002027{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002028 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002029 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002030 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002031
2032 if (next == NULL)
2033 return FAIL;
2034 *arg = skipwhite(next);
2035 }
2036 return OK;
2037}
2038
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002039/*
2040 * Idem, and give an error when failed.
2041 */
2042 static int
2043may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2044{
2045 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2046 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002047 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002048 return FAIL;
2049 }
2050 return OK;
2051}
2052
2053
Bram Moolenaara5565e42020-05-09 15:44:01 +02002054// Structure passed between the compile_expr* functions to keep track of
2055// constants that have been parsed but for which no code was produced yet. If
2056// possible expressions on these constants are applied at compile time. If
2057// that is not possible, the code to push the constants needs to be generated
2058// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002059// Using 50 should be more than enough of 5 levels of ().
2060#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002061typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002062 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002063 int pp_used; // active entries in pp_tv[]
2064} ppconst_T;
2065
Bram Moolenaar1c747212020-05-09 18:28:34 +02002066static int compile_expr0(char_u **arg, cctx_T *cctx);
2067static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2068
Bram Moolenaara5565e42020-05-09 15:44:01 +02002069/*
2070 * Generate a PUSH instruction for "tv".
2071 * "tv" will be consumed or cleared.
2072 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2073 */
2074 static int
2075generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2076{
2077 if (tv != NULL)
2078 {
2079 switch (tv->v_type)
2080 {
2081 case VAR_UNKNOWN:
2082 break;
2083 case VAR_BOOL:
2084 generate_PUSHBOOL(cctx, tv->vval.v_number);
2085 break;
2086 case VAR_SPECIAL:
2087 generate_PUSHSPEC(cctx, tv->vval.v_number);
2088 break;
2089 case VAR_NUMBER:
2090 generate_PUSHNR(cctx, tv->vval.v_number);
2091 break;
2092#ifdef FEAT_FLOAT
2093 case VAR_FLOAT:
2094 generate_PUSHF(cctx, tv->vval.v_float);
2095 break;
2096#endif
2097 case VAR_BLOB:
2098 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2099 tv->vval.v_blob = NULL;
2100 break;
2101 case VAR_STRING:
2102 generate_PUSHS(cctx, tv->vval.v_string);
2103 tv->vval.v_string = NULL;
2104 break;
2105 default:
2106 iemsg("constant type not supported");
2107 clear_tv(tv);
2108 return FAIL;
2109 }
2110 tv->v_type = VAR_UNKNOWN;
2111 }
2112 return OK;
2113}
2114
2115/*
2116 * Generate code for any ppconst entries.
2117 */
2118 static int
2119generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2120{
2121 int i;
2122 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002123 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002124
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002125 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002126 for (i = 0; i < ppconst->pp_used; ++i)
2127 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2128 ret = FAIL;
2129 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002130 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002131 return ret;
2132}
2133
2134/*
2135 * Clear ppconst constants. Used when failing.
2136 */
2137 static void
2138clear_ppconst(ppconst_T *ppconst)
2139{
2140 int i;
2141
2142 for (i = 0; i < ppconst->pp_used; ++i)
2143 clear_tv(&ppconst->pp_tv[i]);
2144 ppconst->pp_used = 0;
2145}
2146
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002147/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002148 * Generate an instruction to load script-local variable "name", without the
2149 * leading "s:".
2150 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 */
2152 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002153compile_load_scriptvar(
2154 cctx_T *cctx,
2155 char_u *name, // variable NUL terminated
2156 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002157 char_u **end, // end of variable
2158 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002160 scriptitem_T *si;
2161 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 imported_T *import;
2163
Bram Moolenaare3d46852020-08-29 13:39:17 +02002164 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2165 return FAIL;
2166 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002167 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002168 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002170 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002171 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2172 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 }
2174 if (idx >= 0)
2175 {
2176 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2177
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002178 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 current_sctx.sc_sid, idx, sv->sv_type);
2180 return OK;
2181 }
2182
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002183 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 if (import != NULL)
2185 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002186 if (import->imp_all)
2187 {
2188 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002189 char_u *exp_name;
2190 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002191 ufunc_T *ufunc;
2192 type_T *type;
2193
2194 // Used "import * as Name", need to lookup the member.
2195 if (*p != '.')
2196 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002197 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002198 return FAIL;
2199 }
2200 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002201 if (VIM_ISWHITE(*p))
2202 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002203 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002204 return FAIL;
2205 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002206
Bram Moolenaar1c991142020-07-04 13:15:31 +02002207 // isolate one name
2208 exp_name = p;
2209 while (eval_isnamec(*p))
2210 ++p;
2211 cc = *p;
2212 *p = NUL;
2213
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002214 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002215 *p = cc;
2216 p = skipwhite(p);
2217
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002218 // TODO: what if it is a function?
2219 if (idx < 0)
2220 return FAIL;
2221 *end = p;
2222
2223 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2224 import->imp_sid,
2225 idx,
2226 type);
2227 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002228 else if (import->imp_funcname != NULL)
2229 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002230 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002231 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2232 import->imp_sid,
2233 import->imp_var_vals_idx,
2234 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 return OK;
2236 }
2237
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002238 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002239 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 return FAIL;
2241}
2242
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002243 static int
2244generate_funcref(cctx_T *cctx, char_u *name)
2245{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002246 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002247
2248 if (ufunc == NULL)
2249 return FAIL;
2250
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002251 // Need to compile any default values to get the argument types.
2252 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2253 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2254 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002255 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002256}
2257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258/*
2259 * Compile a variable name into a load instruction.
2260 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002261 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 * When "error" is FALSE do not give an error when not found.
2263 */
2264 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002265compile_load(
2266 char_u **arg,
2267 char_u *end_arg,
2268 cctx_T *cctx,
2269 int is_expr,
2270 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271{
2272 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002273 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002274 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002276 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277
2278 if (*(*arg + 1) == ':')
2279 {
2280 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002281 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002282 {
2283 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002285 switch (**arg)
2286 {
2287 case 'g': isn_type = ISN_LOADGDICT; break;
2288 case 'w': isn_type = ISN_LOADWDICT; break;
2289 case 't': isn_type = ISN_LOADTDICT; break;
2290 case 'b': isn_type = ISN_LOADBDICT; break;
2291 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002292 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002293 goto theend;
2294 }
2295 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2296 goto theend;
2297 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 else
2300 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002301 isntype_T isn_type = ISN_DROP;
2302
2303 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2304 if (name == NULL)
2305 return FAIL;
2306
2307 switch (**arg)
2308 {
2309 case 'v': res = generate_LOADV(cctx, name, error);
2310 break;
2311 case 's': res = compile_load_scriptvar(cctx, name,
2312 NULL, NULL, error);
2313 break;
2314 case 'g': isn_type = ISN_LOADG; break;
2315 case 'w': isn_type = ISN_LOADW; break;
2316 case 't': isn_type = ISN_LOADT; break;
2317 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002318 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002319 goto theend;
2320 }
2321 if (isn_type != ISN_DROP)
2322 {
2323 // Global, Buffer-local, Window-local and Tabpage-local
2324 // variables can be defined later, thus we don't check if it
2325 // exists, give error at runtime.
2326 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 }
2329 }
2330 else
2331 {
2332 size_t len = end - *arg;
2333 int idx;
2334 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002335 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336
2337 name = vim_strnsave(*arg, end - *arg);
2338 if (name == NULL)
2339 return FAIL;
2340
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002341 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002343 if (!gen_load_outer)
2344 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 }
2346 else
2347 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002348 lvar_T *lvar = lookup_local(*arg, len, cctx);
2349
2350 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002352 type = lvar->lv_type;
2353 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002354 if (lvar->lv_from_outer)
2355 gen_load_outer = TRUE;
2356 else
2357 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 }
2359 else
2360 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002361 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002362 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002363 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002364 || find_imported(name, 0, cctx) != NULL)
2365 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002366
Bram Moolenaar0f769812020-09-12 18:32:34 +02002367 // When evaluating an expression and the name starts with an
2368 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002369 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002370 if (res == FAIL && is_expr
2371 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002372 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 }
2374 }
2375 if (gen_load)
2376 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002377 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002378 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002379 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002380 cctx->ctx_outer_used = TRUE;
2381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 }
2383
2384 *arg = end;
2385
2386theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002387 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002388 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 vim_free(name);
2390 return res;
2391}
2392
2393/*
2394 * Compile the argument expressions.
2395 * "arg" points to just after the "(" and is advanced to after the ")"
2396 */
2397 static int
2398compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2399{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002400 char_u *p = *arg;
2401 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002402 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403
Bram Moolenaare6085c52020-04-12 20:19:16 +02002404 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002406 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2407 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002408 if (*p == ')')
2409 {
2410 *arg = p + 1;
2411 return OK;
2412 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002413 if (must_end)
2414 {
2415 semsg(_(e_missing_comma_before_argument_str), p);
2416 return FAIL;
2417 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002418
Bram Moolenaara5565e42020-05-09 15:44:01 +02002419 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 return FAIL;
2421 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002422
2423 if (*p != ',' && *skipwhite(p) == ',')
2424 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002425 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002426 p = skipwhite(p);
2427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002429 {
2430 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002431 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002432 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002433 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002434 else
2435 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002436 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002437 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002439failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002440 emsg(_(e_missing_close));
2441 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442}
2443
2444/*
2445 * Compile a function call: name(arg1, arg2)
2446 * "arg" points to "name", "arg + varlen" to the "(".
2447 * "argcount_init" is 1 for "value->method()"
2448 * Instructions:
2449 * EVAL arg1
2450 * EVAL arg2
2451 * BCALL / DCALL / UCALL
2452 */
2453 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002454compile_call(
2455 char_u **arg,
2456 size_t varlen,
2457 cctx_T *cctx,
2458 ppconst_T *ppconst,
2459 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460{
2461 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002462 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 int argcount = argcount_init;
2464 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002465 char_u fname_buf[FLEN_FIXED + 1];
2466 char_u *tofree = NULL;
2467 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002469 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002470 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471
Bram Moolenaara5565e42020-05-09 15:44:01 +02002472 // we can evaluate "has('name')" at compile time
2473 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2474 {
2475 char_u *s = skipwhite(*arg + varlen + 1);
2476 typval_T argvars[2];
2477
2478 argvars[0].v_type = VAR_UNKNOWN;
2479 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002480 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002481 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002482 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002483 s = skipwhite(s);
2484 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2485 {
2486 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2487
2488 *arg = s + 1;
2489 argvars[1].v_type = VAR_UNKNOWN;
2490 tv->v_type = VAR_NUMBER;
2491 tv->vval.v_number = 0;
2492 f_has(argvars, tv);
2493 clear_tv(&argvars[0]);
2494 ++ppconst->pp_used;
2495 return OK;
2496 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002497 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002498 }
2499
2500 if (generate_ppconst(cctx, ppconst) == FAIL)
2501 return FAIL;
2502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 if (varlen >= sizeof(namebuf))
2504 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002505 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 return FAIL;
2507 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002508 vim_strncpy(namebuf, *arg, varlen);
2509 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510
2511 *arg = skipwhite(*arg + varlen + 1);
2512 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002513 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514
Bram Moolenaara1773442020-08-12 15:21:22 +02002515 is_autoload = vim_strchr(name, '#') != NULL;
2516 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 {
2518 int idx;
2519
2520 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002521 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002523 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002524 else
2525 semsg(_(e_unknownfunc), namebuf);
2526 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 }
2528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002530 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002531 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002532 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002533 {
2534 res = generate_CALL(cctx, ufunc, argcount);
2535 goto theend;
2536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537
2538 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002539 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002540 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002542 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002543 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002544 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002545 garray_T *stack = &cctx->ctx_type_stack;
2546 type_T *type;
2547
2548 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2549 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002550 goto theend;
2551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552
Bram Moolenaar0f769812020-09-12 18:32:34 +02002553 // If we can find a global function by name generate the right call.
2554 if (ufunc != NULL)
2555 {
2556 res = generate_CALL(cctx, ufunc, argcount);
2557 goto theend;
2558 }
2559
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002560 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002561 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002562 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002563 res = generate_UCALL(cctx, name, argcount);
2564 else
2565 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002566
2567theend:
2568 vim_free(tofree);
2569 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570}
2571
2572// like NAMESPACE_CHAR but with 'a' and 'l'.
2573#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2574
2575/*
2576 * Find the end of a variable or function name. Unlike find_name_end() this
2577 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002578 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 * Return a pointer to just after the name. Equal to "arg" if there is no
2580 * valid name.
2581 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002582 static char_u *
2583to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584{
2585 char_u *p;
2586
2587 // Quick check for valid starting character.
2588 if (!eval_isnamec1(*arg))
2589 return arg;
2590
2591 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2592 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2593 // and can be used in slice "[n:]".
2594 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002595 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2597 break;
2598 return p;
2599}
2600
2601/*
2602 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002603 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 */
2605 char_u *
2606to_name_const_end(char_u *arg)
2607{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002608 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 typval_T rettv;
2610
2611 if (p == arg && *arg == '[')
2612 {
2613
2614 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002615 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 p = arg;
2617 }
2618 else if (p == arg && *arg == '#' && arg[1] == '{')
2619 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002620 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002622 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 p = arg;
2624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625
2626 return p;
2627}
2628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629/*
2630 * parse a list: [expr, expr]
2631 * "*arg" points to the '['.
2632 */
2633 static int
2634compile_list(char_u **arg, cctx_T *cctx)
2635{
2636 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002637 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 int count = 0;
2639
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002640 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002642 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002643 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002644 semsg(_(e_list_end), *arg);
2645 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002646 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002647 if (*p == ',')
2648 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002649 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002650 return FAIL;
2651 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002652 if (*p == ']')
2653 {
2654 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002655 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002656 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002657 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002658 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 ++count;
2660 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002661 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002663 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2664 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002665 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002666 return FAIL;
2667 }
2668 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002669 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 p = skipwhite(p);
2671 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002672 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673
2674 generate_NEWLIST(cctx, count);
2675 return OK;
2676}
2677
2678/*
2679 * parse a lambda: {arg, arg -> expr}
2680 * "*arg" points to the '{'.
2681 */
2682 static int
2683compile_lambda(char_u **arg, cctx_T *cctx)
2684{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 typval_T rettv;
2686 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002687 evalarg_T evalarg;
2688
2689 CLEAR_FIELD(evalarg);
2690 evalarg.eval_flags = EVAL_EVALUATE;
2691 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692
2693 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002694 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002695 {
2696 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002698 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002701 ++ufunc->uf_refcount;
2702 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002703 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704
2705 // The function will have one line: "return {expr}".
2706 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002707 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002709 clear_evalarg(&evalarg, NULL);
2710
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002711 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002712 {
2713 // The return type will now be known.
2714 set_function_type(ufunc);
2715
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002716 // The function reference count will be 1. When the ISN_FUNCREF
2717 // instruction is deleted the reference count is decremented and the
2718 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002719 return generate_FUNCREF(cctx, ufunc);
2720 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002721
2722 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 return FAIL;
2724}
2725
2726/*
2727 * Compile a lamda call: expr->{lambda}(args)
2728 * "arg" points to the "{".
2729 */
2730 static int
2731compile_lambda_call(char_u **arg, cctx_T *cctx)
2732{
2733 ufunc_T *ufunc;
2734 typval_T rettv;
2735 int argcount = 1;
2736 int ret = FAIL;
2737
2738 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002739 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 return FAIL;
2741
2742 if (**arg != '(')
2743 {
2744 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002745 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 else
2747 semsg(_(e_missing_paren), "lambda");
2748 clear_tv(&rettv);
2749 return FAIL;
2750 }
2751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 ufunc = rettv.vval.v_partial->pt_func;
2753 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002754 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002755 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002756
Bram Moolenaara05e5242020-09-19 18:19:19 +02002757 // The function will have one line: "return {expr}". Compile it into
2758 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002759 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760
2761 // compile the arguments
2762 *arg = skipwhite(*arg + 1);
2763 if (compile_arguments(arg, cctx, &argcount) == OK)
2764 // call the compiled function
2765 ret = generate_CALL(cctx, ufunc, argcount);
2766
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002767 if (ret == FAIL)
2768 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 return ret;
2770}
2771
2772/*
2773 * parse a dict: {'key': val} or #{key: val}
2774 * "*arg" points to the '{'.
2775 */
2776 static int
2777compile_dict(char_u **arg, cctx_T *cctx, int literal)
2778{
2779 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002780 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 int count = 0;
2782 dict_T *d = dict_alloc();
2783 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002784 char_u *whitep = *arg;
2785 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
2787 if (d == NULL)
2788 return FAIL;
2789 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002790 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 {
2792 char_u *key = NULL;
2793
Bram Moolenaar23c55272020-06-21 16:58:13 +02002794 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002795 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002796 *arg = NULL;
2797 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002798 }
2799
2800 if (**arg == '}')
2801 break;
2802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 if (literal)
2804 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002805 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806
Bram Moolenaar2c330432020-04-13 14:41:35 +02002807 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002809 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 return FAIL;
2811 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002812 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 if (generate_PUSHS(cctx, key) == FAIL)
2814 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002815 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 }
2817 else
2818 {
2819 isn_T *isn;
2820
Bram Moolenaara5565e42020-05-09 15:44:01 +02002821 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2824 if (isn->isn_type == ISN_PUSHS)
2825 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002826 else
2827 {
2828 type_T *keytype = ((type_T **)stack->ga_data)
2829 [stack->ga_len - 1];
2830 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2831 return FAIL;
2832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 }
2834
2835 // Check for duplicate keys, if using string keys.
2836 if (key != NULL)
2837 {
2838 item = dict_find(d, key, -1);
2839 if (item != NULL)
2840 {
2841 semsg(_(e_duplicate_key), key);
2842 goto failret;
2843 }
2844 item = dictitem_alloc(key);
2845 if (item != NULL)
2846 {
2847 item->di_tv.v_type = VAR_UNKNOWN;
2848 item->di_tv.v_lock = 0;
2849 if (dict_add(d, item) == FAIL)
2850 dictitem_free(item);
2851 }
2852 }
2853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 if (**arg != ':')
2855 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002856 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002857 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002858 else
2859 semsg(_(e_missing_dict_colon), *arg);
2860 return FAIL;
2861 }
2862 whitep = *arg + 1;
2863 if (!IS_WHITE_OR_NUL(*whitep))
2864 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002865 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 return FAIL;
2867 }
2868
2869 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002870 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002871 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002872 *arg = NULL;
2873 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002874 }
2875
Bram Moolenaara5565e42020-05-09 15:44:01 +02002876 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 return FAIL;
2878 ++count;
2879
Bram Moolenaar2c330432020-04-13 14:41:35 +02002880 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002881 *arg = skipwhite(*arg);
2882 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002883 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002884 *arg = NULL;
2885 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 if (**arg == '}')
2888 break;
2889 if (**arg != ',')
2890 {
2891 semsg(_(e_missing_dict_comma), *arg);
2892 goto failret;
2893 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002894 if (IS_WHITE_OR_NUL(*whitep))
2895 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002896 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002897 return FAIL;
2898 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002899 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 *arg = skipwhite(*arg + 1);
2901 }
2902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 *arg = *arg + 1;
2904
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002905 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002906 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002907 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002908 *arg += STRLEN(*arg);
2909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 dict_unref(d);
2911 return generate_NEWDICT(cctx, count);
2912
2913failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002914 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002915 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002916 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002917 *arg = (char_u *)"";
2918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 dict_unref(d);
2920 return FAIL;
2921}
2922
2923/*
2924 * Compile "&option".
2925 */
2926 static int
2927compile_get_option(char_u **arg, cctx_T *cctx)
2928{
2929 typval_T rettv;
2930 char_u *start = *arg;
2931 int ret;
2932
2933 // parse the option and get the current value to get the type.
2934 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002935 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 if (ret == OK)
2937 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002938 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 char_u *name = vim_strnsave(start, *arg - start);
2940 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2941
2942 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2943 vim_free(name);
2944 }
2945 clear_tv(&rettv);
2946
2947 return ret;
2948}
2949
2950/*
2951 * Compile "$VAR".
2952 */
2953 static int
2954compile_get_env(char_u **arg, cctx_T *cctx)
2955{
2956 char_u *start = *arg;
2957 int len;
2958 int ret;
2959 char_u *name;
2960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 ++*arg;
2962 len = get_env_len(arg);
2963 if (len == 0)
2964 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002965 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 return FAIL;
2967 }
2968
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002969 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 name = vim_strnsave(start, len + 1);
2971 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2972 vim_free(name);
2973 return ret;
2974}
2975
2976/*
2977 * Compile "@r".
2978 */
2979 static int
2980compile_get_register(char_u **arg, cctx_T *cctx)
2981{
2982 int ret;
2983
2984 ++*arg;
2985 if (**arg == NUL)
2986 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002987 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 return FAIL;
2989 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002990 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 {
2992 emsg_invreg(**arg);
2993 return FAIL;
2994 }
2995 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2996 ++*arg;
2997 return ret;
2998}
2999
3000/*
3001 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003002 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 */
3004 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003005apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003007 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008
3009 // this works from end to start
3010 while (p > start)
3011 {
3012 --p;
3013 if (*p == '-' || *p == '+')
3014 {
3015 // only '-' has an effect, for '+' we only check the type
3016#ifdef FEAT_FLOAT
3017 if (rettv->v_type == VAR_FLOAT)
3018 {
3019 if (*p == '-')
3020 rettv->vval.v_float = -rettv->vval.v_float;
3021 }
3022 else
3023#endif
3024 {
3025 varnumber_T val;
3026 int error = FALSE;
3027
3028 // tv_get_number_chk() accepts a string, but we don't want that
3029 // here
3030 if (check_not_string(rettv) == FAIL)
3031 return FAIL;
3032 val = tv_get_number_chk(rettv, &error);
3033 clear_tv(rettv);
3034 if (error)
3035 return FAIL;
3036 if (*p == '-')
3037 val = -val;
3038 rettv->v_type = VAR_NUMBER;
3039 rettv->vval.v_number = val;
3040 }
3041 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003042 else if (numeric_only)
3043 {
3044 ++p;
3045 break;
3046 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003047 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 {
3049 int v = tv2bool(rettv);
3050
3051 // '!' is permissive in the type.
3052 clear_tv(rettv);
3053 rettv->v_type = VAR_BOOL;
3054 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3055 }
3056 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003057 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 return OK;
3059}
3060
3061/*
3062 * Recognize v: variables that are constants and set "rettv".
3063 */
3064 static void
3065get_vim_constant(char_u **arg, typval_T *rettv)
3066{
3067 if (STRNCMP(*arg, "v:true", 6) == 0)
3068 {
3069 rettv->v_type = VAR_BOOL;
3070 rettv->vval.v_number = VVAL_TRUE;
3071 *arg += 6;
3072 }
3073 else if (STRNCMP(*arg, "v:false", 7) == 0)
3074 {
3075 rettv->v_type = VAR_BOOL;
3076 rettv->vval.v_number = VVAL_FALSE;
3077 *arg += 7;
3078 }
3079 else if (STRNCMP(*arg, "v:null", 6) == 0)
3080 {
3081 rettv->v_type = VAR_SPECIAL;
3082 rettv->vval.v_number = VVAL_NULL;
3083 *arg += 6;
3084 }
3085 else if (STRNCMP(*arg, "v:none", 6) == 0)
3086 {
3087 rettv->v_type = VAR_SPECIAL;
3088 rettv->vval.v_number = VVAL_NONE;
3089 *arg += 6;
3090 }
3091}
3092
Bram Moolenaar696ba232020-07-29 21:20:41 +02003093 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003094get_compare_type(char_u *p, int *len, int *type_is)
3095{
3096 exptype_T type = EXPR_UNKNOWN;
3097 int i;
3098
3099 switch (p[0])
3100 {
3101 case '=': if (p[1] == '=')
3102 type = EXPR_EQUAL;
3103 else if (p[1] == '~')
3104 type = EXPR_MATCH;
3105 break;
3106 case '!': if (p[1] == '=')
3107 type = EXPR_NEQUAL;
3108 else if (p[1] == '~')
3109 type = EXPR_NOMATCH;
3110 break;
3111 case '>': if (p[1] != '=')
3112 {
3113 type = EXPR_GREATER;
3114 *len = 1;
3115 }
3116 else
3117 type = EXPR_GEQUAL;
3118 break;
3119 case '<': if (p[1] != '=')
3120 {
3121 type = EXPR_SMALLER;
3122 *len = 1;
3123 }
3124 else
3125 type = EXPR_SEQUAL;
3126 break;
3127 case 'i': if (p[1] == 's')
3128 {
3129 // "is" and "isnot"; but not a prefix of a name
3130 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3131 *len = 5;
3132 i = p[*len];
3133 if (!isalnum(i) && i != '_')
3134 {
3135 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3136 *type_is = TRUE;
3137 }
3138 }
3139 break;
3140 }
3141 return type;
3142}
3143
3144/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003146 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 */
3148 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003149compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003151 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152
3153 // this works from end to start
3154 while (p > start)
3155 {
3156 --p;
3157 if (*p == '-' || *p == '+')
3158 {
3159 int negate = *p == '-';
3160 isn_T *isn;
3161
3162 // TODO: check type
3163 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3164 {
3165 --p;
3166 if (*p == '-')
3167 negate = !negate;
3168 }
3169 // only '-' has an effect, for '+' we only check the type
3170 if (negate)
3171 isn = generate_instr(cctx, ISN_NEGATENR);
3172 else
3173 isn = generate_instr(cctx, ISN_CHECKNR);
3174 if (isn == NULL)
3175 return FAIL;
3176 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003177 else if (numeric_only)
3178 {
3179 ++p;
3180 break;
3181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 else
3183 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003184 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003186 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003188 if (p[-1] == '!')
3189 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 }
3192 if (generate_2BOOL(cctx, invert) == FAIL)
3193 return FAIL;
3194 }
3195 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003196 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 return OK;
3198}
3199
3200/*
3201 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003202 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 */
3204 static int
3205compile_subscript(
3206 char_u **arg,
3207 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003208 char_u *start_leader,
3209 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003210 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003212 char_u *name_start = *end_leader;
3213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 for (;;)
3215 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003216 char_u *p = skipwhite(*arg);
3217
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003218 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003219 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003220 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003221
3222 // If a following line starts with "->{" or "->X" advance to that
3223 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003224 // Also if a following line starts with ".x".
3225 if (next != NULL &&
3226 ((next[0] == '-' && next[1] == '>'
3227 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003228 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003229 {
3230 next = next_line_from_context(cctx, TRUE);
3231 if (next == NULL)
3232 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003233 *arg = next;
3234 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003235 }
3236 }
3237
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003238 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3239 // not a function call.
3240 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003242 garray_T *stack = &cctx->ctx_type_stack;
3243 type_T *type;
3244 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003246 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003247 return FAIL;
3248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003250 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3251
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003252 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3254 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003255 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 return FAIL;
3257 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003258 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003260 char_u *pstart = p;
3261
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003262 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003263 return FAIL;
3264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 // something->method()
3266 // Apply the '!', '-' and '+' first:
3267 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003268 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003271 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003272 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003273 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 if (**arg == '{')
3275 {
3276 // lambda call: list->{lambda}
3277 if (compile_lambda_call(arg, cctx) == FAIL)
3278 return FAIL;
3279 }
3280 else
3281 {
3282 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003283 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003284 if (!eval_isnamec1(*p))
3285 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003286 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003287 return FAIL;
3288 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003289 if (ASCII_ISALPHA(*p) && p[1] == ':')
3290 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003291 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 ;
3293 if (*p != '(')
3294 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003295 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 return FAIL;
3297 }
3298 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003299 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 return FAIL;
3301 }
3302 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003303 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003305 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003306 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003307 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003308 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003309 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003310
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003311 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003312 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003313 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003314 // TODO: blob index
3315 // TODO: more arguments
3316 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003317 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003318 return FAIL;
3319
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003320 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003321 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003322 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003323 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003324 if (**arg == ':')
3325 // missing first index is equal to zero
3326 generate_PUSHNR(cctx, 0);
3327 else
3328 {
3329 if (compile_expr0(arg, cctx) == FAIL)
3330 return FAIL;
3331 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3332 return FAIL;
3333 *arg = skipwhite(*arg);
3334 }
3335 if (**arg == ':')
3336 {
3337 *arg = skipwhite(*arg + 1);
3338 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3339 return FAIL;
3340 if (**arg == ']')
3341 // missing second index is equal to end of string
3342 generate_PUSHNR(cctx, -1);
3343 else
3344 {
3345 if (compile_expr0(arg, cctx) == FAIL)
3346 return FAIL;
3347 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3348 return FAIL;
3349 *arg = skipwhite(*arg);
3350 }
3351 is_slice = TRUE;
3352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353
3354 if (**arg != ']')
3355 {
3356 emsg(_(e_missbrac));
3357 return FAIL;
3358 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003359 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003361 // We can index a list and a dict. If we don't know the type
3362 // we can use the index value type.
3363 // TODO: If we don't know use an instruction to figure it out at
3364 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003365 typep = ((type_T **)stack->ga_data) + stack->ga_len
3366 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003367 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003368 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3369 // If the index is a string, the variable must be a Dict.
3370 if (*typep == &t_any && valtype == &t_string)
3371 vtype = VAR_DICT;
3372 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003373 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003374 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3375 return FAIL;
3376 if (is_slice)
3377 {
3378 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3379 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3380 return FAIL;
3381 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003382 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003383
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003384 if (vtype == VAR_DICT)
3385 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003386 if (is_slice)
3387 {
3388 emsg(_(e_cannot_slice_dictionary));
3389 return FAIL;
3390 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003391 if ((*typep)->tt_type == VAR_DICT)
3392 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003393 else
3394 {
3395 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3396 return FAIL;
3397 *typep = &t_any;
3398 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003399 if (may_generate_2STRING(-1, cctx) == FAIL)
3400 return FAIL;
3401 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3402 return FAIL;
3403 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003404 else if (vtype == VAR_STRING)
3405 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003406 *typep = &t_string;
3407 if ((is_slice
3408 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3409 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003410 return FAIL;
3411 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003412 else if (vtype == VAR_BLOB)
3413 {
3414 emsg("Sorry, blob index and slice not implemented yet");
3415 return FAIL;
3416 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003417 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003418 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003419 if (is_slice)
3420 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003421 if (generate_instr_drop(cctx,
3422 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3423 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003424 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003425 }
Bram Moolenaared591872020-08-15 22:14:53 +02003426 else
3427 {
3428 if ((*typep)->tt_type == VAR_LIST)
3429 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003430 if (generate_instr_drop(cctx,
3431 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3432 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003433 return FAIL;
3434 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003435 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003436 else
3437 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003438 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003439 return FAIL;
3440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003442 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003444 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003445 return FAIL;
3446
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003447 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003448 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003449 {
3450 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003451 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003454 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003455 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 while (eval_isnamec(*p))
3457 MB_PTR_ADV(p);
3458 if (p == *arg)
3459 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003460 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 return FAIL;
3462 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003463 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 return FAIL;
3465 *arg = p;
3466 }
3467 else
3468 break;
3469 }
3470
3471 // TODO - see handle_subscript():
3472 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3473 // Don't do this when "Func" is already a partial that was bound
3474 // explicitly (pt_auto is FALSE).
3475
3476 return OK;
3477}
3478
3479/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003480 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3481 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003483 * If the value is a constant "ppconst->pp_ret" will be set.
3484 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003485 *
3486 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 */
3488
3489/*
3490 * number number constant
3491 * 0zFFFFFFFF Blob constant
3492 * "string" string constant
3493 * 'string' literal string constant
3494 * &option-name option value
3495 * @r register contents
3496 * identifier variable value
3497 * function() function call
3498 * $VAR environment variable
3499 * (expression) nested expression
3500 * [expr, expr] List
3501 * {key: val, key: val} Dictionary
3502 * #{key: val, key: val} Dictionary with literal keys
3503 *
3504 * Also handle:
3505 * ! in front logical NOT
3506 * - in front unary minus
3507 * + in front unary plus (ignored)
3508 * trailing (arg) funcref/partial call
3509 * trailing [] subscript in String or List
3510 * trailing .name entry in Dictionary
3511 * trailing ->name() method call
3512 */
3513 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003514compile_expr7(
3515 char_u **arg,
3516 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003517 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 char_u *start_leader, *end_leader;
3520 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003521 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003522 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523
3524 /*
3525 * Skip '!', '-' and '+' characters. They are handled later.
3526 */
3527 start_leader = *arg;
3528 while (**arg == '!' || **arg == '-' || **arg == '+')
3529 *arg = skipwhite(*arg + 1);
3530 end_leader = *arg;
3531
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003532 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 switch (**arg)
3534 {
3535 /*
3536 * Number constant.
3537 */
3538 case '0': // also for blob starting with 0z
3539 case '1':
3540 case '2':
3541 case '3':
3542 case '4':
3543 case '5':
3544 case '6':
3545 case '7':
3546 case '8':
3547 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003548 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003550 // Apply "-" and "+" just before the number now, right to
3551 // left. Matters especially when "->" follows. Stops at
3552 // '!'.
3553 if (apply_leader(rettv, TRUE,
3554 start_leader, &end_leader) == FAIL)
3555 {
3556 clear_tv(rettv);
3557 return FAIL;
3558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 break;
3560
3561 /*
3562 * String constant: "string".
3563 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003564 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 return FAIL;
3566 break;
3567
3568 /*
3569 * Literal string constant: 'str''ing'.
3570 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003571 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 return FAIL;
3573 break;
3574
3575 /*
3576 * Constant Vim variable.
3577 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003578 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 ret = NOTDONE;
3580 break;
3581
3582 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003583 * "true" constant
3584 */
3585 case 't': if (STRNCMP(*arg, "true", 4) == 0
3586 && !eval_isnamec((*arg)[4]))
3587 {
3588 *arg += 4;
3589 rettv->v_type = VAR_BOOL;
3590 rettv->vval.v_number = VVAL_TRUE;
3591 }
3592 else
3593 ret = NOTDONE;
3594 break;
3595
3596 /*
3597 * "false" constant
3598 */
3599 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3600 && !eval_isnamec((*arg)[5]))
3601 {
3602 *arg += 5;
3603 rettv->v_type = VAR_BOOL;
3604 rettv->vval.v_number = VVAL_FALSE;
3605 }
3606 else
3607 ret = NOTDONE;
3608 break;
3609
3610 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 * List: [expr, expr]
3612 */
3613 case '[': ret = compile_list(arg, cctx);
3614 break;
3615
3616 /*
3617 * Dictionary: #{key: val, key: val}
3618 */
3619 case '#': if ((*arg)[1] == '{')
3620 {
3621 ++*arg;
3622 ret = compile_dict(arg, cctx, TRUE);
3623 }
3624 else
3625 ret = NOTDONE;
3626 break;
3627
3628 /*
3629 * Lambda: {arg, arg -> expr}
3630 * Dictionary: {'key': val, 'key': val}
3631 */
3632 case '{': {
3633 char_u *start = skipwhite(*arg + 1);
3634
3635 // Find out what comes after the arguments.
3636 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003637 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 if (ret != FAIL && *start == '>')
3639 ret = compile_lambda(arg, cctx);
3640 else
3641 ret = compile_dict(arg, cctx, FALSE);
3642 }
3643 break;
3644
3645 /*
3646 * Option value: &name
3647 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003648 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3649 return FAIL;
3650 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 break;
3652
3653 /*
3654 * Environment variable: $VAR.
3655 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003656 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3657 return FAIL;
3658 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 break;
3660
3661 /*
3662 * Register contents: @r.
3663 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003664 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3665 return FAIL;
3666 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 break;
3668 /*
3669 * nested expression: (expression).
3670 */
3671 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003672
3673 // recursive!
3674 if (ppconst->pp_used <= PPSIZE - 10)
3675 {
3676 ret = compile_expr1(arg, cctx, ppconst);
3677 }
3678 else
3679 {
3680 // Not enough space in ppconst, flush constants.
3681 if (generate_ppconst(cctx, ppconst) == FAIL)
3682 return FAIL;
3683 ret = compile_expr0(arg, cctx);
3684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 *arg = skipwhite(*arg);
3686 if (**arg == ')')
3687 ++*arg;
3688 else if (ret == OK)
3689 {
3690 emsg(_(e_missing_close));
3691 ret = FAIL;
3692 }
3693 break;
3694
3695 default: ret = NOTDONE;
3696 break;
3697 }
3698 if (ret == FAIL)
3699 return FAIL;
3700
Bram Moolenaar1c747212020-05-09 18:28:34 +02003701 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003703 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003704 clear_tv(rettv);
3705 else
3706 // A constant expression can possibly be handled compile time,
3707 // return the value instead of generating code.
3708 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 }
3710 else if (ret == NOTDONE)
3711 {
3712 char_u *p;
3713 int r;
3714
3715 if (!eval_isnamec1(**arg))
3716 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003717 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 return FAIL;
3719 }
3720
3721 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003722 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003724 {
3725 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003728 {
3729 if (generate_ppconst(cctx, ppconst) == FAIL)
3730 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003731 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 if (r == FAIL)
3734 return FAIL;
3735 }
3736
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003737 // Handle following "[]", ".member", etc.
3738 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003739 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003740 ppconst) == FAIL)
3741 return FAIL;
3742 if (ppconst->pp_used > 0)
3743 {
3744 // apply the '!', '-' and '+' before the constant
3745 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003746 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003747 return FAIL;
3748 return OK;
3749 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003750 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003752 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753}
3754
3755/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003756 * Give the "white on both sides" error, taking the operator from "p[len]".
3757 */
3758 void
3759error_white_both(char_u *op, int len)
3760{
3761 char_u buf[10];
3762
3763 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003764 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003765}
3766
3767/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003768 * <type>expr7: runtime type check / conversion
3769 */
3770 static int
3771compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3772{
3773 type_T *want_type = NULL;
3774
3775 // Recognize <type>
3776 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3777 {
3778 int called_emsg_before = called_emsg;
3779
3780 ++*arg;
3781 want_type = parse_type(arg, cctx->ctx_type_list);
3782 if (called_emsg != called_emsg_before)
3783 return FAIL;
3784
3785 if (**arg != '>')
3786 {
3787 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003788 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003789 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003790 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003791 return FAIL;
3792 }
3793 ++*arg;
3794 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3795 return FAIL;
3796 }
3797
3798 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3799 return FAIL;
3800
3801 if (want_type != NULL)
3802 {
3803 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003804 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003805
Bram Moolenaard1103582020-08-14 22:44:25 +02003806 generate_ppconst(cctx, ppconst);
3807 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003808 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003809 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003810 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3811 return FAIL;
3812 }
3813 }
3814
3815 return OK;
3816}
3817
3818/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 * * number multiplication
3820 * / number division
3821 * % number modulo
3822 */
3823 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003824compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825{
3826 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003827 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003828 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003830 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003831 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 return FAIL;
3833
3834 /*
3835 * Repeat computing, until no "*", "/" or "%" is following.
3836 */
3837 for (;;)
3838 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003839 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 if (*op != '*' && *op != '/' && *op != '%')
3841 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003842 if (next != NULL)
3843 {
3844 *arg = next_line_from_context(cctx, TRUE);
3845 op = skipwhite(*arg);
3846 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003847
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003848 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003850 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003851 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 }
3853 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003854 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003855 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003857 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003858 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003860
3861 if (ppconst->pp_used == ppconst_used + 2
3862 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3863 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003864 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003865 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3866 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003867 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003869 // both are numbers: compute the result
3870 switch (*op)
3871 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003872 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003873 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003874 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003875 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003876 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003877 break;
3878 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003879 tv1->vval.v_number = res;
3880 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003881 }
3882 else
3883 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003884 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003885 generate_two_op(cctx, op);
3886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 }
3888
3889 return OK;
3890}
3891
3892/*
3893 * + number addition
3894 * - number subtraction
3895 * .. string concatenation
3896 */
3897 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003898compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899{
3900 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003901 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003903 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904
3905 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003906 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 return FAIL;
3908
3909 /*
3910 * Repeat computing, until no "+", "-" or ".." is following.
3911 */
3912 for (;;)
3913 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003914 op = may_peek_next_line(cctx, *arg, &next);
3915 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 break;
3917 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003918 if (next != NULL)
3919 {
3920 *arg = next_line_from_context(cctx, TRUE);
3921 op = skipwhite(*arg);
3922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003924 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003926 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003927 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 }
3929
3930 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003931 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003932 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003934 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003935 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 return FAIL;
3937
Bram Moolenaara5565e42020-05-09 15:44:01 +02003938 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003939 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003940 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3941 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3942 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3943 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3946 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003947
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003948 // concat/subtract/add constant numbers
3949 if (*op == '+')
3950 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3951 else if (*op == '-')
3952 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3953 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003954 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003955 // concatenate constant strings
3956 char_u *s1 = tv1->vval.v_string;
3957 char_u *s2 = tv2->vval.v_string;
3958 size_t len1 = STRLEN(s1);
3959
3960 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3961 if (tv1->vval.v_string == NULL)
3962 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003963 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003964 return FAIL;
3965 }
3966 mch_memmove(tv1->vval.v_string, s1, len1);
3967 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003968 vim_free(s1);
3969 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003970 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003971 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 }
3973 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003974 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003975 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003976 if (*op == '.')
3977 {
3978 if (may_generate_2STRING(-2, cctx) == FAIL
3979 || may_generate_2STRING(-1, cctx) == FAIL)
3980 return FAIL;
3981 generate_instr_drop(cctx, ISN_CONCAT, 1);
3982 }
3983 else
3984 generate_two_op(cctx, op);
3985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 }
3987
3988 return OK;
3989}
3990
3991/*
3992 * expr5a == expr5b
3993 * expr5a =~ expr5b
3994 * expr5a != expr5b
3995 * expr5a !~ expr5b
3996 * expr5a > expr5b
3997 * expr5a >= expr5b
3998 * expr5a < expr5b
3999 * expr5a <= expr5b
4000 * expr5a is expr5b
4001 * expr5a isnot expr5b
4002 *
4003 * Produces instructions:
4004 * EVAL expr5a Push result of "expr5a"
4005 * EVAL expr5b Push result of "expr5b"
4006 * COMPARE one of the compare instructions
4007 */
4008 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010{
4011 exptype_T type = EXPR_UNKNOWN;
4012 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004013 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004016 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017
4018 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004019 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 return FAIL;
4021
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004022 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004023 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024
4025 /*
4026 * If there is a comparative operator, use it.
4027 */
4028 if (type != EXPR_UNKNOWN)
4029 {
4030 int ic = FALSE; // Default: do not ignore case
4031
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004032 if (next != NULL)
4033 {
4034 *arg = next_line_from_context(cctx, TRUE);
4035 p = skipwhite(*arg);
4036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 if (type_is && (p[len] == '?' || p[len] == '#'))
4038 {
4039 semsg(_(e_invexpr2), *arg);
4040 return FAIL;
4041 }
4042 // extra question mark appended: ignore case
4043 if (p[len] == '?')
4044 {
4045 ic = TRUE;
4046 ++len;
4047 }
4048 // extra '#' appended: match case (ignored)
4049 else if (p[len] == '#')
4050 ++len;
4051 // nothing appended: match case
4052
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004053 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004055 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004056 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 }
4058
4059 // get the second variable
4060 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004061 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004062 return FAIL;
4063
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 return FAIL;
4066
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 if (ppconst->pp_used == ppconst_used + 2)
4068 {
4069 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4070 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4071 int ret;
4072
4073 // Both sides are a constant, compute the result now.
4074 // First check for a valid combination of types, this is more
4075 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004076 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004077 ret = FAIL;
4078 else
4079 {
4080 ret = typval_compare(tv1, tv2, type, ic);
4081 tv1->v_type = VAR_BOOL;
4082 tv1->vval.v_number = tv1->vval.v_number
4083 ? VVAL_TRUE : VVAL_FALSE;
4084 clear_tv(tv2);
4085 --ppconst->pp_used;
4086 }
4087 return ret;
4088 }
4089
4090 generate_ppconst(cctx, ppconst);
4091 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 }
4093
4094 return OK;
4095}
4096
Bram Moolenaar7f141552020-05-09 17:35:53 +02004097static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099/*
4100 * Compile || or &&.
4101 */
4102 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103compile_and_or(
4104 char_u **arg,
4105 cctx_T *cctx,
4106 char *op,
4107 ppconst_T *ppconst,
4108 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004110 char_u *next;
4111 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 int opchar = *op;
4113
4114 if (p[0] == opchar && p[1] == opchar)
4115 {
4116 garray_T *instr = &cctx->ctx_instr;
4117 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004118 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004119 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
4121 /*
4122 * Repeat until there is no following "||" or "&&"
4123 */
4124 ga_init2(&end_ga, sizeof(int), 10);
4125 while (p[0] == opchar && p[1] == opchar)
4126 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004127 if (next != NULL)
4128 {
4129 *arg = next_line_from_context(cctx, TRUE);
4130 p = skipwhite(*arg);
4131 }
4132
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004133 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4134 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004135 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004136 return FAIL;
4137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004139 // TODO: use ppconst if the value is a constant and check
4140 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141 generate_ppconst(cctx, ppconst);
4142
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004143 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4144 all_bool_values = FALSE;
4145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 if (ga_grow(&end_ga, 1) == FAIL)
4147 {
4148 ga_clear(&end_ga);
4149 return FAIL;
4150 }
4151 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4152 ++end_ga.ga_len;
4153 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004154 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155
4156 // eval the next expression
4157 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004158 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004159 return FAIL;
4160
Bram Moolenaara5565e42020-05-09 15:44:01 +02004161 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4162 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 {
4164 ga_clear(&end_ga);
4165 return FAIL;
4166 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004167
4168 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004170 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171
4172 // Fill in the end label in all jumps.
4173 while (end_ga.ga_len > 0)
4174 {
4175 isn_T *isn;
4176
4177 --end_ga.ga_len;
4178 isn = ((isn_T *)instr->ga_data)
4179 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4180 isn->isn_arg.jump.jump_where = instr->ga_len;
4181 }
4182 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004183
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004184 // The resulting type is converted to bool if needed.
4185 if (!all_bool_values)
4186 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 }
4188
4189 return OK;
4190}
4191
4192/*
4193 * expr4a && expr4a && expr4a logical AND
4194 *
4195 * Produces instructions:
4196 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004197 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004199 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004201 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 * end:
4203 */
4204 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004205compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004207 int ppconst_used = ppconst->pp_used;
4208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 return FAIL;
4212
4213 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004214 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215}
4216
4217/*
4218 * expr3a || expr3b || expr3c logical OR
4219 *
4220 * Produces instructions:
4221 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004222 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004224 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004226 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 * end:
4228 */
4229 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004230compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232 int ppconst_used = ppconst->pp_used;
4233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 return FAIL;
4237
4238 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004239 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240}
4241
4242/*
4243 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004245 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 * JUMP_IF_FALSE alt jump if false
4247 * EVAL expr1a
4248 * JUMP_ALWAYS end
4249 * alt: EVAL expr1b
4250 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004251 *
4252 * Toplevel expression: expr2 ?? expr1
4253 * Produces instructions:
4254 * EVAL expr2 Push result of "expr2"
4255 * JUMP_AND_KEEP_IF_TRUE end jump if true
4256 * EVAL expr1
4257 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 */
4259 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004260compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261{
4262 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004263 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004264 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265
Bram Moolenaar3988f642020-08-27 22:43:03 +02004266 // Ignore all kinds of errors when not producing code.
4267 if (cctx->ctx_skip == SKIP_YES)
4268 {
4269 skip_expr(arg);
4270 return OK;
4271 }
4272
Bram Moolenaar61a89812020-05-07 16:58:17 +02004273 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004274 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 return FAIL;
4276
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004277 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 if (*p == '?')
4279 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004280 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 garray_T *instr = &cctx->ctx_instr;
4282 garray_T *stack = &cctx->ctx_type_stack;
4283 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004284 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004286 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287 int has_const_expr = FALSE;
4288 int const_value = FALSE;
4289 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004291 if (next != NULL)
4292 {
4293 *arg = next_line_from_context(cctx, TRUE);
4294 p = skipwhite(*arg);
4295 }
4296
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004297 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004298 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004299 semsg(_(e_white_space_required_before_and_after_str),
4300 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004301 return FAIL;
4302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 if (ppconst->pp_used == ppconst_used + 1)
4305 {
4306 // the condition is a constant, we know whether the ? or the :
4307 // expression is to be evaluated.
4308 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004309 if (op_falsy)
4310 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4311 else
4312 {
4313 int error = FALSE;
4314
4315 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4316 &error);
4317 if (error)
4318 return FAIL;
4319 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004320 cctx->ctx_skip = save_skip == SKIP_YES ||
4321 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4322
4323 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4324 // "left ?? right" and "left" is truthy: produce "left"
4325 generate_ppconst(cctx, ppconst);
4326 else
4327 {
4328 clear_tv(&ppconst->pp_tv[ppconst_used]);
4329 --ppconst->pp_used;
4330 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004331 }
4332 else
4333 {
4334 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004335 if (op_falsy)
4336 end_idx = instr->ga_len;
4337 generate_JUMP(cctx, op_falsy
4338 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4339 if (op_falsy)
4340 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342
4343 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004344 *arg = skipwhite(p + 1 + op_falsy);
4345 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004346 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004348 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349
Bram Moolenaara5565e42020-05-09 15:44:01 +02004350 if (!has_const_expr)
4351 {
4352 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004354 if (!op_falsy)
4355 {
4356 // remember the type and drop it
4357 --stack->ga_len;
4358 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004360 end_idx = instr->ga_len;
4361 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004363 // jump here from JUMP_IF_FALSE
4364 isn = ((isn_T *)instr->ga_data) + alt_idx;
4365 isn->isn_arg.jump.jump_where = instr->ga_len;
4366 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004369 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004371 // Check for the ":".
4372 p = may_peek_next_line(cctx, *arg, &next);
4373 if (*p != ':')
4374 {
4375 emsg(_(e_missing_colon));
4376 return FAIL;
4377 }
4378 if (next != NULL)
4379 {
4380 *arg = next_line_from_context(cctx, TRUE);
4381 p = skipwhite(*arg);
4382 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004383
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004384 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4385 {
4386 semsg(_(e_white_space_required_before_and_after_str), ":");
4387 return FAIL;
4388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004390 // evaluate the third expression
4391 if (has_const_expr)
4392 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004393 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004394 *arg = skipwhite(p + 1);
4395 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4396 return FAIL;
4397 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4398 return FAIL;
4399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 if (!has_const_expr)
4402 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004403 type_T **typep;
4404
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
Bram Moolenaara5565e42020-05-09 15:44:01 +02004407 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004408 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4409 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004411 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412 isn = ((isn_T *)instr->ga_data) + end_idx;
4413 isn->isn_arg.jump.jump_where = instr->ga_len;
4414 }
4415
4416 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 }
4418 return OK;
4419}
4420
4421/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 * Toplevel expression.
4423 */
4424 static int
4425compile_expr0(char_u **arg, cctx_T *cctx)
4426{
4427 ppconst_T ppconst;
4428
4429 CLEAR_FIELD(ppconst);
4430 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4431 {
4432 clear_ppconst(&ppconst);
4433 return FAIL;
4434 }
4435 if (generate_ppconst(cctx, &ppconst) == FAIL)
4436 return FAIL;
4437 return OK;
4438}
4439
4440/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 * compile "return [expr]"
4442 */
4443 static char_u *
4444compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4445{
4446 char_u *p = arg;
4447 garray_T *stack = &cctx->ctx_type_stack;
4448 type_T *stack_type;
4449
4450 if (*p != NUL && *p != '|' && *p != '\n')
4451 {
4452 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 return NULL;
4455
4456 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4457 if (set_return_type)
4458 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004459 else
4460 {
4461 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4462 && stack_type->tt_type != VAR_VOID
4463 && stack_type->tt_type != VAR_UNKNOWN)
4464 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004465 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004466 return NULL;
4467 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004468 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4469 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004470 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 }
4473 else
4474 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004475 // "set_return_type" cannot be TRUE, only used for a lambda which
4476 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004477 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4478 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004480 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 return NULL;
4482 }
4483
4484 // No argument, return zero.
4485 generate_PUSHNR(cctx, 0);
4486 }
4487
4488 if (generate_instr(cctx, ISN_RETURN) == NULL)
4489 return NULL;
4490
4491 // "return val | endif" is possible
4492 return skipwhite(p);
4493}
4494
4495/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004496 * Get a line from the compilation context, compatible with exarg_T getline().
4497 * Return a pointer to the line in allocated memory.
4498 * Return NULL for end-of-file or some error.
4499 */
4500 static char_u *
4501exarg_getline(
4502 int c UNUSED,
4503 void *cookie,
4504 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004505 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004506{
4507 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004508 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004509
Bram Moolenaar66250c92020-08-20 15:02:42 +02004510 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004511 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004512 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004513 return NULL;
4514 ++cctx->ctx_lnum;
4515 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4516 // Comment lines result in NULL pointers, skip them.
4517 if (p != NULL)
4518 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004519 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004520}
4521
4522/*
4523 * Compile a nested :def command.
4524 */
4525 static char_u *
4526compile_nested_function(exarg_T *eap, cctx_T *cctx)
4527{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004528 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004529 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004530 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004531 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004532 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004533 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004534
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004535 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004536 {
4537 emsg(_(e_cannot_use_bang_with_nested_def));
4538 return NULL;
4539 }
4540
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004541 // Only g:Func() can use a namespace.
4542 if (name_start[1] == ':' && !is_global)
4543 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004544 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004545 return NULL;
4546 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004547 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4548 return NULL;
4549
Bram Moolenaar04b12692020-05-04 23:24:44 +02004550 eap->arg = name_end;
4551 eap->getline = exarg_getline;
4552 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004553 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004554 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004555 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004556 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004557
Bram Moolenaar822ba242020-05-24 23:00:18 +02004558 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004559 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004560 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004561 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004562 {
4563 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004564 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004565 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004566
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004567 if (is_global)
4568 {
4569 char_u *func_name = vim_strnsave(name_start + 2,
4570 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004571
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004572 if (func_name == NULL)
4573 r = FAIL;
4574 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004575 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004576 }
4577 else
4578 {
4579 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004580 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004581 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004582 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004583
Bram Moolenaareef21022020-08-01 22:16:43 +02004584 if (lvar == NULL)
4585 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004586 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004587 return NULL;
4588 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004589
4590 // copy over the block scope IDs
4591 if (block_depth > 0)
4592 {
4593 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4594 if (ufunc->uf_block_ids != NULL)
4595 {
4596 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4597 sizeof(int) * block_depth);
4598 ufunc->uf_block_depth = block_depth;
4599 }
4600 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004601 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004602
Bram Moolenaar61a89812020-05-07 16:58:17 +02004603 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004604 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004605}
4606
4607/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 * Return the length of an assignment operator, or zero if there isn't one.
4609 */
4610 int
4611assignment_len(char_u *p, int *heredoc)
4612{
4613 if (*p == '=')
4614 {
4615 if (p[1] == '<' && p[2] == '<')
4616 {
4617 *heredoc = TRUE;
4618 return 3;
4619 }
4620 return 1;
4621 }
4622 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4623 return 2;
4624 if (STRNCMP(p, "..=", 3) == 0)
4625 return 3;
4626 return 0;
4627}
4628
4629// words that cannot be used as a variable
4630static char *reserved[] = {
4631 "true",
4632 "false",
4633 NULL
4634};
4635
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004636typedef enum {
4637 dest_local,
4638 dest_option,
4639 dest_env,
4640 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004641 dest_buffer,
4642 dest_window,
4643 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004644 dest_vimvar,
4645 dest_script,
4646 dest_reg,
4647} assign_dest_T;
4648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004650 * Generate the load instruction for "name".
4651 */
4652 static void
4653generate_loadvar(
4654 cctx_T *cctx,
4655 assign_dest_T dest,
4656 char_u *name,
4657 lvar_T *lvar,
4658 type_T *type)
4659{
4660 switch (dest)
4661 {
4662 case dest_option:
4663 // TODO: check the option exists
4664 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4665 break;
4666 case dest_global:
4667 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4668 break;
4669 case dest_buffer:
4670 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4671 break;
4672 case dest_window:
4673 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4674 break;
4675 case dest_tab:
4676 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4677 break;
4678 case dest_script:
4679 compile_load_scriptvar(cctx,
4680 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4681 break;
4682 case dest_env:
4683 // Include $ in the name here
4684 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4685 break;
4686 case dest_reg:
4687 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4688 break;
4689 case dest_vimvar:
4690 generate_LOADV(cctx, name + 2, TRUE);
4691 break;
4692 case dest_local:
4693 if (lvar->lv_from_outer)
4694 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4695 NULL, type);
4696 else
4697 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4698 break;
4699 }
4700}
4701
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004702 void
4703vim9_declare_error(char_u *name)
4704{
4705 char *scope = "";
4706
4707 switch (*name)
4708 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004709 case 'g': scope = _("global"); break;
4710 case 'b': scope = _("buffer"); break;
4711 case 'w': scope = _("window"); break;
4712 case 't': scope = _("tab"); break;
4713 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004714 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004715 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004716 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004717 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004718 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004719 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004720 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004721 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004722 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004723}
4724
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004725/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004726 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004727 * "let name"
4728 * "var name = expr"
4729 * "final name = expr"
4730 * "const name = expr"
4731 * "name = expr"
4732 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004733 * Return NULL for an error.
4734 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 */
4736 static char_u *
4737compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4738{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004739 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004741 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 char_u *ret = NULL;
4743 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004744 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004745 int scriptvar_sid = 0;
4746 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004749 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 int oplen = 0;
4752 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004753 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004754 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004755 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004757 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4758 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004760 // Skip over the "var" or "[var, var]" to get to any "=".
4761 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4762 if (p == NULL)
4763 return *arg == '[' ? arg : NULL;
4764
4765 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004767 // TODO: should we allow this, and figure out type inference from list
4768 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004769 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 return NULL;
4771 }
4772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 sp = p;
4774 p = skipwhite(p);
4775 op = p;
4776 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777
4778 if (var_count > 0 && oplen == 0)
4779 // can be something like "[1, 2]->func()"
4780 return arg;
4781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4783 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004784 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004785 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004786 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 if (heredoc)
4789 {
4790 list_T *l;
4791 listitem_T *li;
4792
4793 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004794 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004796 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004797 if (l == NULL)
4798 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799
Bram Moolenaar078269b2020-09-21 20:35:55 +02004800 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004802 // Push each line and the create the list.
4803 FOR_ALL_LIST_ITEMS(l, li)
4804 {
4805 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4806 li->li_tv.vval.v_string = NULL;
4807 }
4808 generate_NEWLIST(cctx, l->lv_len);
4809 type = &t_list_string;
4810 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 list_free(l);
4813 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004814 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004816 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004818 // for "[var, var] = expr" evaluate the expression here, loop over the
4819 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004820
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004821 p = skipwhite(op + oplen);
4822 if (compile_expr0(&p, cctx) == FAIL)
4823 return NULL;
4824 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004826 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004828 type_T *stacktype;
4829
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004830 stacktype = stack->ga_len == 0 ? &t_void
4831 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004832 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004834 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004835 goto theend;
4836 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004837 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004839 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004840 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4841 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004842 }
4843 }
4844
4845 /*
4846 * Loop over variables in "[var, var] = expr".
4847 * For "var = expr" and "let var: type" this is done only once.
4848 */
4849 if (var_count > 0)
4850 var_start = skipwhite(arg + 1); // skip over the "["
4851 else
4852 var_start = arg;
4853 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4854 {
4855 char_u *var_end = skip_var_one(var_start, FALSE);
4856 size_t varlen;
4857 int new_local = FALSE;
4858 int opt_type;
4859 int opt_flags = 0;
4860 assign_dest_T dest = dest_local;
4861 int vimvaridx = -1;
4862 lvar_T *lvar = NULL;
4863 lvar_T arg_lvar;
4864 int has_type = FALSE;
4865 int has_index = FALSE;
4866 int instr_count = -1;
4867
Bram Moolenaar65821722020-08-02 18:58:54 +02004868 if (*var_start == '@')
4869 p = var_start + 2;
4870 else
4871 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004872 // skip over the leading "&", "&l:", "&g:" and "$"
4873 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004874 p = to_name_end(p, TRUE);
4875 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876
4877 // "a: type" is declaring variable "a" with a type, not "a:".
4878 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4879 --var_end;
4880 if (is_decl && p == var_start + 2 && p[-1] == ':')
4881 --p;
4882
4883 varlen = p - var_start;
4884 vim_free(name);
4885 name = vim_strnsave(var_start, varlen);
4886 if (name == NULL)
4887 return NULL;
4888 if (!heredoc)
4889 type = &t_any;
4890
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004891 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004892 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004893 int declare_error = FALSE;
4894
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004895 if (*var_start == '&')
4896 {
4897 int cc;
4898 long numval;
4899
4900 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004901 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004903 emsg(_(e_const_option));
4904 goto theend;
4905 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004906 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004907 p = var_start;
4908 p = find_option_end(&p, &opt_flags);
4909 if (p == NULL)
4910 {
4911 // cannot happen?
4912 emsg(_(e_letunexp));
4913 goto theend;
4914 }
4915 cc = *p;
4916 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004917 opt_type = get_option_value(skip_option_env_lead(var_start),
4918 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004919 *p = cc;
4920 if (opt_type == -3)
4921 {
4922 semsg(_(e_unknown_option), var_start);
4923 goto theend;
4924 }
4925 if (opt_type == -2 || opt_type == 0)
4926 type = &t_string;
4927 else
4928 type = &t_number; // both number and boolean option
4929 }
4930 else if (*var_start == '$')
4931 {
4932 dest = dest_env;
4933 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004934 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 }
4936 else if (*var_start == '@')
4937 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004938 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004939 {
4940 emsg_invreg(var_start[1]);
4941 goto theend;
4942 }
4943 dest = dest_reg;
4944 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004945 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004947 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 {
4949 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004950 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004951 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004952 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004953 {
4954 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004955 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004956 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004957 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004958 {
4959 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004960 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004962 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004963 {
4964 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004965 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004966 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004967 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 {
4969 typval_T *vtv;
4970 int di_flags;
4971
4972 vimvaridx = find_vim_var(name + 2, &di_flags);
4973 if (vimvaridx < 0)
4974 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004975 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004976 goto theend;
4977 }
4978 // We use the current value of "sandbox" here, is that OK?
4979 if (var_check_ro(di_flags, name, FALSE))
4980 goto theend;
4981 dest = dest_vimvar;
4982 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004983 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004984 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985 }
4986 else
4987 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004988 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989
4990 for (idx = 0; reserved[idx] != NULL; ++idx)
4991 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004992 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004993 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004994 goto theend;
4995 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996
4997 lvar = lookup_local(var_start, varlen, cctx);
4998 if (lvar == NULL)
4999 {
5000 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005001 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5003 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005004 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005 if (is_decl)
5006 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005007 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005008 goto theend;
5009 }
5010 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005011 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005012 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013 if (lvar != NULL)
5014 {
5015 if (is_decl)
5016 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005017 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 goto theend;
5019 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005021 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005022 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005023 int script_namespace = varlen > 1
5024 && STRNCMP(var_start, "s:", 2) == 0;
5025 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005026 ? script_var_exists(var_start + 2, varlen - 2,
5027 FALSE, cctx)
5028 : script_var_exists(var_start, varlen,
5029 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005030 imported_T *import =
5031 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005032
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005033 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005035 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5036
5037 if (is_decl)
5038 {
5039 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005040 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005041 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005042 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005043 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005044 name);
5045 goto theend;
5046 }
5047 else if (cctx->ctx_ufunc->uf_script_ctx_version
5048 == SCRIPT_VERSION_VIM9
5049 && script_namespace
5050 && !script_var && import == NULL)
5051 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005052 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005053 goto theend;
5054 }
5055
5056 dest = dest_script;
5057
5058 // existing script-local variables should have a type
5059 scriptvar_sid = current_sctx.sc_sid;
5060 if (import != NULL)
5061 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005062 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005063 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005064 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005065 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005066 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005067 {
5068 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5069 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005070 ((svar_T *)si->sn_var_vals.ga_data)
5071 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005072 type = sv->sv_type;
5073 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005074 }
5075 }
5076 else if (name[1] == ':' && name[2] != NUL)
5077 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005078 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 goto theend;
5080 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005081 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005082 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005083 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005084 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005085 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005086 else if (check_defined(var_start, varlen, cctx) == FAIL)
5087 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005090
5091 if (declare_error)
5092 {
5093 vim9_declare_error(name);
5094 goto theend;
5095 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 }
5097
5098 // handle "a:name" as a name, not index "name" on "a"
5099 if (varlen > 1 || var_start[varlen] != ':')
5100 p = var_end;
5101
5102 if (dest != dest_option)
5103 {
5104 if (is_decl && *p == ':')
5105 {
5106 // parse optional type: "let var: type = expr"
5107 if (!VIM_ISWHITE(p[1]))
5108 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005109 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005110 goto theend;
5111 }
5112 p = skipwhite(p + 1);
5113 type = parse_type(&p, cctx->ctx_type_list);
5114 has_type = TRUE;
5115 }
5116 else if (lvar != NULL)
5117 type = lvar->lv_type;
5118 }
5119
5120 if (oplen == 3 && !heredoc && dest != dest_global
5121 && type->tt_type != VAR_STRING
5122 && type->tt_type != VAR_ANY)
5123 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005124 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005125 goto theend;
5126 }
5127
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005128 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005129 {
5130 if (oplen > 1 && !heredoc)
5131 {
5132 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005133 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 goto theend;
5135 }
5136
5137 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005138 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5139 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140 goto theend;
5141 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005142 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 if (lvar == NULL)
5144 goto theend;
5145 new_local = TRUE;
5146 }
5147
5148 member_type = type;
5149 if (var_end > var_start + varlen)
5150 {
5151 // Something follows after the variable: "var[idx]".
5152 if (is_decl)
5153 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005154 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 goto theend;
5156 }
5157
5158 if (var_start[varlen] == '[')
5159 {
5160 has_index = TRUE;
5161 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005162 member_type = &t_any;
5163 else
5164 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 }
5166 else
5167 {
5168 semsg("Not supported yet: %s", var_start);
5169 goto theend;
5170 }
5171 }
5172 else if (lvar == &arg_lvar)
5173 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005174 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 goto theend;
5176 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005177 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5178 {
5179 semsg(_(e_cannot_assign_to_constant), name);
5180 goto theend;
5181 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005182
5183 if (!heredoc)
5184 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005185 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005186 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005187 if (oplen > 0 && var_count == 0)
5188 {
5189 // skip over the "=" and the expression
5190 p = skipwhite(op + oplen);
5191 compile_expr0(&p, cctx);
5192 }
5193 }
5194 else if (oplen > 0)
5195 {
5196 type_T *stacktype;
5197
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005198 // For "var = expr" evaluate the expression.
5199 if (var_count == 0)
5200 {
5201 int r;
5202
5203 // for "+=", "*=", "..=" etc. first load the current value
5204 if (*op != '=')
5205 {
5206 generate_loadvar(cctx, dest, name, lvar, type);
5207
5208 if (has_index)
5209 {
5210 // TODO: get member from list or dict
5211 emsg("Index with operation not supported yet");
5212 goto theend;
5213 }
5214 }
5215
5216 // Compile the expression. Temporarily hide the new local
5217 // variable here, it is not available to this expression.
5218 if (new_local)
5219 --cctx->ctx_locals.ga_len;
5220 instr_count = instr->ga_len;
5221 p = skipwhite(op + oplen);
5222 r = compile_expr0(&p, cctx);
5223 if (new_local)
5224 ++cctx->ctx_locals.ga_len;
5225 if (r == FAIL)
5226 goto theend;
5227 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005228 else if (semicolon && var_idx == var_count - 1)
5229 {
5230 // For "[var; var] = expr" get the rest of the list
5231 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5232 goto theend;
5233 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005234 else
5235 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005236 // For "[var, var] = expr" get the "var_idx" item from the
5237 // list.
5238 if (generate_GETITEM(cctx, var_idx) == FAIL)
5239 return FAIL;
5240 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005241
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005242 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005243 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005244 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005245 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005246 if ((stacktype->tt_type == VAR_FUNC
5247 || stacktype->tt_type == VAR_PARTIAL)
5248 && var_wrong_func_name(name, TRUE))
5249 goto theend;
5250
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005251 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005252 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005253 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005255 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005256 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005257 }
5258 else
5259 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005260 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005261 // for a variable that implies &t_any.
5262 if (stacktype == &t_list_empty)
5263 lvar->lv_type = &t_list_any;
5264 else if (stacktype == &t_dict_empty)
5265 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005266 else if (stacktype == &t_unknown)
5267 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005268 else
5269 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005270 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005271 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005272 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005273 {
5274 type_T *use_type = lvar->lv_type;
5275
Bram Moolenaar71abe482020-09-14 22:28:30 +02005276 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005277 if (has_index)
5278 {
5279 use_type = use_type->tt_member;
5280 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005281 // could be indexing "any"
5282 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005283 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005284 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005285 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005286 goto theend;
5287 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005288 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005289 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005290 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005291 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005293 else if (cmdidx == CMD_final)
5294 {
5295 emsg(_(e_final_requires_a_value));
5296 goto theend;
5297 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005300 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005301 goto theend;
5302 }
5303 else if (!has_type || dest == dest_option)
5304 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005305 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 goto theend;
5307 }
5308 else
5309 {
5310 // variables are always initialized
5311 if (ga_grow(instr, 1) == FAIL)
5312 goto theend;
5313 switch (member_type->tt_type)
5314 {
5315 case VAR_BOOL:
5316 generate_PUSHBOOL(cctx, VVAL_FALSE);
5317 break;
5318 case VAR_FLOAT:
5319#ifdef FEAT_FLOAT
5320 generate_PUSHF(cctx, 0.0);
5321#endif
5322 break;
5323 case VAR_STRING:
5324 generate_PUSHS(cctx, NULL);
5325 break;
5326 case VAR_BLOB:
5327 generate_PUSHBLOB(cctx, NULL);
5328 break;
5329 case VAR_FUNC:
5330 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5331 break;
5332 case VAR_LIST:
5333 generate_NEWLIST(cctx, 0);
5334 break;
5335 case VAR_DICT:
5336 generate_NEWDICT(cctx, 0);
5337 break;
5338 case VAR_JOB:
5339 generate_PUSHJOB(cctx, NULL);
5340 break;
5341 case VAR_CHANNEL:
5342 generate_PUSHCHANNEL(cctx, NULL);
5343 break;
5344 case VAR_NUMBER:
5345 case VAR_UNKNOWN:
5346 case VAR_ANY:
5347 case VAR_PARTIAL:
5348 case VAR_VOID:
5349 case VAR_SPECIAL: // cannot happen
5350 generate_PUSHNR(cctx, 0);
5351 break;
5352 }
5353 }
5354 if (var_count == 0)
5355 end = p;
5356 }
5357
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005358 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005359 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005360 break;
5361
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005362 if (oplen > 0 && *op != '=')
5363 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005364 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365 type_T *stacktype;
5366
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005367 if (*op == '.')
5368 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005369 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005370 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005372 if (
5373#ifdef FEAT_FLOAT
5374 // If variable is float operation with number is OK.
5375 !(expected == &t_float && stacktype == &t_number) &&
5376#endif
5377 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378 goto theend;
5379
5380 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005381 {
5382 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5383 goto theend;
5384 }
5385 else if (*op == '+')
5386 {
5387 if (generate_add_instr(cctx,
5388 operator_type(member_type, stacktype),
5389 member_type, stacktype) == FAIL)
5390 goto theend;
5391 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005392 else if (generate_two_op(cctx, op) == FAIL)
5393 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005396 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005397 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398 int r;
5399
5400 // Compile the "idx" in "var[idx]".
5401 if (new_local)
5402 --cctx->ctx_locals.ga_len;
5403 p = skipwhite(var_start + varlen + 1);
5404 r = compile_expr0(&p, cctx);
5405 if (new_local)
5406 ++cctx->ctx_locals.ga_len;
5407 if (r == FAIL)
5408 goto theend;
5409 if (*skipwhite(p) != ']')
5410 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005411 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005412 emsg(_(e_missbrac));
5413 goto theend;
5414 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005415 if (type == &t_any)
5416 {
5417 type_T *idx_type = ((type_T **)stack->ga_data)[
5418 stack->ga_len - 1];
5419 // Index on variable of unknown type: guess the type from the
5420 // index type: number is dict, otherwise dict.
5421 // TODO: should do the assignment at runtime
5422 if (idx_type->tt_type == VAR_NUMBER)
5423 type = &t_list_any;
5424 else
5425 type = &t_dict_any;
5426 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005427 if (type->tt_type == VAR_DICT
5428 && may_generate_2STRING(-1, cctx) == FAIL)
5429 goto theend;
5430 if (type->tt_type == VAR_LIST
5431 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005432 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 {
5434 emsg(_(e_number_exp));
5435 goto theend;
5436 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005437
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 // Load the dict or list. On the stack we then have:
5439 // - value
5440 // - index
5441 // - variable
5442 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005443
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005444 if (type->tt_type == VAR_LIST)
5445 {
5446 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5447 return FAIL;
5448 }
5449 else if (type->tt_type == VAR_DICT)
5450 {
5451 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5452 return FAIL;
5453 }
5454 else
5455 {
5456 emsg(_(e_listreq));
5457 goto theend;
5458 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005459 }
5460 else
5461 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005462 if (is_decl && cmdidx == CMD_const
5463 && (dest == dest_script || dest == dest_local))
5464 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005465 generate_LOCKCONST(cctx);
5466
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005467 switch (dest)
5468 {
5469 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005470 generate_STOREOPT(cctx, skip_option_env_lead(name),
5471 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005472 break;
5473 case dest_global:
5474 // include g: with the name, easier to execute that way
5475 generate_STORE(cctx, ISN_STOREG, 0, name);
5476 break;
5477 case dest_buffer:
5478 // include b: with the name, easier to execute that way
5479 generate_STORE(cctx, ISN_STOREB, 0, name);
5480 break;
5481 case dest_window:
5482 // include w: with the name, easier to execute that way
5483 generate_STORE(cctx, ISN_STOREW, 0, name);
5484 break;
5485 case dest_tab:
5486 // include t: with the name, easier to execute that way
5487 generate_STORE(cctx, ISN_STORET, 0, name);
5488 break;
5489 case dest_env:
5490 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5491 break;
5492 case dest_reg:
5493 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5494 break;
5495 case dest_vimvar:
5496 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5497 break;
5498 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005499 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005500 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005501 {
5502 char_u *name_s = name;
5503
5504 // Include s: in the name for store_var()
5505 if (name[1] != ':')
5506 {
5507 int len = (int)STRLEN(name) + 3;
5508
5509 name_s = alloc(len);
5510 if (name_s == NULL)
5511 name_s = name;
5512 else
5513 vim_snprintf((char *)name_s, len,
5514 "s:%s", name);
5515 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005516 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5517 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005518 if (name_s != name)
5519 vim_free(name_s);
5520 }
5521 else
5522 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005523 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005524 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005525 break;
5526 case dest_local:
5527 if (lvar != NULL)
5528 {
5529 isn_T *isn = ((isn_T *)instr->ga_data)
5530 + instr->ga_len - 1;
5531
5532 // optimization: turn "var = 123" from ISN_PUSHNR +
5533 // ISN_STORE into ISN_STORENR
5534 if (!lvar->lv_from_outer
5535 && instr->ga_len == instr_count + 1
5536 && isn->isn_type == ISN_PUSHNR)
5537 {
5538 varnumber_T val = isn->isn_arg.number;
5539
5540 isn->isn_type = ISN_STORENR;
5541 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5542 isn->isn_arg.storenr.stnr_val = val;
5543 if (stack->ga_len > 0)
5544 --stack->ga_len;
5545 }
5546 else if (lvar->lv_from_outer)
5547 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005548 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 else
5550 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5551 }
5552 break;
5553 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005554 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555
5556 if (var_idx + 1 < var_count)
5557 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005559
5560 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005561 if (var_count > 0 && !semicolon)
5562 {
5563 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5564 goto theend;
5565 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005566
Bram Moolenaarb2097502020-07-19 17:17:02 +02005567 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
5569theend:
5570 vim_free(name);
5571 return ret;
5572}
5573
5574/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005575 * Check if "name" can be "unlet".
5576 */
5577 int
5578check_vim9_unlet(char_u *name)
5579{
5580 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5581 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005582 // "unlet s:var" is allowed in legacy script.
5583 if (*name == 's' && !script_is_vim9())
5584 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005585 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005586 return FAIL;
5587 }
5588 return OK;
5589}
5590
5591/*
5592 * Callback passed to ex_unletlock().
5593 */
5594 static int
5595compile_unlet(
5596 lval_T *lvp,
5597 char_u *name_end,
5598 exarg_T *eap,
5599 int deep UNUSED,
5600 void *coookie)
5601{
5602 cctx_T *cctx = coookie;
5603
5604 if (lvp->ll_tv == NULL)
5605 {
5606 char_u *p = lvp->ll_name;
5607 int cc = *name_end;
5608 int ret = OK;
5609
5610 // Normal name. Only supports g:, w:, t: and b: namespaces.
5611 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005612 if (*p == '$')
5613 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5614 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005615 ret = FAIL;
5616 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005617 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005618
5619 *name_end = cc;
5620 return ret;
5621 }
5622
5623 // TODO: unlet {list}[idx]
5624 // TODO: unlet {dict}[key]
5625 emsg("Sorry, :unlet not fully implemented yet");
5626 return FAIL;
5627}
5628
5629/*
5630 * compile "unlet var", "lock var" and "unlock var"
5631 * "arg" points to "var".
5632 */
5633 static char_u *
5634compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5635{
5636 char_u *p = arg;
5637
5638 if (eap->cmdidx != CMD_unlet)
5639 {
5640 emsg("Sorry, :lock and unlock not implemented yet");
5641 return NULL;
5642 }
5643
5644 if (*p == '!')
5645 {
5646 p = skipwhite(p + 1);
5647 eap->forceit = TRUE;
5648 }
5649
5650 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5651 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5652}
5653
5654/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655 * Compile an :import command.
5656 */
5657 static char_u *
5658compile_import(char_u *arg, cctx_T *cctx)
5659{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005660 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661}
5662
5663/*
5664 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5665 */
5666 static int
5667compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5668{
5669 garray_T *instr = &cctx->ctx_instr;
5670 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5671
5672 if (endlabel == NULL)
5673 return FAIL;
5674 endlabel->el_next = *el;
5675 *el = endlabel;
5676 endlabel->el_end_label = instr->ga_len;
5677
5678 generate_JUMP(cctx, when, 0);
5679 return OK;
5680}
5681
5682 static void
5683compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5684{
5685 garray_T *instr = &cctx->ctx_instr;
5686
5687 while (*el != NULL)
5688 {
5689 endlabel_T *cur = (*el);
5690 isn_T *isn;
5691
5692 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5693 isn->isn_arg.jump.jump_where = instr->ga_len;
5694 *el = cur->el_next;
5695 vim_free(cur);
5696 }
5697}
5698
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005699 static void
5700compile_free_jump_to_end(endlabel_T **el)
5701{
5702 while (*el != NULL)
5703 {
5704 endlabel_T *cur = (*el);
5705
5706 *el = cur->el_next;
5707 vim_free(cur);
5708 }
5709}
5710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711/*
5712 * Create a new scope and set up the generic items.
5713 */
5714 static scope_T *
5715new_scope(cctx_T *cctx, scopetype_T type)
5716{
5717 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5718
5719 if (scope == NULL)
5720 return NULL;
5721 scope->se_outer = cctx->ctx_scope;
5722 cctx->ctx_scope = scope;
5723 scope->se_type = type;
5724 scope->se_local_count = cctx->ctx_locals.ga_len;
5725 return scope;
5726}
5727
5728/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005729 * Free the current scope and go back to the outer scope.
5730 */
5731 static void
5732drop_scope(cctx_T *cctx)
5733{
5734 scope_T *scope = cctx->ctx_scope;
5735
5736 if (scope == NULL)
5737 {
5738 iemsg("calling drop_scope() without a scope");
5739 return;
5740 }
5741 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005742 switch (scope->se_type)
5743 {
5744 case IF_SCOPE:
5745 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5746 case FOR_SCOPE:
5747 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5748 case WHILE_SCOPE:
5749 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5750 case TRY_SCOPE:
5751 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5752 case NO_SCOPE:
5753 case BLOCK_SCOPE:
5754 break;
5755 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005756 vim_free(scope);
5757}
5758
5759/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005760 * Check that the top of the type stack has a type that can be used as a
5761 * condition. Give an error and return FAIL if not.
5762 */
5763 static int
5764bool_on_stack(cctx_T *cctx)
5765{
5766 garray_T *stack = &cctx->ctx_type_stack;
5767 type_T *type;
5768
5769 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5770 if (type != &t_bool && type != &t_number && type != &t_any
5771 && need_type(type, &t_bool, -1, cctx, FALSE) == FAIL)
5772 return FAIL;
5773 return OK;
5774}
5775
5776/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777 * compile "if expr"
5778 *
5779 * "if expr" Produces instructions:
5780 * EVAL expr Push result of "expr"
5781 * JUMP_IF_FALSE end
5782 * ... body ...
5783 * end:
5784 *
5785 * "if expr | else" Produces instructions:
5786 * EVAL expr Push result of "expr"
5787 * JUMP_IF_FALSE else
5788 * ... body ...
5789 * JUMP_ALWAYS end
5790 * else:
5791 * ... body ...
5792 * end:
5793 *
5794 * "if expr1 | elseif expr2 | else" Produces instructions:
5795 * EVAL expr Push result of "expr"
5796 * JUMP_IF_FALSE elseif
5797 * ... body ...
5798 * JUMP_ALWAYS end
5799 * elseif:
5800 * EVAL expr Push result of "expr"
5801 * JUMP_IF_FALSE else
5802 * ... body ...
5803 * JUMP_ALWAYS end
5804 * else:
5805 * ... body ...
5806 * end:
5807 */
5808 static char_u *
5809compile_if(char_u *arg, cctx_T *cctx)
5810{
5811 char_u *p = arg;
5812 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005813 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005814 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005815 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005816 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817
Bram Moolenaara5565e42020-05-09 15:44:01 +02005818 CLEAR_FIELD(ppconst);
5819 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005820 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005821 clear_ppconst(&ppconst);
5822 return NULL;
5823 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005824 if (cctx->ctx_skip == SKIP_YES)
5825 clear_ppconst(&ppconst);
5826 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005827 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005828 int error = FALSE;
5829 int v;
5830
Bram Moolenaara5565e42020-05-09 15:44:01 +02005831 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005832 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005833 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005834 if (error)
5835 return NULL;
5836 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005837 }
5838 else
5839 {
5840 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005841 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005842 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005843 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005844 if (bool_on_stack(cctx) == FAIL)
5845 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005847
5848 scope = new_scope(cctx, IF_SCOPE);
5849 if (scope == NULL)
5850 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005851 scope->se_skip_save = skip_save;
5852 // "is_had_return" will be reset if any block does not end in :return
5853 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005855 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005856 {
5857 // "where" is set when ":elseif", "else" or ":endif" is found
5858 scope->se_u.se_if.is_if_label = instr->ga_len;
5859 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5860 }
5861 else
5862 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863
5864 return p;
5865}
5866
5867 static char_u *
5868compile_elseif(char_u *arg, cctx_T *cctx)
5869{
5870 char_u *p = arg;
5871 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005872 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873 isn_T *isn;
5874 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005875 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005876 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877
5878 if (scope == NULL || scope->se_type != IF_SCOPE)
5879 {
5880 emsg(_(e_elseif_without_if));
5881 return NULL;
5882 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005883 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005884 if (!cctx->ctx_had_return)
5885 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005887 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005888 {
5889 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005891 return NULL;
5892 // previous "if" or "elseif" jumps here
5893 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5894 isn->isn_arg.jump.jump_where = instr->ga_len;
5895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005897 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005898 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005899 if (cctx->ctx_skip == SKIP_YES)
5900 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005901 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005902 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005903 clear_ppconst(&ppconst);
5904 return NULL;
5905 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005906 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005907 if (scope->se_skip_save == SKIP_YES)
5908 clear_ppconst(&ppconst);
5909 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005910 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005911 int error = FALSE;
5912 int v;
5913
Bram Moolenaar7f141552020-05-09 17:35:53 +02005914 // The expression results in a constant.
5915 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02005916 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5917 if (error)
5918 return NULL;
5919 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005920 clear_ppconst(&ppconst);
5921 scope->se_u.se_if.is_if_label = -1;
5922 }
5923 else
5924 {
5925 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005926 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005927 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005928 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005929 if (bool_on_stack(cctx) == FAIL)
5930 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005932 // "where" is set when ":elseif", "else" or ":endif" is found
5933 scope->se_u.se_if.is_if_label = instr->ga_len;
5934 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936
5937 return p;
5938}
5939
5940 static char_u *
5941compile_else(char_u *arg, cctx_T *cctx)
5942{
5943 char_u *p = arg;
5944 garray_T *instr = &cctx->ctx_instr;
5945 isn_T *isn;
5946 scope_T *scope = cctx->ctx_scope;
5947
5948 if (scope == NULL || scope->se_type != IF_SCOPE)
5949 {
5950 emsg(_(e_else_without_if));
5951 return NULL;
5952 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005953 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005954 if (!cctx->ctx_had_return)
5955 scope->se_u.se_if.is_had_return = FALSE;
5956 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957
Bram Moolenaarefd88552020-06-18 20:50:10 +02005958 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005959 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005960 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005961 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005962 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005963 if (!cctx->ctx_had_return
5964 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5965 JUMP_ALWAYS, cctx) == FAIL)
5966 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005967 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005968
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005969 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005970 {
5971 if (scope->se_u.se_if.is_if_label >= 0)
5972 {
5973 // previous "if" or "elseif" jumps here
5974 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5975 isn->isn_arg.jump.jump_where = instr->ga_len;
5976 scope->se_u.se_if.is_if_label = -1;
5977 }
5978 }
5979
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005980 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005981 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983
5984 return p;
5985}
5986
5987 static char_u *
5988compile_endif(char_u *arg, cctx_T *cctx)
5989{
5990 scope_T *scope = cctx->ctx_scope;
5991 ifscope_T *ifscope;
5992 garray_T *instr = &cctx->ctx_instr;
5993 isn_T *isn;
5994
5995 if (scope == NULL || scope->se_type != IF_SCOPE)
5996 {
5997 emsg(_(e_endif_without_if));
5998 return NULL;
5999 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006000 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006001 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006002 if (!cctx->ctx_had_return)
6003 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006005 if (scope->se_u.se_if.is_if_label >= 0)
6006 {
6007 // previous "if" or "elseif" jumps here
6008 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6009 isn->isn_arg.jump.jump_where = instr->ga_len;
6010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 // Fill in the "end" label in jumps at the end of the blocks.
6012 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006013 cctx->ctx_skip = scope->se_skip_save;
6014
6015 // If all the blocks end in :return and there is an :else then the
6016 // had_return flag is set.
6017 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006019 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 return arg;
6021}
6022
6023/*
6024 * compile "for var in expr"
6025 *
6026 * Produces instructions:
6027 * PUSHNR -1
6028 * STORE loop-idx Set index to -1
6029 * EVAL expr Push result of "expr"
6030 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6031 * - if beyond end, jump to "end"
6032 * - otherwise get item from list and push it
6033 * STORE var Store item in "var"
6034 * ... body ...
6035 * JUMP top Jump back to repeat
6036 * end: DROP Drop the result of "expr"
6037 *
6038 */
6039 static char_u *
6040compile_for(char_u *arg, cctx_T *cctx)
6041{
6042 char_u *p;
6043 size_t varlen;
6044 garray_T *instr = &cctx->ctx_instr;
6045 garray_T *stack = &cctx->ctx_type_stack;
6046 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006047 lvar_T *loop_lvar; // loop iteration variable
6048 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 type_T *vartype;
6050
6051 // TODO: list of variables: "for [key, value] in dict"
6052 // parse "var"
6053 for (p = arg; eval_isnamec1(*p); ++p)
6054 ;
6055 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006056 var_lvar = lookup_local(arg, varlen, cctx);
6057 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006059 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 return NULL;
6061 }
6062
6063 // consume "in"
6064 p = skipwhite(p);
6065 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6066 {
6067 emsg(_(e_missing_in));
6068 return NULL;
6069 }
6070 p = skipwhite(p + 2);
6071
6072
6073 scope = new_scope(cctx, FOR_SCOPE);
6074 if (scope == NULL)
6075 return NULL;
6076
6077 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006078 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6079 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006080 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006081 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006082 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006083 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085
6086 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006087 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6088 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006089 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006090 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006091 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006095 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
6097 // compile "expr", it remains on the stack until "endfor"
6098 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006099 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006100 {
6101 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006105 // Now that we know the type of "var", check that it is a list, now or at
6106 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006108 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006110 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 return NULL;
6112 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006113 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006114 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115
6116 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006117 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006119 generate_FOR(cctx, loop_lvar->lv_idx);
6120 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121
6122 return arg;
6123}
6124
6125/*
6126 * compile "endfor"
6127 */
6128 static char_u *
6129compile_endfor(char_u *arg, cctx_T *cctx)
6130{
6131 garray_T *instr = &cctx->ctx_instr;
6132 scope_T *scope = cctx->ctx_scope;
6133 forscope_T *forscope;
6134 isn_T *isn;
6135
6136 if (scope == NULL || scope->se_type != FOR_SCOPE)
6137 {
6138 emsg(_(e_for));
6139 return NULL;
6140 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006141 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006143 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144
6145 // At end of ":for" scope jump back to the FOR instruction.
6146 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6147
6148 // Fill in the "end" label in the FOR statement so it can jump here
6149 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6150 isn->isn_arg.forloop.for_end = instr->ga_len;
6151
6152 // Fill in the "end" label any BREAK statements
6153 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6154
6155 // Below the ":for" scope drop the "expr" list from the stack.
6156 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6157 return NULL;
6158
6159 vim_free(scope);
6160
6161 return arg;
6162}
6163
6164/*
6165 * compile "while expr"
6166 *
6167 * Produces instructions:
6168 * top: EVAL expr Push result of "expr"
6169 * JUMP_IF_FALSE end jump if false
6170 * ... body ...
6171 * JUMP top Jump back to repeat
6172 * end:
6173 *
6174 */
6175 static char_u *
6176compile_while(char_u *arg, cctx_T *cctx)
6177{
6178 char_u *p = arg;
6179 garray_T *instr = &cctx->ctx_instr;
6180 scope_T *scope;
6181
6182 scope = new_scope(cctx, WHILE_SCOPE);
6183 if (scope == NULL)
6184 return NULL;
6185
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006186 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187
6188 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006189 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 return NULL;
6191
Bram Moolenaar13106602020-10-04 16:06:05 +02006192 if (bool_on_stack(cctx) == FAIL)
6193 return FAIL;
6194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006196 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197 JUMP_IF_FALSE, cctx) == FAIL)
6198 return FAIL;
6199
6200 return p;
6201}
6202
6203/*
6204 * compile "endwhile"
6205 */
6206 static char_u *
6207compile_endwhile(char_u *arg, cctx_T *cctx)
6208{
6209 scope_T *scope = cctx->ctx_scope;
6210
6211 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6212 {
6213 emsg(_(e_while));
6214 return NULL;
6215 }
6216 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006217 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218
6219 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006220 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221
6222 // Fill in the "end" label in the WHILE statement so it can jump here.
6223 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006224 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225
6226 vim_free(scope);
6227
6228 return arg;
6229}
6230
6231/*
6232 * compile "continue"
6233 */
6234 static char_u *
6235compile_continue(char_u *arg, cctx_T *cctx)
6236{
6237 scope_T *scope = cctx->ctx_scope;
6238
6239 for (;;)
6240 {
6241 if (scope == NULL)
6242 {
6243 emsg(_(e_continue));
6244 return NULL;
6245 }
6246 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6247 break;
6248 scope = scope->se_outer;
6249 }
6250
6251 // Jump back to the FOR or WHILE instruction.
6252 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006253 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6254 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255 return arg;
6256}
6257
6258/*
6259 * compile "break"
6260 */
6261 static char_u *
6262compile_break(char_u *arg, cctx_T *cctx)
6263{
6264 scope_T *scope = cctx->ctx_scope;
6265 endlabel_T **el;
6266
6267 for (;;)
6268 {
6269 if (scope == NULL)
6270 {
6271 emsg(_(e_break));
6272 return NULL;
6273 }
6274 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6275 break;
6276 scope = scope->se_outer;
6277 }
6278
6279 // Jump to the end of the FOR or WHILE loop.
6280 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006281 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006283 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6285 return FAIL;
6286
6287 return arg;
6288}
6289
6290/*
6291 * compile "{" start of block
6292 */
6293 static char_u *
6294compile_block(char_u *arg, cctx_T *cctx)
6295{
6296 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6297 return NULL;
6298 return skipwhite(arg + 1);
6299}
6300
6301/*
6302 * compile end of block: drop one scope
6303 */
6304 static void
6305compile_endblock(cctx_T *cctx)
6306{
6307 scope_T *scope = cctx->ctx_scope;
6308
6309 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006310 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 vim_free(scope);
6312}
6313
6314/*
6315 * compile "try"
6316 * Creates a new scope for the try-endtry, pointing to the first catch and
6317 * finally.
6318 * Creates another scope for the "try" block itself.
6319 * TRY instruction sets up exception handling at runtime.
6320 *
6321 * "try"
6322 * TRY -> catch1, -> finally push trystack entry
6323 * ... try block
6324 * "throw {exception}"
6325 * EVAL {exception}
6326 * THROW create exception
6327 * ... try block
6328 * " catch {expr}"
6329 * JUMP -> finally
6330 * catch1: PUSH exeception
6331 * EVAL {expr}
6332 * MATCH
6333 * JUMP nomatch -> catch2
6334 * CATCH remove exception
6335 * ... catch block
6336 * " catch"
6337 * JUMP -> finally
6338 * catch2: CATCH remove exception
6339 * ... catch block
6340 * " finally"
6341 * finally:
6342 * ... finally block
6343 * " endtry"
6344 * ENDTRY pop trystack entry, may rethrow
6345 */
6346 static char_u *
6347compile_try(char_u *arg, cctx_T *cctx)
6348{
6349 garray_T *instr = &cctx->ctx_instr;
6350 scope_T *try_scope;
6351 scope_T *scope;
6352
6353 // scope that holds the jumps that go to catch/finally/endtry
6354 try_scope = new_scope(cctx, TRY_SCOPE);
6355 if (try_scope == NULL)
6356 return NULL;
6357
6358 // "catch" is set when the first ":catch" is found.
6359 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006360 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 if (generate_instr(cctx, ISN_TRY) == NULL)
6362 return NULL;
6363
6364 // scope for the try block itself
6365 scope = new_scope(cctx, BLOCK_SCOPE);
6366 if (scope == NULL)
6367 return NULL;
6368
6369 return arg;
6370}
6371
6372/*
6373 * compile "catch {expr}"
6374 */
6375 static char_u *
6376compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6377{
6378 scope_T *scope = cctx->ctx_scope;
6379 garray_T *instr = &cctx->ctx_instr;
6380 char_u *p;
6381 isn_T *isn;
6382
6383 // end block scope from :try or :catch
6384 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6385 compile_endblock(cctx);
6386 scope = cctx->ctx_scope;
6387
6388 // Error if not in a :try scope
6389 if (scope == NULL || scope->se_type != TRY_SCOPE)
6390 {
6391 emsg(_(e_catch));
6392 return NULL;
6393 }
6394
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006395 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006396 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006397 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 return NULL;
6399 }
6400
6401 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006402 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 JUMP_ALWAYS, cctx) == FAIL)
6404 return NULL;
6405
6406 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006407 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 if (isn->isn_arg.try.try_catch == 0)
6409 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006410 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 {
6412 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006413 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 isn->isn_arg.jump.jump_where = instr->ga_len;
6415 }
6416
6417 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006418 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006420 scope->se_u.se_try.ts_caught_all = TRUE;
6421 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 }
6423 else
6424 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006425 char_u *end;
6426 char_u *pat;
6427 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006428 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006429 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 // Push v:exception, push {expr} and MATCH
6432 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6433
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006434 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006435 if (*end != *p)
6436 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006437 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006438 vim_free(tofree);
6439 return FAIL;
6440 }
6441 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006442 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006443 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006444 len = (int)(end - tofree);
6445 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006446 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006447 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006448 if (pat == NULL)
6449 return FAIL;
6450 if (generate_PUSHS(cctx, pat) == FAIL)
6451 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6454 return NULL;
6455
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006456 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6458 return NULL;
6459 }
6460
6461 if (generate_instr(cctx, ISN_CATCH) == NULL)
6462 return NULL;
6463
6464 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6465 return NULL;
6466 return p;
6467}
6468
6469 static char_u *
6470compile_finally(char_u *arg, cctx_T *cctx)
6471{
6472 scope_T *scope = cctx->ctx_scope;
6473 garray_T *instr = &cctx->ctx_instr;
6474 isn_T *isn;
6475
6476 // end block scope from :try or :catch
6477 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6478 compile_endblock(cctx);
6479 scope = cctx->ctx_scope;
6480
6481 // Error if not in a :try scope
6482 if (scope == NULL || scope->se_type != TRY_SCOPE)
6483 {
6484 emsg(_(e_finally));
6485 return NULL;
6486 }
6487
6488 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006489 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 if (isn->isn_arg.try.try_finally != 0)
6491 {
6492 emsg(_(e_finally_dup));
6493 return NULL;
6494 }
6495
6496 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006497 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498
Bram Moolenaar585fea72020-04-02 22:33:21 +02006499 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006500 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 {
6502 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006503 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006505 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006506 }
6507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508 // TODO: set index in ts_finally_label jumps
6509
6510 return arg;
6511}
6512
6513 static char_u *
6514compile_endtry(char_u *arg, cctx_T *cctx)
6515{
6516 scope_T *scope = cctx->ctx_scope;
6517 garray_T *instr = &cctx->ctx_instr;
6518 isn_T *isn;
6519
6520 // end block scope from :catch or :finally
6521 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6522 compile_endblock(cctx);
6523 scope = cctx->ctx_scope;
6524
6525 // Error if not in a :try scope
6526 if (scope == NULL || scope->se_type != TRY_SCOPE)
6527 {
6528 if (scope == NULL)
6529 emsg(_(e_no_endtry));
6530 else if (scope->se_type == WHILE_SCOPE)
6531 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006532 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 emsg(_(e_endfor));
6534 else
6535 emsg(_(e_endif));
6536 return NULL;
6537 }
6538
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006539 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6541 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006542 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 return NULL;
6544 }
6545
6546 // Fill in the "end" label in jumps at the end of the blocks, if not done
6547 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006548 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549
6550 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006551 if (isn->isn_arg.try.try_catch == 0)
6552 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 if (isn->isn_arg.try.try_finally == 0)
6554 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006555
6556 if (scope->se_u.se_try.ts_catch_label != 0)
6557 {
6558 // Last catch without match jumps here
6559 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6560 isn->isn_arg.jump.jump_where = instr->ga_len;
6561 }
6562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 compile_endblock(cctx);
6564
6565 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6566 return NULL;
6567 return arg;
6568}
6569
6570/*
6571 * compile "throw {expr}"
6572 */
6573 static char_u *
6574compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6575{
6576 char_u *p = skipwhite(arg);
6577
Bram Moolenaara5565e42020-05-09 15:44:01 +02006578 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579 return NULL;
6580 if (may_generate_2STRING(-1, cctx) == FAIL)
6581 return NULL;
6582 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6583 return NULL;
6584
6585 return p;
6586}
6587
6588/*
6589 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006590 * compile "echomsg expr"
6591 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006592 * compile "execute expr"
6593 */
6594 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006595compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006596{
6597 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006598 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006599 int count = 0;
6600
6601 for (;;)
6602 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006603 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006604 return NULL;
6605 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006606 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006607 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006608 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006609 break;
6610 }
6611
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006612 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6613 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6614 else if (cmdidx == CMD_execute)
6615 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6616 else if (cmdidx == CMD_echomsg)
6617 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6618 else
6619 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 return p;
6621}
6622
6623/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006624 * :put r
6625 * :put ={expr}
6626 */
6627 static char_u *
6628compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6629{
6630 char_u *line = arg;
6631 linenr_T lnum;
6632 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006633 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006634
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006635 eap->regname = *line;
6636
6637 if (eap->regname == '=')
6638 {
6639 char_u *p = line + 1;
6640
6641 if (compile_expr0(&p, cctx) == FAIL)
6642 return NULL;
6643 line = p;
6644 }
6645 else if (eap->regname != NUL)
6646 ++line;
6647
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006648 // "errormsg" will not be set because the range is ADDR_LINES.
6649 // TODO: if the range contains something like "$" or "." need to evaluate
6650 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006651 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6652 return NULL;
6653 if (eap->addr_count == 0)
6654 lnum = -1;
6655 else
6656 lnum = eap->line2;
6657 if (above)
6658 --lnum;
6659
6660 generate_PUT(cctx, eap->regname, lnum);
6661 return line;
6662}
6663
6664/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006665 * A command that is not compiled, execute with legacy code.
6666 */
6667 static char_u *
6668compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6669{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006670 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006671 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006672 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006673
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006674 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006675 goto theend;
6676
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006677 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006678 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006679 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006680 int usefilter = FALSE;
6681
6682 has_expr = argt & (EX_XFILE | EX_EXPAND);
6683
6684 // If the command can be followed by a bar, find the bar and truncate
6685 // it, so that the following command can be compiled.
6686 // The '|' is overwritten with a NUL, it is put back below.
6687 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6688 && *eap->arg == '!')
6689 // :w !filter or :r !filter or :r! filter
6690 usefilter = TRUE;
6691 if ((argt & EX_TRLBAR) && !usefilter)
6692 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006693 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006694 separate_nextcmd(eap);
6695 if (eap->nextcmd != NULL)
6696 nextcmd = eap->nextcmd;
6697 }
6698 }
6699
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006700 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6701 {
6702 // expand filename in "syntax include [@group] filename"
6703 has_expr = TRUE;
6704 eap->arg = skipwhite(eap->arg + 7);
6705 if (*eap->arg == '@')
6706 eap->arg = skiptowhite(eap->arg);
6707 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006708
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006709 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006710 {
6711 int count = 0;
6712 char_u *start = skipwhite(line);
6713
6714 // :cmd xxx`=expr1`yyy`=expr2`zzz
6715 // PUSHS ":cmd xxx"
6716 // eval expr1
6717 // PUSHS "yyy"
6718 // eval expr2
6719 // PUSHS "zzz"
6720 // EXECCONCAT 5
6721 for (;;)
6722 {
6723 if (p > start)
6724 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006725 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006726 ++count;
6727 }
6728 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006729 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006730 return NULL;
6731 may_generate_2STRING(-1, cctx);
6732 ++count;
6733 p = skipwhite(p);
6734 if (*p != '`')
6735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006736 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006737 return NULL;
6738 }
6739 start = p + 1;
6740
6741 p = (char_u *)strstr((char *)start, "`=");
6742 if (p == NULL)
6743 {
6744 if (*skipwhite(start) != NUL)
6745 {
6746 generate_PUSHS(cctx, vim_strsave(start));
6747 ++count;
6748 }
6749 break;
6750 }
6751 }
6752 generate_EXECCONCAT(cctx, count);
6753 }
6754 else
6755 generate_EXEC(cctx, line);
6756
6757theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006758 if (*nextcmd != NUL)
6759 {
6760 // the parser expects a pointer to the bar, put it back
6761 --nextcmd;
6762 *nextcmd = '|';
6763 }
6764
6765 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006766}
6767
6768/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006769 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006770 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006771 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006772 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006773add_def_function(ufunc_T *ufunc)
6774{
6775 dfunc_T *dfunc;
6776
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006777 if (def_functions.ga_len == 0)
6778 {
6779 // The first position is not used, so that a zero uf_dfunc_idx means it
6780 // wasn't set.
6781 if (ga_grow(&def_functions, 1) == FAIL)
6782 return FAIL;
6783 ++def_functions.ga_len;
6784 }
6785
Bram Moolenaar09689a02020-05-09 22:50:08 +02006786 // Add the function to "def_functions".
6787 if (ga_grow(&def_functions, 1) == FAIL)
6788 return FAIL;
6789 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6790 CLEAR_POINTER(dfunc);
6791 dfunc->df_idx = def_functions.ga_len;
6792 ufunc->uf_dfunc_idx = dfunc->df_idx;
6793 dfunc->df_ufunc = ufunc;
6794 ++def_functions.ga_len;
6795 return OK;
6796}
6797
6798/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799 * After ex_function() has collected all the function lines: parse and compile
6800 * the lines into instructions.
6801 * Adds the function to "def_functions".
6802 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6803 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006804 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006805 * This can be used recursively through compile_lambda(), which may reallocate
6806 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006807 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006809 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006810compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 char_u *line = NULL;
6813 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 cctx_T cctx;
6816 garray_T *instr;
6817 int called_emsg_before = called_emsg;
6818 int ret = FAIL;
6819 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006820 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006821 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006822 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006823 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006825 // When using a function that was compiled before: Free old instructions.
6826 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006827 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006829 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6830 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006831 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006833 else
6834 {
6835 if (add_def_function(ufunc) == FAIL)
6836 return FAIL;
6837 new_def_function = TRUE;
6838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839
Bram Moolenaar985116a2020-07-12 17:31:09 +02006840 ufunc->uf_def_status = UF_COMPILING;
6841
Bram Moolenaara80faa82020-04-12 19:37:17 +02006842 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 cctx.ctx_ufunc = ufunc;
6844 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006845 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6847 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6848 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6849 cctx.ctx_type_list = &ufunc->uf_type_list;
6850 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6851 instr = &cctx.ctx_instr;
6852
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006853 // Set the context to the function, it may be compiled when called from
6854 // another script. Set the script version to the most modern one.
6855 // The line number will be set in next_line_from_context().
6856 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6858
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006859 // Make sure error messages are OK.
6860 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6861 if (do_estack_push)
6862 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006863 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006864
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006865 if (ufunc->uf_def_args.ga_len > 0)
6866 {
6867 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006868 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006869 int i;
6870 char_u *arg;
6871 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006872 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006873
6874 // Produce instructions for the default values of optional arguments.
6875 // Store the instruction index in uf_def_arg_idx[] so that we know
6876 // where to start when the function is called, depending on the number
6877 // of arguments.
6878 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6879 if (ufunc->uf_def_arg_idx == NULL)
6880 goto erret;
6881 for (i = 0; i < count; ++i)
6882 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006883 garray_T *stack = &cctx.ctx_type_stack;
6884 type_T *val_type;
6885 int arg_idx = first_def_arg + i;
6886
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006887 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6888 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006889 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006890 goto erret;
6891
6892 // If no type specified use the type of the default value.
6893 // Otherwise check that the default value type matches the
6894 // specified type.
6895 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6896 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006897 {
6898 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006899 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006900 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006901 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6902 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006903 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006904
6905 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006906 goto erret;
6907 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006908 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006909
6910 if (did_set_arg_type)
6911 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006912 }
6913
6914 /*
6915 * Loop over all the lines of the function and generate instructions.
6916 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 for (;;)
6918 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006919 exarg_T ea;
6920 cmdmod_T save_cmdmod;
6921 int starts_with_colon = FALSE;
6922 char_u *cmd;
6923 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006924
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006925 // Bail out on the first error to avoid a flood of errors and report
6926 // the right line number when inside try/catch.
6927 if (emsg_before != called_emsg)
6928 goto erret;
6929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 if (line != NULL && *line == '|')
6931 // the line continues after a '|'
6932 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006933 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006934 && !(*line == '#' && (line == cctx.ctx_line_start
6935 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006937 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 goto erret;
6939 }
6940 else
6941 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006942 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006943 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006944 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006947 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948
Bram Moolenaara80faa82020-04-12 19:37:17 +02006949 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 ea.cmdlinep = &line;
6951 ea.cmd = skipwhite(line);
6952
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006953 // Some things can be recognized by the first character.
6954 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006956 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006957 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006958 if (ea.cmd[1] != '{')
6959 {
6960 line = (char_u *)"";
6961 continue;
6962 }
6963 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006965 case '}':
6966 {
6967 // "}" ends a block scope
6968 scopetype_T stype = cctx.ctx_scope == NULL
6969 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006971 if (stype == BLOCK_SCOPE)
6972 {
6973 compile_endblock(&cctx);
6974 line = ea.cmd;
6975 }
6976 else
6977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006978 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006979 goto erret;
6980 }
6981 if (line != NULL)
6982 line = skipwhite(ea.cmd + 1);
6983 continue;
6984 }
6985
6986 case '{':
6987 // "{" starts a block scope
6988 // "{'a': 1}->func() is something else
6989 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6990 {
6991 line = compile_block(ea.cmd, &cctx);
6992 continue;
6993 }
6994 break;
6995
6996 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006997 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006998 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 }
7000
7001 /*
7002 * COMMAND MODIFIERS
7003 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007004 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7006 {
7007 if (errormsg != NULL)
7008 goto erret;
7009 // empty line or comment
7010 line = (char_u *)"";
7011 continue;
7012 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007013 // TODO: use modifiers in the command
7014 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007015 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016
7017 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007018 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007020 {
7021 if (*ea.cmd == '(')
7022 // not for "call()"
7023 ea.cmd = p;
7024 else
7025 ea.cmd = skipwhite(ea.cmd);
7026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007028 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007030 char_u *pskip;
7031
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007032 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007033 // find what follows.
7034 // Skip over "var.member", "var[idx]" and the like.
7035 // Also "&opt = val", "$ENV = val" and "@r = val".
7036 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007037 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007038 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007039 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007041 char_u *var_end;
7042 int oplen;
7043 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044
Bram Moolenaar65821722020-08-02 18:58:54 +02007045 if (ea.cmd[0] == '@')
7046 var_end = ea.cmd + 2;
7047 else
7048 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007049 FNE_CHECK_START | FNE_INCL_BR);
7050 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007051 if (oplen > 0)
7052 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007053 size_t len = p - ea.cmd;
7054
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007055 // Recognize an assignment if we recognize the variable
7056 // name:
7057 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007058 // "local = expr" where "local" is a local var.
7059 // "script = expr" where "script" is a script-local var.
7060 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007061 // "&opt = expr"
7062 // "$ENV = expr"
7063 // "@r = expr"
7064 if (*ea.cmd == '&'
7065 || *ea.cmd == '$'
7066 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007067 || ((len) > 2 && ea.cmd[1] == ':')
7068 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007069 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007070 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007071 || script_var_exists(ea.cmd, len,
7072 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007073 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007074 {
7075 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007076 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007077 goto erret;
7078 continue;
7079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 }
7081 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007082
7083 if (*ea.cmd == '[')
7084 {
7085 // [var, var] = expr
7086 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7087 if (line == NULL)
7088 goto erret;
7089 if (line != ea.cmd)
7090 continue;
7091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 }
7093
7094 /*
7095 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007096 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007098 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007099 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007100 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007101 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007102 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007103 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007104 if (!starts_with_colon)
7105 {
7106 emsg(_(e_colon_required_before_a_range));
7107 goto erret;
7108 }
7109 if (ends_excmd2(line, ea.cmd))
7110 {
7111 // A range without a command: jump to the line.
7112 // TODO: compile to a more efficient command, possibly
7113 // calling parse_cmd_address().
7114 ea.cmdidx = CMD_SIZE;
7115 line = compile_exec(line, &ea, &cctx);
7116 goto nextline;
7117 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007118 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007119 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007120 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007121 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007122 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123
7124 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7125 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007126 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007127 {
7128 line += STRLEN(line);
7129 continue;
7130 }
7131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007133 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007135 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007136 iemsg("Command from find_ex_command() not handled");
7137 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 }
7140
Bram Moolenaar3988f642020-08-27 22:43:03 +02007141 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007142 && ea.cmdidx != CMD_elseif
7143 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007144 && ea.cmdidx != CMD_endif
7145 && ea.cmdidx != CMD_endfor
7146 && ea.cmdidx != CMD_endwhile
7147 && ea.cmdidx != CMD_catch
7148 && ea.cmdidx != CMD_finally
7149 && ea.cmdidx != CMD_endtry)
7150 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007151 emsg(_(e_unreachable_code_after_return));
7152 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007153 }
7154
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007155 p = skipwhite(p);
7156 if (ea.cmdidx != CMD_SIZE
7157 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7158 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007159 if (ea.cmdidx >= 0)
7160 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007161 if ((ea.argt & EX_BANG) && *p == '!')
7162 {
7163 ea.forceit = TRUE;
7164 p = skipwhite(p + 1);
7165 }
7166 }
7167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 switch (ea.cmdidx)
7169 {
7170 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007171 ea.arg = p;
7172 line = compile_nested_function(&ea, &cctx);
7173 break;
7174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007176 // TODO: should we allow this, e.g. to declare a global
7177 // function?
7178 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 goto erret;
7180
7181 case CMD_return:
7182 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007183 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 break;
7185
7186 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007187 if (get_vim_var_nr(VV_DISALLOW_LET))
7188 {
7189 emsg(_(e_cannot_use_let_in_vim9_script));
7190 break;
7191 }
7192 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007193 case CMD_var:
7194 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 case CMD_const:
7196 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007197 if (line == p)
7198 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199 break;
7200
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007201 case CMD_unlet:
7202 case CMD_unlockvar:
7203 case CMD_lockvar:
7204 line = compile_unletlock(p, &ea, &cctx);
7205 break;
7206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 case CMD_import:
7208 line = compile_import(p, &cctx);
7209 break;
7210
7211 case CMD_if:
7212 line = compile_if(p, &cctx);
7213 break;
7214 case CMD_elseif:
7215 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007216 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 break;
7218 case CMD_else:
7219 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007220 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 break;
7222 case CMD_endif:
7223 line = compile_endif(p, &cctx);
7224 break;
7225
7226 case CMD_while:
7227 line = compile_while(p, &cctx);
7228 break;
7229 case CMD_endwhile:
7230 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007231 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 break;
7233
7234 case CMD_for:
7235 line = compile_for(p, &cctx);
7236 break;
7237 case CMD_endfor:
7238 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007239 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240 break;
7241 case CMD_continue:
7242 line = compile_continue(p, &cctx);
7243 break;
7244 case CMD_break:
7245 line = compile_break(p, &cctx);
7246 break;
7247
7248 case CMD_try:
7249 line = compile_try(p, &cctx);
7250 break;
7251 case CMD_catch:
7252 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007253 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254 break;
7255 case CMD_finally:
7256 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007257 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 break;
7259 case CMD_endtry:
7260 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007261 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262 break;
7263 case CMD_throw:
7264 line = compile_throw(p, &cctx);
7265 break;
7266
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007267 case CMD_eval:
7268 if (compile_expr0(&p, &cctx) == FAIL)
7269 goto erret;
7270
Bram Moolenaar3988f642020-08-27 22:43:03 +02007271 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007272 generate_instr_drop(&cctx, ISN_DROP, 1);
7273
7274 line = skipwhite(p);
7275 break;
7276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007279 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007280 case CMD_echomsg:
7281 case CMD_echoerr:
7282 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007283 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007285 case CMD_put:
7286 ea.cmd = cmd;
7287 line = compile_put(p, &ea, &cctx);
7288 break;
7289
Bram Moolenaar3988f642020-08-27 22:43:03 +02007290 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007291
Bram Moolenaarae616492020-07-28 20:07:27 +02007292 case CMD_append:
7293 case CMD_change:
7294 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007295 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007296 case CMD_xit:
7297 not_in_vim9(&ea);
7298 goto erret;
7299
Bram Moolenaar002262f2020-07-08 17:47:57 +02007300 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007301 if (cctx.ctx_skip != SKIP_YES)
7302 {
7303 semsg(_(e_invalid_command_str), ea.cmd);
7304 goto erret;
7305 }
7306 // We don't check for a next command here.
7307 line = (char_u *)"";
7308 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007311 if (cctx.ctx_skip == SKIP_YES)
7312 {
7313 // We don't check for a next command here.
7314 line = (char_u *)"";
7315 }
7316 else
7317 {
7318 // Not recognized, execute with do_cmdline_cmd().
7319 ea.arg = p;
7320 line = compile_exec(line, &ea, &cctx);
7321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 break;
7323 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007324nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 if (line == NULL)
7326 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007327 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328
7329 if (cctx.ctx_type_stack.ga_len < 0)
7330 {
7331 iemsg("Type stack underflow");
7332 goto erret;
7333 }
7334 }
7335
7336 if (cctx.ctx_scope != NULL)
7337 {
7338 if (cctx.ctx_scope->se_type == IF_SCOPE)
7339 emsg(_(e_endif));
7340 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7341 emsg(_(e_endwhile));
7342 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7343 emsg(_(e_endfor));
7344 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007345 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 goto erret;
7347 }
7348
Bram Moolenaarefd88552020-06-18 20:50:10 +02007349 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 {
7351 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7352 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007353 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 goto erret;
7355 }
7356
7357 // Return zero if there is no return at the end.
7358 generate_PUSHNR(&cctx, 0);
7359 generate_instr(&cctx, ISN_RETURN);
7360 }
7361
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007362 {
7363 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7364 + ufunc->uf_dfunc_idx;
7365 dfunc->df_deleted = FALSE;
7366 dfunc->df_instr = instr->ga_data;
7367 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007368 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007369 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007370 if (cctx.ctx_outer_used)
7371 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007372 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374
7375 ret = OK;
7376
7377erret:
7378 if (ret == FAIL)
7379 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007380 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007381 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7382 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007383
7384 for (idx = 0; idx < instr->ga_len; ++idx)
7385 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007387
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007388 // If using the last entry in the table and it was added above, we
7389 // might as well remove it.
7390 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007391 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007392 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007393 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007394 ufunc->uf_dfunc_idx = 0;
7395 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007396 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007397
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007398 while (cctx.ctx_scope != NULL)
7399 drop_scope(&cctx);
7400
Bram Moolenaar20431c92020-03-20 18:39:46 +01007401 // Don't execute this function body.
7402 ga_clear_strings(&ufunc->uf_lines);
7403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 if (errormsg != NULL)
7405 emsg(errormsg);
7406 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007407 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 }
7409
7410 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007411 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007412 if (do_estack_push)
7413 estack_pop();
7414
Bram Moolenaar20431c92020-03-20 18:39:46 +01007415 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007416 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007418 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419}
7420
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007421 void
7422set_function_type(ufunc_T *ufunc)
7423{
7424 int varargs = ufunc->uf_va_name != NULL;
7425 int argcount = ufunc->uf_args.ga_len;
7426
7427 // Create a type for the function, with the return type and any
7428 // argument types.
7429 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7430 // The type is included in "tt_args".
7431 if (argcount > 0 || varargs)
7432 {
7433 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7434 argcount, &ufunc->uf_type_list);
7435 // Add argument types to the function type.
7436 if (func_type_add_arg_types(ufunc->uf_func_type,
7437 argcount + varargs,
7438 &ufunc->uf_type_list) == FAIL)
7439 return;
7440 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7441 ufunc->uf_func_type->tt_min_argcount =
7442 argcount - ufunc->uf_def_args.ga_len;
7443 if (ufunc->uf_arg_types == NULL)
7444 {
7445 int i;
7446
7447 // lambda does not have argument types.
7448 for (i = 0; i < argcount; ++i)
7449 ufunc->uf_func_type->tt_args[i] = &t_any;
7450 }
7451 else
7452 mch_memmove(ufunc->uf_func_type->tt_args,
7453 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7454 if (varargs)
7455 {
7456 ufunc->uf_func_type->tt_args[argcount] =
7457 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7458 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7459 }
7460 }
7461 else
7462 // No arguments, can use a predefined type.
7463 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7464 argcount, &ufunc->uf_type_list);
7465}
7466
7467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468/*
7469 * Delete an instruction, free what it contains.
7470 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007471 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472delete_instr(isn_T *isn)
7473{
7474 switch (isn->isn_type)
7475 {
7476 case ISN_EXEC:
7477 case ISN_LOADENV:
7478 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007479 case ISN_LOADB:
7480 case ISN_LOADW:
7481 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007483 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 case ISN_PUSHEXC:
7485 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007486 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007488 case ISN_STOREB:
7489 case ISN_STOREW:
7490 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007491 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 vim_free(isn->isn_arg.string);
7493 break;
7494
7495 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007496 case ISN_STORES:
7497 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498 break;
7499
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007500 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007501 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007502 vim_free(isn->isn_arg.unlet.ul_name);
7503 break;
7504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 case ISN_STOREOPT:
7506 vim_free(isn->isn_arg.storeopt.so_name);
7507 break;
7508
7509 case ISN_PUSHBLOB: // push blob isn_arg.blob
7510 blob_unref(isn->isn_arg.blob);
7511 break;
7512
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007513 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007515 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007516#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007517 break;
7518
7519 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007520#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007521 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007522#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007523 break;
7524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 case ISN_UCALL:
7526 vim_free(isn->isn_arg.ufunc.cuf_name);
7527 break;
7528
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007529 case ISN_FUNCREF:
7530 {
7531 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7532 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007533
7534 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7535 func_ptr_unref(dfunc->df_ufunc);
7536 }
7537 break;
7538
7539 case ISN_DCALL:
7540 {
7541 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7542 + isn->isn_arg.dfunc.cdf_idx;
7543
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007544 if (dfunc->df_ufunc != NULL
7545 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007546 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007547 }
7548 break;
7549
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007550 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007551 {
7552 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7553 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7554
7555 if (ufunc != NULL)
7556 {
7557 // Clear uf_dfunc_idx so that the function is deleted.
7558 clear_def_function(ufunc);
7559 ufunc->uf_dfunc_idx = 0;
7560 func_ptr_unref(ufunc);
7561 }
7562
7563 vim_free(lambda);
7564 vim_free(isn->isn_arg.newfunc.nf_global);
7565 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007566 break;
7567
Bram Moolenaar5e654232020-09-16 15:22:00 +02007568 case ISN_CHECKTYPE:
7569 free_type(isn->isn_arg.type.ct_type);
7570 break;
7571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 case ISN_2BOOL:
7573 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007574 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575 case ISN_ADDBLOB:
7576 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007577 case ISN_ANYINDEX:
7578 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 case ISN_BCALL:
7580 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007581 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007583 case ISN_COMPAREANY:
7584 case ISN_COMPAREBLOB:
7585 case ISN_COMPAREBOOL:
7586 case ISN_COMPAREDICT:
7587 case ISN_COMPAREFLOAT:
7588 case ISN_COMPAREFUNC:
7589 case ISN_COMPARELIST:
7590 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591 case ISN_COMPARESPECIAL:
7592 case ISN_COMPARESTRING:
7593 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007594 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 case ISN_DROP:
7596 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007597 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007598 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007600 case ISN_EXECCONCAT:
7601 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007603 case ISN_GETITEM:
7604 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007605 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007606 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007608 case ISN_LOADBDICT:
7609 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007610 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007612 case ISN_LOADSCRIPT:
7613 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007614 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007615 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007616 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007617 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618 case ISN_NEGATENR:
7619 case ISN_NEWDICT:
7620 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007622 case ISN_OPFLOAT:
7623 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007625 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007626 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 case ISN_PUSHF:
7628 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007630 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007632 case ISN_SHUFFLE:
7633 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007635 case ISN_STOREDICT:
7636 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007637 case ISN_STORENR:
7638 case ISN_STOREOUTER:
7639 case ISN_STOREREG:
7640 case ISN_STORESCRIPT:
7641 case ISN_STOREV:
7642 case ISN_STRINDEX:
7643 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007644 case ISN_THROW:
7645 case ISN_TRY:
7646 // nothing allocated
7647 break;
7648 }
7649}
7650
7651/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007652 * Free all instructions for "dfunc".
7653 */
7654 static void
7655delete_def_function_contents(dfunc_T *dfunc)
7656{
7657 int idx;
7658
7659 ga_clear(&dfunc->df_def_args_isn);
7660
7661 if (dfunc->df_instr != NULL)
7662 {
7663 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7664 delete_instr(dfunc->df_instr + idx);
7665 VIM_CLEAR(dfunc->df_instr);
7666 }
7667
7668 dfunc->df_deleted = TRUE;
7669}
7670
7671/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007672 * When a user function is deleted, clear the contents of any associated def
7673 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674 */
7675 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007676clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007678 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679 {
7680 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7681 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682
Bram Moolenaar20431c92020-03-20 18:39:46 +01007683 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007684 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685 }
7686}
7687
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007688/*
7689 * Used when a user function is about to be deleted: remove the pointer to it.
7690 * The entry in def_functions is then unused.
7691 */
7692 void
7693unlink_def_function(ufunc_T *ufunc)
7694{
7695 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7696
7697 dfunc->df_ufunc = NULL;
7698}
7699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007701/*
7702 * Free all functions defined with ":def".
7703 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 void
7705free_def_functions(void)
7706{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007707 int idx;
7708
7709 for (idx = 0; idx < def_functions.ga_len; ++idx)
7710 {
7711 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7712
7713 delete_def_function_contents(dfunc);
7714 }
7715
7716 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717}
7718#endif
7719
7720
7721#endif // FEAT_EVAL