blob: ad9b9dfd6a4012ef1333320d73101bc6162cb4d8 [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 {
1843 sallvar_T *sav = find_script_var(name, (size_t)-1, cctx);
1844
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 Moolenaar8a7d6542020-01-26 15:56:19 +01003044 else
3045 {
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 {
3181 int invert = TRUE;
3182
3183 while (p > start && p[-1] == '!')
3184 {
3185 --p;
3186 invert = !invert;
3187 }
3188 if (generate_2BOOL(cctx, invert) == FAIL)
3189 return FAIL;
3190 }
3191 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003192 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 return OK;
3194}
3195
3196/*
3197 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003198 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 */
3200 static int
3201compile_subscript(
3202 char_u **arg,
3203 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003204 char_u *start_leader,
3205 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003206 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003208 char_u *name_start = *end_leader;
3209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 for (;;)
3211 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003212 char_u *p = skipwhite(*arg);
3213
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003214 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003215 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003216 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003217
3218 // If a following line starts with "->{" or "->X" advance to that
3219 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003220 // Also if a following line starts with ".x".
3221 if (next != NULL &&
3222 ((next[0] == '-' && next[1] == '>'
3223 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003224 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003225 {
3226 next = next_line_from_context(cctx, TRUE);
3227 if (next == NULL)
3228 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003229 *arg = next;
3230 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003231 }
3232 }
3233
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003234 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3235 // not a function call.
3236 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003238 garray_T *stack = &cctx->ctx_type_stack;
3239 type_T *type;
3240 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003242 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003243 return FAIL;
3244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003246 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3247
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003248 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3250 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003251 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 return FAIL;
3253 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003254 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003256 char_u *pstart = p;
3257
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003258 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003259 return FAIL;
3260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 // something->method()
3262 // Apply the '!', '-' and '+' first:
3263 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003264 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003267 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003268 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003269 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 if (**arg == '{')
3271 {
3272 // lambda call: list->{lambda}
3273 if (compile_lambda_call(arg, cctx) == FAIL)
3274 return FAIL;
3275 }
3276 else
3277 {
3278 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003279 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003280 if (!eval_isnamec1(*p))
3281 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003282 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003283 return FAIL;
3284 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003285 if (ASCII_ISALPHA(*p) && p[1] == ':')
3286 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003287 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 ;
3289 if (*p != '(')
3290 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003291 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 return FAIL;
3293 }
3294 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003295 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 return FAIL;
3297 }
3298 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003299 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003301 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003302 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003303 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003304 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003305 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003306
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003307 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003308 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003309 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003310 // TODO: blob index
3311 // TODO: more arguments
3312 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003313 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003314 return FAIL;
3315
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003316 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003317 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003318 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003319 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003320 if (**arg == ':')
3321 // missing first index is equal to zero
3322 generate_PUSHNR(cctx, 0);
3323 else
3324 {
3325 if (compile_expr0(arg, cctx) == FAIL)
3326 return FAIL;
3327 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3328 return FAIL;
3329 *arg = skipwhite(*arg);
3330 }
3331 if (**arg == ':')
3332 {
3333 *arg = skipwhite(*arg + 1);
3334 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3335 return FAIL;
3336 if (**arg == ']')
3337 // missing second index is equal to end of string
3338 generate_PUSHNR(cctx, -1);
3339 else
3340 {
3341 if (compile_expr0(arg, cctx) == FAIL)
3342 return FAIL;
3343 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3344 return FAIL;
3345 *arg = skipwhite(*arg);
3346 }
3347 is_slice = TRUE;
3348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349
3350 if (**arg != ']')
3351 {
3352 emsg(_(e_missbrac));
3353 return FAIL;
3354 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003355 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003357 // We can index a list and a dict. If we don't know the type
3358 // we can use the index value type.
3359 // TODO: If we don't know use an instruction to figure it out at
3360 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003361 typep = ((type_T **)stack->ga_data) + stack->ga_len
3362 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003363 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003364 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3365 // If the index is a string, the variable must be a Dict.
3366 if (*typep == &t_any && valtype == &t_string)
3367 vtype = VAR_DICT;
3368 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003369 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003370 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3371 return FAIL;
3372 if (is_slice)
3373 {
3374 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3375 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3376 return FAIL;
3377 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003378 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003379
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003380 if (vtype == VAR_DICT)
3381 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003382 if (is_slice)
3383 {
3384 emsg(_(e_cannot_slice_dictionary));
3385 return FAIL;
3386 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003387 if ((*typep)->tt_type == VAR_DICT)
3388 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003389 else
3390 {
3391 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3392 return FAIL;
3393 *typep = &t_any;
3394 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003395 if (may_generate_2STRING(-1, cctx) == FAIL)
3396 return FAIL;
3397 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3398 return FAIL;
3399 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003400 else if (vtype == VAR_STRING)
3401 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003402 *typep = &t_string;
3403 if ((is_slice
3404 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3405 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003406 return FAIL;
3407 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003408 else if (vtype == VAR_BLOB)
3409 {
3410 emsg("Sorry, blob index and slice not implemented yet");
3411 return FAIL;
3412 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003413 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003414 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003415 if (is_slice)
3416 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003417 if (generate_instr_drop(cctx,
3418 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3419 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003420 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003421 }
Bram Moolenaared591872020-08-15 22:14:53 +02003422 else
3423 {
3424 if ((*typep)->tt_type == VAR_LIST)
3425 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003426 if (generate_instr_drop(cctx,
3427 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3428 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003429 return FAIL;
3430 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003431 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003432 else
3433 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003434 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003435 return FAIL;
3436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003438 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003440 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003441 return FAIL;
3442
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003443 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003444 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003445 {
3446 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003447 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003450 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003451 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 while (eval_isnamec(*p))
3453 MB_PTR_ADV(p);
3454 if (p == *arg)
3455 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003456 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 return FAIL;
3458 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003459 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 return FAIL;
3461 *arg = p;
3462 }
3463 else
3464 break;
3465 }
3466
3467 // TODO - see handle_subscript():
3468 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3469 // Don't do this when "Func" is already a partial that was bound
3470 // explicitly (pt_auto is FALSE).
3471
3472 return OK;
3473}
3474
3475/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003476 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3477 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003479 * If the value is a constant "ppconst->pp_ret" will be set.
3480 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003481 *
3482 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 */
3484
3485/*
3486 * number number constant
3487 * 0zFFFFFFFF Blob constant
3488 * "string" string constant
3489 * 'string' literal string constant
3490 * &option-name option value
3491 * @r register contents
3492 * identifier variable value
3493 * function() function call
3494 * $VAR environment variable
3495 * (expression) nested expression
3496 * [expr, expr] List
3497 * {key: val, key: val} Dictionary
3498 * #{key: val, key: val} Dictionary with literal keys
3499 *
3500 * Also handle:
3501 * ! in front logical NOT
3502 * - in front unary minus
3503 * + in front unary plus (ignored)
3504 * trailing (arg) funcref/partial call
3505 * trailing [] subscript in String or List
3506 * trailing .name entry in Dictionary
3507 * trailing ->name() method call
3508 */
3509 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003510compile_expr7(
3511 char_u **arg,
3512 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003513 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 char_u *start_leader, *end_leader;
3516 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003517 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003518 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519
3520 /*
3521 * Skip '!', '-' and '+' characters. They are handled later.
3522 */
3523 start_leader = *arg;
3524 while (**arg == '!' || **arg == '-' || **arg == '+')
3525 *arg = skipwhite(*arg + 1);
3526 end_leader = *arg;
3527
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003528 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 switch (**arg)
3530 {
3531 /*
3532 * Number constant.
3533 */
3534 case '0': // also for blob starting with 0z
3535 case '1':
3536 case '2':
3537 case '3':
3538 case '4':
3539 case '5':
3540 case '6':
3541 case '7':
3542 case '8':
3543 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003544 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003546 // Apply "-" and "+" just before the number now, right to
3547 // left. Matters especially when "->" follows. Stops at
3548 // '!'.
3549 if (apply_leader(rettv, TRUE,
3550 start_leader, &end_leader) == FAIL)
3551 {
3552 clear_tv(rettv);
3553 return FAIL;
3554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 break;
3556
3557 /*
3558 * String constant: "string".
3559 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003560 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 return FAIL;
3562 break;
3563
3564 /*
3565 * Literal string constant: 'str''ing'.
3566 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003567 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 return FAIL;
3569 break;
3570
3571 /*
3572 * Constant Vim variable.
3573 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003574 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 ret = NOTDONE;
3576 break;
3577
3578 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003579 * "true" constant
3580 */
3581 case 't': if (STRNCMP(*arg, "true", 4) == 0
3582 && !eval_isnamec((*arg)[4]))
3583 {
3584 *arg += 4;
3585 rettv->v_type = VAR_BOOL;
3586 rettv->vval.v_number = VVAL_TRUE;
3587 }
3588 else
3589 ret = NOTDONE;
3590 break;
3591
3592 /*
3593 * "false" constant
3594 */
3595 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3596 && !eval_isnamec((*arg)[5]))
3597 {
3598 *arg += 5;
3599 rettv->v_type = VAR_BOOL;
3600 rettv->vval.v_number = VVAL_FALSE;
3601 }
3602 else
3603 ret = NOTDONE;
3604 break;
3605
3606 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 * List: [expr, expr]
3608 */
3609 case '[': ret = compile_list(arg, cctx);
3610 break;
3611
3612 /*
3613 * Dictionary: #{key: val, key: val}
3614 */
3615 case '#': if ((*arg)[1] == '{')
3616 {
3617 ++*arg;
3618 ret = compile_dict(arg, cctx, TRUE);
3619 }
3620 else
3621 ret = NOTDONE;
3622 break;
3623
3624 /*
3625 * Lambda: {arg, arg -> expr}
3626 * Dictionary: {'key': val, 'key': val}
3627 */
3628 case '{': {
3629 char_u *start = skipwhite(*arg + 1);
3630
3631 // Find out what comes after the arguments.
3632 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003633 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 if (ret != FAIL && *start == '>')
3635 ret = compile_lambda(arg, cctx);
3636 else
3637 ret = compile_dict(arg, cctx, FALSE);
3638 }
3639 break;
3640
3641 /*
3642 * Option value: &name
3643 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003644 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3645 return FAIL;
3646 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648
3649 /*
3650 * Environment variable: $VAR.
3651 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003652 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3653 return FAIL;
3654 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 break;
3656
3657 /*
3658 * Register contents: @r.
3659 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003660 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3661 return FAIL;
3662 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 break;
3664 /*
3665 * nested expression: (expression).
3666 */
3667 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003668
3669 // recursive!
3670 if (ppconst->pp_used <= PPSIZE - 10)
3671 {
3672 ret = compile_expr1(arg, cctx, ppconst);
3673 }
3674 else
3675 {
3676 // Not enough space in ppconst, flush constants.
3677 if (generate_ppconst(cctx, ppconst) == FAIL)
3678 return FAIL;
3679 ret = compile_expr0(arg, cctx);
3680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 *arg = skipwhite(*arg);
3682 if (**arg == ')')
3683 ++*arg;
3684 else if (ret == OK)
3685 {
3686 emsg(_(e_missing_close));
3687 ret = FAIL;
3688 }
3689 break;
3690
3691 default: ret = NOTDONE;
3692 break;
3693 }
3694 if (ret == FAIL)
3695 return FAIL;
3696
Bram Moolenaar1c747212020-05-09 18:28:34 +02003697 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003699 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003700 clear_tv(rettv);
3701 else
3702 // A constant expression can possibly be handled compile time,
3703 // return the value instead of generating code.
3704 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 }
3706 else if (ret == NOTDONE)
3707 {
3708 char_u *p;
3709 int r;
3710
3711 if (!eval_isnamec1(**arg))
3712 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003713 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 return FAIL;
3715 }
3716
3717 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003718 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003720 {
3721 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003724 {
3725 if (generate_ppconst(cctx, ppconst) == FAIL)
3726 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003727 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 if (r == FAIL)
3730 return FAIL;
3731 }
3732
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003733 // Handle following "[]", ".member", etc.
3734 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003735 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003736 ppconst) == FAIL)
3737 return FAIL;
3738 if (ppconst->pp_used > 0)
3739 {
3740 // apply the '!', '-' and '+' before the constant
3741 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003742 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003743 return FAIL;
3744 return OK;
3745 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003746 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003748 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749}
3750
3751/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003752 * Give the "white on both sides" error, taking the operator from "p[len]".
3753 */
3754 void
3755error_white_both(char_u *op, int len)
3756{
3757 char_u buf[10];
3758
3759 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003760 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003761}
3762
3763/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003764 * <type>expr7: runtime type check / conversion
3765 */
3766 static int
3767compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3768{
3769 type_T *want_type = NULL;
3770
3771 // Recognize <type>
3772 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3773 {
3774 int called_emsg_before = called_emsg;
3775
3776 ++*arg;
3777 want_type = parse_type(arg, cctx->ctx_type_list);
3778 if (called_emsg != called_emsg_before)
3779 return FAIL;
3780
3781 if (**arg != '>')
3782 {
3783 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003784 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003785 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003786 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003787 return FAIL;
3788 }
3789 ++*arg;
3790 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3791 return FAIL;
3792 }
3793
3794 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3795 return FAIL;
3796
3797 if (want_type != NULL)
3798 {
3799 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003800 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003801
Bram Moolenaard1103582020-08-14 22:44:25 +02003802 generate_ppconst(cctx, ppconst);
3803 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003804 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003805 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003806 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3807 return FAIL;
3808 }
3809 }
3810
3811 return OK;
3812}
3813
3814/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 * * number multiplication
3816 * / number division
3817 * % number modulo
3818 */
3819 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003820compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821{
3822 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003823 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003824 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003826 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003827 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 return FAIL;
3829
3830 /*
3831 * Repeat computing, until no "*", "/" or "%" is following.
3832 */
3833 for (;;)
3834 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003835 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 if (*op != '*' && *op != '/' && *op != '%')
3837 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003838 if (next != NULL)
3839 {
3840 *arg = next_line_from_context(cctx, TRUE);
3841 op = skipwhite(*arg);
3842 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003843
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003844 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003846 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003847 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 }
3849 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003850 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003851 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003853 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003854 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003856
3857 if (ppconst->pp_used == ppconst_used + 2
3858 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3859 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003860 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003861 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3862 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003863 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003865 // both are numbers: compute the result
3866 switch (*op)
3867 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003868 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003869 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003870 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003871 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003872 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003873 break;
3874 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003875 tv1->vval.v_number = res;
3876 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003877 }
3878 else
3879 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003880 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003881 generate_two_op(cctx, op);
3882 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 }
3884
3885 return OK;
3886}
3887
3888/*
3889 * + number addition
3890 * - number subtraction
3891 * .. string concatenation
3892 */
3893 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003894compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895{
3896 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003897 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003899 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900
3901 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003902 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 return FAIL;
3904
3905 /*
3906 * Repeat computing, until no "+", "-" or ".." is following.
3907 */
3908 for (;;)
3909 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003910 op = may_peek_next_line(cctx, *arg, &next);
3911 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 break;
3913 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003914 if (next != NULL)
3915 {
3916 *arg = next_line_from_context(cctx, TRUE);
3917 op = skipwhite(*arg);
3918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003920 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003922 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003923 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 }
3925
3926 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003927 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003928 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003930 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003931 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 return FAIL;
3933
Bram Moolenaara5565e42020-05-09 15:44:01 +02003934 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003935 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003936 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3937 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3938 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3939 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003941 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3942 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003943
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003944 // concat/subtract/add constant numbers
3945 if (*op == '+')
3946 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3947 else if (*op == '-')
3948 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3949 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003950 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003951 // concatenate constant strings
3952 char_u *s1 = tv1->vval.v_string;
3953 char_u *s2 = tv2->vval.v_string;
3954 size_t len1 = STRLEN(s1);
3955
3956 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3957 if (tv1->vval.v_string == NULL)
3958 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003959 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003960 return FAIL;
3961 }
3962 mch_memmove(tv1->vval.v_string, s1, len1);
3963 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003964 vim_free(s1);
3965 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003966 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003967 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 }
3969 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003970 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003971 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003972 if (*op == '.')
3973 {
3974 if (may_generate_2STRING(-2, cctx) == FAIL
3975 || may_generate_2STRING(-1, cctx) == FAIL)
3976 return FAIL;
3977 generate_instr_drop(cctx, ISN_CONCAT, 1);
3978 }
3979 else
3980 generate_two_op(cctx, op);
3981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 }
3983
3984 return OK;
3985}
3986
3987/*
3988 * expr5a == expr5b
3989 * expr5a =~ expr5b
3990 * expr5a != expr5b
3991 * expr5a !~ expr5b
3992 * expr5a > expr5b
3993 * expr5a >= expr5b
3994 * expr5a < expr5b
3995 * expr5a <= expr5b
3996 * expr5a is expr5b
3997 * expr5a isnot expr5b
3998 *
3999 * Produces instructions:
4000 * EVAL expr5a Push result of "expr5a"
4001 * EVAL expr5b Push result of "expr5b"
4002 * COMPARE one of the compare instructions
4003 */
4004 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004005compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006{
4007 exptype_T type = EXPR_UNKNOWN;
4008 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004009 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004012 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013
4014 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 return FAIL;
4017
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004018 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004019 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020
4021 /*
4022 * If there is a comparative operator, use it.
4023 */
4024 if (type != EXPR_UNKNOWN)
4025 {
4026 int ic = FALSE; // Default: do not ignore case
4027
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004028 if (next != NULL)
4029 {
4030 *arg = next_line_from_context(cctx, TRUE);
4031 p = skipwhite(*arg);
4032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 if (type_is && (p[len] == '?' || p[len] == '#'))
4034 {
4035 semsg(_(e_invexpr2), *arg);
4036 return FAIL;
4037 }
4038 // extra question mark appended: ignore case
4039 if (p[len] == '?')
4040 {
4041 ic = TRUE;
4042 ++len;
4043 }
4044 // extra '#' appended: match case (ignored)
4045 else if (p[len] == '#')
4046 ++len;
4047 // nothing appended: match case
4048
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004049 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004051 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004052 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 }
4054
4055 // get the second variable
4056 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004057 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004058 return FAIL;
4059
Bram Moolenaara5565e42020-05-09 15:44:01 +02004060 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 return FAIL;
4062
Bram Moolenaara5565e42020-05-09 15:44:01 +02004063 if (ppconst->pp_used == ppconst_used + 2)
4064 {
4065 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4066 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4067 int ret;
4068
4069 // Both sides are a constant, compute the result now.
4070 // First check for a valid combination of types, this is more
4071 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004072 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004073 ret = FAIL;
4074 else
4075 {
4076 ret = typval_compare(tv1, tv2, type, ic);
4077 tv1->v_type = VAR_BOOL;
4078 tv1->vval.v_number = tv1->vval.v_number
4079 ? VVAL_TRUE : VVAL_FALSE;
4080 clear_tv(tv2);
4081 --ppconst->pp_used;
4082 }
4083 return ret;
4084 }
4085
4086 generate_ppconst(cctx, ppconst);
4087 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 }
4089
4090 return OK;
4091}
4092
Bram Moolenaar7f141552020-05-09 17:35:53 +02004093static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095/*
4096 * Compile || or &&.
4097 */
4098 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099compile_and_or(
4100 char_u **arg,
4101 cctx_T *cctx,
4102 char *op,
4103 ppconst_T *ppconst,
4104 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004106 char_u *next;
4107 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 int opchar = *op;
4109
4110 if (p[0] == opchar && p[1] == opchar)
4111 {
4112 garray_T *instr = &cctx->ctx_instr;
4113 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004114 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004115 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116
4117 /*
4118 * Repeat until there is no following "||" or "&&"
4119 */
4120 ga_init2(&end_ga, sizeof(int), 10);
4121 while (p[0] == opchar && p[1] == opchar)
4122 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004123 if (next != NULL)
4124 {
4125 *arg = next_line_from_context(cctx, TRUE);
4126 p = skipwhite(*arg);
4127 }
4128
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004129 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4130 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004131 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004132 return FAIL;
4133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004135 // TODO: use ppconst if the value is a constant and check
4136 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004137 generate_ppconst(cctx, ppconst);
4138
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004139 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4140 all_bool_values = FALSE;
4141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 if (ga_grow(&end_ga, 1) == FAIL)
4143 {
4144 ga_clear(&end_ga);
4145 return FAIL;
4146 }
4147 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4148 ++end_ga.ga_len;
4149 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004150 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151
4152 // eval the next expression
4153 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004154 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004155 return FAIL;
4156
Bram Moolenaara5565e42020-05-09 15:44:01 +02004157 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4158 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 {
4160 ga_clear(&end_ga);
4161 return FAIL;
4162 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004163
4164 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167
4168 // Fill in the end label in all jumps.
4169 while (end_ga.ga_len > 0)
4170 {
4171 isn_T *isn;
4172
4173 --end_ga.ga_len;
4174 isn = ((isn_T *)instr->ga_data)
4175 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4176 isn->isn_arg.jump.jump_where = instr->ga_len;
4177 }
4178 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004179
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004180 // The resulting type is converted to bool if needed.
4181 if (!all_bool_values)
4182 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * expr4a && expr4a && expr4a logical AND
4190 *
4191 * Produces instructions:
4192 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004193 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004195 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004197 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 * end:
4199 */
4200 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004201compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 int ppconst_used = ppconst->pp_used;
4204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 return FAIL;
4208
4209 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211}
4212
4213/*
4214 * expr3a || expr3b || expr3c logical OR
4215 *
4216 * Produces instructions:
4217 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004218 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004220 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004222 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 * end:
4224 */
4225 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004226compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004228 int ppconst_used = ppconst->pp_used;
4229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 return FAIL;
4233
4234 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236}
4237
4238/*
4239 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004241 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 * JUMP_IF_FALSE alt jump if false
4243 * EVAL expr1a
4244 * JUMP_ALWAYS end
4245 * alt: EVAL expr1b
4246 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004247 *
4248 * Toplevel expression: expr2 ?? expr1
4249 * Produces instructions:
4250 * EVAL expr2 Push result of "expr2"
4251 * JUMP_AND_KEEP_IF_TRUE end jump if true
4252 * EVAL expr1
4253 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 */
4255 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257{
4258 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004260 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261
Bram Moolenaar3988f642020-08-27 22:43:03 +02004262 // Ignore all kinds of errors when not producing code.
4263 if (cctx->ctx_skip == SKIP_YES)
4264 {
4265 skip_expr(arg);
4266 return OK;
4267 }
4268
Bram Moolenaar61a89812020-05-07 16:58:17 +02004269 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 return FAIL;
4272
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004273 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 if (*p == '?')
4275 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004276 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 garray_T *instr = &cctx->ctx_instr;
4278 garray_T *stack = &cctx->ctx_type_stack;
4279 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004280 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004282 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004283 int has_const_expr = FALSE;
4284 int const_value = FALSE;
4285 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004287 if (next != NULL)
4288 {
4289 *arg = next_line_from_context(cctx, TRUE);
4290 p = skipwhite(*arg);
4291 }
4292
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004293 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004294 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004295 semsg(_(e_white_space_required_before_and_after_str),
4296 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004297 return FAIL;
4298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
Bram Moolenaara5565e42020-05-09 15:44:01 +02004300 if (ppconst->pp_used == ppconst_used + 1)
4301 {
4302 // the condition is a constant, we know whether the ? or the :
4303 // expression is to be evaluated.
4304 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004305 if (op_falsy)
4306 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4307 else
4308 {
4309 int error = FALSE;
4310
4311 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4312 &error);
4313 if (error)
4314 return FAIL;
4315 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004316 cctx->ctx_skip = save_skip == SKIP_YES ||
4317 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4318
4319 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4320 // "left ?? right" and "left" is truthy: produce "left"
4321 generate_ppconst(cctx, ppconst);
4322 else
4323 {
4324 clear_tv(&ppconst->pp_tv[ppconst_used]);
4325 --ppconst->pp_used;
4326 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327 }
4328 else
4329 {
4330 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004331 if (op_falsy)
4332 end_idx = instr->ga_len;
4333 generate_JUMP(cctx, op_falsy
4334 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4335 if (op_falsy)
4336 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338
4339 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004340 *arg = skipwhite(p + 1 + op_falsy);
4341 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004342 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004343 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004344 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 if (!has_const_expr)
4347 {
4348 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004350 if (!op_falsy)
4351 {
4352 // remember the type and drop it
4353 --stack->ga_len;
4354 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004356 end_idx = instr->ga_len;
4357 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004358
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004359 // jump here from JUMP_IF_FALSE
4360 isn = ((isn_T *)instr->ga_data) + alt_idx;
4361 isn->isn_arg.jump.jump_where = instr->ga_len;
4362 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004365 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004367 // Check for the ":".
4368 p = may_peek_next_line(cctx, *arg, &next);
4369 if (*p != ':')
4370 {
4371 emsg(_(e_missing_colon));
4372 return FAIL;
4373 }
4374 if (next != NULL)
4375 {
4376 *arg = next_line_from_context(cctx, TRUE);
4377 p = skipwhite(*arg);
4378 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004379
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004380 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4381 {
4382 semsg(_(e_white_space_required_before_and_after_str), ":");
4383 return FAIL;
4384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004386 // evaluate the third expression
4387 if (has_const_expr)
4388 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004389 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004390 *arg = skipwhite(p + 1);
4391 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4392 return FAIL;
4393 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4394 return FAIL;
4395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 if (!has_const_expr)
4398 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004399 type_T **typep;
4400
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004404 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4405 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004406
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004407 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 isn = ((isn_T *)instr->ga_data) + end_idx;
4409 isn->isn_arg.jump.jump_where = instr->ga_len;
4410 }
4411
4412 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 }
4414 return OK;
4415}
4416
4417/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 * Toplevel expression.
4419 */
4420 static int
4421compile_expr0(char_u **arg, cctx_T *cctx)
4422{
4423 ppconst_T ppconst;
4424
4425 CLEAR_FIELD(ppconst);
4426 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4427 {
4428 clear_ppconst(&ppconst);
4429 return FAIL;
4430 }
4431 if (generate_ppconst(cctx, &ppconst) == FAIL)
4432 return FAIL;
4433 return OK;
4434}
4435
4436/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 * compile "return [expr]"
4438 */
4439 static char_u *
4440compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4441{
4442 char_u *p = arg;
4443 garray_T *stack = &cctx->ctx_type_stack;
4444 type_T *stack_type;
4445
4446 if (*p != NUL && *p != '|' && *p != '\n')
4447 {
4448 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 return NULL;
4451
4452 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4453 if (set_return_type)
4454 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004455 else
4456 {
4457 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4458 && stack_type->tt_type != VAR_VOID
4459 && stack_type->tt_type != VAR_UNKNOWN)
4460 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004461 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004462 return NULL;
4463 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004464 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4465 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004466 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004467 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 }
4469 else
4470 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004471 // "set_return_type" cannot be TRUE, only used for a lambda which
4472 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004473 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4474 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004476 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 return NULL;
4478 }
4479
4480 // No argument, return zero.
4481 generate_PUSHNR(cctx, 0);
4482 }
4483
4484 if (generate_instr(cctx, ISN_RETURN) == NULL)
4485 return NULL;
4486
4487 // "return val | endif" is possible
4488 return skipwhite(p);
4489}
4490
4491/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004492 * Get a line from the compilation context, compatible with exarg_T getline().
4493 * Return a pointer to the line in allocated memory.
4494 * Return NULL for end-of-file or some error.
4495 */
4496 static char_u *
4497exarg_getline(
4498 int c UNUSED,
4499 void *cookie,
4500 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004501 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004502{
4503 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004504 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004505
Bram Moolenaar66250c92020-08-20 15:02:42 +02004506 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004507 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004508 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004509 return NULL;
4510 ++cctx->ctx_lnum;
4511 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4512 // Comment lines result in NULL pointers, skip them.
4513 if (p != NULL)
4514 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004515 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004516}
4517
4518/*
4519 * Compile a nested :def command.
4520 */
4521 static char_u *
4522compile_nested_function(exarg_T *eap, cctx_T *cctx)
4523{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004524 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004525 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004526 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004527 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004528 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004529 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004530
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004531 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004532 {
4533 emsg(_(e_cannot_use_bang_with_nested_def));
4534 return NULL;
4535 }
4536
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004537 // Only g:Func() can use a namespace.
4538 if (name_start[1] == ':' && !is_global)
4539 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004540 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004541 return NULL;
4542 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004543 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4544 return NULL;
4545
Bram Moolenaar04b12692020-05-04 23:24:44 +02004546 eap->arg = name_end;
4547 eap->getline = exarg_getline;
4548 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004549 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004550 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004551 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004552 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004553
Bram Moolenaar822ba242020-05-24 23:00:18 +02004554 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004555 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004556 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004557 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004558 {
4559 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004560 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004561 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004562
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004563 if (is_global)
4564 {
4565 char_u *func_name = vim_strnsave(name_start + 2,
4566 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004567
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004568 if (func_name == NULL)
4569 r = FAIL;
4570 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004571 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004572 }
4573 else
4574 {
4575 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004576 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004577 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004578 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004579
Bram Moolenaareef21022020-08-01 22:16:43 +02004580 if (lvar == NULL)
4581 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004582 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004583 return NULL;
4584 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004585
4586 // copy over the block scope IDs
4587 if (block_depth > 0)
4588 {
4589 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4590 if (ufunc->uf_block_ids != NULL)
4591 {
4592 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4593 sizeof(int) * block_depth);
4594 ufunc->uf_block_depth = block_depth;
4595 }
4596 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004597 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004598
Bram Moolenaar61a89812020-05-07 16:58:17 +02004599 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004600 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004601}
4602
4603/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 * Return the length of an assignment operator, or zero if there isn't one.
4605 */
4606 int
4607assignment_len(char_u *p, int *heredoc)
4608{
4609 if (*p == '=')
4610 {
4611 if (p[1] == '<' && p[2] == '<')
4612 {
4613 *heredoc = TRUE;
4614 return 3;
4615 }
4616 return 1;
4617 }
4618 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4619 return 2;
4620 if (STRNCMP(p, "..=", 3) == 0)
4621 return 3;
4622 return 0;
4623}
4624
4625// words that cannot be used as a variable
4626static char *reserved[] = {
4627 "true",
4628 "false",
4629 NULL
4630};
4631
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004632typedef enum {
4633 dest_local,
4634 dest_option,
4635 dest_env,
4636 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004637 dest_buffer,
4638 dest_window,
4639 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004640 dest_vimvar,
4641 dest_script,
4642 dest_reg,
4643} assign_dest_T;
4644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004646 * Generate the load instruction for "name".
4647 */
4648 static void
4649generate_loadvar(
4650 cctx_T *cctx,
4651 assign_dest_T dest,
4652 char_u *name,
4653 lvar_T *lvar,
4654 type_T *type)
4655{
4656 switch (dest)
4657 {
4658 case dest_option:
4659 // TODO: check the option exists
4660 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4661 break;
4662 case dest_global:
4663 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4664 break;
4665 case dest_buffer:
4666 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4667 break;
4668 case dest_window:
4669 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4670 break;
4671 case dest_tab:
4672 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4673 break;
4674 case dest_script:
4675 compile_load_scriptvar(cctx,
4676 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4677 break;
4678 case dest_env:
4679 // Include $ in the name here
4680 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4681 break;
4682 case dest_reg:
4683 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4684 break;
4685 case dest_vimvar:
4686 generate_LOADV(cctx, name + 2, TRUE);
4687 break;
4688 case dest_local:
4689 if (lvar->lv_from_outer)
4690 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4691 NULL, type);
4692 else
4693 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4694 break;
4695 }
4696}
4697
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004698 void
4699vim9_declare_error(char_u *name)
4700{
4701 char *scope = "";
4702
4703 switch (*name)
4704 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004705 case 'g': scope = _("global"); break;
4706 case 'b': scope = _("buffer"); break;
4707 case 'w': scope = _("window"); break;
4708 case 't': scope = _("tab"); break;
4709 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004710 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004711 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004712 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004713 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004714 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004715 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004716 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004717 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004718 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004719}
4720
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004721/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004722 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004723 * "let name"
4724 * "var name = expr"
4725 * "final name = expr"
4726 * "const name = expr"
4727 * "name = expr"
4728 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004729 * Return NULL for an error.
4730 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 */
4732 static char_u *
4733compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4734{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004735 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004737 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 char_u *ret = NULL;
4739 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004740 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004741 int scriptvar_sid = 0;
4742 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004745 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 int oplen = 0;
4748 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004749 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004750 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004751 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004753 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4754 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004756 // Skip over the "var" or "[var, var]" to get to any "=".
4757 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4758 if (p == NULL)
4759 return *arg == '[' ? arg : NULL;
4760
4761 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004763 // TODO: should we allow this, and figure out type inference from list
4764 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004765 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 return NULL;
4767 }
4768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 sp = p;
4770 p = skipwhite(p);
4771 op = p;
4772 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004773
4774 if (var_count > 0 && oplen == 0)
4775 // can be something like "[1, 2]->func()"
4776 return arg;
4777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4779 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004780 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004781 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004782 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 if (heredoc)
4785 {
4786 list_T *l;
4787 listitem_T *li;
4788
4789 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004790 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004792 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004793 if (l == NULL)
4794 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795
Bram Moolenaar078269b2020-09-21 20:35:55 +02004796 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004798 // Push each line and the create the list.
4799 FOR_ALL_LIST_ITEMS(l, li)
4800 {
4801 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4802 li->li_tv.vval.v_string = NULL;
4803 }
4804 generate_NEWLIST(cctx, l->lv_len);
4805 type = &t_list_string;
4806 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 list_free(l);
4809 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004810 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004812 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004814 // for "[var, var] = expr" evaluate the expression here, loop over the
4815 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004816
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004817 p = skipwhite(op + oplen);
4818 if (compile_expr0(&p, cctx) == FAIL)
4819 return NULL;
4820 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004822 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004824 type_T *stacktype;
4825
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004826 stacktype = stack->ga_len == 0 ? &t_void
4827 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004830 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004831 goto theend;
4832 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004833 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004834 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004835 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004836 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4837 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 }
4839 }
4840
4841 /*
4842 * Loop over variables in "[var, var] = expr".
4843 * For "var = expr" and "let var: type" this is done only once.
4844 */
4845 if (var_count > 0)
4846 var_start = skipwhite(arg + 1); // skip over the "["
4847 else
4848 var_start = arg;
4849 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4850 {
4851 char_u *var_end = skip_var_one(var_start, FALSE);
4852 size_t varlen;
4853 int new_local = FALSE;
4854 int opt_type;
4855 int opt_flags = 0;
4856 assign_dest_T dest = dest_local;
4857 int vimvaridx = -1;
4858 lvar_T *lvar = NULL;
4859 lvar_T arg_lvar;
4860 int has_type = FALSE;
4861 int has_index = FALSE;
4862 int instr_count = -1;
4863
Bram Moolenaar65821722020-08-02 18:58:54 +02004864 if (*var_start == '@')
4865 p = var_start + 2;
4866 else
4867 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004868 // skip over the leading "&", "&l:", "&g:" and "$"
4869 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004870 p = to_name_end(p, TRUE);
4871 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872
4873 // "a: type" is declaring variable "a" with a type, not "a:".
4874 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4875 --var_end;
4876 if (is_decl && p == var_start + 2 && p[-1] == ':')
4877 --p;
4878
4879 varlen = p - var_start;
4880 vim_free(name);
4881 name = vim_strnsave(var_start, varlen);
4882 if (name == NULL)
4883 return NULL;
4884 if (!heredoc)
4885 type = &t_any;
4886
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004887 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004888 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004889 int declare_error = FALSE;
4890
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004891 if (*var_start == '&')
4892 {
4893 int cc;
4894 long numval;
4895
4896 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004897 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004899 emsg(_(e_const_option));
4900 goto theend;
4901 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004902 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004903 p = var_start;
4904 p = find_option_end(&p, &opt_flags);
4905 if (p == NULL)
4906 {
4907 // cannot happen?
4908 emsg(_(e_letunexp));
4909 goto theend;
4910 }
4911 cc = *p;
4912 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004913 opt_type = get_option_value(skip_option_env_lead(var_start),
4914 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 *p = cc;
4916 if (opt_type == -3)
4917 {
4918 semsg(_(e_unknown_option), var_start);
4919 goto theend;
4920 }
4921 if (opt_type == -2 || opt_type == 0)
4922 type = &t_string;
4923 else
4924 type = &t_number; // both number and boolean option
4925 }
4926 else if (*var_start == '$')
4927 {
4928 dest = dest_env;
4929 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004930 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004931 }
4932 else if (*var_start == '@')
4933 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004934 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 {
4936 emsg_invreg(var_start[1]);
4937 goto theend;
4938 }
4939 dest = dest_reg;
4940 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004941 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004942 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004943 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004944 {
4945 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004946 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004947 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004948 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949 {
4950 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004951 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004953 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004954 {
4955 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004956 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004958 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959 {
4960 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004961 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004963 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004964 {
4965 typval_T *vtv;
4966 int di_flags;
4967
4968 vimvaridx = find_vim_var(name + 2, &di_flags);
4969 if (vimvaridx < 0)
4970 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004971 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004972 goto theend;
4973 }
4974 // We use the current value of "sandbox" here, is that OK?
4975 if (var_check_ro(di_flags, name, FALSE))
4976 goto theend;
4977 dest = dest_vimvar;
4978 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004979 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004980 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004981 }
4982 else
4983 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004984 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985
4986 for (idx = 0; reserved[idx] != NULL; ++idx)
4987 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004988 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004989 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004990 goto theend;
4991 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004992
4993 lvar = lookup_local(var_start, varlen, cctx);
4994 if (lvar == NULL)
4995 {
4996 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004997 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4999 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005000 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 if (is_decl)
5002 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005003 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 goto theend;
5005 }
5006 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005007 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005008 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005009 if (lvar != NULL)
5010 {
5011 if (is_decl)
5012 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005013 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005014 goto theend;
5015 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005016 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005017 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005019 int script_namespace = varlen > 1
5020 && STRNCMP(var_start, "s:", 2) == 0;
5021 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005022 ? script_var_exists(var_start + 2, varlen - 2,
5023 FALSE, cctx)
5024 : script_var_exists(var_start, varlen,
5025 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005026 imported_T *import =
5027 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005028
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005029 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005031 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5032
5033 if (is_decl)
5034 {
5035 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005036 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005037 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005038 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005039 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005040 name);
5041 goto theend;
5042 }
5043 else if (cctx->ctx_ufunc->uf_script_ctx_version
5044 == SCRIPT_VERSION_VIM9
5045 && script_namespace
5046 && !script_var && import == NULL)
5047 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005048 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005049 goto theend;
5050 }
5051
5052 dest = dest_script;
5053
5054 // existing script-local variables should have a type
5055 scriptvar_sid = current_sctx.sc_sid;
5056 if (import != NULL)
5057 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005058 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005059 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005060 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005061 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005062 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005063 {
5064 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5065 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005066 ((svar_T *)si->sn_var_vals.ga_data)
5067 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005068 type = sv->sv_type;
5069 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005070 }
5071 }
5072 else if (name[1] == ':' && name[2] != NUL)
5073 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005074 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 goto theend;
5076 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005077 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005078 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005079 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005080 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005081 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005082 else if (check_defined(var_start, varlen, cctx) == FAIL)
5083 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005086
5087 if (declare_error)
5088 {
5089 vim9_declare_error(name);
5090 goto theend;
5091 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 }
5093
5094 // handle "a:name" as a name, not index "name" on "a"
5095 if (varlen > 1 || var_start[varlen] != ':')
5096 p = var_end;
5097
5098 if (dest != dest_option)
5099 {
5100 if (is_decl && *p == ':')
5101 {
5102 // parse optional type: "let var: type = expr"
5103 if (!VIM_ISWHITE(p[1]))
5104 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005105 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005106 goto theend;
5107 }
5108 p = skipwhite(p + 1);
5109 type = parse_type(&p, cctx->ctx_type_list);
5110 has_type = TRUE;
5111 }
5112 else if (lvar != NULL)
5113 type = lvar->lv_type;
5114 }
5115
5116 if (oplen == 3 && !heredoc && dest != dest_global
5117 && type->tt_type != VAR_STRING
5118 && type->tt_type != VAR_ANY)
5119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005120 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 goto theend;
5122 }
5123
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005124 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005125 {
5126 if (oplen > 1 && !heredoc)
5127 {
5128 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005129 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 goto theend;
5131 }
5132
5133 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005134 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5135 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 goto theend;
5137 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005138 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 if (lvar == NULL)
5140 goto theend;
5141 new_local = TRUE;
5142 }
5143
5144 member_type = type;
5145 if (var_end > var_start + varlen)
5146 {
5147 // Something follows after the variable: "var[idx]".
5148 if (is_decl)
5149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005150 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 goto theend;
5152 }
5153
5154 if (var_start[varlen] == '[')
5155 {
5156 has_index = TRUE;
5157 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005158 member_type = &t_any;
5159 else
5160 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005161 }
5162 else
5163 {
5164 semsg("Not supported yet: %s", var_start);
5165 goto theend;
5166 }
5167 }
5168 else if (lvar == &arg_lvar)
5169 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005170 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 goto theend;
5172 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005173 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5174 {
5175 semsg(_(e_cannot_assign_to_constant), name);
5176 goto theend;
5177 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005178
5179 if (!heredoc)
5180 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005181 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005182 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005183 if (oplen > 0 && var_count == 0)
5184 {
5185 // skip over the "=" and the expression
5186 p = skipwhite(op + oplen);
5187 compile_expr0(&p, cctx);
5188 }
5189 }
5190 else if (oplen > 0)
5191 {
5192 type_T *stacktype;
5193
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005194 // For "var = expr" evaluate the expression.
5195 if (var_count == 0)
5196 {
5197 int r;
5198
5199 // for "+=", "*=", "..=" etc. first load the current value
5200 if (*op != '=')
5201 {
5202 generate_loadvar(cctx, dest, name, lvar, type);
5203
5204 if (has_index)
5205 {
5206 // TODO: get member from list or dict
5207 emsg("Index with operation not supported yet");
5208 goto theend;
5209 }
5210 }
5211
5212 // Compile the expression. Temporarily hide the new local
5213 // variable here, it is not available to this expression.
5214 if (new_local)
5215 --cctx->ctx_locals.ga_len;
5216 instr_count = instr->ga_len;
5217 p = skipwhite(op + oplen);
5218 r = compile_expr0(&p, cctx);
5219 if (new_local)
5220 ++cctx->ctx_locals.ga_len;
5221 if (r == FAIL)
5222 goto theend;
5223 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005224 else if (semicolon && var_idx == var_count - 1)
5225 {
5226 // For "[var; var] = expr" get the rest of the list
5227 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5228 goto theend;
5229 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005230 else
5231 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005232 // For "[var, var] = expr" get the "var_idx" item from the
5233 // list.
5234 if (generate_GETITEM(cctx, var_idx) == FAIL)
5235 return FAIL;
5236 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005237
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005238 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005239 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005240 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005242 if ((stacktype->tt_type == VAR_FUNC
5243 || stacktype->tt_type == VAR_PARTIAL)
5244 && var_wrong_func_name(name, TRUE))
5245 goto theend;
5246
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005247 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005248 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005249 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005250 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005251 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005252 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253 }
5254 else
5255 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005256 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005257 // for a variable that implies &t_any.
5258 if (stacktype == &t_list_empty)
5259 lvar->lv_type = &t_list_any;
5260 else if (stacktype == &t_dict_empty)
5261 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005262 else if (stacktype == &t_unknown)
5263 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005264 else
5265 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005267 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005268 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005269 {
5270 type_T *use_type = lvar->lv_type;
5271
Bram Moolenaar71abe482020-09-14 22:28:30 +02005272 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005273 if (has_index)
5274 {
5275 use_type = use_type->tt_member;
5276 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005277 // could be indexing "any"
5278 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005279 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005280 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005281 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005282 goto theend;
5283 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005284 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005285 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005286 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005287 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005289 else if (cmdidx == CMD_final)
5290 {
5291 emsg(_(e_final_requires_a_value));
5292 goto theend;
5293 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005296 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 goto theend;
5298 }
5299 else if (!has_type || dest == dest_option)
5300 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005301 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302 goto theend;
5303 }
5304 else
5305 {
5306 // variables are always initialized
5307 if (ga_grow(instr, 1) == FAIL)
5308 goto theend;
5309 switch (member_type->tt_type)
5310 {
5311 case VAR_BOOL:
5312 generate_PUSHBOOL(cctx, VVAL_FALSE);
5313 break;
5314 case VAR_FLOAT:
5315#ifdef FEAT_FLOAT
5316 generate_PUSHF(cctx, 0.0);
5317#endif
5318 break;
5319 case VAR_STRING:
5320 generate_PUSHS(cctx, NULL);
5321 break;
5322 case VAR_BLOB:
5323 generate_PUSHBLOB(cctx, NULL);
5324 break;
5325 case VAR_FUNC:
5326 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5327 break;
5328 case VAR_LIST:
5329 generate_NEWLIST(cctx, 0);
5330 break;
5331 case VAR_DICT:
5332 generate_NEWDICT(cctx, 0);
5333 break;
5334 case VAR_JOB:
5335 generate_PUSHJOB(cctx, NULL);
5336 break;
5337 case VAR_CHANNEL:
5338 generate_PUSHCHANNEL(cctx, NULL);
5339 break;
5340 case VAR_NUMBER:
5341 case VAR_UNKNOWN:
5342 case VAR_ANY:
5343 case VAR_PARTIAL:
5344 case VAR_VOID:
5345 case VAR_SPECIAL: // cannot happen
5346 generate_PUSHNR(cctx, 0);
5347 break;
5348 }
5349 }
5350 if (var_count == 0)
5351 end = p;
5352 }
5353
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005354 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005355 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005356 break;
5357
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 if (oplen > 0 && *op != '=')
5359 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005360 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 type_T *stacktype;
5362
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005363 if (*op == '.')
5364 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005365 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005366 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005367 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005368 if (
5369#ifdef FEAT_FLOAT
5370 // If variable is float operation with number is OK.
5371 !(expected == &t_float && stacktype == &t_number) &&
5372#endif
5373 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374 goto theend;
5375
5376 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005377 {
5378 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5379 goto theend;
5380 }
5381 else if (*op == '+')
5382 {
5383 if (generate_add_instr(cctx,
5384 operator_type(member_type, stacktype),
5385 member_type, stacktype) == FAIL)
5386 goto theend;
5387 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005388 else if (generate_two_op(cctx, op) == FAIL)
5389 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005392 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005393 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005394 int r;
5395
5396 // Compile the "idx" in "var[idx]".
5397 if (new_local)
5398 --cctx->ctx_locals.ga_len;
5399 p = skipwhite(var_start + varlen + 1);
5400 r = compile_expr0(&p, cctx);
5401 if (new_local)
5402 ++cctx->ctx_locals.ga_len;
5403 if (r == FAIL)
5404 goto theend;
5405 if (*skipwhite(p) != ']')
5406 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005407 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 emsg(_(e_missbrac));
5409 goto theend;
5410 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005411 if (type == &t_any)
5412 {
5413 type_T *idx_type = ((type_T **)stack->ga_data)[
5414 stack->ga_len - 1];
5415 // Index on variable of unknown type: guess the type from the
5416 // index type: number is dict, otherwise dict.
5417 // TODO: should do the assignment at runtime
5418 if (idx_type->tt_type == VAR_NUMBER)
5419 type = &t_list_any;
5420 else
5421 type = &t_dict_any;
5422 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 if (type->tt_type == VAR_DICT
5424 && may_generate_2STRING(-1, cctx) == FAIL)
5425 goto theend;
5426 if (type->tt_type == VAR_LIST
5427 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005428 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 {
5430 emsg(_(e_number_exp));
5431 goto theend;
5432 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005433
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005434 // Load the dict or list. On the stack we then have:
5435 // - value
5436 // - index
5437 // - variable
5438 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005439
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005440 if (type->tt_type == VAR_LIST)
5441 {
5442 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5443 return FAIL;
5444 }
5445 else if (type->tt_type == VAR_DICT)
5446 {
5447 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5448 return FAIL;
5449 }
5450 else
5451 {
5452 emsg(_(e_listreq));
5453 goto theend;
5454 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005455 }
5456 else
5457 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005458 if (is_decl && cmdidx == CMD_const
5459 && (dest == dest_script || dest == dest_local))
5460 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005461 generate_LOCKCONST(cctx);
5462
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005463 switch (dest)
5464 {
5465 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005466 generate_STOREOPT(cctx, skip_option_env_lead(name),
5467 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005468 break;
5469 case dest_global:
5470 // include g: with the name, easier to execute that way
5471 generate_STORE(cctx, ISN_STOREG, 0, name);
5472 break;
5473 case dest_buffer:
5474 // include b: with the name, easier to execute that way
5475 generate_STORE(cctx, ISN_STOREB, 0, name);
5476 break;
5477 case dest_window:
5478 // include w: with the name, easier to execute that way
5479 generate_STORE(cctx, ISN_STOREW, 0, name);
5480 break;
5481 case dest_tab:
5482 // include t: with the name, easier to execute that way
5483 generate_STORE(cctx, ISN_STORET, 0, name);
5484 break;
5485 case dest_env:
5486 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5487 break;
5488 case dest_reg:
5489 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5490 break;
5491 case dest_vimvar:
5492 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5493 break;
5494 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005495 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005496 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005497 {
5498 char_u *name_s = name;
5499
5500 // Include s: in the name for store_var()
5501 if (name[1] != ':')
5502 {
5503 int len = (int)STRLEN(name) + 3;
5504
5505 name_s = alloc(len);
5506 if (name_s == NULL)
5507 name_s = name;
5508 else
5509 vim_snprintf((char *)name_s, len,
5510 "s:%s", name);
5511 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005512 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5513 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005514 if (name_s != name)
5515 vim_free(name_s);
5516 }
5517 else
5518 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005519 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005520 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521 break;
5522 case dest_local:
5523 if (lvar != NULL)
5524 {
5525 isn_T *isn = ((isn_T *)instr->ga_data)
5526 + instr->ga_len - 1;
5527
5528 // optimization: turn "var = 123" from ISN_PUSHNR +
5529 // ISN_STORE into ISN_STORENR
5530 if (!lvar->lv_from_outer
5531 && instr->ga_len == instr_count + 1
5532 && isn->isn_type == ISN_PUSHNR)
5533 {
5534 varnumber_T val = isn->isn_arg.number;
5535
5536 isn->isn_type = ISN_STORENR;
5537 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5538 isn->isn_arg.storenr.stnr_val = val;
5539 if (stack->ga_len > 0)
5540 --stack->ga_len;
5541 }
5542 else if (lvar->lv_from_outer)
5543 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005544 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 else
5546 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5547 }
5548 break;
5549 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005550 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551
5552 if (var_idx + 1 < var_count)
5553 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555
5556 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005557 if (var_count > 0 && !semicolon)
5558 {
5559 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5560 goto theend;
5561 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005562
Bram Moolenaarb2097502020-07-19 17:17:02 +02005563 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564
5565theend:
5566 vim_free(name);
5567 return ret;
5568}
5569
5570/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005571 * Check if "name" can be "unlet".
5572 */
5573 int
5574check_vim9_unlet(char_u *name)
5575{
5576 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5577 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005578 // "unlet s:var" is allowed in legacy script.
5579 if (*name == 's' && !script_is_vim9())
5580 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005581 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005582 return FAIL;
5583 }
5584 return OK;
5585}
5586
5587/*
5588 * Callback passed to ex_unletlock().
5589 */
5590 static int
5591compile_unlet(
5592 lval_T *lvp,
5593 char_u *name_end,
5594 exarg_T *eap,
5595 int deep UNUSED,
5596 void *coookie)
5597{
5598 cctx_T *cctx = coookie;
5599
5600 if (lvp->ll_tv == NULL)
5601 {
5602 char_u *p = lvp->ll_name;
5603 int cc = *name_end;
5604 int ret = OK;
5605
5606 // Normal name. Only supports g:, w:, t: and b: namespaces.
5607 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005608 if (*p == '$')
5609 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5610 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005611 ret = FAIL;
5612 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005613 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005614
5615 *name_end = cc;
5616 return ret;
5617 }
5618
5619 // TODO: unlet {list}[idx]
5620 // TODO: unlet {dict}[key]
5621 emsg("Sorry, :unlet not fully implemented yet");
5622 return FAIL;
5623}
5624
5625/*
5626 * compile "unlet var", "lock var" and "unlock var"
5627 * "arg" points to "var".
5628 */
5629 static char_u *
5630compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5631{
5632 char_u *p = arg;
5633
5634 if (eap->cmdidx != CMD_unlet)
5635 {
5636 emsg("Sorry, :lock and unlock not implemented yet");
5637 return NULL;
5638 }
5639
5640 if (*p == '!')
5641 {
5642 p = skipwhite(p + 1);
5643 eap->forceit = TRUE;
5644 }
5645
5646 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5647 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5648}
5649
5650/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651 * Compile an :import command.
5652 */
5653 static char_u *
5654compile_import(char_u *arg, cctx_T *cctx)
5655{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005656 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657}
5658
5659/*
5660 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5661 */
5662 static int
5663compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5664{
5665 garray_T *instr = &cctx->ctx_instr;
5666 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5667
5668 if (endlabel == NULL)
5669 return FAIL;
5670 endlabel->el_next = *el;
5671 *el = endlabel;
5672 endlabel->el_end_label = instr->ga_len;
5673
5674 generate_JUMP(cctx, when, 0);
5675 return OK;
5676}
5677
5678 static void
5679compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5680{
5681 garray_T *instr = &cctx->ctx_instr;
5682
5683 while (*el != NULL)
5684 {
5685 endlabel_T *cur = (*el);
5686 isn_T *isn;
5687
5688 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5689 isn->isn_arg.jump.jump_where = instr->ga_len;
5690 *el = cur->el_next;
5691 vim_free(cur);
5692 }
5693}
5694
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005695 static void
5696compile_free_jump_to_end(endlabel_T **el)
5697{
5698 while (*el != NULL)
5699 {
5700 endlabel_T *cur = (*el);
5701
5702 *el = cur->el_next;
5703 vim_free(cur);
5704 }
5705}
5706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707/*
5708 * Create a new scope and set up the generic items.
5709 */
5710 static scope_T *
5711new_scope(cctx_T *cctx, scopetype_T type)
5712{
5713 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5714
5715 if (scope == NULL)
5716 return NULL;
5717 scope->se_outer = cctx->ctx_scope;
5718 cctx->ctx_scope = scope;
5719 scope->se_type = type;
5720 scope->se_local_count = cctx->ctx_locals.ga_len;
5721 return scope;
5722}
5723
5724/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005725 * Free the current scope and go back to the outer scope.
5726 */
5727 static void
5728drop_scope(cctx_T *cctx)
5729{
5730 scope_T *scope = cctx->ctx_scope;
5731
5732 if (scope == NULL)
5733 {
5734 iemsg("calling drop_scope() without a scope");
5735 return;
5736 }
5737 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005738 switch (scope->se_type)
5739 {
5740 case IF_SCOPE:
5741 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5742 case FOR_SCOPE:
5743 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5744 case WHILE_SCOPE:
5745 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5746 case TRY_SCOPE:
5747 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5748 case NO_SCOPE:
5749 case BLOCK_SCOPE:
5750 break;
5751 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005752 vim_free(scope);
5753}
5754
5755/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005756 * Check that the top of the type stack has a type that can be used as a
5757 * condition. Give an error and return FAIL if not.
5758 */
5759 static int
5760bool_on_stack(cctx_T *cctx)
5761{
5762 garray_T *stack = &cctx->ctx_type_stack;
5763 type_T *type;
5764
5765 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5766 if (type != &t_bool && type != &t_number && type != &t_any
5767 && need_type(type, &t_bool, -1, cctx, FALSE) == FAIL)
5768 return FAIL;
5769 return OK;
5770}
5771
5772/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 * compile "if expr"
5774 *
5775 * "if expr" Produces instructions:
5776 * EVAL expr Push result of "expr"
5777 * JUMP_IF_FALSE end
5778 * ... body ...
5779 * end:
5780 *
5781 * "if expr | else" Produces instructions:
5782 * EVAL expr Push result of "expr"
5783 * JUMP_IF_FALSE else
5784 * ... body ...
5785 * JUMP_ALWAYS end
5786 * else:
5787 * ... body ...
5788 * end:
5789 *
5790 * "if expr1 | elseif expr2 | else" Produces instructions:
5791 * EVAL expr Push result of "expr"
5792 * JUMP_IF_FALSE elseif
5793 * ... body ...
5794 * JUMP_ALWAYS end
5795 * elseif:
5796 * EVAL expr Push result of "expr"
5797 * JUMP_IF_FALSE else
5798 * ... body ...
5799 * JUMP_ALWAYS end
5800 * else:
5801 * ... body ...
5802 * end:
5803 */
5804 static char_u *
5805compile_if(char_u *arg, cctx_T *cctx)
5806{
5807 char_u *p = arg;
5808 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005809 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005811 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005812 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813
Bram Moolenaara5565e42020-05-09 15:44:01 +02005814 CLEAR_FIELD(ppconst);
5815 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005816 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005817 clear_ppconst(&ppconst);
5818 return NULL;
5819 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005820 if (cctx->ctx_skip == SKIP_YES)
5821 clear_ppconst(&ppconst);
5822 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005823 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005824 int error = FALSE;
5825 int v;
5826
Bram Moolenaara5565e42020-05-09 15:44:01 +02005827 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005828 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005829 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005830 if (error)
5831 return NULL;
5832 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005833 }
5834 else
5835 {
5836 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005837 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005838 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005839 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005840 if (bool_on_stack(cctx) == FAIL)
5841 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005843
5844 scope = new_scope(cctx, IF_SCOPE);
5845 if (scope == NULL)
5846 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005847 scope->se_skip_save = skip_save;
5848 // "is_had_return" will be reset if any block does not end in :return
5849 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005851 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005852 {
5853 // "where" is set when ":elseif", "else" or ":endif" is found
5854 scope->se_u.se_if.is_if_label = instr->ga_len;
5855 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5856 }
5857 else
5858 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859
5860 return p;
5861}
5862
5863 static char_u *
5864compile_elseif(char_u *arg, cctx_T *cctx)
5865{
5866 char_u *p = arg;
5867 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005868 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 isn_T *isn;
5870 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005871 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005872 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873
5874 if (scope == NULL || scope->se_type != IF_SCOPE)
5875 {
5876 emsg(_(e_elseif_without_if));
5877 return NULL;
5878 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005879 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005880 if (!cctx->ctx_had_return)
5881 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005882
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005883 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005884 {
5885 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005887 return NULL;
5888 // previous "if" or "elseif" jumps here
5889 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5890 isn->isn_arg.jump.jump_where = instr->ga_len;
5891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005893 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005894 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005895 if (cctx->ctx_skip == SKIP_YES)
5896 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005897 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005898 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005899 clear_ppconst(&ppconst);
5900 return NULL;
5901 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005902 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005903 if (scope->se_skip_save == SKIP_YES)
5904 clear_ppconst(&ppconst);
5905 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005906 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005907 int error = FALSE;
5908 int v;
5909
Bram Moolenaar7f141552020-05-09 17:35:53 +02005910 // The expression results in a constant.
5911 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02005912 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5913 if (error)
5914 return NULL;
5915 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005916 clear_ppconst(&ppconst);
5917 scope->se_u.se_if.is_if_label = -1;
5918 }
5919 else
5920 {
5921 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005922 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005923 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005924 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005925 if (bool_on_stack(cctx) == FAIL)
5926 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005928 // "where" is set when ":elseif", "else" or ":endif" is found
5929 scope->se_u.se_if.is_if_label = instr->ga_len;
5930 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932
5933 return p;
5934}
5935
5936 static char_u *
5937compile_else(char_u *arg, cctx_T *cctx)
5938{
5939 char_u *p = arg;
5940 garray_T *instr = &cctx->ctx_instr;
5941 isn_T *isn;
5942 scope_T *scope = cctx->ctx_scope;
5943
5944 if (scope == NULL || scope->se_type != IF_SCOPE)
5945 {
5946 emsg(_(e_else_without_if));
5947 return NULL;
5948 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005949 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005950 if (!cctx->ctx_had_return)
5951 scope->se_u.se_if.is_had_return = FALSE;
5952 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953
Bram Moolenaarefd88552020-06-18 20:50:10 +02005954 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005955 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005956 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005957 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005958 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005959 if (!cctx->ctx_had_return
5960 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5961 JUMP_ALWAYS, cctx) == FAIL)
5962 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005963 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005964
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005965 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005966 {
5967 if (scope->se_u.se_if.is_if_label >= 0)
5968 {
5969 // previous "if" or "elseif" jumps here
5970 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5971 isn->isn_arg.jump.jump_where = instr->ga_len;
5972 scope->se_u.se_if.is_if_label = -1;
5973 }
5974 }
5975
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005976 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005977 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979
5980 return p;
5981}
5982
5983 static char_u *
5984compile_endif(char_u *arg, cctx_T *cctx)
5985{
5986 scope_T *scope = cctx->ctx_scope;
5987 ifscope_T *ifscope;
5988 garray_T *instr = &cctx->ctx_instr;
5989 isn_T *isn;
5990
5991 if (scope == NULL || scope->se_type != IF_SCOPE)
5992 {
5993 emsg(_(e_endif_without_if));
5994 return NULL;
5995 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005996 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005997 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005998 if (!cctx->ctx_had_return)
5999 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006001 if (scope->se_u.se_if.is_if_label >= 0)
6002 {
6003 // previous "if" or "elseif" jumps here
6004 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6005 isn->isn_arg.jump.jump_where = instr->ga_len;
6006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 // Fill in the "end" label in jumps at the end of the blocks.
6008 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006009 cctx->ctx_skip = scope->se_skip_save;
6010
6011 // If all the blocks end in :return and there is an :else then the
6012 // had_return flag is set.
6013 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006014
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006015 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 return arg;
6017}
6018
6019/*
6020 * compile "for var in expr"
6021 *
6022 * Produces instructions:
6023 * PUSHNR -1
6024 * STORE loop-idx Set index to -1
6025 * EVAL expr Push result of "expr"
6026 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6027 * - if beyond end, jump to "end"
6028 * - otherwise get item from list and push it
6029 * STORE var Store item in "var"
6030 * ... body ...
6031 * JUMP top Jump back to repeat
6032 * end: DROP Drop the result of "expr"
6033 *
6034 */
6035 static char_u *
6036compile_for(char_u *arg, cctx_T *cctx)
6037{
6038 char_u *p;
6039 size_t varlen;
6040 garray_T *instr = &cctx->ctx_instr;
6041 garray_T *stack = &cctx->ctx_type_stack;
6042 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006043 lvar_T *loop_lvar; // loop iteration variable
6044 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 type_T *vartype;
6046
6047 // TODO: list of variables: "for [key, value] in dict"
6048 // parse "var"
6049 for (p = arg; eval_isnamec1(*p); ++p)
6050 ;
6051 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006052 var_lvar = lookup_local(arg, varlen, cctx);
6053 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006055 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 return NULL;
6057 }
6058
6059 // consume "in"
6060 p = skipwhite(p);
6061 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6062 {
6063 emsg(_(e_missing_in));
6064 return NULL;
6065 }
6066 p = skipwhite(p + 2);
6067
6068
6069 scope = new_scope(cctx, FOR_SCOPE);
6070 if (scope == NULL)
6071 return NULL;
6072
6073 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006074 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6075 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006076 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006077 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006078 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006079 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081
6082 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006083 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6084 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006085 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006086 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006087 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006091 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092
6093 // compile "expr", it remains on the stack until "endfor"
6094 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006095 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006096 {
6097 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006101 // Now that we know the type of "var", check that it is a list, now or at
6102 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006104 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006106 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 return NULL;
6108 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006109 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006110 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111
6112 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006113 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006115 generate_FOR(cctx, loop_lvar->lv_idx);
6116 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117
6118 return arg;
6119}
6120
6121/*
6122 * compile "endfor"
6123 */
6124 static char_u *
6125compile_endfor(char_u *arg, cctx_T *cctx)
6126{
6127 garray_T *instr = &cctx->ctx_instr;
6128 scope_T *scope = cctx->ctx_scope;
6129 forscope_T *forscope;
6130 isn_T *isn;
6131
6132 if (scope == NULL || scope->se_type != FOR_SCOPE)
6133 {
6134 emsg(_(e_for));
6135 return NULL;
6136 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006137 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006139 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140
6141 // At end of ":for" scope jump back to the FOR instruction.
6142 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6143
6144 // Fill in the "end" label in the FOR statement so it can jump here
6145 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6146 isn->isn_arg.forloop.for_end = instr->ga_len;
6147
6148 // Fill in the "end" label any BREAK statements
6149 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6150
6151 // Below the ":for" scope drop the "expr" list from the stack.
6152 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6153 return NULL;
6154
6155 vim_free(scope);
6156
6157 return arg;
6158}
6159
6160/*
6161 * compile "while expr"
6162 *
6163 * Produces instructions:
6164 * top: EVAL expr Push result of "expr"
6165 * JUMP_IF_FALSE end jump if false
6166 * ... body ...
6167 * JUMP top Jump back to repeat
6168 * end:
6169 *
6170 */
6171 static char_u *
6172compile_while(char_u *arg, cctx_T *cctx)
6173{
6174 char_u *p = arg;
6175 garray_T *instr = &cctx->ctx_instr;
6176 scope_T *scope;
6177
6178 scope = new_scope(cctx, WHILE_SCOPE);
6179 if (scope == NULL)
6180 return NULL;
6181
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006182 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183
6184 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006185 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 return NULL;
6187
Bram Moolenaar13106602020-10-04 16:06:05 +02006188 if (bool_on_stack(cctx) == FAIL)
6189 return FAIL;
6190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006192 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 JUMP_IF_FALSE, cctx) == FAIL)
6194 return FAIL;
6195
6196 return p;
6197}
6198
6199/*
6200 * compile "endwhile"
6201 */
6202 static char_u *
6203compile_endwhile(char_u *arg, cctx_T *cctx)
6204{
6205 scope_T *scope = cctx->ctx_scope;
6206
6207 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6208 {
6209 emsg(_(e_while));
6210 return NULL;
6211 }
6212 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006213 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214
6215 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006216 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217
6218 // Fill in the "end" label in the WHILE statement so it can jump here.
6219 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006220 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221
6222 vim_free(scope);
6223
6224 return arg;
6225}
6226
6227/*
6228 * compile "continue"
6229 */
6230 static char_u *
6231compile_continue(char_u *arg, cctx_T *cctx)
6232{
6233 scope_T *scope = cctx->ctx_scope;
6234
6235 for (;;)
6236 {
6237 if (scope == NULL)
6238 {
6239 emsg(_(e_continue));
6240 return NULL;
6241 }
6242 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6243 break;
6244 scope = scope->se_outer;
6245 }
6246
6247 // Jump back to the FOR or WHILE instruction.
6248 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006249 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6250 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 return arg;
6252}
6253
6254/*
6255 * compile "break"
6256 */
6257 static char_u *
6258compile_break(char_u *arg, cctx_T *cctx)
6259{
6260 scope_T *scope = cctx->ctx_scope;
6261 endlabel_T **el;
6262
6263 for (;;)
6264 {
6265 if (scope == NULL)
6266 {
6267 emsg(_(e_break));
6268 return NULL;
6269 }
6270 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6271 break;
6272 scope = scope->se_outer;
6273 }
6274
6275 // Jump to the end of the FOR or WHILE loop.
6276 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006277 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006279 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6281 return FAIL;
6282
6283 return arg;
6284}
6285
6286/*
6287 * compile "{" start of block
6288 */
6289 static char_u *
6290compile_block(char_u *arg, cctx_T *cctx)
6291{
6292 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6293 return NULL;
6294 return skipwhite(arg + 1);
6295}
6296
6297/*
6298 * compile end of block: drop one scope
6299 */
6300 static void
6301compile_endblock(cctx_T *cctx)
6302{
6303 scope_T *scope = cctx->ctx_scope;
6304
6305 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006306 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 vim_free(scope);
6308}
6309
6310/*
6311 * compile "try"
6312 * Creates a new scope for the try-endtry, pointing to the first catch and
6313 * finally.
6314 * Creates another scope for the "try" block itself.
6315 * TRY instruction sets up exception handling at runtime.
6316 *
6317 * "try"
6318 * TRY -> catch1, -> finally push trystack entry
6319 * ... try block
6320 * "throw {exception}"
6321 * EVAL {exception}
6322 * THROW create exception
6323 * ... try block
6324 * " catch {expr}"
6325 * JUMP -> finally
6326 * catch1: PUSH exeception
6327 * EVAL {expr}
6328 * MATCH
6329 * JUMP nomatch -> catch2
6330 * CATCH remove exception
6331 * ... catch block
6332 * " catch"
6333 * JUMP -> finally
6334 * catch2: CATCH remove exception
6335 * ... catch block
6336 * " finally"
6337 * finally:
6338 * ... finally block
6339 * " endtry"
6340 * ENDTRY pop trystack entry, may rethrow
6341 */
6342 static char_u *
6343compile_try(char_u *arg, cctx_T *cctx)
6344{
6345 garray_T *instr = &cctx->ctx_instr;
6346 scope_T *try_scope;
6347 scope_T *scope;
6348
6349 // scope that holds the jumps that go to catch/finally/endtry
6350 try_scope = new_scope(cctx, TRY_SCOPE);
6351 if (try_scope == NULL)
6352 return NULL;
6353
6354 // "catch" is set when the first ":catch" is found.
6355 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006356 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357 if (generate_instr(cctx, ISN_TRY) == NULL)
6358 return NULL;
6359
6360 // scope for the try block itself
6361 scope = new_scope(cctx, BLOCK_SCOPE);
6362 if (scope == NULL)
6363 return NULL;
6364
6365 return arg;
6366}
6367
6368/*
6369 * compile "catch {expr}"
6370 */
6371 static char_u *
6372compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6373{
6374 scope_T *scope = cctx->ctx_scope;
6375 garray_T *instr = &cctx->ctx_instr;
6376 char_u *p;
6377 isn_T *isn;
6378
6379 // end block scope from :try or :catch
6380 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6381 compile_endblock(cctx);
6382 scope = cctx->ctx_scope;
6383
6384 // Error if not in a :try scope
6385 if (scope == NULL || scope->se_type != TRY_SCOPE)
6386 {
6387 emsg(_(e_catch));
6388 return NULL;
6389 }
6390
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006391 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006393 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 return NULL;
6395 }
6396
6397 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006398 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 JUMP_ALWAYS, cctx) == FAIL)
6400 return NULL;
6401
6402 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006403 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 if (isn->isn_arg.try.try_catch == 0)
6405 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006406 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 {
6408 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006409 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 isn->isn_arg.jump.jump_where = instr->ga_len;
6411 }
6412
6413 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006414 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006416 scope->se_u.se_try.ts_caught_all = TRUE;
6417 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 }
6419 else
6420 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006421 char_u *end;
6422 char_u *pat;
6423 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006424 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006425 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 // Push v:exception, push {expr} and MATCH
6428 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6429
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006430 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006431 if (*end != *p)
6432 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006433 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006434 vim_free(tofree);
6435 return FAIL;
6436 }
6437 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006438 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006439 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006440 len = (int)(end - tofree);
6441 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006442 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006443 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006444 if (pat == NULL)
6445 return FAIL;
6446 if (generate_PUSHS(cctx, pat) == FAIL)
6447 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6450 return NULL;
6451
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006452 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6454 return NULL;
6455 }
6456
6457 if (generate_instr(cctx, ISN_CATCH) == NULL)
6458 return NULL;
6459
6460 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6461 return NULL;
6462 return p;
6463}
6464
6465 static char_u *
6466compile_finally(char_u *arg, cctx_T *cctx)
6467{
6468 scope_T *scope = cctx->ctx_scope;
6469 garray_T *instr = &cctx->ctx_instr;
6470 isn_T *isn;
6471
6472 // end block scope from :try or :catch
6473 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6474 compile_endblock(cctx);
6475 scope = cctx->ctx_scope;
6476
6477 // Error if not in a :try scope
6478 if (scope == NULL || scope->se_type != TRY_SCOPE)
6479 {
6480 emsg(_(e_finally));
6481 return NULL;
6482 }
6483
6484 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006485 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 if (isn->isn_arg.try.try_finally != 0)
6487 {
6488 emsg(_(e_finally_dup));
6489 return NULL;
6490 }
6491
6492 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006493 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494
Bram Moolenaar585fea72020-04-02 22:33:21 +02006495 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006496 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 {
6498 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006499 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006500 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006501 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006502 }
6503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 // TODO: set index in ts_finally_label jumps
6505
6506 return arg;
6507}
6508
6509 static char_u *
6510compile_endtry(char_u *arg, cctx_T *cctx)
6511{
6512 scope_T *scope = cctx->ctx_scope;
6513 garray_T *instr = &cctx->ctx_instr;
6514 isn_T *isn;
6515
6516 // end block scope from :catch or :finally
6517 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6518 compile_endblock(cctx);
6519 scope = cctx->ctx_scope;
6520
6521 // Error if not in a :try scope
6522 if (scope == NULL || scope->se_type != TRY_SCOPE)
6523 {
6524 if (scope == NULL)
6525 emsg(_(e_no_endtry));
6526 else if (scope->se_type == WHILE_SCOPE)
6527 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006528 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 emsg(_(e_endfor));
6530 else
6531 emsg(_(e_endif));
6532 return NULL;
6533 }
6534
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006535 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6537 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006538 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 return NULL;
6540 }
6541
6542 // Fill in the "end" label in jumps at the end of the blocks, if not done
6543 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006544 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545
6546 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006547 if (isn->isn_arg.try.try_catch == 0)
6548 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 if (isn->isn_arg.try.try_finally == 0)
6550 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006551
6552 if (scope->se_u.se_try.ts_catch_label != 0)
6553 {
6554 // Last catch without match jumps here
6555 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6556 isn->isn_arg.jump.jump_where = instr->ga_len;
6557 }
6558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 compile_endblock(cctx);
6560
6561 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6562 return NULL;
6563 return arg;
6564}
6565
6566/*
6567 * compile "throw {expr}"
6568 */
6569 static char_u *
6570compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6571{
6572 char_u *p = skipwhite(arg);
6573
Bram Moolenaara5565e42020-05-09 15:44:01 +02006574 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 return NULL;
6576 if (may_generate_2STRING(-1, cctx) == FAIL)
6577 return NULL;
6578 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6579 return NULL;
6580
6581 return p;
6582}
6583
6584/*
6585 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006586 * compile "echomsg expr"
6587 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006588 * compile "execute expr"
6589 */
6590 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006591compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006592{
6593 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006594 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006595 int count = 0;
6596
6597 for (;;)
6598 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006599 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006600 return NULL;
6601 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006602 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006603 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006604 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006605 break;
6606 }
6607
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006608 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6609 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6610 else if (cmdidx == CMD_execute)
6611 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6612 else if (cmdidx == CMD_echomsg)
6613 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6614 else
6615 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 return p;
6617}
6618
6619/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006620 * :put r
6621 * :put ={expr}
6622 */
6623 static char_u *
6624compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6625{
6626 char_u *line = arg;
6627 linenr_T lnum;
6628 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006629 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006630
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006631 eap->regname = *line;
6632
6633 if (eap->regname == '=')
6634 {
6635 char_u *p = line + 1;
6636
6637 if (compile_expr0(&p, cctx) == FAIL)
6638 return NULL;
6639 line = p;
6640 }
6641 else if (eap->regname != NUL)
6642 ++line;
6643
6644 // TODO: if the range is something like "$" need to evaluate at runtime
6645 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6646 return NULL;
6647 if (eap->addr_count == 0)
6648 lnum = -1;
6649 else
6650 lnum = eap->line2;
6651 if (above)
6652 --lnum;
6653
6654 generate_PUT(cctx, eap->regname, lnum);
6655 return line;
6656}
6657
6658/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006659 * A command that is not compiled, execute with legacy code.
6660 */
6661 static char_u *
6662compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6663{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006664 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006665 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006666 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006667
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006668 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006669 goto theend;
6670
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006671 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006672 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006673 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006674 int usefilter = FALSE;
6675
6676 has_expr = argt & (EX_XFILE | EX_EXPAND);
6677
6678 // If the command can be followed by a bar, find the bar and truncate
6679 // it, so that the following command can be compiled.
6680 // The '|' is overwritten with a NUL, it is put back below.
6681 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6682 && *eap->arg == '!')
6683 // :w !filter or :r !filter or :r! filter
6684 usefilter = TRUE;
6685 if ((argt & EX_TRLBAR) && !usefilter)
6686 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006687 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006688 separate_nextcmd(eap);
6689 if (eap->nextcmd != NULL)
6690 nextcmd = eap->nextcmd;
6691 }
6692 }
6693
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006694 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6695 {
6696 // expand filename in "syntax include [@group] filename"
6697 has_expr = TRUE;
6698 eap->arg = skipwhite(eap->arg + 7);
6699 if (*eap->arg == '@')
6700 eap->arg = skiptowhite(eap->arg);
6701 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006702
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006703 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006704 {
6705 int count = 0;
6706 char_u *start = skipwhite(line);
6707
6708 // :cmd xxx`=expr1`yyy`=expr2`zzz
6709 // PUSHS ":cmd xxx"
6710 // eval expr1
6711 // PUSHS "yyy"
6712 // eval expr2
6713 // PUSHS "zzz"
6714 // EXECCONCAT 5
6715 for (;;)
6716 {
6717 if (p > start)
6718 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006719 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006720 ++count;
6721 }
6722 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006723 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006724 return NULL;
6725 may_generate_2STRING(-1, cctx);
6726 ++count;
6727 p = skipwhite(p);
6728 if (*p != '`')
6729 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006730 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006731 return NULL;
6732 }
6733 start = p + 1;
6734
6735 p = (char_u *)strstr((char *)start, "`=");
6736 if (p == NULL)
6737 {
6738 if (*skipwhite(start) != NUL)
6739 {
6740 generate_PUSHS(cctx, vim_strsave(start));
6741 ++count;
6742 }
6743 break;
6744 }
6745 }
6746 generate_EXECCONCAT(cctx, count);
6747 }
6748 else
6749 generate_EXEC(cctx, line);
6750
6751theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006752 if (*nextcmd != NUL)
6753 {
6754 // the parser expects a pointer to the bar, put it back
6755 --nextcmd;
6756 *nextcmd = '|';
6757 }
6758
6759 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006760}
6761
6762/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006763 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006764 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006765 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006766 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006767add_def_function(ufunc_T *ufunc)
6768{
6769 dfunc_T *dfunc;
6770
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006771 if (def_functions.ga_len == 0)
6772 {
6773 // The first position is not used, so that a zero uf_dfunc_idx means it
6774 // wasn't set.
6775 if (ga_grow(&def_functions, 1) == FAIL)
6776 return FAIL;
6777 ++def_functions.ga_len;
6778 }
6779
Bram Moolenaar09689a02020-05-09 22:50:08 +02006780 // Add the function to "def_functions".
6781 if (ga_grow(&def_functions, 1) == FAIL)
6782 return FAIL;
6783 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6784 CLEAR_POINTER(dfunc);
6785 dfunc->df_idx = def_functions.ga_len;
6786 ufunc->uf_dfunc_idx = dfunc->df_idx;
6787 dfunc->df_ufunc = ufunc;
6788 ++def_functions.ga_len;
6789 return OK;
6790}
6791
6792/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 * After ex_function() has collected all the function lines: parse and compile
6794 * the lines into instructions.
6795 * Adds the function to "def_functions".
6796 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6797 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006798 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006799 * This can be used recursively through compile_lambda(), which may reallocate
6800 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006801 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006803 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006804compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 char_u *line = NULL;
6807 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 cctx_T cctx;
6810 garray_T *instr;
6811 int called_emsg_before = called_emsg;
6812 int ret = FAIL;
6813 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006814 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006815 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006816 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006817 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006819 // When using a function that was compiled before: Free old instructions.
6820 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006821 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006823 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6824 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006825 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006827 else
6828 {
6829 if (add_def_function(ufunc) == FAIL)
6830 return FAIL;
6831 new_def_function = TRUE;
6832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833
Bram Moolenaar985116a2020-07-12 17:31:09 +02006834 ufunc->uf_def_status = UF_COMPILING;
6835
Bram Moolenaara80faa82020-04-12 19:37:17 +02006836 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837 cctx.ctx_ufunc = ufunc;
6838 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006839 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6841 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6842 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6843 cctx.ctx_type_list = &ufunc->uf_type_list;
6844 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6845 instr = &cctx.ctx_instr;
6846
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006847 // Set the context to the function, it may be compiled when called from
6848 // another script. Set the script version to the most modern one.
6849 // The line number will be set in next_line_from_context().
6850 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006851 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6852
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006853 // Make sure error messages are OK.
6854 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6855 if (do_estack_push)
6856 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006857 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006858
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006859 if (ufunc->uf_def_args.ga_len > 0)
6860 {
6861 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006862 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006863 int i;
6864 char_u *arg;
6865 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006866 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006867
6868 // Produce instructions for the default values of optional arguments.
6869 // Store the instruction index in uf_def_arg_idx[] so that we know
6870 // where to start when the function is called, depending on the number
6871 // of arguments.
6872 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6873 if (ufunc->uf_def_arg_idx == NULL)
6874 goto erret;
6875 for (i = 0; i < count; ++i)
6876 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006877 garray_T *stack = &cctx.ctx_type_stack;
6878 type_T *val_type;
6879 int arg_idx = first_def_arg + i;
6880
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006881 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6882 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006883 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006884 goto erret;
6885
6886 // If no type specified use the type of the default value.
6887 // Otherwise check that the default value type matches the
6888 // specified type.
6889 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6890 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006891 {
6892 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006893 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006894 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006895 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6896 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006897 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006898
6899 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006900 goto erret;
6901 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006902 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006903
6904 if (did_set_arg_type)
6905 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006906 }
6907
6908 /*
6909 * Loop over all the lines of the function and generate instructions.
6910 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 for (;;)
6912 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006913 exarg_T ea;
6914 cmdmod_T save_cmdmod;
6915 int starts_with_colon = FALSE;
6916 char_u *cmd;
6917 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006918
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006919 // Bail out on the first error to avoid a flood of errors and report
6920 // the right line number when inside try/catch.
6921 if (emsg_before != called_emsg)
6922 goto erret;
6923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 if (line != NULL && *line == '|')
6925 // the line continues after a '|'
6926 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006927 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006928 && !(*line == '#' && (line == cctx.ctx_line_start
6929 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006931 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 goto erret;
6933 }
6934 else
6935 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006936 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006937 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006938 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006941 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
Bram Moolenaara80faa82020-04-12 19:37:17 +02006943 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 ea.cmdlinep = &line;
6945 ea.cmd = skipwhite(line);
6946
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006947 // Some things can be recognized by the first character.
6948 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006950 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006951 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006952 if (ea.cmd[1] != '{')
6953 {
6954 line = (char_u *)"";
6955 continue;
6956 }
6957 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006959 case '}':
6960 {
6961 // "}" ends a block scope
6962 scopetype_T stype = cctx.ctx_scope == NULL
6963 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006965 if (stype == BLOCK_SCOPE)
6966 {
6967 compile_endblock(&cctx);
6968 line = ea.cmd;
6969 }
6970 else
6971 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006972 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006973 goto erret;
6974 }
6975 if (line != NULL)
6976 line = skipwhite(ea.cmd + 1);
6977 continue;
6978 }
6979
6980 case '{':
6981 // "{" starts a block scope
6982 // "{'a': 1}->func() is something else
6983 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6984 {
6985 line = compile_block(ea.cmd, &cctx);
6986 continue;
6987 }
6988 break;
6989
6990 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006991 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006992 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 }
6994
6995 /*
6996 * COMMAND MODIFIERS
6997 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006998 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7000 {
7001 if (errormsg != NULL)
7002 goto erret;
7003 // empty line or comment
7004 line = (char_u *)"";
7005 continue;
7006 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007007 // TODO: use modifiers in the command
7008 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007009 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010
7011 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007012 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007014 {
7015 if (*ea.cmd == '(')
7016 // not for "call()"
7017 ea.cmd = p;
7018 else
7019 ea.cmd = skipwhite(ea.cmd);
7020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007022 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007024 char_u *pskip;
7025
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007026 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007027 // find what follows.
7028 // Skip over "var.member", "var[idx]" and the like.
7029 // Also "&opt = val", "$ENV = val" and "@r = val".
7030 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007031 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007032 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007033 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007035 char_u *var_end;
7036 int oplen;
7037 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038
Bram Moolenaar65821722020-08-02 18:58:54 +02007039 if (ea.cmd[0] == '@')
7040 var_end = ea.cmd + 2;
7041 else
7042 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007043 FNE_CHECK_START | FNE_INCL_BR);
7044 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007045 if (oplen > 0)
7046 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007047 size_t len = p - ea.cmd;
7048
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007049 // Recognize an assignment if we recognize the variable
7050 // name:
7051 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007052 // "local = expr" where "local" is a local var.
7053 // "script = expr" where "script" is a script-local var.
7054 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007055 // "&opt = expr"
7056 // "$ENV = expr"
7057 // "@r = expr"
7058 if (*ea.cmd == '&'
7059 || *ea.cmd == '$'
7060 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007061 || ((len) > 2 && ea.cmd[1] == ':')
7062 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007063 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007064 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007065 || script_var_exists(ea.cmd, len,
7066 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007067 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007068 {
7069 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007070 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007071 goto erret;
7072 continue;
7073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 }
7075 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007076
7077 if (*ea.cmd == '[')
7078 {
7079 // [var, var] = expr
7080 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7081 if (line == NULL)
7082 goto erret;
7083 if (line != ea.cmd)
7084 continue;
7085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086 }
7087
7088 /*
7089 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007090 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007092 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007093 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007094 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007095 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007096 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007097 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007098 if (!starts_with_colon)
7099 {
7100 emsg(_(e_colon_required_before_a_range));
7101 goto erret;
7102 }
7103 if (ends_excmd2(line, ea.cmd))
7104 {
7105 // A range without a command: jump to the line.
7106 // TODO: compile to a more efficient command, possibly
7107 // calling parse_cmd_address().
7108 ea.cmdidx = CMD_SIZE;
7109 line = compile_exec(line, &ea, &cctx);
7110 goto nextline;
7111 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007112 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007113 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007114 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007115 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007116 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117
7118 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7119 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007120 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007121 {
7122 line += STRLEN(line);
7123 continue;
7124 }
7125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007127 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007129 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007130 iemsg("Command from find_ex_command() not handled");
7131 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 }
7134
Bram Moolenaar3988f642020-08-27 22:43:03 +02007135 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007136 && ea.cmdidx != CMD_elseif
7137 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007138 && ea.cmdidx != CMD_endif
7139 && ea.cmdidx != CMD_endfor
7140 && ea.cmdidx != CMD_endwhile
7141 && ea.cmdidx != CMD_catch
7142 && ea.cmdidx != CMD_finally
7143 && ea.cmdidx != CMD_endtry)
7144 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007145 emsg(_(e_unreachable_code_after_return));
7146 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007147 }
7148
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007149 p = skipwhite(p);
7150 if (ea.cmdidx != CMD_SIZE
7151 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7152 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007153 if (ea.cmdidx >= 0)
7154 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007155 if ((ea.argt & EX_BANG) && *p == '!')
7156 {
7157 ea.forceit = TRUE;
7158 p = skipwhite(p + 1);
7159 }
7160 }
7161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 switch (ea.cmdidx)
7163 {
7164 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007165 ea.arg = p;
7166 line = compile_nested_function(&ea, &cctx);
7167 break;
7168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007170 // TODO: should we allow this, e.g. to declare a global
7171 // function?
7172 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 goto erret;
7174
7175 case CMD_return:
7176 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007177 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 break;
7179
7180 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007181 if (get_vim_var_nr(VV_DISALLOW_LET))
7182 {
7183 emsg(_(e_cannot_use_let_in_vim9_script));
7184 break;
7185 }
7186 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007187 case CMD_var:
7188 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 case CMD_const:
7190 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007191 if (line == p)
7192 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193 break;
7194
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007195 case CMD_unlet:
7196 case CMD_unlockvar:
7197 case CMD_lockvar:
7198 line = compile_unletlock(p, &ea, &cctx);
7199 break;
7200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 case CMD_import:
7202 line = compile_import(p, &cctx);
7203 break;
7204
7205 case CMD_if:
7206 line = compile_if(p, &cctx);
7207 break;
7208 case CMD_elseif:
7209 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007210 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 break;
7212 case CMD_else:
7213 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007214 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 break;
7216 case CMD_endif:
7217 line = compile_endif(p, &cctx);
7218 break;
7219
7220 case CMD_while:
7221 line = compile_while(p, &cctx);
7222 break;
7223 case CMD_endwhile:
7224 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007225 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226 break;
7227
7228 case CMD_for:
7229 line = compile_for(p, &cctx);
7230 break;
7231 case CMD_endfor:
7232 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007233 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 break;
7235 case CMD_continue:
7236 line = compile_continue(p, &cctx);
7237 break;
7238 case CMD_break:
7239 line = compile_break(p, &cctx);
7240 break;
7241
7242 case CMD_try:
7243 line = compile_try(p, &cctx);
7244 break;
7245 case CMD_catch:
7246 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007247 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248 break;
7249 case CMD_finally:
7250 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007251 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252 break;
7253 case CMD_endtry:
7254 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007255 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 break;
7257 case CMD_throw:
7258 line = compile_throw(p, &cctx);
7259 break;
7260
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007261 case CMD_eval:
7262 if (compile_expr0(&p, &cctx) == FAIL)
7263 goto erret;
7264
Bram Moolenaar3988f642020-08-27 22:43:03 +02007265 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007266 generate_instr_drop(&cctx, ISN_DROP, 1);
7267
7268 line = skipwhite(p);
7269 break;
7270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007273 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007274 case CMD_echomsg:
7275 case CMD_echoerr:
7276 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007277 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007279 case CMD_put:
7280 ea.cmd = cmd;
7281 line = compile_put(p, &ea, &cctx);
7282 break;
7283
Bram Moolenaar3988f642020-08-27 22:43:03 +02007284 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007285
Bram Moolenaarae616492020-07-28 20:07:27 +02007286 case CMD_append:
7287 case CMD_change:
7288 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007289 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007290 case CMD_xit:
7291 not_in_vim9(&ea);
7292 goto erret;
7293
Bram Moolenaar002262f2020-07-08 17:47:57 +02007294 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007295 if (cctx.ctx_skip != SKIP_YES)
7296 {
7297 semsg(_(e_invalid_command_str), ea.cmd);
7298 goto erret;
7299 }
7300 // We don't check for a next command here.
7301 line = (char_u *)"";
7302 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007305 if (cctx.ctx_skip == SKIP_YES)
7306 {
7307 // We don't check for a next command here.
7308 line = (char_u *)"";
7309 }
7310 else
7311 {
7312 // Not recognized, execute with do_cmdline_cmd().
7313 ea.arg = p;
7314 line = compile_exec(line, &ea, &cctx);
7315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 break;
7317 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007318nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 if (line == NULL)
7320 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007321 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322
7323 if (cctx.ctx_type_stack.ga_len < 0)
7324 {
7325 iemsg("Type stack underflow");
7326 goto erret;
7327 }
7328 }
7329
7330 if (cctx.ctx_scope != NULL)
7331 {
7332 if (cctx.ctx_scope->se_type == IF_SCOPE)
7333 emsg(_(e_endif));
7334 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7335 emsg(_(e_endwhile));
7336 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7337 emsg(_(e_endfor));
7338 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007339 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 goto erret;
7341 }
7342
Bram Moolenaarefd88552020-06-18 20:50:10 +02007343 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 {
7345 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7346 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007347 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 goto erret;
7349 }
7350
7351 // Return zero if there is no return at the end.
7352 generate_PUSHNR(&cctx, 0);
7353 generate_instr(&cctx, ISN_RETURN);
7354 }
7355
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007356 {
7357 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7358 + ufunc->uf_dfunc_idx;
7359 dfunc->df_deleted = FALSE;
7360 dfunc->df_instr = instr->ga_data;
7361 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007362 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007363 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007364 if (cctx.ctx_outer_used)
7365 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007366 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
7369 ret = OK;
7370
7371erret:
7372 if (ret == FAIL)
7373 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007374 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007375 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7376 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007377
7378 for (idx = 0; idx < instr->ga_len; ++idx)
7379 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007381
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007382 // If using the last entry in the table and it was added above, we
7383 // might as well remove it.
7384 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007385 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007386 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007387 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007388 ufunc->uf_dfunc_idx = 0;
7389 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007390 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007391
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007392 while (cctx.ctx_scope != NULL)
7393 drop_scope(&cctx);
7394
Bram Moolenaar20431c92020-03-20 18:39:46 +01007395 // Don't execute this function body.
7396 ga_clear_strings(&ufunc->uf_lines);
7397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 if (errormsg != NULL)
7399 emsg(errormsg);
7400 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007401 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 }
7403
7404 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007405 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007406 if (do_estack_push)
7407 estack_pop();
7408
Bram Moolenaar20431c92020-03-20 18:39:46 +01007409 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007410 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007412 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413}
7414
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007415 void
7416set_function_type(ufunc_T *ufunc)
7417{
7418 int varargs = ufunc->uf_va_name != NULL;
7419 int argcount = ufunc->uf_args.ga_len;
7420
7421 // Create a type for the function, with the return type and any
7422 // argument types.
7423 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7424 // The type is included in "tt_args".
7425 if (argcount > 0 || varargs)
7426 {
7427 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7428 argcount, &ufunc->uf_type_list);
7429 // Add argument types to the function type.
7430 if (func_type_add_arg_types(ufunc->uf_func_type,
7431 argcount + varargs,
7432 &ufunc->uf_type_list) == FAIL)
7433 return;
7434 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7435 ufunc->uf_func_type->tt_min_argcount =
7436 argcount - ufunc->uf_def_args.ga_len;
7437 if (ufunc->uf_arg_types == NULL)
7438 {
7439 int i;
7440
7441 // lambda does not have argument types.
7442 for (i = 0; i < argcount; ++i)
7443 ufunc->uf_func_type->tt_args[i] = &t_any;
7444 }
7445 else
7446 mch_memmove(ufunc->uf_func_type->tt_args,
7447 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7448 if (varargs)
7449 {
7450 ufunc->uf_func_type->tt_args[argcount] =
7451 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7452 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7453 }
7454 }
7455 else
7456 // No arguments, can use a predefined type.
7457 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7458 argcount, &ufunc->uf_type_list);
7459}
7460
7461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462/*
7463 * Delete an instruction, free what it contains.
7464 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007465 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466delete_instr(isn_T *isn)
7467{
7468 switch (isn->isn_type)
7469 {
7470 case ISN_EXEC:
7471 case ISN_LOADENV:
7472 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007473 case ISN_LOADB:
7474 case ISN_LOADW:
7475 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007477 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 case ISN_PUSHEXC:
7479 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007480 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007482 case ISN_STOREB:
7483 case ISN_STOREW:
7484 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007485 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 vim_free(isn->isn_arg.string);
7487 break;
7488
7489 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007490 case ISN_STORES:
7491 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 break;
7493
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007494 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007495 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007496 vim_free(isn->isn_arg.unlet.ul_name);
7497 break;
7498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 case ISN_STOREOPT:
7500 vim_free(isn->isn_arg.storeopt.so_name);
7501 break;
7502
7503 case ISN_PUSHBLOB: // push blob isn_arg.blob
7504 blob_unref(isn->isn_arg.blob);
7505 break;
7506
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007507 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007508#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007509 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007510#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007511 break;
7512
7513 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007515 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007516#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007517 break;
7518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 case ISN_UCALL:
7520 vim_free(isn->isn_arg.ufunc.cuf_name);
7521 break;
7522
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007523 case ISN_FUNCREF:
7524 {
7525 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7526 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007527
7528 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7529 func_ptr_unref(dfunc->df_ufunc);
7530 }
7531 break;
7532
7533 case ISN_DCALL:
7534 {
7535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7536 + isn->isn_arg.dfunc.cdf_idx;
7537
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007538 if (dfunc->df_ufunc != NULL
7539 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007540 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007541 }
7542 break;
7543
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007544 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007545 {
7546 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7547 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7548
7549 if (ufunc != NULL)
7550 {
7551 // Clear uf_dfunc_idx so that the function is deleted.
7552 clear_def_function(ufunc);
7553 ufunc->uf_dfunc_idx = 0;
7554 func_ptr_unref(ufunc);
7555 }
7556
7557 vim_free(lambda);
7558 vim_free(isn->isn_arg.newfunc.nf_global);
7559 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007560 break;
7561
Bram Moolenaar5e654232020-09-16 15:22:00 +02007562 case ISN_CHECKTYPE:
7563 free_type(isn->isn_arg.type.ct_type);
7564 break;
7565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007566 case ISN_2BOOL:
7567 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007568 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569 case ISN_ADDBLOB:
7570 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007571 case ISN_ANYINDEX:
7572 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 case ISN_BCALL:
7574 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007575 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577 case ISN_COMPAREANY:
7578 case ISN_COMPAREBLOB:
7579 case ISN_COMPAREBOOL:
7580 case ISN_COMPAREDICT:
7581 case ISN_COMPAREFLOAT:
7582 case ISN_COMPAREFUNC:
7583 case ISN_COMPARELIST:
7584 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 case ISN_COMPARESPECIAL:
7586 case ISN_COMPARESTRING:
7587 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007588 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 case ISN_DROP:
7590 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007591 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007592 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007594 case ISN_EXECCONCAT:
7595 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007597 case ISN_GETITEM:
7598 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007599 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007600 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007602 case ISN_LOADBDICT:
7603 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007604 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007606 case ISN_LOADSCRIPT:
7607 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007608 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007609 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007610 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007611 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612 case ISN_NEGATENR:
7613 case ISN_NEWDICT:
7614 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007616 case ISN_OPFLOAT:
7617 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007619 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007620 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 case ISN_PUSHF:
7622 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007624 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007625 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007626 case ISN_SHUFFLE:
7627 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007629 case ISN_STOREDICT:
7630 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007631 case ISN_STORENR:
7632 case ISN_STOREOUTER:
7633 case ISN_STOREREG:
7634 case ISN_STORESCRIPT:
7635 case ISN_STOREV:
7636 case ISN_STRINDEX:
7637 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007638 case ISN_THROW:
7639 case ISN_TRY:
7640 // nothing allocated
7641 break;
7642 }
7643}
7644
7645/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007646 * Free all instructions for "dfunc".
7647 */
7648 static void
7649delete_def_function_contents(dfunc_T *dfunc)
7650{
7651 int idx;
7652
7653 ga_clear(&dfunc->df_def_args_isn);
7654
7655 if (dfunc->df_instr != NULL)
7656 {
7657 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7658 delete_instr(dfunc->df_instr + idx);
7659 VIM_CLEAR(dfunc->df_instr);
7660 }
7661
7662 dfunc->df_deleted = TRUE;
7663}
7664
7665/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007666 * When a user function is deleted, clear the contents of any associated def
7667 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668 */
7669 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007670clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007672 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 {
7674 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7675 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676
Bram Moolenaar20431c92020-03-20 18:39:46 +01007677 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007678 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679 }
7680}
7681
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007682/*
7683 * Used when a user function is about to be deleted: remove the pointer to it.
7684 * The entry in def_functions is then unused.
7685 */
7686 void
7687unlink_def_function(ufunc_T *ufunc)
7688{
7689 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7690
7691 dfunc->df_ufunc = NULL;
7692}
7693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007695/*
7696 * Free all functions defined with ":def".
7697 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007698 void
7699free_def_functions(void)
7700{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007701 int idx;
7702
7703 for (idx = 0; idx < def_functions.ga_len; ++idx)
7704 {
7705 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7706
7707 delete_def_function_contents(dfunc);
7708 }
7709
7710 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007711}
7712#endif
7713
7714
7715#endif // FEAT_EVAL