blob: edfa32566a7b51080a818816542c32a494c90ae8 [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 Moolenaar8a7d6542020-01-26 15:56:19 +01002695 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002698 ++ufunc->uf_refcount;
2699 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002700 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
2702 // The function will have one line: "return {expr}".
2703 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002704 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002706 clear_evalarg(&evalarg, NULL);
2707
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002708 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002709 {
2710 // The return type will now be known.
2711 set_function_type(ufunc);
2712
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002713 // The function reference count will be 1. When the ISN_FUNCREF
2714 // instruction is deleted the reference count is decremented and the
2715 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002716 return generate_FUNCREF(cctx, ufunc);
2717 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002718
2719 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 return FAIL;
2721}
2722
2723/*
2724 * Compile a lamda call: expr->{lambda}(args)
2725 * "arg" points to the "{".
2726 */
2727 static int
2728compile_lambda_call(char_u **arg, cctx_T *cctx)
2729{
2730 ufunc_T *ufunc;
2731 typval_T rettv;
2732 int argcount = 1;
2733 int ret = FAIL;
2734
2735 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002736 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 return FAIL;
2738
2739 if (**arg != '(')
2740 {
2741 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002742 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 else
2744 semsg(_(e_missing_paren), "lambda");
2745 clear_tv(&rettv);
2746 return FAIL;
2747 }
2748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 ufunc = rettv.vval.v_partial->pt_func;
2750 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002751 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002752 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002753
Bram Moolenaara05e5242020-09-19 18:19:19 +02002754 // The function will have one line: "return {expr}". Compile it into
2755 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002756 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757
2758 // compile the arguments
2759 *arg = skipwhite(*arg + 1);
2760 if (compile_arguments(arg, cctx, &argcount) == OK)
2761 // call the compiled function
2762 ret = generate_CALL(cctx, ufunc, argcount);
2763
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002764 if (ret == FAIL)
2765 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 return ret;
2767}
2768
2769/*
2770 * parse a dict: {'key': val} or #{key: val}
2771 * "*arg" points to the '{'.
2772 */
2773 static int
2774compile_dict(char_u **arg, cctx_T *cctx, int literal)
2775{
2776 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002777 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 int count = 0;
2779 dict_T *d = dict_alloc();
2780 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002781 char_u *whitep = *arg;
2782 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783
2784 if (d == NULL)
2785 return FAIL;
2786 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002787 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 {
2789 char_u *key = NULL;
2790
Bram Moolenaar23c55272020-06-21 16:58:13 +02002791 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002792 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002793 *arg = NULL;
2794 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002795 }
2796
2797 if (**arg == '}')
2798 break;
2799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 if (literal)
2801 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002802 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803
Bram Moolenaar2c330432020-04-13 14:41:35 +02002804 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002806 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 return FAIL;
2808 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002809 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 if (generate_PUSHS(cctx, key) == FAIL)
2811 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002812 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 }
2814 else
2815 {
2816 isn_T *isn;
2817
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2821 if (isn->isn_type == ISN_PUSHS)
2822 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002823 else
2824 {
2825 type_T *keytype = ((type_T **)stack->ga_data)
2826 [stack->ga_len - 1];
2827 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2828 return FAIL;
2829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 }
2831
2832 // Check for duplicate keys, if using string keys.
2833 if (key != NULL)
2834 {
2835 item = dict_find(d, key, -1);
2836 if (item != NULL)
2837 {
2838 semsg(_(e_duplicate_key), key);
2839 goto failret;
2840 }
2841 item = dictitem_alloc(key);
2842 if (item != NULL)
2843 {
2844 item->di_tv.v_type = VAR_UNKNOWN;
2845 item->di_tv.v_lock = 0;
2846 if (dict_add(d, item) == FAIL)
2847 dictitem_free(item);
2848 }
2849 }
2850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 if (**arg != ':')
2852 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002853 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002854 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002855 else
2856 semsg(_(e_missing_dict_colon), *arg);
2857 return FAIL;
2858 }
2859 whitep = *arg + 1;
2860 if (!IS_WHITE_OR_NUL(*whitep))
2861 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002862 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 return FAIL;
2864 }
2865
2866 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002867 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002868 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002869 *arg = NULL;
2870 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002871 }
2872
Bram Moolenaara5565e42020-05-09 15:44:01 +02002873 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 return FAIL;
2875 ++count;
2876
Bram Moolenaar2c330432020-04-13 14:41:35 +02002877 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002878 *arg = skipwhite(*arg);
2879 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002880 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002881 *arg = NULL;
2882 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 if (**arg == '}')
2885 break;
2886 if (**arg != ',')
2887 {
2888 semsg(_(e_missing_dict_comma), *arg);
2889 goto failret;
2890 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002891 if (IS_WHITE_OR_NUL(*whitep))
2892 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002893 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002894 return FAIL;
2895 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002896 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 *arg = skipwhite(*arg + 1);
2898 }
2899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 *arg = *arg + 1;
2901
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002902 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002903 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002904 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002905 *arg += STRLEN(*arg);
2906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 dict_unref(d);
2908 return generate_NEWDICT(cctx, count);
2909
2910failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002911 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002912 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002913 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002914 *arg = (char_u *)"";
2915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 dict_unref(d);
2917 return FAIL;
2918}
2919
2920/*
2921 * Compile "&option".
2922 */
2923 static int
2924compile_get_option(char_u **arg, cctx_T *cctx)
2925{
2926 typval_T rettv;
2927 char_u *start = *arg;
2928 int ret;
2929
2930 // parse the option and get the current value to get the type.
2931 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002932 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 if (ret == OK)
2934 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002935 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 char_u *name = vim_strnsave(start, *arg - start);
2937 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2938
2939 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2940 vim_free(name);
2941 }
2942 clear_tv(&rettv);
2943
2944 return ret;
2945}
2946
2947/*
2948 * Compile "$VAR".
2949 */
2950 static int
2951compile_get_env(char_u **arg, cctx_T *cctx)
2952{
2953 char_u *start = *arg;
2954 int len;
2955 int ret;
2956 char_u *name;
2957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 ++*arg;
2959 len = get_env_len(arg);
2960 if (len == 0)
2961 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002962 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 return FAIL;
2964 }
2965
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002966 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 name = vim_strnsave(start, len + 1);
2968 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2969 vim_free(name);
2970 return ret;
2971}
2972
2973/*
2974 * Compile "@r".
2975 */
2976 static int
2977compile_get_register(char_u **arg, cctx_T *cctx)
2978{
2979 int ret;
2980
2981 ++*arg;
2982 if (**arg == NUL)
2983 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002984 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 return FAIL;
2986 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002987 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 {
2989 emsg_invreg(**arg);
2990 return FAIL;
2991 }
2992 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2993 ++*arg;
2994 return ret;
2995}
2996
2997/*
2998 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002999 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 */
3001 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003002apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003004 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
3006 // this works from end to start
3007 while (p > start)
3008 {
3009 --p;
3010 if (*p == '-' || *p == '+')
3011 {
3012 // only '-' has an effect, for '+' we only check the type
3013#ifdef FEAT_FLOAT
3014 if (rettv->v_type == VAR_FLOAT)
3015 {
3016 if (*p == '-')
3017 rettv->vval.v_float = -rettv->vval.v_float;
3018 }
3019 else
3020#endif
3021 {
3022 varnumber_T val;
3023 int error = FALSE;
3024
3025 // tv_get_number_chk() accepts a string, but we don't want that
3026 // here
3027 if (check_not_string(rettv) == FAIL)
3028 return FAIL;
3029 val = tv_get_number_chk(rettv, &error);
3030 clear_tv(rettv);
3031 if (error)
3032 return FAIL;
3033 if (*p == '-')
3034 val = -val;
3035 rettv->v_type = VAR_NUMBER;
3036 rettv->vval.v_number = val;
3037 }
3038 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003039 else if (numeric_only)
3040 {
3041 ++p;
3042 break;
3043 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003044 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 {
3046 int v = tv2bool(rettv);
3047
3048 // '!' is permissive in the type.
3049 clear_tv(rettv);
3050 rettv->v_type = VAR_BOOL;
3051 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3052 }
3053 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003054 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 return OK;
3056}
3057
3058/*
3059 * Recognize v: variables that are constants and set "rettv".
3060 */
3061 static void
3062get_vim_constant(char_u **arg, typval_T *rettv)
3063{
3064 if (STRNCMP(*arg, "v:true", 6) == 0)
3065 {
3066 rettv->v_type = VAR_BOOL;
3067 rettv->vval.v_number = VVAL_TRUE;
3068 *arg += 6;
3069 }
3070 else if (STRNCMP(*arg, "v:false", 7) == 0)
3071 {
3072 rettv->v_type = VAR_BOOL;
3073 rettv->vval.v_number = VVAL_FALSE;
3074 *arg += 7;
3075 }
3076 else if (STRNCMP(*arg, "v:null", 6) == 0)
3077 {
3078 rettv->v_type = VAR_SPECIAL;
3079 rettv->vval.v_number = VVAL_NULL;
3080 *arg += 6;
3081 }
3082 else if (STRNCMP(*arg, "v:none", 6) == 0)
3083 {
3084 rettv->v_type = VAR_SPECIAL;
3085 rettv->vval.v_number = VVAL_NONE;
3086 *arg += 6;
3087 }
3088}
3089
Bram Moolenaar696ba232020-07-29 21:20:41 +02003090 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003091get_compare_type(char_u *p, int *len, int *type_is)
3092{
3093 exptype_T type = EXPR_UNKNOWN;
3094 int i;
3095
3096 switch (p[0])
3097 {
3098 case '=': if (p[1] == '=')
3099 type = EXPR_EQUAL;
3100 else if (p[1] == '~')
3101 type = EXPR_MATCH;
3102 break;
3103 case '!': if (p[1] == '=')
3104 type = EXPR_NEQUAL;
3105 else if (p[1] == '~')
3106 type = EXPR_NOMATCH;
3107 break;
3108 case '>': if (p[1] != '=')
3109 {
3110 type = EXPR_GREATER;
3111 *len = 1;
3112 }
3113 else
3114 type = EXPR_GEQUAL;
3115 break;
3116 case '<': if (p[1] != '=')
3117 {
3118 type = EXPR_SMALLER;
3119 *len = 1;
3120 }
3121 else
3122 type = EXPR_SEQUAL;
3123 break;
3124 case 'i': if (p[1] == 's')
3125 {
3126 // "is" and "isnot"; but not a prefix of a name
3127 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3128 *len = 5;
3129 i = p[*len];
3130 if (!isalnum(i) && i != '_')
3131 {
3132 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3133 *type_is = TRUE;
3134 }
3135 }
3136 break;
3137 }
3138 return type;
3139}
3140
3141/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003143 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 */
3145 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003146compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003148 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149
3150 // this works from end to start
3151 while (p > start)
3152 {
3153 --p;
3154 if (*p == '-' || *p == '+')
3155 {
3156 int negate = *p == '-';
3157 isn_T *isn;
3158
3159 // TODO: check type
3160 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3161 {
3162 --p;
3163 if (*p == '-')
3164 negate = !negate;
3165 }
3166 // only '-' has an effect, for '+' we only check the type
3167 if (negate)
3168 isn = generate_instr(cctx, ISN_NEGATENR);
3169 else
3170 isn = generate_instr(cctx, ISN_CHECKNR);
3171 if (isn == NULL)
3172 return FAIL;
3173 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003174 else if (numeric_only)
3175 {
3176 ++p;
3177 break;
3178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 else
3180 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003181 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003183 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003185 if (p[-1] == '!')
3186 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 }
3189 if (generate_2BOOL(cctx, invert) == FAIL)
3190 return FAIL;
3191 }
3192 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003193 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 return OK;
3195}
3196
3197/*
3198 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003199 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 */
3201 static int
3202compile_subscript(
3203 char_u **arg,
3204 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003205 char_u *start_leader,
3206 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003207 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003209 char_u *name_start = *end_leader;
3210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 for (;;)
3212 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003213 char_u *p = skipwhite(*arg);
3214
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003215 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003216 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003217 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003218
3219 // If a following line starts with "->{" or "->X" advance to that
3220 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003221 // Also if a following line starts with ".x".
3222 if (next != NULL &&
3223 ((next[0] == '-' && next[1] == '>'
3224 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003225 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003226 {
3227 next = next_line_from_context(cctx, TRUE);
3228 if (next == NULL)
3229 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003230 *arg = next;
3231 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003232 }
3233 }
3234
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003235 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3236 // not a function call.
3237 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003239 garray_T *stack = &cctx->ctx_type_stack;
3240 type_T *type;
3241 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003243 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003244 return FAIL;
3245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003247 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3248
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003249 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3251 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003252 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 return FAIL;
3254 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003255 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003257 char_u *pstart = p;
3258
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003259 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003260 return FAIL;
3261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 // something->method()
3263 // Apply the '!', '-' and '+' first:
3264 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003265 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003268 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003269 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003270 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 if (**arg == '{')
3272 {
3273 // lambda call: list->{lambda}
3274 if (compile_lambda_call(arg, cctx) == FAIL)
3275 return FAIL;
3276 }
3277 else
3278 {
3279 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003280 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003281 if (!eval_isnamec1(*p))
3282 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003283 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003284 return FAIL;
3285 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003286 if (ASCII_ISALPHA(*p) && p[1] == ':')
3287 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003288 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 ;
3290 if (*p != '(')
3291 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003292 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 return FAIL;
3294 }
3295 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003296 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 return FAIL;
3298 }
3299 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003300 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003302 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003303 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003304 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003305 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003306 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003307
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003308 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003309 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003310 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003311 // TODO: blob index
3312 // TODO: more arguments
3313 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003314 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003315 return FAIL;
3316
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003317 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003318 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003319 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003320 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003321 if (**arg == ':')
3322 // missing first index is equal to zero
3323 generate_PUSHNR(cctx, 0);
3324 else
3325 {
3326 if (compile_expr0(arg, cctx) == FAIL)
3327 return FAIL;
3328 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3329 return FAIL;
3330 *arg = skipwhite(*arg);
3331 }
3332 if (**arg == ':')
3333 {
3334 *arg = skipwhite(*arg + 1);
3335 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3336 return FAIL;
3337 if (**arg == ']')
3338 // missing second index is equal to end of string
3339 generate_PUSHNR(cctx, -1);
3340 else
3341 {
3342 if (compile_expr0(arg, cctx) == FAIL)
3343 return FAIL;
3344 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3345 return FAIL;
3346 *arg = skipwhite(*arg);
3347 }
3348 is_slice = TRUE;
3349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350
3351 if (**arg != ']')
3352 {
3353 emsg(_(e_missbrac));
3354 return FAIL;
3355 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003356 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003358 // We can index a list and a dict. If we don't know the type
3359 // we can use the index value type.
3360 // TODO: If we don't know use an instruction to figure it out at
3361 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003362 typep = ((type_T **)stack->ga_data) + stack->ga_len
3363 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003364 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003365 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3366 // If the index is a string, the variable must be a Dict.
3367 if (*typep == &t_any && valtype == &t_string)
3368 vtype = VAR_DICT;
3369 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003370 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003371 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3372 return FAIL;
3373 if (is_slice)
3374 {
3375 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3376 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3377 return FAIL;
3378 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003379 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003380
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003381 if (vtype == VAR_DICT)
3382 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003383 if (is_slice)
3384 {
3385 emsg(_(e_cannot_slice_dictionary));
3386 return FAIL;
3387 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003388 if ((*typep)->tt_type == VAR_DICT)
3389 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003390 else
3391 {
3392 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3393 return FAIL;
3394 *typep = &t_any;
3395 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003396 if (may_generate_2STRING(-1, cctx) == FAIL)
3397 return FAIL;
3398 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3399 return FAIL;
3400 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003401 else if (vtype == VAR_STRING)
3402 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003403 *typep = &t_string;
3404 if ((is_slice
3405 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3406 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003407 return FAIL;
3408 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003409 else if (vtype == VAR_BLOB)
3410 {
3411 emsg("Sorry, blob index and slice not implemented yet");
3412 return FAIL;
3413 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003414 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003415 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003416 if (is_slice)
3417 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003418 if (generate_instr_drop(cctx,
3419 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3420 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003421 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003422 }
Bram Moolenaared591872020-08-15 22:14:53 +02003423 else
3424 {
3425 if ((*typep)->tt_type == VAR_LIST)
3426 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003427 if (generate_instr_drop(cctx,
3428 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3429 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003430 return FAIL;
3431 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003432 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003433 else
3434 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003435 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003436 return FAIL;
3437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003439 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003441 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003442 return FAIL;
3443
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003444 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003445 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003446 {
3447 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003448 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003451 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003452 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 while (eval_isnamec(*p))
3454 MB_PTR_ADV(p);
3455 if (p == *arg)
3456 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003457 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 return FAIL;
3459 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003460 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 return FAIL;
3462 *arg = p;
3463 }
3464 else
3465 break;
3466 }
3467
3468 // TODO - see handle_subscript():
3469 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3470 // Don't do this when "Func" is already a partial that was bound
3471 // explicitly (pt_auto is FALSE).
3472
3473 return OK;
3474}
3475
3476/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003477 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3478 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003480 * If the value is a constant "ppconst->pp_ret" will be set.
3481 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003482 *
3483 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 */
3485
3486/*
3487 * number number constant
3488 * 0zFFFFFFFF Blob constant
3489 * "string" string constant
3490 * 'string' literal string constant
3491 * &option-name option value
3492 * @r register contents
3493 * identifier variable value
3494 * function() function call
3495 * $VAR environment variable
3496 * (expression) nested expression
3497 * [expr, expr] List
3498 * {key: val, key: val} Dictionary
3499 * #{key: val, key: val} Dictionary with literal keys
3500 *
3501 * Also handle:
3502 * ! in front logical NOT
3503 * - in front unary minus
3504 * + in front unary plus (ignored)
3505 * trailing (arg) funcref/partial call
3506 * trailing [] subscript in String or List
3507 * trailing .name entry in Dictionary
3508 * trailing ->name() method call
3509 */
3510 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003511compile_expr7(
3512 char_u **arg,
3513 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003514 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 char_u *start_leader, *end_leader;
3517 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003518 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003519 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520
3521 /*
3522 * Skip '!', '-' and '+' characters. They are handled later.
3523 */
3524 start_leader = *arg;
3525 while (**arg == '!' || **arg == '-' || **arg == '+')
3526 *arg = skipwhite(*arg + 1);
3527 end_leader = *arg;
3528
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003529 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 switch (**arg)
3531 {
3532 /*
3533 * Number constant.
3534 */
3535 case '0': // also for blob starting with 0z
3536 case '1':
3537 case '2':
3538 case '3':
3539 case '4':
3540 case '5':
3541 case '6':
3542 case '7':
3543 case '8':
3544 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003545 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003547 // Apply "-" and "+" just before the number now, right to
3548 // left. Matters especially when "->" follows. Stops at
3549 // '!'.
3550 if (apply_leader(rettv, TRUE,
3551 start_leader, &end_leader) == FAIL)
3552 {
3553 clear_tv(rettv);
3554 return FAIL;
3555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 break;
3557
3558 /*
3559 * String constant: "string".
3560 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003561 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 return FAIL;
3563 break;
3564
3565 /*
3566 * Literal string constant: 'str''ing'.
3567 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003568 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 return FAIL;
3570 break;
3571
3572 /*
3573 * Constant Vim variable.
3574 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003575 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 ret = NOTDONE;
3577 break;
3578
3579 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003580 * "true" constant
3581 */
3582 case 't': if (STRNCMP(*arg, "true", 4) == 0
3583 && !eval_isnamec((*arg)[4]))
3584 {
3585 *arg += 4;
3586 rettv->v_type = VAR_BOOL;
3587 rettv->vval.v_number = VVAL_TRUE;
3588 }
3589 else
3590 ret = NOTDONE;
3591 break;
3592
3593 /*
3594 * "false" constant
3595 */
3596 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3597 && !eval_isnamec((*arg)[5]))
3598 {
3599 *arg += 5;
3600 rettv->v_type = VAR_BOOL;
3601 rettv->vval.v_number = VVAL_FALSE;
3602 }
3603 else
3604 ret = NOTDONE;
3605 break;
3606
3607 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 * List: [expr, expr]
3609 */
3610 case '[': ret = compile_list(arg, cctx);
3611 break;
3612
3613 /*
3614 * Dictionary: #{key: val, key: val}
3615 */
3616 case '#': if ((*arg)[1] == '{')
3617 {
3618 ++*arg;
3619 ret = compile_dict(arg, cctx, TRUE);
3620 }
3621 else
3622 ret = NOTDONE;
3623 break;
3624
3625 /*
3626 * Lambda: {arg, arg -> expr}
3627 * Dictionary: {'key': val, 'key': val}
3628 */
3629 case '{': {
3630 char_u *start = skipwhite(*arg + 1);
3631
3632 // Find out what comes after the arguments.
3633 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003634 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 if (ret != FAIL && *start == '>')
3636 ret = compile_lambda(arg, cctx);
3637 else
3638 ret = compile_dict(arg, cctx, FALSE);
3639 }
3640 break;
3641
3642 /*
3643 * Option value: &name
3644 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003645 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3646 return FAIL;
3647 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 break;
3649
3650 /*
3651 * Environment variable: $VAR.
3652 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003653 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3654 return FAIL;
3655 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 break;
3657
3658 /*
3659 * Register contents: @r.
3660 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003661 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3662 return FAIL;
3663 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 break;
3665 /*
3666 * nested expression: (expression).
3667 */
3668 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003669
3670 // recursive!
3671 if (ppconst->pp_used <= PPSIZE - 10)
3672 {
3673 ret = compile_expr1(arg, cctx, ppconst);
3674 }
3675 else
3676 {
3677 // Not enough space in ppconst, flush constants.
3678 if (generate_ppconst(cctx, ppconst) == FAIL)
3679 return FAIL;
3680 ret = compile_expr0(arg, cctx);
3681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 *arg = skipwhite(*arg);
3683 if (**arg == ')')
3684 ++*arg;
3685 else if (ret == OK)
3686 {
3687 emsg(_(e_missing_close));
3688 ret = FAIL;
3689 }
3690 break;
3691
3692 default: ret = NOTDONE;
3693 break;
3694 }
3695 if (ret == FAIL)
3696 return FAIL;
3697
Bram Moolenaar1c747212020-05-09 18:28:34 +02003698 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003700 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003701 clear_tv(rettv);
3702 else
3703 // A constant expression can possibly be handled compile time,
3704 // return the value instead of generating code.
3705 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
3707 else if (ret == NOTDONE)
3708 {
3709 char_u *p;
3710 int r;
3711
3712 if (!eval_isnamec1(**arg))
3713 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003714 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 return FAIL;
3716 }
3717
3718 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003719 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003721 {
3722 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003725 {
3726 if (generate_ppconst(cctx, ppconst) == FAIL)
3727 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003728 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 if (r == FAIL)
3731 return FAIL;
3732 }
3733
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003734 // Handle following "[]", ".member", etc.
3735 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003736 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003737 ppconst) == FAIL)
3738 return FAIL;
3739 if (ppconst->pp_used > 0)
3740 {
3741 // apply the '!', '-' and '+' before the constant
3742 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003743 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003744 return FAIL;
3745 return OK;
3746 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003747 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003749 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750}
3751
3752/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003753 * Give the "white on both sides" error, taking the operator from "p[len]".
3754 */
3755 void
3756error_white_both(char_u *op, int len)
3757{
3758 char_u buf[10];
3759
3760 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003761 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003762}
3763
3764/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003765 * <type>expr7: runtime type check / conversion
3766 */
3767 static int
3768compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3769{
3770 type_T *want_type = NULL;
3771
3772 // Recognize <type>
3773 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3774 {
3775 int called_emsg_before = called_emsg;
3776
3777 ++*arg;
3778 want_type = parse_type(arg, cctx->ctx_type_list);
3779 if (called_emsg != called_emsg_before)
3780 return FAIL;
3781
3782 if (**arg != '>')
3783 {
3784 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003785 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003786 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003787 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003788 return FAIL;
3789 }
3790 ++*arg;
3791 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3792 return FAIL;
3793 }
3794
3795 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3796 return FAIL;
3797
3798 if (want_type != NULL)
3799 {
3800 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003801 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003802
Bram Moolenaard1103582020-08-14 22:44:25 +02003803 generate_ppconst(cctx, ppconst);
3804 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003805 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003806 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003807 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3808 return FAIL;
3809 }
3810 }
3811
3812 return OK;
3813}
3814
3815/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 * * number multiplication
3817 * / number division
3818 * % number modulo
3819 */
3820 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003821compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822{
3823 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003824 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003825 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003827 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003828 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 return FAIL;
3830
3831 /*
3832 * Repeat computing, until no "*", "/" or "%" is following.
3833 */
3834 for (;;)
3835 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003836 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 if (*op != '*' && *op != '/' && *op != '%')
3838 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003839 if (next != NULL)
3840 {
3841 *arg = next_line_from_context(cctx, TRUE);
3842 op = skipwhite(*arg);
3843 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003844
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003845 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003847 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003848 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 }
3850 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003851 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003852 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003854 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003855 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003857
3858 if (ppconst->pp_used == ppconst_used + 2
3859 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3860 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003861 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003862 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3863 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003864 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003866 // both are numbers: compute the result
3867 switch (*op)
3868 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003869 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003870 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003871 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003872 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003873 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003874 break;
3875 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003876 tv1->vval.v_number = res;
3877 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003878 }
3879 else
3880 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003881 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003882 generate_two_op(cctx, op);
3883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 }
3885
3886 return OK;
3887}
3888
3889/*
3890 * + number addition
3891 * - number subtraction
3892 * .. string concatenation
3893 */
3894 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003895compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896{
3897 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003898 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003900 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901
3902 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003903 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 return FAIL;
3905
3906 /*
3907 * Repeat computing, until no "+", "-" or ".." is following.
3908 */
3909 for (;;)
3910 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003911 op = may_peek_next_line(cctx, *arg, &next);
3912 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 break;
3914 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003915 if (next != NULL)
3916 {
3917 *arg = next_line_from_context(cctx, TRUE);
3918 op = skipwhite(*arg);
3919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003921 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003923 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003924 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 }
3926
3927 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003928 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003929 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003931 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003932 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 return FAIL;
3934
Bram Moolenaara5565e42020-05-09 15:44:01 +02003935 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003936 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003937 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3938 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3939 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3940 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003942 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3943 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003944
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003945 // concat/subtract/add constant numbers
3946 if (*op == '+')
3947 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3948 else if (*op == '-')
3949 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3950 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003951 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003952 // concatenate constant strings
3953 char_u *s1 = tv1->vval.v_string;
3954 char_u *s2 = tv2->vval.v_string;
3955 size_t len1 = STRLEN(s1);
3956
3957 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3958 if (tv1->vval.v_string == NULL)
3959 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003960 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003961 return FAIL;
3962 }
3963 mch_memmove(tv1->vval.v_string, s1, len1);
3964 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003965 vim_free(s1);
3966 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003967 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003968 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 }
3970 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003971 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003972 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003973 if (*op == '.')
3974 {
3975 if (may_generate_2STRING(-2, cctx) == FAIL
3976 || may_generate_2STRING(-1, cctx) == FAIL)
3977 return FAIL;
3978 generate_instr_drop(cctx, ISN_CONCAT, 1);
3979 }
3980 else
3981 generate_two_op(cctx, op);
3982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 }
3984
3985 return OK;
3986}
3987
3988/*
3989 * expr5a == expr5b
3990 * expr5a =~ expr5b
3991 * expr5a != expr5b
3992 * expr5a !~ expr5b
3993 * expr5a > expr5b
3994 * expr5a >= expr5b
3995 * expr5a < expr5b
3996 * expr5a <= expr5b
3997 * expr5a is expr5b
3998 * expr5a isnot expr5b
3999 *
4000 * Produces instructions:
4001 * EVAL expr5a Push result of "expr5a"
4002 * EVAL expr5b Push result of "expr5b"
4003 * COMPARE one of the compare instructions
4004 */
4005 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007{
4008 exptype_T type = EXPR_UNKNOWN;
4009 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004010 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014
4015 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004016 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 return FAIL;
4018
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004019 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004020 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
4022 /*
4023 * If there is a comparative operator, use it.
4024 */
4025 if (type != EXPR_UNKNOWN)
4026 {
4027 int ic = FALSE; // Default: do not ignore case
4028
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004029 if (next != NULL)
4030 {
4031 *arg = next_line_from_context(cctx, TRUE);
4032 p = skipwhite(*arg);
4033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 if (type_is && (p[len] == '?' || p[len] == '#'))
4035 {
4036 semsg(_(e_invexpr2), *arg);
4037 return FAIL;
4038 }
4039 // extra question mark appended: ignore case
4040 if (p[len] == '?')
4041 {
4042 ic = TRUE;
4043 ++len;
4044 }
4045 // extra '#' appended: match case (ignored)
4046 else if (p[len] == '#')
4047 ++len;
4048 // nothing appended: match case
4049
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004050 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004052 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004053 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 }
4055
4056 // get the second variable
4057 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004058 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004059 return FAIL;
4060
Bram Moolenaara5565e42020-05-09 15:44:01 +02004061 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 return FAIL;
4063
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 if (ppconst->pp_used == ppconst_used + 2)
4065 {
4066 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4067 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4068 int ret;
4069
4070 // Both sides are a constant, compute the result now.
4071 // First check for a valid combination of types, this is more
4072 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004073 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004074 ret = FAIL;
4075 else
4076 {
4077 ret = typval_compare(tv1, tv2, type, ic);
4078 tv1->v_type = VAR_BOOL;
4079 tv1->vval.v_number = tv1->vval.v_number
4080 ? VVAL_TRUE : VVAL_FALSE;
4081 clear_tv(tv2);
4082 --ppconst->pp_used;
4083 }
4084 return ret;
4085 }
4086
4087 generate_ppconst(cctx, ppconst);
4088 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 }
4090
4091 return OK;
4092}
4093
Bram Moolenaar7f141552020-05-09 17:35:53 +02004094static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096/*
4097 * Compile || or &&.
4098 */
4099 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100compile_and_or(
4101 char_u **arg,
4102 cctx_T *cctx,
4103 char *op,
4104 ppconst_T *ppconst,
4105 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004107 char_u *next;
4108 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 int opchar = *op;
4110
4111 if (p[0] == opchar && p[1] == opchar)
4112 {
4113 garray_T *instr = &cctx->ctx_instr;
4114 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004115 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004116 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117
4118 /*
4119 * Repeat until there is no following "||" or "&&"
4120 */
4121 ga_init2(&end_ga, sizeof(int), 10);
4122 while (p[0] == opchar && p[1] == opchar)
4123 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004124 if (next != NULL)
4125 {
4126 *arg = next_line_from_context(cctx, TRUE);
4127 p = skipwhite(*arg);
4128 }
4129
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004130 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4131 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004132 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004133 return FAIL;
4134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004136 // TODO: use ppconst if the value is a constant and check
4137 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004138 generate_ppconst(cctx, ppconst);
4139
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004140 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4141 all_bool_values = FALSE;
4142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 if (ga_grow(&end_ga, 1) == FAIL)
4144 {
4145 ga_clear(&end_ga);
4146 return FAIL;
4147 }
4148 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4149 ++end_ga.ga_len;
4150 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004151 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152
4153 // eval the next expression
4154 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004155 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004156 return FAIL;
4157
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4159 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 {
4161 ga_clear(&end_ga);
4162 return FAIL;
4163 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004164
4165 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004167 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168
4169 // Fill in the end label in all jumps.
4170 while (end_ga.ga_len > 0)
4171 {
4172 isn_T *isn;
4173
4174 --end_ga.ga_len;
4175 isn = ((isn_T *)instr->ga_data)
4176 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4177 isn->isn_arg.jump.jump_where = instr->ga_len;
4178 }
4179 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004180
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004181 // The resulting type is converted to bool if needed.
4182 if (!all_bool_values)
4183 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 }
4185
4186 return OK;
4187}
4188
4189/*
4190 * expr4a && expr4a && expr4a logical AND
4191 *
4192 * Produces instructions:
4193 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004194 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004196 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004198 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 * end:
4200 */
4201 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004202compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004204 int ppconst_used = ppconst->pp_used;
4205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004207 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 return FAIL;
4209
4210 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004211 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212}
4213
4214/*
4215 * expr3a || expr3b || expr3c logical OR
4216 *
4217 * Produces instructions:
4218 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004219 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004221 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004223 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 * end:
4225 */
4226 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004229 int ppconst_used = ppconst->pp_used;
4230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 return FAIL;
4234
4235 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004236 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237}
4238
4239/*
4240 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004242 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 * JUMP_IF_FALSE alt jump if false
4244 * EVAL expr1a
4245 * JUMP_ALWAYS end
4246 * alt: EVAL expr1b
4247 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004248 *
4249 * Toplevel expression: expr2 ?? expr1
4250 * Produces instructions:
4251 * EVAL expr2 Push result of "expr2"
4252 * JUMP_AND_KEEP_IF_TRUE end jump if true
4253 * EVAL expr1
4254 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 */
4256 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004257compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258{
4259 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004260 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004261 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262
Bram Moolenaar3988f642020-08-27 22:43:03 +02004263 // Ignore all kinds of errors when not producing code.
4264 if (cctx->ctx_skip == SKIP_YES)
4265 {
4266 skip_expr(arg);
4267 return OK;
4268 }
4269
Bram Moolenaar61a89812020-05-07 16:58:17 +02004270 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004271 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 return FAIL;
4273
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004274 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 if (*p == '?')
4276 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004277 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 garray_T *instr = &cctx->ctx_instr;
4279 garray_T *stack = &cctx->ctx_type_stack;
4280 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004281 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004283 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004284 int has_const_expr = FALSE;
4285 int const_value = FALSE;
4286 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004288 if (next != NULL)
4289 {
4290 *arg = next_line_from_context(cctx, TRUE);
4291 p = skipwhite(*arg);
4292 }
4293
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004294 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004295 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004296 semsg(_(e_white_space_required_before_and_after_str),
4297 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004298 return FAIL;
4299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301 if (ppconst->pp_used == ppconst_used + 1)
4302 {
4303 // the condition is a constant, we know whether the ? or the :
4304 // expression is to be evaluated.
4305 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004306 if (op_falsy)
4307 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4308 else
4309 {
4310 int error = FALSE;
4311
4312 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4313 &error);
4314 if (error)
4315 return FAIL;
4316 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004317 cctx->ctx_skip = save_skip == SKIP_YES ||
4318 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4319
4320 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4321 // "left ?? right" and "left" is truthy: produce "left"
4322 generate_ppconst(cctx, ppconst);
4323 else
4324 {
4325 clear_tv(&ppconst->pp_tv[ppconst_used]);
4326 --ppconst->pp_used;
4327 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004328 }
4329 else
4330 {
4331 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004332 if (op_falsy)
4333 end_idx = instr->ga_len;
4334 generate_JUMP(cctx, op_falsy
4335 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4336 if (op_falsy)
4337 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
4340 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004341 *arg = skipwhite(p + 1 + op_falsy);
4342 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004343 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004345 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 if (!has_const_expr)
4348 {
4349 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004351 if (!op_falsy)
4352 {
4353 // remember the type and drop it
4354 --stack->ga_len;
4355 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004357 end_idx = instr->ga_len;
4358 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004359
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004360 // jump here from JUMP_IF_FALSE
4361 isn = ((isn_T *)instr->ga_data) + alt_idx;
4362 isn->isn_arg.jump.jump_where = instr->ga_len;
4363 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004366 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004368 // Check for the ":".
4369 p = may_peek_next_line(cctx, *arg, &next);
4370 if (*p != ':')
4371 {
4372 emsg(_(e_missing_colon));
4373 return FAIL;
4374 }
4375 if (next != NULL)
4376 {
4377 *arg = next_line_from_context(cctx, TRUE);
4378 p = skipwhite(*arg);
4379 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004380
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004381 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4382 {
4383 semsg(_(e_white_space_required_before_and_after_str), ":");
4384 return FAIL;
4385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004387 // evaluate the third expression
4388 if (has_const_expr)
4389 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004390 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004391 *arg = skipwhite(p + 1);
4392 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4393 return FAIL;
4394 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4395 return FAIL;
4396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 if (!has_const_expr)
4399 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004400 type_T **typep;
4401
Bram Moolenaara5565e42020-05-09 15:44:01 +02004402 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403
Bram Moolenaara5565e42020-05-09 15:44:01 +02004404 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004405 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4406 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004407
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004408 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004409 isn = ((isn_T *)instr->ga_data) + end_idx;
4410 isn->isn_arg.jump.jump_where = instr->ga_len;
4411 }
4412
4413 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 }
4415 return OK;
4416}
4417
4418/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 * Toplevel expression.
4420 */
4421 static int
4422compile_expr0(char_u **arg, cctx_T *cctx)
4423{
4424 ppconst_T ppconst;
4425
4426 CLEAR_FIELD(ppconst);
4427 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4428 {
4429 clear_ppconst(&ppconst);
4430 return FAIL;
4431 }
4432 if (generate_ppconst(cctx, &ppconst) == FAIL)
4433 return FAIL;
4434 return OK;
4435}
4436
4437/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 * compile "return [expr]"
4439 */
4440 static char_u *
4441compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4442{
4443 char_u *p = arg;
4444 garray_T *stack = &cctx->ctx_type_stack;
4445 type_T *stack_type;
4446
4447 if (*p != NUL && *p != '|' && *p != '\n')
4448 {
4449 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 return NULL;
4452
4453 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4454 if (set_return_type)
4455 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004456 else
4457 {
4458 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4459 && stack_type->tt_type != VAR_VOID
4460 && stack_type->tt_type != VAR_UNKNOWN)
4461 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004462 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004463 return NULL;
4464 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004465 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4466 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004467 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 }
4470 else
4471 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004472 // "set_return_type" cannot be TRUE, only used for a lambda which
4473 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004474 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4475 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004477 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 return NULL;
4479 }
4480
4481 // No argument, return zero.
4482 generate_PUSHNR(cctx, 0);
4483 }
4484
4485 if (generate_instr(cctx, ISN_RETURN) == NULL)
4486 return NULL;
4487
4488 // "return val | endif" is possible
4489 return skipwhite(p);
4490}
4491
4492/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004493 * Get a line from the compilation context, compatible with exarg_T getline().
4494 * Return a pointer to the line in allocated memory.
4495 * Return NULL for end-of-file or some error.
4496 */
4497 static char_u *
4498exarg_getline(
4499 int c UNUSED,
4500 void *cookie,
4501 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004502 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004503{
4504 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004505 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004506
Bram Moolenaar66250c92020-08-20 15:02:42 +02004507 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004508 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004509 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004510 return NULL;
4511 ++cctx->ctx_lnum;
4512 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4513 // Comment lines result in NULL pointers, skip them.
4514 if (p != NULL)
4515 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004516 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004517}
4518
4519/*
4520 * Compile a nested :def command.
4521 */
4522 static char_u *
4523compile_nested_function(exarg_T *eap, cctx_T *cctx)
4524{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004525 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004526 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004527 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004528 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004529 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004530 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004531
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004532 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004533 {
4534 emsg(_(e_cannot_use_bang_with_nested_def));
4535 return NULL;
4536 }
4537
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004538 // Only g:Func() can use a namespace.
4539 if (name_start[1] == ':' && !is_global)
4540 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004541 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004542 return NULL;
4543 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004544 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4545 return NULL;
4546
Bram Moolenaar04b12692020-05-04 23:24:44 +02004547 eap->arg = name_end;
4548 eap->getline = exarg_getline;
4549 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004550 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004551 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004552 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004553 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004554
Bram Moolenaar822ba242020-05-24 23:00:18 +02004555 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004556 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004557 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004558 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004559 {
4560 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004561 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004562 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004563
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004564 if (is_global)
4565 {
4566 char_u *func_name = vim_strnsave(name_start + 2,
4567 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004568
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004569 if (func_name == NULL)
4570 r = FAIL;
4571 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004572 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004573 }
4574 else
4575 {
4576 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004577 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004578 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004579 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004580
Bram Moolenaareef21022020-08-01 22:16:43 +02004581 if (lvar == NULL)
4582 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004583 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004584 return NULL;
4585 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004586
4587 // copy over the block scope IDs
4588 if (block_depth > 0)
4589 {
4590 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4591 if (ufunc->uf_block_ids != NULL)
4592 {
4593 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4594 sizeof(int) * block_depth);
4595 ufunc->uf_block_depth = block_depth;
4596 }
4597 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004598 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004599
Bram Moolenaar61a89812020-05-07 16:58:17 +02004600 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004601 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004602}
4603
4604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 * Return the length of an assignment operator, or zero if there isn't one.
4606 */
4607 int
4608assignment_len(char_u *p, int *heredoc)
4609{
4610 if (*p == '=')
4611 {
4612 if (p[1] == '<' && p[2] == '<')
4613 {
4614 *heredoc = TRUE;
4615 return 3;
4616 }
4617 return 1;
4618 }
4619 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4620 return 2;
4621 if (STRNCMP(p, "..=", 3) == 0)
4622 return 3;
4623 return 0;
4624}
4625
4626// words that cannot be used as a variable
4627static char *reserved[] = {
4628 "true",
4629 "false",
4630 NULL
4631};
4632
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004633typedef enum {
4634 dest_local,
4635 dest_option,
4636 dest_env,
4637 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004638 dest_buffer,
4639 dest_window,
4640 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004641 dest_vimvar,
4642 dest_script,
4643 dest_reg,
4644} assign_dest_T;
4645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004647 * Generate the load instruction for "name".
4648 */
4649 static void
4650generate_loadvar(
4651 cctx_T *cctx,
4652 assign_dest_T dest,
4653 char_u *name,
4654 lvar_T *lvar,
4655 type_T *type)
4656{
4657 switch (dest)
4658 {
4659 case dest_option:
4660 // TODO: check the option exists
4661 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4662 break;
4663 case dest_global:
4664 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4665 break;
4666 case dest_buffer:
4667 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4668 break;
4669 case dest_window:
4670 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4671 break;
4672 case dest_tab:
4673 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4674 break;
4675 case dest_script:
4676 compile_load_scriptvar(cctx,
4677 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4678 break;
4679 case dest_env:
4680 // Include $ in the name here
4681 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4682 break;
4683 case dest_reg:
4684 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4685 break;
4686 case dest_vimvar:
4687 generate_LOADV(cctx, name + 2, TRUE);
4688 break;
4689 case dest_local:
4690 if (lvar->lv_from_outer)
4691 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4692 NULL, type);
4693 else
4694 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4695 break;
4696 }
4697}
4698
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004699 void
4700vim9_declare_error(char_u *name)
4701{
4702 char *scope = "";
4703
4704 switch (*name)
4705 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004706 case 'g': scope = _("global"); break;
4707 case 'b': scope = _("buffer"); break;
4708 case 'w': scope = _("window"); break;
4709 case 't': scope = _("tab"); break;
4710 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004711 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004712 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004713 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004714 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004715 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004716 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004717 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004718 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004719 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004720}
4721
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004722/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004723 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004724 * "let name"
4725 * "var name = expr"
4726 * "final name = expr"
4727 * "const name = expr"
4728 * "name = expr"
4729 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004730 * Return NULL for an error.
4731 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 */
4733 static char_u *
4734compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4735{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004736 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004738 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 char_u *ret = NULL;
4740 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004741 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004742 int scriptvar_sid = 0;
4743 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004746 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 int oplen = 0;
4749 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004750 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004751 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004752 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004754 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4755 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004757 // Skip over the "var" or "[var, var]" to get to any "=".
4758 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4759 if (p == NULL)
4760 return *arg == '[' ? arg : NULL;
4761
4762 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004764 // TODO: should we allow this, and figure out type inference from list
4765 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004766 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 return NULL;
4768 }
4769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 sp = p;
4771 p = skipwhite(p);
4772 op = p;
4773 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004774
4775 if (var_count > 0 && oplen == 0)
4776 // can be something like "[1, 2]->func()"
4777 return arg;
4778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4780 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004781 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004782 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004783 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 if (heredoc)
4786 {
4787 list_T *l;
4788 listitem_T *li;
4789
4790 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004791 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004793 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004794 if (l == NULL)
4795 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796
Bram Moolenaar078269b2020-09-21 20:35:55 +02004797 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004799 // Push each line and the create the list.
4800 FOR_ALL_LIST_ITEMS(l, li)
4801 {
4802 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4803 li->li_tv.vval.v_string = NULL;
4804 }
4805 generate_NEWLIST(cctx, l->lv_len);
4806 type = &t_list_string;
4807 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 list_free(l);
4810 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004811 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004813 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 // for "[var, var] = expr" evaluate the expression here, loop over the
4816 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004817
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004818 p = skipwhite(op + oplen);
4819 if (compile_expr0(&p, cctx) == FAIL)
4820 return NULL;
4821 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004823 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004825 type_T *stacktype;
4826
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004827 stacktype = stack->ga_len == 0 ? &t_void
4828 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004831 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004832 goto theend;
4833 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004834 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004835 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004836 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004837 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4838 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004839 }
4840 }
4841
4842 /*
4843 * Loop over variables in "[var, var] = expr".
4844 * For "var = expr" and "let var: type" this is done only once.
4845 */
4846 if (var_count > 0)
4847 var_start = skipwhite(arg + 1); // skip over the "["
4848 else
4849 var_start = arg;
4850 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4851 {
4852 char_u *var_end = skip_var_one(var_start, FALSE);
4853 size_t varlen;
4854 int new_local = FALSE;
4855 int opt_type;
4856 int opt_flags = 0;
4857 assign_dest_T dest = dest_local;
4858 int vimvaridx = -1;
4859 lvar_T *lvar = NULL;
4860 lvar_T arg_lvar;
4861 int has_type = FALSE;
4862 int has_index = FALSE;
4863 int instr_count = -1;
4864
Bram Moolenaar65821722020-08-02 18:58:54 +02004865 if (*var_start == '@')
4866 p = var_start + 2;
4867 else
4868 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004869 // skip over the leading "&", "&l:", "&g:" and "$"
4870 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004871 p = to_name_end(p, TRUE);
4872 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004873
4874 // "a: type" is declaring variable "a" with a type, not "a:".
4875 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4876 --var_end;
4877 if (is_decl && p == var_start + 2 && p[-1] == ':')
4878 --p;
4879
4880 varlen = p - var_start;
4881 vim_free(name);
4882 name = vim_strnsave(var_start, varlen);
4883 if (name == NULL)
4884 return NULL;
4885 if (!heredoc)
4886 type = &t_any;
4887
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004888 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004889 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004890 int declare_error = FALSE;
4891
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004892 if (*var_start == '&')
4893 {
4894 int cc;
4895 long numval;
4896
4897 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004898 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004900 emsg(_(e_const_option));
4901 goto theend;
4902 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004903 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904 p = var_start;
4905 p = find_option_end(&p, &opt_flags);
4906 if (p == NULL)
4907 {
4908 // cannot happen?
4909 emsg(_(e_letunexp));
4910 goto theend;
4911 }
4912 cc = *p;
4913 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004914 opt_type = get_option_value(skip_option_env_lead(var_start),
4915 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004916 *p = cc;
4917 if (opt_type == -3)
4918 {
4919 semsg(_(e_unknown_option), var_start);
4920 goto theend;
4921 }
4922 if (opt_type == -2 || opt_type == 0)
4923 type = &t_string;
4924 else
4925 type = &t_number; // both number and boolean option
4926 }
4927 else if (*var_start == '$')
4928 {
4929 dest = dest_env;
4930 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004931 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 }
4933 else if (*var_start == '@')
4934 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004935 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004936 {
4937 emsg_invreg(var_start[1]);
4938 goto theend;
4939 }
4940 dest = dest_reg;
4941 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004942 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004943 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004944 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004945 {
4946 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004947 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004949 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 {
4951 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004952 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004953 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004954 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004955 {
4956 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004957 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004958 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004959 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004960 {
4961 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004962 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004963 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004964 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004965 {
4966 typval_T *vtv;
4967 int di_flags;
4968
4969 vimvaridx = find_vim_var(name + 2, &di_flags);
4970 if (vimvaridx < 0)
4971 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004972 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004973 goto theend;
4974 }
4975 // We use the current value of "sandbox" here, is that OK?
4976 if (var_check_ro(di_flags, name, FALSE))
4977 goto theend;
4978 dest = dest_vimvar;
4979 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004980 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004981 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 }
4983 else
4984 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004985 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004986
4987 for (idx = 0; reserved[idx] != NULL; ++idx)
4988 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004989 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004990 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004991 goto theend;
4992 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993
4994 lvar = lookup_local(var_start, varlen, cctx);
4995 if (lvar == NULL)
4996 {
4997 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004998 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004999 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5000 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005001 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 if (is_decl)
5003 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005004 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005 goto theend;
5006 }
5007 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005008 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005009 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005010 if (lvar != NULL)
5011 {
5012 if (is_decl)
5013 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005014 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 goto theend;
5016 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005017 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005018 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005019 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005020 int script_namespace = varlen > 1
5021 && STRNCMP(var_start, "s:", 2) == 0;
5022 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005023 ? script_var_exists(var_start + 2, varlen - 2,
5024 FALSE, cctx)
5025 : script_var_exists(var_start, varlen,
5026 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005027 imported_T *import =
5028 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005029
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005030 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005032 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5033
5034 if (is_decl)
5035 {
5036 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005037 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005038 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005039 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005040 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005041 name);
5042 goto theend;
5043 }
5044 else if (cctx->ctx_ufunc->uf_script_ctx_version
5045 == SCRIPT_VERSION_VIM9
5046 && script_namespace
5047 && !script_var && import == NULL)
5048 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005049 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005050 goto theend;
5051 }
5052
5053 dest = dest_script;
5054
5055 // existing script-local variables should have a type
5056 scriptvar_sid = current_sctx.sc_sid;
5057 if (import != NULL)
5058 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005059 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005060 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005061 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005062 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005063 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005064 {
5065 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5066 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005067 ((svar_T *)si->sn_var_vals.ga_data)
5068 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005069 type = sv->sv_type;
5070 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005071 }
5072 }
5073 else if (name[1] == ':' && name[2] != NUL)
5074 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005075 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 goto theend;
5077 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005078 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005079 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005080 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005081 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005082 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005083 else if (check_defined(var_start, varlen, cctx) == FAIL)
5084 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005087
5088 if (declare_error)
5089 {
5090 vim9_declare_error(name);
5091 goto theend;
5092 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093 }
5094
5095 // handle "a:name" as a name, not index "name" on "a"
5096 if (varlen > 1 || var_start[varlen] != ':')
5097 p = var_end;
5098
5099 if (dest != dest_option)
5100 {
5101 if (is_decl && *p == ':')
5102 {
5103 // parse optional type: "let var: type = expr"
5104 if (!VIM_ISWHITE(p[1]))
5105 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005106 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 goto theend;
5108 }
5109 p = skipwhite(p + 1);
5110 type = parse_type(&p, cctx->ctx_type_list);
5111 has_type = TRUE;
5112 }
5113 else if (lvar != NULL)
5114 type = lvar->lv_type;
5115 }
5116
5117 if (oplen == 3 && !heredoc && dest != dest_global
5118 && type->tt_type != VAR_STRING
5119 && type->tt_type != VAR_ANY)
5120 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005121 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 goto theend;
5123 }
5124
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005125 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005126 {
5127 if (oplen > 1 && !heredoc)
5128 {
5129 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005130 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 goto theend;
5132 }
5133
5134 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005135 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5136 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 goto theend;
5138 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005139 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140 if (lvar == NULL)
5141 goto theend;
5142 new_local = TRUE;
5143 }
5144
5145 member_type = type;
5146 if (var_end > var_start + varlen)
5147 {
5148 // Something follows after the variable: "var[idx]".
5149 if (is_decl)
5150 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005151 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005152 goto theend;
5153 }
5154
5155 if (var_start[varlen] == '[')
5156 {
5157 has_index = TRUE;
5158 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005159 member_type = &t_any;
5160 else
5161 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005162 }
5163 else
5164 {
5165 semsg("Not supported yet: %s", var_start);
5166 goto theend;
5167 }
5168 }
5169 else if (lvar == &arg_lvar)
5170 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005171 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005172 goto theend;
5173 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005174 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5175 {
5176 semsg(_(e_cannot_assign_to_constant), name);
5177 goto theend;
5178 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179
5180 if (!heredoc)
5181 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005182 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005184 if (oplen > 0 && var_count == 0)
5185 {
5186 // skip over the "=" and the expression
5187 p = skipwhite(op + oplen);
5188 compile_expr0(&p, cctx);
5189 }
5190 }
5191 else if (oplen > 0)
5192 {
5193 type_T *stacktype;
5194
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 // For "var = expr" evaluate the expression.
5196 if (var_count == 0)
5197 {
5198 int r;
5199
5200 // for "+=", "*=", "..=" etc. first load the current value
5201 if (*op != '=')
5202 {
5203 generate_loadvar(cctx, dest, name, lvar, type);
5204
5205 if (has_index)
5206 {
5207 // TODO: get member from list or dict
5208 emsg("Index with operation not supported yet");
5209 goto theend;
5210 }
5211 }
5212
5213 // Compile the expression. Temporarily hide the new local
5214 // variable here, it is not available to this expression.
5215 if (new_local)
5216 --cctx->ctx_locals.ga_len;
5217 instr_count = instr->ga_len;
5218 p = skipwhite(op + oplen);
5219 r = compile_expr0(&p, cctx);
5220 if (new_local)
5221 ++cctx->ctx_locals.ga_len;
5222 if (r == FAIL)
5223 goto theend;
5224 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005225 else if (semicolon && var_idx == var_count - 1)
5226 {
5227 // For "[var; var] = expr" get the rest of the list
5228 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5229 goto theend;
5230 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005231 else
5232 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 // For "[var, var] = expr" get the "var_idx" item from the
5234 // list.
5235 if (generate_GETITEM(cctx, var_idx) == FAIL)
5236 return FAIL;
5237 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005238
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005239 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005240 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005241 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005243 if ((stacktype->tt_type == VAR_FUNC
5244 || stacktype->tt_type == VAR_PARTIAL)
5245 && var_wrong_func_name(name, TRUE))
5246 goto theend;
5247
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005248 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005249 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005250 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005252 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005253 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 }
5255 else
5256 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005257 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005258 // for a variable that implies &t_any.
5259 if (stacktype == &t_list_empty)
5260 lvar->lv_type = &t_list_any;
5261 else if (stacktype == &t_dict_empty)
5262 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005263 else if (stacktype == &t_unknown)
5264 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005265 else
5266 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005267 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005268 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005269 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005270 {
5271 type_T *use_type = lvar->lv_type;
5272
Bram Moolenaar71abe482020-09-14 22:28:30 +02005273 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005274 if (has_index)
5275 {
5276 use_type = use_type->tt_member;
5277 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005278 // could be indexing "any"
5279 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005280 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005281 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005282 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005283 goto theend;
5284 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005285 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005286 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005287 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005288 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005290 else if (cmdidx == CMD_final)
5291 {
5292 emsg(_(e_final_requires_a_value));
5293 goto theend;
5294 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005297 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 goto theend;
5299 }
5300 else if (!has_type || dest == dest_option)
5301 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005302 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 goto theend;
5304 }
5305 else
5306 {
5307 // variables are always initialized
5308 if (ga_grow(instr, 1) == FAIL)
5309 goto theend;
5310 switch (member_type->tt_type)
5311 {
5312 case VAR_BOOL:
5313 generate_PUSHBOOL(cctx, VVAL_FALSE);
5314 break;
5315 case VAR_FLOAT:
5316#ifdef FEAT_FLOAT
5317 generate_PUSHF(cctx, 0.0);
5318#endif
5319 break;
5320 case VAR_STRING:
5321 generate_PUSHS(cctx, NULL);
5322 break;
5323 case VAR_BLOB:
5324 generate_PUSHBLOB(cctx, NULL);
5325 break;
5326 case VAR_FUNC:
5327 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5328 break;
5329 case VAR_LIST:
5330 generate_NEWLIST(cctx, 0);
5331 break;
5332 case VAR_DICT:
5333 generate_NEWDICT(cctx, 0);
5334 break;
5335 case VAR_JOB:
5336 generate_PUSHJOB(cctx, NULL);
5337 break;
5338 case VAR_CHANNEL:
5339 generate_PUSHCHANNEL(cctx, NULL);
5340 break;
5341 case VAR_NUMBER:
5342 case VAR_UNKNOWN:
5343 case VAR_ANY:
5344 case VAR_PARTIAL:
5345 case VAR_VOID:
5346 case VAR_SPECIAL: // cannot happen
5347 generate_PUSHNR(cctx, 0);
5348 break;
5349 }
5350 }
5351 if (var_count == 0)
5352 end = p;
5353 }
5354
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005355 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005356 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005357 break;
5358
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005359 if (oplen > 0 && *op != '=')
5360 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005361 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005362 type_T *stacktype;
5363
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005364 if (*op == '.')
5365 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005366 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005367 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005369 if (
5370#ifdef FEAT_FLOAT
5371 // If variable is float operation with number is OK.
5372 !(expected == &t_float && stacktype == &t_number) &&
5373#endif
5374 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005375 goto theend;
5376
5377 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005378 {
5379 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5380 goto theend;
5381 }
5382 else if (*op == '+')
5383 {
5384 if (generate_add_instr(cctx,
5385 operator_type(member_type, stacktype),
5386 member_type, stacktype) == FAIL)
5387 goto theend;
5388 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005389 else if (generate_two_op(cctx, op) == FAIL)
5390 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005393 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005394 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 int r;
5396
5397 // Compile the "idx" in "var[idx]".
5398 if (new_local)
5399 --cctx->ctx_locals.ga_len;
5400 p = skipwhite(var_start + varlen + 1);
5401 r = compile_expr0(&p, cctx);
5402 if (new_local)
5403 ++cctx->ctx_locals.ga_len;
5404 if (r == FAIL)
5405 goto theend;
5406 if (*skipwhite(p) != ']')
5407 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005408 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409 emsg(_(e_missbrac));
5410 goto theend;
5411 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005412 if (type == &t_any)
5413 {
5414 type_T *idx_type = ((type_T **)stack->ga_data)[
5415 stack->ga_len - 1];
5416 // Index on variable of unknown type: guess the type from the
5417 // index type: number is dict, otherwise dict.
5418 // TODO: should do the assignment at runtime
5419 if (idx_type->tt_type == VAR_NUMBER)
5420 type = &t_list_any;
5421 else
5422 type = &t_dict_any;
5423 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 if (type->tt_type == VAR_DICT
5425 && may_generate_2STRING(-1, cctx) == FAIL)
5426 goto theend;
5427 if (type->tt_type == VAR_LIST
5428 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005429 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 {
5431 emsg(_(e_number_exp));
5432 goto theend;
5433 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005434
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005435 // Load the dict or list. On the stack we then have:
5436 // - value
5437 // - index
5438 // - variable
5439 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005440
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005441 if (type->tt_type == VAR_LIST)
5442 {
5443 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5444 return FAIL;
5445 }
5446 else if (type->tt_type == VAR_DICT)
5447 {
5448 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5449 return FAIL;
5450 }
5451 else
5452 {
5453 emsg(_(e_listreq));
5454 goto theend;
5455 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005456 }
5457 else
5458 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005459 if (is_decl && cmdidx == CMD_const
5460 && (dest == dest_script || dest == dest_local))
5461 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005462 generate_LOCKCONST(cctx);
5463
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 switch (dest)
5465 {
5466 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005467 generate_STOREOPT(cctx, skip_option_env_lead(name),
5468 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005469 break;
5470 case dest_global:
5471 // include g: with the name, easier to execute that way
5472 generate_STORE(cctx, ISN_STOREG, 0, name);
5473 break;
5474 case dest_buffer:
5475 // include b: with the name, easier to execute that way
5476 generate_STORE(cctx, ISN_STOREB, 0, name);
5477 break;
5478 case dest_window:
5479 // include w: with the name, easier to execute that way
5480 generate_STORE(cctx, ISN_STOREW, 0, name);
5481 break;
5482 case dest_tab:
5483 // include t: with the name, easier to execute that way
5484 generate_STORE(cctx, ISN_STORET, 0, name);
5485 break;
5486 case dest_env:
5487 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5488 break;
5489 case dest_reg:
5490 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5491 break;
5492 case dest_vimvar:
5493 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5494 break;
5495 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005496 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005497 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005498 {
5499 char_u *name_s = name;
5500
5501 // Include s: in the name for store_var()
5502 if (name[1] != ':')
5503 {
5504 int len = (int)STRLEN(name) + 3;
5505
5506 name_s = alloc(len);
5507 if (name_s == NULL)
5508 name_s = name;
5509 else
5510 vim_snprintf((char *)name_s, len,
5511 "s:%s", name);
5512 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005513 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5514 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 if (name_s != name)
5516 vim_free(name_s);
5517 }
5518 else
5519 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005520 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005521 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005522 break;
5523 case dest_local:
5524 if (lvar != NULL)
5525 {
5526 isn_T *isn = ((isn_T *)instr->ga_data)
5527 + instr->ga_len - 1;
5528
5529 // optimization: turn "var = 123" from ISN_PUSHNR +
5530 // ISN_STORE into ISN_STORENR
5531 if (!lvar->lv_from_outer
5532 && instr->ga_len == instr_count + 1
5533 && isn->isn_type == ISN_PUSHNR)
5534 {
5535 varnumber_T val = isn->isn_arg.number;
5536
5537 isn->isn_type = ISN_STORENR;
5538 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5539 isn->isn_arg.storenr.stnr_val = val;
5540 if (stack->ga_len > 0)
5541 --stack->ga_len;
5542 }
5543 else if (lvar->lv_from_outer)
5544 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005545 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005546 else
5547 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5548 }
5549 break;
5550 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005551 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005552
5553 if (var_idx + 1 < var_count)
5554 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005556
5557 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005558 if (var_count > 0 && !semicolon)
5559 {
5560 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5561 goto theend;
5562 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563
Bram Moolenaarb2097502020-07-19 17:17:02 +02005564 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565
5566theend:
5567 vim_free(name);
5568 return ret;
5569}
5570
5571/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005572 * Check if "name" can be "unlet".
5573 */
5574 int
5575check_vim9_unlet(char_u *name)
5576{
5577 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5578 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005579 // "unlet s:var" is allowed in legacy script.
5580 if (*name == 's' && !script_is_vim9())
5581 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005582 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005583 return FAIL;
5584 }
5585 return OK;
5586}
5587
5588/*
5589 * Callback passed to ex_unletlock().
5590 */
5591 static int
5592compile_unlet(
5593 lval_T *lvp,
5594 char_u *name_end,
5595 exarg_T *eap,
5596 int deep UNUSED,
5597 void *coookie)
5598{
5599 cctx_T *cctx = coookie;
5600
5601 if (lvp->ll_tv == NULL)
5602 {
5603 char_u *p = lvp->ll_name;
5604 int cc = *name_end;
5605 int ret = OK;
5606
5607 // Normal name. Only supports g:, w:, t: and b: namespaces.
5608 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005609 if (*p == '$')
5610 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5611 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005612 ret = FAIL;
5613 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005614 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005615
5616 *name_end = cc;
5617 return ret;
5618 }
5619
5620 // TODO: unlet {list}[idx]
5621 // TODO: unlet {dict}[key]
5622 emsg("Sorry, :unlet not fully implemented yet");
5623 return FAIL;
5624}
5625
5626/*
5627 * compile "unlet var", "lock var" and "unlock var"
5628 * "arg" points to "var".
5629 */
5630 static char_u *
5631compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5632{
5633 char_u *p = arg;
5634
5635 if (eap->cmdidx != CMD_unlet)
5636 {
5637 emsg("Sorry, :lock and unlock not implemented yet");
5638 return NULL;
5639 }
5640
5641 if (*p == '!')
5642 {
5643 p = skipwhite(p + 1);
5644 eap->forceit = TRUE;
5645 }
5646
5647 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5648 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5649}
5650
5651/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 * Compile an :import command.
5653 */
5654 static char_u *
5655compile_import(char_u *arg, cctx_T *cctx)
5656{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005657 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658}
5659
5660/*
5661 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5662 */
5663 static int
5664compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5665{
5666 garray_T *instr = &cctx->ctx_instr;
5667 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5668
5669 if (endlabel == NULL)
5670 return FAIL;
5671 endlabel->el_next = *el;
5672 *el = endlabel;
5673 endlabel->el_end_label = instr->ga_len;
5674
5675 generate_JUMP(cctx, when, 0);
5676 return OK;
5677}
5678
5679 static void
5680compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5681{
5682 garray_T *instr = &cctx->ctx_instr;
5683
5684 while (*el != NULL)
5685 {
5686 endlabel_T *cur = (*el);
5687 isn_T *isn;
5688
5689 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5690 isn->isn_arg.jump.jump_where = instr->ga_len;
5691 *el = cur->el_next;
5692 vim_free(cur);
5693 }
5694}
5695
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005696 static void
5697compile_free_jump_to_end(endlabel_T **el)
5698{
5699 while (*el != NULL)
5700 {
5701 endlabel_T *cur = (*el);
5702
5703 *el = cur->el_next;
5704 vim_free(cur);
5705 }
5706}
5707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708/*
5709 * Create a new scope and set up the generic items.
5710 */
5711 static scope_T *
5712new_scope(cctx_T *cctx, scopetype_T type)
5713{
5714 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5715
5716 if (scope == NULL)
5717 return NULL;
5718 scope->se_outer = cctx->ctx_scope;
5719 cctx->ctx_scope = scope;
5720 scope->se_type = type;
5721 scope->se_local_count = cctx->ctx_locals.ga_len;
5722 return scope;
5723}
5724
5725/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005726 * Free the current scope and go back to the outer scope.
5727 */
5728 static void
5729drop_scope(cctx_T *cctx)
5730{
5731 scope_T *scope = cctx->ctx_scope;
5732
5733 if (scope == NULL)
5734 {
5735 iemsg("calling drop_scope() without a scope");
5736 return;
5737 }
5738 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005739 switch (scope->se_type)
5740 {
5741 case IF_SCOPE:
5742 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5743 case FOR_SCOPE:
5744 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5745 case WHILE_SCOPE:
5746 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5747 case TRY_SCOPE:
5748 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5749 case NO_SCOPE:
5750 case BLOCK_SCOPE:
5751 break;
5752 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005753 vim_free(scope);
5754}
5755
5756/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005757 * Check that the top of the type stack has a type that can be used as a
5758 * condition. Give an error and return FAIL if not.
5759 */
5760 static int
5761bool_on_stack(cctx_T *cctx)
5762{
5763 garray_T *stack = &cctx->ctx_type_stack;
5764 type_T *type;
5765
5766 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5767 if (type != &t_bool && type != &t_number && type != &t_any
5768 && need_type(type, &t_bool, -1, cctx, FALSE) == FAIL)
5769 return FAIL;
5770 return OK;
5771}
5772
5773/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005774 * compile "if expr"
5775 *
5776 * "if expr" Produces instructions:
5777 * EVAL expr Push result of "expr"
5778 * JUMP_IF_FALSE end
5779 * ... body ...
5780 * end:
5781 *
5782 * "if expr | else" Produces instructions:
5783 * EVAL expr Push result of "expr"
5784 * JUMP_IF_FALSE else
5785 * ... body ...
5786 * JUMP_ALWAYS end
5787 * else:
5788 * ... body ...
5789 * end:
5790 *
5791 * "if expr1 | elseif expr2 | else" Produces instructions:
5792 * EVAL expr Push result of "expr"
5793 * JUMP_IF_FALSE elseif
5794 * ... body ...
5795 * JUMP_ALWAYS end
5796 * elseif:
5797 * EVAL expr Push result of "expr"
5798 * JUMP_IF_FALSE else
5799 * ... body ...
5800 * JUMP_ALWAYS end
5801 * else:
5802 * ... body ...
5803 * end:
5804 */
5805 static char_u *
5806compile_if(char_u *arg, cctx_T *cctx)
5807{
5808 char_u *p = arg;
5809 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005810 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005811 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005812 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005813 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005814
Bram Moolenaara5565e42020-05-09 15:44:01 +02005815 CLEAR_FIELD(ppconst);
5816 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005817 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005818 clear_ppconst(&ppconst);
5819 return NULL;
5820 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005821 if (cctx->ctx_skip == SKIP_YES)
5822 clear_ppconst(&ppconst);
5823 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005824 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005825 int error = FALSE;
5826 int v;
5827
Bram Moolenaara5565e42020-05-09 15:44:01 +02005828 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005829 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005830 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005831 if (error)
5832 return NULL;
5833 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005834 }
5835 else
5836 {
5837 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005838 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005839 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005840 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005841 if (bool_on_stack(cctx) == FAIL)
5842 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844
5845 scope = new_scope(cctx, IF_SCOPE);
5846 if (scope == NULL)
5847 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005848 scope->se_skip_save = skip_save;
5849 // "is_had_return" will be reset if any block does not end in :return
5850 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005851
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005852 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005853 {
5854 // "where" is set when ":elseif", "else" or ":endif" is found
5855 scope->se_u.se_if.is_if_label = instr->ga_len;
5856 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5857 }
5858 else
5859 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005860
5861 return p;
5862}
5863
5864 static char_u *
5865compile_elseif(char_u *arg, cctx_T *cctx)
5866{
5867 char_u *p = arg;
5868 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005869 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 isn_T *isn;
5871 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005872 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005873 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874
5875 if (scope == NULL || scope->se_type != IF_SCOPE)
5876 {
5877 emsg(_(e_elseif_without_if));
5878 return NULL;
5879 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005880 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005881 if (!cctx->ctx_had_return)
5882 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005884 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005885 {
5886 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005888 return NULL;
5889 // previous "if" or "elseif" jumps here
5890 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5891 isn->isn_arg.jump.jump_where = instr->ga_len;
5892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005894 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005895 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005896 if (cctx->ctx_skip == SKIP_YES)
5897 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005898 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005899 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005900 clear_ppconst(&ppconst);
5901 return NULL;
5902 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005903 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005904 if (scope->se_skip_save == SKIP_YES)
5905 clear_ppconst(&ppconst);
5906 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005907 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005908 int error = FALSE;
5909 int v;
5910
Bram Moolenaar7f141552020-05-09 17:35:53 +02005911 // The expression results in a constant.
5912 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02005913 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5914 if (error)
5915 return NULL;
5916 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005917 clear_ppconst(&ppconst);
5918 scope->se_u.se_if.is_if_label = -1;
5919 }
5920 else
5921 {
5922 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005923 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005924 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005925 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005926 if (bool_on_stack(cctx) == FAIL)
5927 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005928
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005929 // "where" is set when ":elseif", "else" or ":endif" is found
5930 scope->se_u.se_if.is_if_label = instr->ga_len;
5931 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933
5934 return p;
5935}
5936
5937 static char_u *
5938compile_else(char_u *arg, cctx_T *cctx)
5939{
5940 char_u *p = arg;
5941 garray_T *instr = &cctx->ctx_instr;
5942 isn_T *isn;
5943 scope_T *scope = cctx->ctx_scope;
5944
5945 if (scope == NULL || scope->se_type != IF_SCOPE)
5946 {
5947 emsg(_(e_else_without_if));
5948 return NULL;
5949 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005950 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005951 if (!cctx->ctx_had_return)
5952 scope->se_u.se_if.is_had_return = FALSE;
5953 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954
Bram Moolenaarefd88552020-06-18 20:50:10 +02005955 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005956 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005957 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005958 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005959 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005960 if (!cctx->ctx_had_return
5961 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5962 JUMP_ALWAYS, cctx) == FAIL)
5963 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005964 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005965
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005966 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005967 {
5968 if (scope->se_u.se_if.is_if_label >= 0)
5969 {
5970 // previous "if" or "elseif" jumps here
5971 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5972 isn->isn_arg.jump.jump_where = instr->ga_len;
5973 scope->se_u.se_if.is_if_label = -1;
5974 }
5975 }
5976
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005977 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005978 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980
5981 return p;
5982}
5983
5984 static char_u *
5985compile_endif(char_u *arg, cctx_T *cctx)
5986{
5987 scope_T *scope = cctx->ctx_scope;
5988 ifscope_T *ifscope;
5989 garray_T *instr = &cctx->ctx_instr;
5990 isn_T *isn;
5991
5992 if (scope == NULL || scope->se_type != IF_SCOPE)
5993 {
5994 emsg(_(e_endif_without_if));
5995 return NULL;
5996 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005997 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005998 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005999 if (!cctx->ctx_had_return)
6000 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006002 if (scope->se_u.se_if.is_if_label >= 0)
6003 {
6004 // previous "if" or "elseif" jumps here
6005 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6006 isn->isn_arg.jump.jump_where = instr->ga_len;
6007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 // Fill in the "end" label in jumps at the end of the blocks.
6009 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006010 cctx->ctx_skip = scope->se_skip_save;
6011
6012 // If all the blocks end in :return and there is an :else then the
6013 // had_return flag is set.
6014 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006016 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 return arg;
6018}
6019
6020/*
6021 * compile "for var in expr"
6022 *
6023 * Produces instructions:
6024 * PUSHNR -1
6025 * STORE loop-idx Set index to -1
6026 * EVAL expr Push result of "expr"
6027 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6028 * - if beyond end, jump to "end"
6029 * - otherwise get item from list and push it
6030 * STORE var Store item in "var"
6031 * ... body ...
6032 * JUMP top Jump back to repeat
6033 * end: DROP Drop the result of "expr"
6034 *
6035 */
6036 static char_u *
6037compile_for(char_u *arg, cctx_T *cctx)
6038{
6039 char_u *p;
6040 size_t varlen;
6041 garray_T *instr = &cctx->ctx_instr;
6042 garray_T *stack = &cctx->ctx_type_stack;
6043 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006044 lvar_T *loop_lvar; // loop iteration variable
6045 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006046 type_T *vartype;
6047
6048 // TODO: list of variables: "for [key, value] in dict"
6049 // parse "var"
6050 for (p = arg; eval_isnamec1(*p); ++p)
6051 ;
6052 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006053 var_lvar = lookup_local(arg, varlen, cctx);
6054 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006056 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057 return NULL;
6058 }
6059
6060 // consume "in"
6061 p = skipwhite(p);
6062 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6063 {
6064 emsg(_(e_missing_in));
6065 return NULL;
6066 }
6067 p = skipwhite(p + 2);
6068
6069
6070 scope = new_scope(cctx, FOR_SCOPE);
6071 if (scope == NULL)
6072 return NULL;
6073
6074 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006075 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6076 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006077 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006078 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006079 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082
6083 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006084 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6085 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006086 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006087 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006088 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006092 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093
6094 // compile "expr", it remains on the stack until "endfor"
6095 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006096 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006097 {
6098 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006100 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006102 // Now that we know the type of "var", check that it is a list, now or at
6103 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006105 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006107 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 return NULL;
6109 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006110 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006111 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112
6113 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006114 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006116 generate_FOR(cctx, loop_lvar->lv_idx);
6117 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
6119 return arg;
6120}
6121
6122/*
6123 * compile "endfor"
6124 */
6125 static char_u *
6126compile_endfor(char_u *arg, cctx_T *cctx)
6127{
6128 garray_T *instr = &cctx->ctx_instr;
6129 scope_T *scope = cctx->ctx_scope;
6130 forscope_T *forscope;
6131 isn_T *isn;
6132
6133 if (scope == NULL || scope->se_type != FOR_SCOPE)
6134 {
6135 emsg(_(e_for));
6136 return NULL;
6137 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006138 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006140 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141
6142 // At end of ":for" scope jump back to the FOR instruction.
6143 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6144
6145 // Fill in the "end" label in the FOR statement so it can jump here
6146 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6147 isn->isn_arg.forloop.for_end = instr->ga_len;
6148
6149 // Fill in the "end" label any BREAK statements
6150 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6151
6152 // Below the ":for" scope drop the "expr" list from the stack.
6153 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6154 return NULL;
6155
6156 vim_free(scope);
6157
6158 return arg;
6159}
6160
6161/*
6162 * compile "while expr"
6163 *
6164 * Produces instructions:
6165 * top: EVAL expr Push result of "expr"
6166 * JUMP_IF_FALSE end jump if false
6167 * ... body ...
6168 * JUMP top Jump back to repeat
6169 * end:
6170 *
6171 */
6172 static char_u *
6173compile_while(char_u *arg, cctx_T *cctx)
6174{
6175 char_u *p = arg;
6176 garray_T *instr = &cctx->ctx_instr;
6177 scope_T *scope;
6178
6179 scope = new_scope(cctx, WHILE_SCOPE);
6180 if (scope == NULL)
6181 return NULL;
6182
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006183 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184
6185 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006186 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 return NULL;
6188
Bram Moolenaar13106602020-10-04 16:06:05 +02006189 if (bool_on_stack(cctx) == FAIL)
6190 return FAIL;
6191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006193 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 JUMP_IF_FALSE, cctx) == FAIL)
6195 return FAIL;
6196
6197 return p;
6198}
6199
6200/*
6201 * compile "endwhile"
6202 */
6203 static char_u *
6204compile_endwhile(char_u *arg, cctx_T *cctx)
6205{
6206 scope_T *scope = cctx->ctx_scope;
6207
6208 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6209 {
6210 emsg(_(e_while));
6211 return NULL;
6212 }
6213 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006214 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215
6216 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006217 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218
6219 // Fill in the "end" label in the WHILE statement so it can jump here.
6220 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006221 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222
6223 vim_free(scope);
6224
6225 return arg;
6226}
6227
6228/*
6229 * compile "continue"
6230 */
6231 static char_u *
6232compile_continue(char_u *arg, cctx_T *cctx)
6233{
6234 scope_T *scope = cctx->ctx_scope;
6235
6236 for (;;)
6237 {
6238 if (scope == NULL)
6239 {
6240 emsg(_(e_continue));
6241 return NULL;
6242 }
6243 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6244 break;
6245 scope = scope->se_outer;
6246 }
6247
6248 // Jump back to the FOR or WHILE instruction.
6249 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006250 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6251 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 return arg;
6253}
6254
6255/*
6256 * compile "break"
6257 */
6258 static char_u *
6259compile_break(char_u *arg, cctx_T *cctx)
6260{
6261 scope_T *scope = cctx->ctx_scope;
6262 endlabel_T **el;
6263
6264 for (;;)
6265 {
6266 if (scope == NULL)
6267 {
6268 emsg(_(e_break));
6269 return NULL;
6270 }
6271 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6272 break;
6273 scope = scope->se_outer;
6274 }
6275
6276 // Jump to the end of the FOR or WHILE loop.
6277 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006278 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006280 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6282 return FAIL;
6283
6284 return arg;
6285}
6286
6287/*
6288 * compile "{" start of block
6289 */
6290 static char_u *
6291compile_block(char_u *arg, cctx_T *cctx)
6292{
6293 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6294 return NULL;
6295 return skipwhite(arg + 1);
6296}
6297
6298/*
6299 * compile end of block: drop one scope
6300 */
6301 static void
6302compile_endblock(cctx_T *cctx)
6303{
6304 scope_T *scope = cctx->ctx_scope;
6305
6306 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006307 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 vim_free(scope);
6309}
6310
6311/*
6312 * compile "try"
6313 * Creates a new scope for the try-endtry, pointing to the first catch and
6314 * finally.
6315 * Creates another scope for the "try" block itself.
6316 * TRY instruction sets up exception handling at runtime.
6317 *
6318 * "try"
6319 * TRY -> catch1, -> finally push trystack entry
6320 * ... try block
6321 * "throw {exception}"
6322 * EVAL {exception}
6323 * THROW create exception
6324 * ... try block
6325 * " catch {expr}"
6326 * JUMP -> finally
6327 * catch1: PUSH exeception
6328 * EVAL {expr}
6329 * MATCH
6330 * JUMP nomatch -> catch2
6331 * CATCH remove exception
6332 * ... catch block
6333 * " catch"
6334 * JUMP -> finally
6335 * catch2: CATCH remove exception
6336 * ... catch block
6337 * " finally"
6338 * finally:
6339 * ... finally block
6340 * " endtry"
6341 * ENDTRY pop trystack entry, may rethrow
6342 */
6343 static char_u *
6344compile_try(char_u *arg, cctx_T *cctx)
6345{
6346 garray_T *instr = &cctx->ctx_instr;
6347 scope_T *try_scope;
6348 scope_T *scope;
6349
6350 // scope that holds the jumps that go to catch/finally/endtry
6351 try_scope = new_scope(cctx, TRY_SCOPE);
6352 if (try_scope == NULL)
6353 return NULL;
6354
6355 // "catch" is set when the first ":catch" is found.
6356 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006357 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358 if (generate_instr(cctx, ISN_TRY) == NULL)
6359 return NULL;
6360
6361 // scope for the try block itself
6362 scope = new_scope(cctx, BLOCK_SCOPE);
6363 if (scope == NULL)
6364 return NULL;
6365
6366 return arg;
6367}
6368
6369/*
6370 * compile "catch {expr}"
6371 */
6372 static char_u *
6373compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6374{
6375 scope_T *scope = cctx->ctx_scope;
6376 garray_T *instr = &cctx->ctx_instr;
6377 char_u *p;
6378 isn_T *isn;
6379
6380 // end block scope from :try or :catch
6381 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6382 compile_endblock(cctx);
6383 scope = cctx->ctx_scope;
6384
6385 // Error if not in a :try scope
6386 if (scope == NULL || scope->se_type != TRY_SCOPE)
6387 {
6388 emsg(_(e_catch));
6389 return NULL;
6390 }
6391
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006392 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006394 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 return NULL;
6396 }
6397
6398 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006399 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 JUMP_ALWAYS, cctx) == FAIL)
6401 return NULL;
6402
6403 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006404 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405 if (isn->isn_arg.try.try_catch == 0)
6406 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006407 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 {
6409 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006410 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 isn->isn_arg.jump.jump_where = instr->ga_len;
6412 }
6413
6414 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006415 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006417 scope->se_u.se_try.ts_caught_all = TRUE;
6418 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 }
6420 else
6421 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006422 char_u *end;
6423 char_u *pat;
6424 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006425 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006426 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428 // Push v:exception, push {expr} and MATCH
6429 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6430
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006431 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006432 if (*end != *p)
6433 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006434 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006435 vim_free(tofree);
6436 return FAIL;
6437 }
6438 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006439 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006440 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006441 len = (int)(end - tofree);
6442 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006443 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006444 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006445 if (pat == NULL)
6446 return FAIL;
6447 if (generate_PUSHS(cctx, pat) == FAIL)
6448 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6451 return NULL;
6452
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006453 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6455 return NULL;
6456 }
6457
6458 if (generate_instr(cctx, ISN_CATCH) == NULL)
6459 return NULL;
6460
6461 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6462 return NULL;
6463 return p;
6464}
6465
6466 static char_u *
6467compile_finally(char_u *arg, cctx_T *cctx)
6468{
6469 scope_T *scope = cctx->ctx_scope;
6470 garray_T *instr = &cctx->ctx_instr;
6471 isn_T *isn;
6472
6473 // end block scope from :try or :catch
6474 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6475 compile_endblock(cctx);
6476 scope = cctx->ctx_scope;
6477
6478 // Error if not in a :try scope
6479 if (scope == NULL || scope->se_type != TRY_SCOPE)
6480 {
6481 emsg(_(e_finally));
6482 return NULL;
6483 }
6484
6485 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006486 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006487 if (isn->isn_arg.try.try_finally != 0)
6488 {
6489 emsg(_(e_finally_dup));
6490 return NULL;
6491 }
6492
6493 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495
Bram Moolenaar585fea72020-04-02 22:33:21 +02006496 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006497 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 {
6499 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006500 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006502 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503 }
6504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 // TODO: set index in ts_finally_label jumps
6506
6507 return arg;
6508}
6509
6510 static char_u *
6511compile_endtry(char_u *arg, cctx_T *cctx)
6512{
6513 scope_T *scope = cctx->ctx_scope;
6514 garray_T *instr = &cctx->ctx_instr;
6515 isn_T *isn;
6516
6517 // end block scope from :catch or :finally
6518 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6519 compile_endblock(cctx);
6520 scope = cctx->ctx_scope;
6521
6522 // Error if not in a :try scope
6523 if (scope == NULL || scope->se_type != TRY_SCOPE)
6524 {
6525 if (scope == NULL)
6526 emsg(_(e_no_endtry));
6527 else if (scope->se_type == WHILE_SCOPE)
6528 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006529 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 emsg(_(e_endfor));
6531 else
6532 emsg(_(e_endif));
6533 return NULL;
6534 }
6535
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006536 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6538 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006539 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 return NULL;
6541 }
6542
6543 // Fill in the "end" label in jumps at the end of the blocks, if not done
6544 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006545 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546
6547 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006548 if (isn->isn_arg.try.try_catch == 0)
6549 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 if (isn->isn_arg.try.try_finally == 0)
6551 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006552
6553 if (scope->se_u.se_try.ts_catch_label != 0)
6554 {
6555 // Last catch without match jumps here
6556 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6557 isn->isn_arg.jump.jump_where = instr->ga_len;
6558 }
6559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 compile_endblock(cctx);
6561
6562 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6563 return NULL;
6564 return arg;
6565}
6566
6567/*
6568 * compile "throw {expr}"
6569 */
6570 static char_u *
6571compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6572{
6573 char_u *p = skipwhite(arg);
6574
Bram Moolenaara5565e42020-05-09 15:44:01 +02006575 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 return NULL;
6577 if (may_generate_2STRING(-1, cctx) == FAIL)
6578 return NULL;
6579 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6580 return NULL;
6581
6582 return p;
6583}
6584
6585/*
6586 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006587 * compile "echomsg expr"
6588 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006589 * compile "execute expr"
6590 */
6591 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006592compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006593{
6594 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006595 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006596 int count = 0;
6597
6598 for (;;)
6599 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006600 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006601 return NULL;
6602 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006603 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006604 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006605 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006606 break;
6607 }
6608
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006609 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6610 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6611 else if (cmdidx == CMD_execute)
6612 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6613 else if (cmdidx == CMD_echomsg)
6614 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6615 else
6616 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 return p;
6618}
6619
6620/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006621 * :put r
6622 * :put ={expr}
6623 */
6624 static char_u *
6625compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6626{
6627 char_u *line = arg;
6628 linenr_T lnum;
6629 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006630 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006631
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006632 eap->regname = *line;
6633
6634 if (eap->regname == '=')
6635 {
6636 char_u *p = line + 1;
6637
6638 if (compile_expr0(&p, cctx) == FAIL)
6639 return NULL;
6640 line = p;
6641 }
6642 else if (eap->regname != NUL)
6643 ++line;
6644
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006645 // "errormsg" will not be set because the range is ADDR_LINES.
6646 // TODO: if the range contains something like "$" or "." need to evaluate
6647 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006648 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6649 return NULL;
6650 if (eap->addr_count == 0)
6651 lnum = -1;
6652 else
6653 lnum = eap->line2;
6654 if (above)
6655 --lnum;
6656
6657 generate_PUT(cctx, eap->regname, lnum);
6658 return line;
6659}
6660
6661/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006662 * A command that is not compiled, execute with legacy code.
6663 */
6664 static char_u *
6665compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6666{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006667 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006668 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006669 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006670
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006671 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006672 goto theend;
6673
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006674 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006675 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006676 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006677 int usefilter = FALSE;
6678
6679 has_expr = argt & (EX_XFILE | EX_EXPAND);
6680
6681 // If the command can be followed by a bar, find the bar and truncate
6682 // it, so that the following command can be compiled.
6683 // The '|' is overwritten with a NUL, it is put back below.
6684 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6685 && *eap->arg == '!')
6686 // :w !filter or :r !filter or :r! filter
6687 usefilter = TRUE;
6688 if ((argt & EX_TRLBAR) && !usefilter)
6689 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006690 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006691 separate_nextcmd(eap);
6692 if (eap->nextcmd != NULL)
6693 nextcmd = eap->nextcmd;
6694 }
6695 }
6696
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006697 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6698 {
6699 // expand filename in "syntax include [@group] filename"
6700 has_expr = TRUE;
6701 eap->arg = skipwhite(eap->arg + 7);
6702 if (*eap->arg == '@')
6703 eap->arg = skiptowhite(eap->arg);
6704 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006705
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006706 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006707 {
6708 int count = 0;
6709 char_u *start = skipwhite(line);
6710
6711 // :cmd xxx`=expr1`yyy`=expr2`zzz
6712 // PUSHS ":cmd xxx"
6713 // eval expr1
6714 // PUSHS "yyy"
6715 // eval expr2
6716 // PUSHS "zzz"
6717 // EXECCONCAT 5
6718 for (;;)
6719 {
6720 if (p > start)
6721 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006722 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006723 ++count;
6724 }
6725 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006726 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006727 return NULL;
6728 may_generate_2STRING(-1, cctx);
6729 ++count;
6730 p = skipwhite(p);
6731 if (*p != '`')
6732 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006733 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006734 return NULL;
6735 }
6736 start = p + 1;
6737
6738 p = (char_u *)strstr((char *)start, "`=");
6739 if (p == NULL)
6740 {
6741 if (*skipwhite(start) != NUL)
6742 {
6743 generate_PUSHS(cctx, vim_strsave(start));
6744 ++count;
6745 }
6746 break;
6747 }
6748 }
6749 generate_EXECCONCAT(cctx, count);
6750 }
6751 else
6752 generate_EXEC(cctx, line);
6753
6754theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006755 if (*nextcmd != NUL)
6756 {
6757 // the parser expects a pointer to the bar, put it back
6758 --nextcmd;
6759 *nextcmd = '|';
6760 }
6761
6762 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006763}
6764
6765/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006766 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006767 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006768 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006769 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006770add_def_function(ufunc_T *ufunc)
6771{
6772 dfunc_T *dfunc;
6773
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006774 if (def_functions.ga_len == 0)
6775 {
6776 // The first position is not used, so that a zero uf_dfunc_idx means it
6777 // wasn't set.
6778 if (ga_grow(&def_functions, 1) == FAIL)
6779 return FAIL;
6780 ++def_functions.ga_len;
6781 }
6782
Bram Moolenaar09689a02020-05-09 22:50:08 +02006783 // Add the function to "def_functions".
6784 if (ga_grow(&def_functions, 1) == FAIL)
6785 return FAIL;
6786 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6787 CLEAR_POINTER(dfunc);
6788 dfunc->df_idx = def_functions.ga_len;
6789 ufunc->uf_dfunc_idx = dfunc->df_idx;
6790 dfunc->df_ufunc = ufunc;
6791 ++def_functions.ga_len;
6792 return OK;
6793}
6794
6795/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 * After ex_function() has collected all the function lines: parse and compile
6797 * the lines into instructions.
6798 * Adds the function to "def_functions".
6799 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6800 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006801 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006802 * This can be used recursively through compile_lambda(), which may reallocate
6803 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006804 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006806 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006807compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 char_u *line = NULL;
6810 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 cctx_T cctx;
6813 garray_T *instr;
6814 int called_emsg_before = called_emsg;
6815 int ret = FAIL;
6816 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006817 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006818 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006819 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006820 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006822 // When using a function that was compiled before: Free old instructions.
6823 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006824 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006826 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6827 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006828 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006830 else
6831 {
6832 if (add_def_function(ufunc) == FAIL)
6833 return FAIL;
6834 new_def_function = TRUE;
6835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836
Bram Moolenaar985116a2020-07-12 17:31:09 +02006837 ufunc->uf_def_status = UF_COMPILING;
6838
Bram Moolenaara80faa82020-04-12 19:37:17 +02006839 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 cctx.ctx_ufunc = ufunc;
6841 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006842 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6844 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6845 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6846 cctx.ctx_type_list = &ufunc->uf_type_list;
6847 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6848 instr = &cctx.ctx_instr;
6849
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006850 // Set the context to the function, it may be compiled when called from
6851 // another script. Set the script version to the most modern one.
6852 // The line number will be set in next_line_from_context().
6853 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6855
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006856 // Make sure error messages are OK.
6857 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6858 if (do_estack_push)
6859 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006860 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006861
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006862 if (ufunc->uf_def_args.ga_len > 0)
6863 {
6864 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006865 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006866 int i;
6867 char_u *arg;
6868 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006869 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006870
6871 // Produce instructions for the default values of optional arguments.
6872 // Store the instruction index in uf_def_arg_idx[] so that we know
6873 // where to start when the function is called, depending on the number
6874 // of arguments.
6875 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6876 if (ufunc->uf_def_arg_idx == NULL)
6877 goto erret;
6878 for (i = 0; i < count; ++i)
6879 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006880 garray_T *stack = &cctx.ctx_type_stack;
6881 type_T *val_type;
6882 int arg_idx = first_def_arg + i;
6883
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006884 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6885 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006886 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006887 goto erret;
6888
6889 // If no type specified use the type of the default value.
6890 // Otherwise check that the default value type matches the
6891 // specified type.
6892 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6893 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006894 {
6895 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006896 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006897 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006898 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6899 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006900 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006901
6902 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006903 goto erret;
6904 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006905 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006906
6907 if (did_set_arg_type)
6908 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006909 }
6910
6911 /*
6912 * Loop over all the lines of the function and generate instructions.
6913 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 for (;;)
6915 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006916 exarg_T ea;
6917 cmdmod_T save_cmdmod;
6918 int starts_with_colon = FALSE;
6919 char_u *cmd;
6920 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006921
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006922 // Bail out on the first error to avoid a flood of errors and report
6923 // the right line number when inside try/catch.
6924 if (emsg_before != called_emsg)
6925 goto erret;
6926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 if (line != NULL && *line == '|')
6928 // the line continues after a '|'
6929 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006930 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006931 && !(*line == '#' && (line == cctx.ctx_line_start
6932 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006934 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 goto erret;
6936 }
6937 else
6938 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006939 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006940 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006941 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006944 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945
Bram Moolenaara80faa82020-04-12 19:37:17 +02006946 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006947 ea.cmdlinep = &line;
6948 ea.cmd = skipwhite(line);
6949
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006950 // Some things can be recognized by the first character.
6951 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006953 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006954 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006955 if (ea.cmd[1] != '{')
6956 {
6957 line = (char_u *)"";
6958 continue;
6959 }
6960 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006961
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006962 case '}':
6963 {
6964 // "}" ends a block scope
6965 scopetype_T stype = cctx.ctx_scope == NULL
6966 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006968 if (stype == BLOCK_SCOPE)
6969 {
6970 compile_endblock(&cctx);
6971 line = ea.cmd;
6972 }
6973 else
6974 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006975 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006976 goto erret;
6977 }
6978 if (line != NULL)
6979 line = skipwhite(ea.cmd + 1);
6980 continue;
6981 }
6982
6983 case '{':
6984 // "{" starts a block scope
6985 // "{'a': 1}->func() is something else
6986 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6987 {
6988 line = compile_block(ea.cmd, &cctx);
6989 continue;
6990 }
6991 break;
6992
6993 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006994 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006995 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 }
6997
6998 /*
6999 * COMMAND MODIFIERS
7000 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007001 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7003 {
7004 if (errormsg != NULL)
7005 goto erret;
7006 // empty line or comment
7007 line = (char_u *)"";
7008 continue;
7009 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007010 // TODO: use modifiers in the command
7011 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007012 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013
7014 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007015 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007017 {
7018 if (*ea.cmd == '(')
7019 // not for "call()"
7020 ea.cmd = p;
7021 else
7022 ea.cmd = skipwhite(ea.cmd);
7023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007025 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007027 char_u *pskip;
7028
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007029 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007030 // find what follows.
7031 // Skip over "var.member", "var[idx]" and the like.
7032 // Also "&opt = val", "$ENV = val" and "@r = val".
7033 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007034 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007035 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007036 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007038 char_u *var_end;
7039 int oplen;
7040 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041
Bram Moolenaar65821722020-08-02 18:58:54 +02007042 if (ea.cmd[0] == '@')
7043 var_end = ea.cmd + 2;
7044 else
7045 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007046 FNE_CHECK_START | FNE_INCL_BR);
7047 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007048 if (oplen > 0)
7049 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007050 size_t len = p - ea.cmd;
7051
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007052 // Recognize an assignment if we recognize the variable
7053 // name:
7054 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007055 // "local = expr" where "local" is a local var.
7056 // "script = expr" where "script" is a script-local var.
7057 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007058 // "&opt = expr"
7059 // "$ENV = expr"
7060 // "@r = expr"
7061 if (*ea.cmd == '&'
7062 || *ea.cmd == '$'
7063 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007064 || ((len) > 2 && ea.cmd[1] == ':')
7065 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007066 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007067 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007068 || script_var_exists(ea.cmd, len,
7069 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007070 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007071 {
7072 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007073 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007074 goto erret;
7075 continue;
7076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 }
7078 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007079
7080 if (*ea.cmd == '[')
7081 {
7082 // [var, var] = expr
7083 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7084 if (line == NULL)
7085 goto erret;
7086 if (line != ea.cmd)
7087 continue;
7088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 }
7090
7091 /*
7092 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007093 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007095 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007096 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007097 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007098 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007099 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007100 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007101 if (!starts_with_colon)
7102 {
7103 emsg(_(e_colon_required_before_a_range));
7104 goto erret;
7105 }
7106 if (ends_excmd2(line, ea.cmd))
7107 {
7108 // A range without a command: jump to the line.
7109 // TODO: compile to a more efficient command, possibly
7110 // calling parse_cmd_address().
7111 ea.cmdidx = CMD_SIZE;
7112 line = compile_exec(line, &ea, &cctx);
7113 goto nextline;
7114 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007115 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007116 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007117 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007118 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007119 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120
7121 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7122 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007123 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007124 {
7125 line += STRLEN(line);
7126 continue;
7127 }
7128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007130 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007132 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007133 iemsg("Command from find_ex_command() not handled");
7134 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 }
7137
Bram Moolenaar3988f642020-08-27 22:43:03 +02007138 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007139 && ea.cmdidx != CMD_elseif
7140 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007141 && ea.cmdidx != CMD_endif
7142 && ea.cmdidx != CMD_endfor
7143 && ea.cmdidx != CMD_endwhile
7144 && ea.cmdidx != CMD_catch
7145 && ea.cmdidx != CMD_finally
7146 && ea.cmdidx != CMD_endtry)
7147 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007148 emsg(_(e_unreachable_code_after_return));
7149 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007150 }
7151
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007152 p = skipwhite(p);
7153 if (ea.cmdidx != CMD_SIZE
7154 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7155 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007156 if (ea.cmdidx >= 0)
7157 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007158 if ((ea.argt & EX_BANG) && *p == '!')
7159 {
7160 ea.forceit = TRUE;
7161 p = skipwhite(p + 1);
7162 }
7163 }
7164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 switch (ea.cmdidx)
7166 {
7167 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007168 ea.arg = p;
7169 line = compile_nested_function(&ea, &cctx);
7170 break;
7171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007173 // TODO: should we allow this, e.g. to declare a global
7174 // function?
7175 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 goto erret;
7177
7178 case CMD_return:
7179 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007180 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 break;
7182
7183 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007184 if (get_vim_var_nr(VV_DISALLOW_LET))
7185 {
7186 emsg(_(e_cannot_use_let_in_vim9_script));
7187 break;
7188 }
7189 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007190 case CMD_var:
7191 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 case CMD_const:
7193 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007194 if (line == p)
7195 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 break;
7197
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007198 case CMD_unlet:
7199 case CMD_unlockvar:
7200 case CMD_lockvar:
7201 line = compile_unletlock(p, &ea, &cctx);
7202 break;
7203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204 case CMD_import:
7205 line = compile_import(p, &cctx);
7206 break;
7207
7208 case CMD_if:
7209 line = compile_if(p, &cctx);
7210 break;
7211 case CMD_elseif:
7212 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007213 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 break;
7215 case CMD_else:
7216 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007217 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218 break;
7219 case CMD_endif:
7220 line = compile_endif(p, &cctx);
7221 break;
7222
7223 case CMD_while:
7224 line = compile_while(p, &cctx);
7225 break;
7226 case CMD_endwhile:
7227 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007228 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 break;
7230
7231 case CMD_for:
7232 line = compile_for(p, &cctx);
7233 break;
7234 case CMD_endfor:
7235 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007236 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 break;
7238 case CMD_continue:
7239 line = compile_continue(p, &cctx);
7240 break;
7241 case CMD_break:
7242 line = compile_break(p, &cctx);
7243 break;
7244
7245 case CMD_try:
7246 line = compile_try(p, &cctx);
7247 break;
7248 case CMD_catch:
7249 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007250 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 break;
7252 case CMD_finally:
7253 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007254 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 break;
7256 case CMD_endtry:
7257 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007258 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007259 break;
7260 case CMD_throw:
7261 line = compile_throw(p, &cctx);
7262 break;
7263
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007264 case CMD_eval:
7265 if (compile_expr0(&p, &cctx) == FAIL)
7266 goto erret;
7267
Bram Moolenaar3988f642020-08-27 22:43:03 +02007268 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007269 generate_instr_drop(&cctx, ISN_DROP, 1);
7270
7271 line = skipwhite(p);
7272 break;
7273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007276 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007277 case CMD_echomsg:
7278 case CMD_echoerr:
7279 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007280 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007282 case CMD_put:
7283 ea.cmd = cmd;
7284 line = compile_put(p, &ea, &cctx);
7285 break;
7286
Bram Moolenaar3988f642020-08-27 22:43:03 +02007287 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007288
Bram Moolenaarae616492020-07-28 20:07:27 +02007289 case CMD_append:
7290 case CMD_change:
7291 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007292 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007293 case CMD_xit:
7294 not_in_vim9(&ea);
7295 goto erret;
7296
Bram Moolenaar002262f2020-07-08 17:47:57 +02007297 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007298 if (cctx.ctx_skip != SKIP_YES)
7299 {
7300 semsg(_(e_invalid_command_str), ea.cmd);
7301 goto erret;
7302 }
7303 // We don't check for a next command here.
7304 line = (char_u *)"";
7305 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007308 if (cctx.ctx_skip == SKIP_YES)
7309 {
7310 // We don't check for a next command here.
7311 line = (char_u *)"";
7312 }
7313 else
7314 {
7315 // Not recognized, execute with do_cmdline_cmd().
7316 ea.arg = p;
7317 line = compile_exec(line, &ea, &cctx);
7318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 break;
7320 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007321nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 if (line == NULL)
7323 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007324 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325
7326 if (cctx.ctx_type_stack.ga_len < 0)
7327 {
7328 iemsg("Type stack underflow");
7329 goto erret;
7330 }
7331 }
7332
7333 if (cctx.ctx_scope != NULL)
7334 {
7335 if (cctx.ctx_scope->se_type == IF_SCOPE)
7336 emsg(_(e_endif));
7337 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7338 emsg(_(e_endwhile));
7339 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7340 emsg(_(e_endfor));
7341 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007342 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 goto erret;
7344 }
7345
Bram Moolenaarefd88552020-06-18 20:50:10 +02007346 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 {
7348 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7349 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007350 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 goto erret;
7352 }
7353
7354 // Return zero if there is no return at the end.
7355 generate_PUSHNR(&cctx, 0);
7356 generate_instr(&cctx, ISN_RETURN);
7357 }
7358
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007359 {
7360 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7361 + ufunc->uf_dfunc_idx;
7362 dfunc->df_deleted = FALSE;
7363 dfunc->df_instr = instr->ga_data;
7364 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007365 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007366 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007367 if (cctx.ctx_outer_used)
7368 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007369 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371
7372 ret = OK;
7373
7374erret:
7375 if (ret == FAIL)
7376 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007377 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007378 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7379 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007380
7381 for (idx = 0; idx < instr->ga_len; ++idx)
7382 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007384
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007385 // If using the last entry in the table and it was added above, we
7386 // might as well remove it.
7387 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007388 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007389 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007390 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007391 ufunc->uf_dfunc_idx = 0;
7392 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007393 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007394
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007395 while (cctx.ctx_scope != NULL)
7396 drop_scope(&cctx);
7397
Bram Moolenaar20431c92020-03-20 18:39:46 +01007398 // Don't execute this function body.
7399 ga_clear_strings(&ufunc->uf_lines);
7400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 if (errormsg != NULL)
7402 emsg(errormsg);
7403 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007404 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 }
7406
7407 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007408 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007409 if (do_estack_push)
7410 estack_pop();
7411
Bram Moolenaar20431c92020-03-20 18:39:46 +01007412 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007413 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007415 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416}
7417
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007418 void
7419set_function_type(ufunc_T *ufunc)
7420{
7421 int varargs = ufunc->uf_va_name != NULL;
7422 int argcount = ufunc->uf_args.ga_len;
7423
7424 // Create a type for the function, with the return type and any
7425 // argument types.
7426 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7427 // The type is included in "tt_args".
7428 if (argcount > 0 || varargs)
7429 {
7430 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7431 argcount, &ufunc->uf_type_list);
7432 // Add argument types to the function type.
7433 if (func_type_add_arg_types(ufunc->uf_func_type,
7434 argcount + varargs,
7435 &ufunc->uf_type_list) == FAIL)
7436 return;
7437 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7438 ufunc->uf_func_type->tt_min_argcount =
7439 argcount - ufunc->uf_def_args.ga_len;
7440 if (ufunc->uf_arg_types == NULL)
7441 {
7442 int i;
7443
7444 // lambda does not have argument types.
7445 for (i = 0; i < argcount; ++i)
7446 ufunc->uf_func_type->tt_args[i] = &t_any;
7447 }
7448 else
7449 mch_memmove(ufunc->uf_func_type->tt_args,
7450 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7451 if (varargs)
7452 {
7453 ufunc->uf_func_type->tt_args[argcount] =
7454 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7455 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7456 }
7457 }
7458 else
7459 // No arguments, can use a predefined type.
7460 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7461 argcount, &ufunc->uf_type_list);
7462}
7463
7464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465/*
7466 * Delete an instruction, free what it contains.
7467 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007468 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469delete_instr(isn_T *isn)
7470{
7471 switch (isn->isn_type)
7472 {
7473 case ISN_EXEC:
7474 case ISN_LOADENV:
7475 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007476 case ISN_LOADB:
7477 case ISN_LOADW:
7478 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007480 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 case ISN_PUSHEXC:
7482 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007483 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007485 case ISN_STOREB:
7486 case ISN_STOREW:
7487 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007488 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 vim_free(isn->isn_arg.string);
7490 break;
7491
7492 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007493 case ISN_STORES:
7494 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007495 break;
7496
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007497 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007498 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007499 vim_free(isn->isn_arg.unlet.ul_name);
7500 break;
7501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 case ISN_STOREOPT:
7503 vim_free(isn->isn_arg.storeopt.so_name);
7504 break;
7505
7506 case ISN_PUSHBLOB: // push blob isn_arg.blob
7507 blob_unref(isn->isn_arg.blob);
7508 break;
7509
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007510 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007512 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007513#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007514 break;
7515
7516 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007517#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007518 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007519#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007520 break;
7521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 case ISN_UCALL:
7523 vim_free(isn->isn_arg.ufunc.cuf_name);
7524 break;
7525
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007526 case ISN_FUNCREF:
7527 {
7528 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7529 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007530
7531 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7532 func_ptr_unref(dfunc->df_ufunc);
7533 }
7534 break;
7535
7536 case ISN_DCALL:
7537 {
7538 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7539 + isn->isn_arg.dfunc.cdf_idx;
7540
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007541 if (dfunc->df_ufunc != NULL
7542 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007543 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007544 }
7545 break;
7546
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007547 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007548 {
7549 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7550 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7551
7552 if (ufunc != NULL)
7553 {
7554 // Clear uf_dfunc_idx so that the function is deleted.
7555 clear_def_function(ufunc);
7556 ufunc->uf_dfunc_idx = 0;
7557 func_ptr_unref(ufunc);
7558 }
7559
7560 vim_free(lambda);
7561 vim_free(isn->isn_arg.newfunc.nf_global);
7562 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007563 break;
7564
Bram Moolenaar5e654232020-09-16 15:22:00 +02007565 case ISN_CHECKTYPE:
7566 free_type(isn->isn_arg.type.ct_type);
7567 break;
7568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569 case ISN_2BOOL:
7570 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007571 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 case ISN_ADDBLOB:
7573 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007574 case ISN_ANYINDEX:
7575 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 case ISN_BCALL:
7577 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007578 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 case ISN_COMPAREANY:
7581 case ISN_COMPAREBLOB:
7582 case ISN_COMPAREBOOL:
7583 case ISN_COMPAREDICT:
7584 case ISN_COMPAREFLOAT:
7585 case ISN_COMPAREFUNC:
7586 case ISN_COMPARELIST:
7587 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588 case ISN_COMPARESPECIAL:
7589 case ISN_COMPARESTRING:
7590 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007591 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592 case ISN_DROP:
7593 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007594 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007595 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007597 case ISN_EXECCONCAT:
7598 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007600 case ISN_GETITEM:
7601 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007602 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007603 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007605 case ISN_LOADBDICT:
7606 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007607 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007608 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007609 case ISN_LOADSCRIPT:
7610 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007612 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007613 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007614 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 case ISN_NEGATENR:
7616 case ISN_NEWDICT:
7617 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007619 case ISN_OPFLOAT:
7620 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007622 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007623 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 case ISN_PUSHF:
7625 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007627 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007629 case ISN_SHUFFLE:
7630 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007632 case ISN_STOREDICT:
7633 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007634 case ISN_STORENR:
7635 case ISN_STOREOUTER:
7636 case ISN_STOREREG:
7637 case ISN_STORESCRIPT:
7638 case ISN_STOREV:
7639 case ISN_STRINDEX:
7640 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641 case ISN_THROW:
7642 case ISN_TRY:
7643 // nothing allocated
7644 break;
7645 }
7646}
7647
7648/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007649 * Free all instructions for "dfunc".
7650 */
7651 static void
7652delete_def_function_contents(dfunc_T *dfunc)
7653{
7654 int idx;
7655
7656 ga_clear(&dfunc->df_def_args_isn);
7657
7658 if (dfunc->df_instr != NULL)
7659 {
7660 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7661 delete_instr(dfunc->df_instr + idx);
7662 VIM_CLEAR(dfunc->df_instr);
7663 }
7664
7665 dfunc->df_deleted = TRUE;
7666}
7667
7668/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007669 * When a user function is deleted, clear the contents of any associated def
7670 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671 */
7672 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007673clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007675 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 {
7677 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7678 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679
Bram Moolenaar20431c92020-03-20 18:39:46 +01007680 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007681 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682 }
7683}
7684
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007685/*
7686 * Used when a user function is about to be deleted: remove the pointer to it.
7687 * The entry in def_functions is then unused.
7688 */
7689 void
7690unlink_def_function(ufunc_T *ufunc)
7691{
7692 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7693
7694 dfunc->df_ufunc = NULL;
7695}
7696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007698/*
7699 * Free all functions defined with ":def".
7700 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701 void
7702free_def_functions(void)
7703{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007704 int idx;
7705
7706 for (idx = 0; idx < def_functions.ga_len; ++idx)
7707 {
7708 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7709
7710 delete_def_function_contents(dfunc);
7711 }
7712
7713 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714}
7715#endif
7716
7717
7718#endif // FEAT_EVAL