blob: a891006f97fd4e062aaf08015f9fb4cf1cfd7cf6 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200198arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200250 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200262 * Lookup a script-local variable in the current script, possibly defined in a
263 * block that contains the function "cctx->ctx_ufunc".
264 * "cctx" is NULL at the script level.
265 * if "len" is <= 0 "name" must be NUL terminated.
266 * Return NULL when not found.
267 */
268 static sallvar_T *
269find_script_var(char_u *name, size_t len, cctx_T *cctx)
270{
271 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
272 hashitem_T *hi;
273 int cc;
274 sallvar_T *sav;
275 ufunc_T *ufunc;
276
277 // Find the list of all script variables with the right name.
278 if (len > 0)
279 {
280 cc = name[len];
281 name[len] = NUL;
282 }
283 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
284 if (len > 0)
285 name[len] = cc;
286 if (HASHITEM_EMPTY(hi))
287 return NULL;
288
289 sav = HI2SAV(hi);
290 if (sav->sav_block_id == 0 || cctx == NULL)
291 // variable defined in the script scope or not in a function.
292 return sav;
293
294 // Go over the variables with this name and find one that was visible
295 // from the function.
296 ufunc = cctx->ctx_ufunc;
297 while (sav != NULL)
298 {
299 int idx;
300
301 // Go over the blocks that this function was defined in. If the
302 // variable block ID matches it was visible to the function.
303 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
304 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
305 return sav;
306 sav = sav->sav_next;
307 }
308
309 return NULL;
310}
311
312/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200313 * Returnd TRUE if the script context is Vim9 script.
314 */
315 static int
316script_is_vim9()
317{
318 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
319}
320
321/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200322 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200323 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
324 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200325 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 * Returns OK or FAIL.
327 */
328 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200333 if (current_sctx.sc_sid <= 0)
334 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 is_vim9_script = script_is_vim9();
336 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200337 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200338 if (is_vim9_script)
339 {
340 // Check script variables that were visible where the function was
341 // defined.
342 if (find_script_var(name, len, cctx) != NULL)
343 return OK;
344 }
345 else
346 {
347 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
348 dictitem_T *di;
349 int cc;
350
351 // Check script variables that are currently visible
352 cc = name[len];
353 name[len] = NUL;
354 di = find_var_in_ht(ht, 0, name, TRUE);
355 name[len] = cc;
356 if (di != NULL)
357 return OK;
358 }
359
360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361}
362
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100363/*
364 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200365 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200366 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100367 * Return FAIL and give an error if it defined.
368 */
369 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200370check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200372 int c = p[len];
373 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200374
375 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200376 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100377 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200378 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200380 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200381 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100382 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 // A local or script-local function can shadow a global function.
384 if (ufunc == NULL || !func_is_global(ufunc)
385 || (p[0] == 'g' && p[1] == ':'))
386 {
387 p[len] = c;
388 semsg(_(e_name_already_defined_str), p);
389 return FAIL;
390 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100391 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200392 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 return OK;
394}
395
Bram Moolenaar65b95452020-07-19 14:03:09 +0200396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397/////////////////////////////////////////////////////////////////////
398// Following generate_ functions expect the caller to call ga_grow().
399
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200400#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
401#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403/*
404 * Generate an instruction without arguments.
405 * Returns a pointer to the new instruction, NULL if failed.
406 */
407 static isn_T *
408generate_instr(cctx_T *cctx, isntype_T isn_type)
409{
410 garray_T *instr = &cctx->ctx_instr;
411 isn_T *isn;
412
Bram Moolenaar080457c2020-03-03 21:53:32 +0100413 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (ga_grow(instr, 1) == FAIL)
415 return NULL;
416 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
417 isn->isn_type = isn_type;
418 isn->isn_lnum = cctx->ctx_lnum + 1;
419 ++instr->ga_len;
420
421 return isn;
422}
423
424/*
425 * Generate an instruction without arguments.
426 * "drop" will be removed from the stack.
427 * Returns a pointer to the new instruction, NULL if failed.
428 */
429 static isn_T *
430generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
431{
432 garray_T *stack = &cctx->ctx_type_stack;
433
Bram Moolenaar080457c2020-03-03 21:53:32 +0100434 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 stack->ga_len -= drop;
436 return generate_instr(cctx, isn_type);
437}
438
439/*
440 * Generate instruction "isn_type" and put "type" on the type stack.
441 */
442 static isn_T *
443generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
444{
445 isn_T *isn;
446 garray_T *stack = &cctx->ctx_type_stack;
447
448 if ((isn = generate_instr(cctx, isn_type)) == NULL)
449 return NULL;
450
451 if (ga_grow(stack, 1) == FAIL)
452 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200453 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 ++stack->ga_len;
455
456 return isn;
457}
458
459/*
460 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200461 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 */
463 static int
464may_generate_2STRING(int offset, cctx_T *cctx)
465{
466 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200467 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 garray_T *stack = &cctx->ctx_type_stack;
469 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
470
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200471 switch ((*type)->tt_type)
472 {
473 // nothing to be done
474 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200476 // conversion possible
477 case VAR_SPECIAL:
478 case VAR_BOOL:
479 case VAR_NUMBER:
480 case VAR_FLOAT:
481 break;
482
483 // conversion possible (with runtime check)
484 case VAR_ANY:
485 case VAR_UNKNOWN:
486 isntype = ISN_2STRING_ANY;
487 break;
488
489 // conversion not possible
490 case VAR_VOID:
491 case VAR_BLOB:
492 case VAR_FUNC:
493 case VAR_PARTIAL:
494 case VAR_LIST:
495 case VAR_DICT:
496 case VAR_JOB:
497 case VAR_CHANNEL:
498 to_string_error((*type)->tt_type);
499 return FAIL;
500 }
501
502 *type = &t_string;
503 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 return FAIL;
505 isn->isn_arg.number = offset;
506
507 return OK;
508}
509
510 static int
511check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
512{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200513 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 {
517 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200518 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 return FAIL;
522 }
523 return OK;
524}
525
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200526 static int
527generate_add_instr(
528 cctx_T *cctx,
529 vartype_T vartype,
530 type_T *type1,
531 type_T *type2)
532{
533 isn_T *isn = generate_instr_drop(cctx,
534 vartype == VAR_NUMBER ? ISN_OPNR
535 : vartype == VAR_LIST ? ISN_ADDLIST
536 : vartype == VAR_BLOB ? ISN_ADDBLOB
537#ifdef FEAT_FLOAT
538 : vartype == VAR_FLOAT ? ISN_OPFLOAT
539#endif
540 : ISN_OPANY, 1);
541
542 if (vartype != VAR_LIST && vartype != VAR_BLOB
543 && type1->tt_type != VAR_ANY
544 && type2->tt_type != VAR_ANY
545 && check_number_or_float(
546 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
547 return FAIL;
548
549 if (isn != NULL)
550 isn->isn_arg.op.op_type = EXPR_ADD;
551 return isn == NULL ? FAIL : OK;
552}
553
554/*
555 * Get the type to use for an instruction for an operation on "type1" and
556 * "type2". If they are matching use a type-specific instruction. Otherwise
557 * fall back to runtime type checking.
558 */
559 static vartype_T
560operator_type(type_T *type1, type_T *type2)
561{
562 if (type1->tt_type == type2->tt_type
563 && (type1->tt_type == VAR_NUMBER
564 || type1->tt_type == VAR_LIST
565#ifdef FEAT_FLOAT
566 || type1->tt_type == VAR_FLOAT
567#endif
568 || type1->tt_type == VAR_BLOB))
569 return type1->tt_type;
570 return VAR_ANY;
571}
572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573/*
574 * Generate an instruction with two arguments. The instruction depends on the
575 * type of the arguments.
576 */
577 static int
578generate_two_op(cctx_T *cctx, char_u *op)
579{
580 garray_T *stack = &cctx->ctx_type_stack;
581 type_T *type1;
582 type_T *type2;
583 vartype_T vartype;
584 isn_T *isn;
585
Bram Moolenaar080457c2020-03-03 21:53:32 +0100586 RETURN_OK_IF_SKIP(cctx);
587
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200588 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
590 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200591 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592
593 switch (*op)
594 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200595 case '+':
596 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 break;
599
600 case '-':
601 case '*':
602 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
603 op) == FAIL)
604 return FAIL;
605 if (vartype == VAR_NUMBER)
606 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
607#ifdef FEAT_FLOAT
608 else if (vartype == VAR_FLOAT)
609 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
610#endif
611 else
612 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = *op == '*'
615 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
616 break;
617
Bram Moolenaar4c683752020-04-05 21:38:23 +0200618 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type2->tt_type != VAR_NUMBER))
622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200623 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 }
626 isn = generate_instr_drop(cctx,
627 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
628 if (isn != NULL)
629 isn->isn_arg.op.op_type = EXPR_REM;
630 break;
631 }
632
633 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200634 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 {
636 type_T *type = &t_any;
637
638#ifdef FEAT_FLOAT
639 // float+number and number+float results in float
640 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
641 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
642 type = &t_float;
643#endif
644 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
645 }
646
647 return OK;
648}
649
650/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200651 * Get the instruction to use for comparing "type1" with "type2"
652 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200654 static isntype_T
655get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656{
657 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658
Bram Moolenaar4c683752020-04-05 21:38:23 +0200659 if (type1 == VAR_UNKNOWN)
660 type1 = VAR_ANY;
661 if (type2 == VAR_UNKNOWN)
662 type2 = VAR_ANY;
663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 if (type1 == type2)
665 {
666 switch (type1)
667 {
668 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
669 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
670 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
671 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
672 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
673 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
674 case VAR_LIST: isntype = ISN_COMPARELIST; break;
675 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
676 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 default: isntype = ISN_COMPAREANY; break;
678 }
679 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
682 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
683 isntype = ISN_COMPAREANY;
684
685 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
686 && (isntype == ISN_COMPAREBOOL
687 || isntype == ISN_COMPARESPECIAL
688 || isntype == ISN_COMPARENR
689 || isntype == ISN_COMPAREFLOAT))
690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200691 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200693 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 }
695 if (isntype == ISN_DROP
696 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
697 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
698 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
699 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
700 && exptype != EXPR_IS && exptype != EXPR_ISNOT
701 && (type1 == VAR_BLOB || type2 == VAR_BLOB
702 || type1 == VAR_LIST || type2 == VAR_LIST))))
703 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200704 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200706 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return isntype;
709}
710
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200711 int
712check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
713{
714 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
715 return FAIL;
716 return OK;
717}
718
Bram Moolenaara5565e42020-05-09 15:44:01 +0200719/*
720 * Generate an ISN_COMPARE* instruction with a boolean result.
721 */
722 static int
723generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
724{
725 isntype_T isntype;
726 isn_T *isn;
727 garray_T *stack = &cctx->ctx_type_stack;
728 vartype_T type1;
729 vartype_T type2;
730
731 RETURN_OK_IF_SKIP(cctx);
732
733 // Get the known type of the two items on the stack. If they are matching
734 // use a type-specific instruction. Otherwise fall back to runtime type
735 // checking.
736 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
737 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
738 isntype = get_compare_isn(exptype, type1, type2);
739 if (isntype == ISN_DROP)
740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
742 if ((isn = generate_instr(cctx, isntype)) == NULL)
743 return FAIL;
744 isn->isn_arg.op.op_type = exptype;
745 isn->isn_arg.op.op_ic = ic;
746
747 // takes two arguments, puts one bool back
748 if (stack->ga_len >= 2)
749 {
750 --stack->ga_len;
751 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
752 }
753
754 return OK;
755}
756
757/*
758 * Generate an ISN_2BOOL instruction.
759 */
760 static int
761generate_2BOOL(cctx_T *cctx, int invert)
762{
763 isn_T *isn;
764 garray_T *stack = &cctx->ctx_type_stack;
765
Bram Moolenaar080457c2020-03-03 21:53:32 +0100766 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
768 return FAIL;
769 isn->isn_arg.number = invert;
770
771 // type becomes bool
772 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
773
774 return OK;
775}
776
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200777/*
778 * Generate an ISN_COND2BOOL instruction.
779 */
780 static int
781generate_COND2BOOL(cctx_T *cctx)
782{
783 isn_T *isn;
784 garray_T *stack = &cctx->ctx_type_stack;
785
786 RETURN_OK_IF_SKIP(cctx);
787 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
788 return FAIL;
789
790 // type becomes bool
791 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
792
793 return OK;
794}
795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200797generate_TYPECHECK(
798 cctx_T *cctx,
799 type_T *expected,
800 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801{
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804
Bram Moolenaar080457c2020-03-03 21:53:32 +0100805 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
807 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200808 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 isn->isn_arg.type.ct_off = offset;
810
Bram Moolenaar5e654232020-09-16 15:22:00 +0200811 // type becomes expected
812 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 return OK;
815}
816
817/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200818 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
819 * used. Return FALSE if the types will never match.
820 */
821 static int
822use_typecheck(type_T *actual, type_T *expected)
823{
824 if (actual->tt_type == VAR_ANY
825 || actual->tt_type == VAR_UNKNOWN
826 || (actual->tt_type == VAR_FUNC
827 && (expected->tt_type == VAR_FUNC
828 || expected->tt_type == VAR_PARTIAL)
829 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
830 return TRUE;
831 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
832 && actual->tt_type == expected->tt_type)
833 // This takes care of a nested list or dict.
834 return use_typecheck(actual->tt_member, expected->tt_member);
835 return FALSE;
836}
837
838/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200839 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200840 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * - "actual" is a type that can be "expected" type: add a runtime check; or
842 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200843 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
844 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200845 */
846 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200847need_type(
848 type_T *actual,
849 type_T *expected,
850 int offset,
851 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200852 int silent,
853 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200854{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200855 if (expected == &t_bool && actual != &t_bool
856 && (actual->tt_flags & TTFLAG_BOOL_OK))
857 {
858 // Using "0", "1" or the result of an expression with "&&" or "||" as a
859 // boolean is OK but requires a conversion.
860 generate_2BOOL(cctx, FALSE);
861 return OK;
862 }
863
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200864 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200865 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200866
867 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200868 // If it's a constant a runtime check makes no sense.
869 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200871 generate_TYPECHECK(cctx, expected, offset);
872 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200873 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200874
875 if (!silent)
876 type_mismatch(expected, actual);
877 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878}
879
880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 * Generate an ISN_PUSHNR instruction.
882 */
883 static int
884generate_PUSHNR(cctx_T *cctx, varnumber_T number)
885{
886 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200887 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar080457c2020-03-03 21:53:32 +0100889 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
891 return FAIL;
892 isn->isn_arg.number = number;
893
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200894 if (number == 0 || number == 1)
895 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200896 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200897
898 // A 0 or 1 number can also be used as a bool.
899 if (type != NULL)
900 {
901 type->tt_type = VAR_NUMBER;
902 type->tt_flags = TTFLAG_BOOL_OK;
903 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
904 }
905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 return OK;
907}
908
909/*
910 * Generate an ISN_PUSHBOOL instruction.
911 */
912 static int
913generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
914{
915 isn_T *isn;
916
Bram Moolenaar080457c2020-03-03 21:53:32 +0100917 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
919 return FAIL;
920 isn->isn_arg.number = number;
921
922 return OK;
923}
924
925/*
926 * Generate an ISN_PUSHSPEC instruction.
927 */
928 static int
929generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
930{
931 isn_T *isn;
932
Bram Moolenaar080457c2020-03-03 21:53:32 +0100933 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
935 return FAIL;
936 isn->isn_arg.number = number;
937
938 return OK;
939}
940
941#ifdef FEAT_FLOAT
942/*
943 * Generate an ISN_PUSHF instruction.
944 */
945 static int
946generate_PUSHF(cctx_T *cctx, float_T fnumber)
947{
948 isn_T *isn;
949
Bram Moolenaar080457c2020-03-03 21:53:32 +0100950 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
952 return FAIL;
953 isn->isn_arg.fnumber = fnumber;
954
955 return OK;
956}
957#endif
958
959/*
960 * Generate an ISN_PUSHS instruction.
961 * Consumes "str".
962 */
963 static int
964generate_PUSHS(cctx_T *cctx, char_u *str)
965{
966 isn_T *isn;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
970 return FAIL;
971 isn->isn_arg.string = str;
972
973 return OK;
974}
975
976/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100977 * Generate an ISN_PUSHCHANNEL instruction.
978 * Consumes "channel".
979 */
980 static int
981generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
982{
983 isn_T *isn;
984
Bram Moolenaar080457c2020-03-03 21:53:32 +0100985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100986 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
987 return FAIL;
988 isn->isn_arg.channel = channel;
989
990 return OK;
991}
992
993/*
994 * Generate an ISN_PUSHJOB instruction.
995 * Consumes "job".
996 */
997 static int
998generate_PUSHJOB(cctx_T *cctx, job_T *job)
999{
1000 isn_T *isn;
1001
Bram Moolenaar080457c2020-03-03 21:53:32 +01001002 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001003 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001004 return FAIL;
1005 isn->isn_arg.job = job;
1006
1007 return OK;
1008}
1009
1010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 * Generate an ISN_PUSHBLOB instruction.
1012 * Consumes "blob".
1013 */
1014 static int
1015generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1016{
1017 isn_T *isn;
1018
Bram Moolenaar080457c2020-03-03 21:53:32 +01001019 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.blob = blob;
1023
1024 return OK;
1025}
1026
1027/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 * Generate an ISN_PUSHFUNC instruction with name "name".
1029 * Consumes "name".
1030 */
1031 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001032generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001037 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001038 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001039 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040
1041 return OK;
1042}
1043
1044/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 * Generate an ISN_GETITEM instruction with "index".
1046 */
1047 static int
1048generate_GETITEM(cctx_T *cctx, int index)
1049{
1050 isn_T *isn;
1051 garray_T *stack = &cctx->ctx_type_stack;
1052 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1053 type_T *item_type = &t_any;
1054
1055 RETURN_OK_IF_SKIP(cctx);
1056
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001057 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001059 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 emsg(_(e_listreq));
1061 return FAIL;
1062 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001063 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001064 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = index;
1067
1068 // add the item type to the type stack
1069 if (ga_grow(stack, 1) == FAIL)
1070 return FAIL;
1071 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1072 ++stack->ga_len;
1073 return OK;
1074}
1075
1076/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001077 * Generate an ISN_SLICE instruction with "count".
1078 */
1079 static int
1080generate_SLICE(cctx_T *cctx, int count)
1081{
1082 isn_T *isn;
1083
1084 RETURN_OK_IF_SKIP(cctx);
1085 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.number = count;
1088 return OK;
1089}
1090
1091/*
1092 * Generate an ISN_CHECKLEN instruction with "min_len".
1093 */
1094 static int
1095generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1096{
1097 isn_T *isn;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100
1101 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.checklen.cl_min_len = min_len;
1104 isn->isn_arg.checklen.cl_more_OK = more_OK;
1105
1106 return OK;
1107}
1108
1109/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 * Generate an ISN_STORE instruction.
1111 */
1112 static int
1113generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1114{
1115 isn_T *isn;
1116
Bram Moolenaar080457c2020-03-03 21:53:32 +01001117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1119 return FAIL;
1120 if (name != NULL)
1121 isn->isn_arg.string = vim_strsave(name);
1122 else
1123 isn->isn_arg.number = idx;
1124
1125 return OK;
1126}
1127
1128/*
1129 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1130 */
1131 static int
1132generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1133{
1134 isn_T *isn;
1135
Bram Moolenaar080457c2020-03-03 21:53:32 +01001136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1138 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001139 isn->isn_arg.storenr.stnr_idx = idx;
1140 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141
1142 return OK;
1143}
1144
1145/*
1146 * Generate an ISN_STOREOPT instruction
1147 */
1148 static int
1149generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1150{
1151 isn_T *isn;
1152
Bram Moolenaar080457c2020-03-03 21:53:32 +01001153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001154 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 return FAIL;
1156 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1157 isn->isn_arg.storeopt.so_flags = opt_flags;
1158
1159 return OK;
1160}
1161
1162/*
1163 * Generate an ISN_LOAD or similar instruction.
1164 */
1165 static int
1166generate_LOAD(
1167 cctx_T *cctx,
1168 isntype_T isn_type,
1169 int idx,
1170 char_u *name,
1171 type_T *type)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1177 return FAIL;
1178 if (name != NULL)
1179 isn->isn_arg.string = vim_strsave(name);
1180 else
1181 isn->isn_arg.number = idx;
1182
1183 return OK;
1184}
1185
1186/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001187 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188 */
1189 static int
1190generate_LOADV(
1191 cctx_T *cctx,
1192 char_u *name,
1193 int error)
1194{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001195 int di_flags;
1196 int vidx = find_vim_var(name, &di_flags);
1197 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200 if (vidx < 0)
1201 {
1202 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001203 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001204 return FAIL;
1205 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001206 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001207
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001208 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209}
1210
1211/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001212 * Generate an ISN_UNLET instruction.
1213 */
1214 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001215generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001216{
1217 isn_T *isn;
1218
1219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001220 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001221 return FAIL;
1222 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1223 isn->isn_arg.unlet.ul_forceit = forceit;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001229 * Generate an ISN_LOCKCONST instruction.
1230 */
1231 static int
1232generate_LOCKCONST(cctx_T *cctx)
1233{
1234 isn_T *isn;
1235
1236 RETURN_OK_IF_SKIP(cctx);
1237 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1238 return FAIL;
1239 return OK;
1240}
1241
1242/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 * Generate an ISN_LOADS instruction.
1244 */
1245 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250 int sid,
1251 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252{
1253 isn_T *isn;
1254
Bram Moolenaar080457c2020-03-03 21:53:32 +01001255 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001256 if (isn_type == ISN_LOADS)
1257 isn = generate_instr_type(cctx, isn_type, type);
1258 else
1259 isn = generate_instr_drop(cctx, isn_type, 1);
1260 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001262 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1263 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 return OK;
1266}
1267
1268/*
1269 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1270 */
1271 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 cctx_T *cctx,
1274 isntype_T isn_type,
1275 int sid,
1276 int idx,
1277 type_T *type)
1278{
1279 isn_T *isn;
1280
Bram Moolenaar080457c2020-03-03 21:53:32 +01001281 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 if (isn_type == ISN_LOADSCRIPT)
1283 isn = generate_instr_type(cctx, isn_type, type);
1284 else
1285 isn = generate_instr_drop(cctx, isn_type, 1);
1286 if (isn == NULL)
1287 return FAIL;
1288 isn->isn_arg.script.script_sid = sid;
1289 isn->isn_arg.script.script_idx = idx;
1290 return OK;
1291}
1292
1293/*
1294 * Generate an ISN_NEWLIST instruction.
1295 */
1296 static int
1297generate_NEWLIST(cctx_T *cctx, int count)
1298{
1299 isn_T *isn;
1300 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 type_T *type;
1302 type_T *member;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1306 return FAIL;
1307 isn->isn_arg.number = count;
1308
Bram Moolenaar127542b2020-08-09 17:22:04 +02001309 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001310 if (count == 0)
1311 member = &t_void;
1312 else
1313 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001314 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1315 cctx->ctx_type_list);
1316 type = get_list_type(member, cctx->ctx_type_list);
1317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 // drop the value types
1319 stack->ga_len -= count;
1320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 // add the list type to the type stack
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1325 ++stack->ga_len;
1326
1327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_NEWDICT instruction.
1332 */
1333 static int
1334generate_NEWDICT(cctx_T *cctx, int count)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 type_T *type;
1339 type_T *member;
1340
Bram Moolenaar080457c2020-03-03 21:53:32 +01001341 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.number = count;
1345
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001346 if (count == 0)
1347 member = &t_void;
1348 else
1349 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001350 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1351 cctx->ctx_type_list);
1352 type = get_dict_type(member, cctx->ctx_type_list);
1353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 // drop the key and value types
1355 stack->ga_len -= 2 * count;
1356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 // add the dict type to the type stack
1358 if (ga_grow(stack, 1) == FAIL)
1359 return FAIL;
1360 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1361 ++stack->ga_len;
1362
1363 return OK;
1364}
1365
1366/*
1367 * Generate an ISN_FUNCREF instruction.
1368 */
1369 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001370generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371{
1372 isn_T *isn;
1373 garray_T *stack = &cctx->ctx_type_stack;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1377 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001378 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001379 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 if (ga_grow(stack, 1) == FAIL)
1382 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001383 ((type_T **)stack->ga_data)[stack->ga_len] =
1384 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001391 * Generate an ISN_NEWFUNC instruction.
1392 */
1393 static int
1394generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1395{
1396 isn_T *isn;
1397 char_u *name;
1398
1399 RETURN_OK_IF_SKIP(cctx);
1400 name = vim_strsave(lambda_name);
1401 if (name == NULL)
1402 return FAIL;
1403 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1404 return FAIL;
1405 isn->isn_arg.newfunc.nf_lambda = name;
1406 isn->isn_arg.newfunc.nf_global = func_name;
1407
1408 return OK;
1409}
1410
1411/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 * Generate an ISN_JUMP instruction.
1413 */
1414 static int
1415generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1416{
1417 isn_T *isn;
1418 garray_T *stack = &cctx->ctx_type_stack;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.jump.jump_when = when;
1424 isn->isn_arg.jump.jump_where = where;
1425
1426 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1427 --stack->ga_len;
1428
1429 return OK;
1430}
1431
1432 static int
1433generate_FOR(cctx_T *cctx, int loop_idx)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
1437
Bram Moolenaar080457c2020-03-03 21:53:32 +01001438 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1440 return FAIL;
1441 isn->isn_arg.forloop.for_idx = loop_idx;
1442
1443 if (ga_grow(stack, 1) == FAIL)
1444 return FAIL;
1445 // type doesn't matter, will be stored next
1446 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1447 ++stack->ga_len;
1448
1449 return OK;
1450}
1451
1452/*
1453 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001454 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 * Return FAIL if the number of arguments is wrong.
1456 */
1457 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001458generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001462 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001463 type_T *argtypes[MAX_FUNC_ARGS];
1464 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465
Bram Moolenaar080457c2020-03-03 21:53:32 +01001466 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001467 argoff = check_internal_func(func_idx, argcount);
1468 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 return FAIL;
1470
Bram Moolenaar389df252020-07-09 21:20:47 +02001471 if (method_call && argoff > 1)
1472 {
1473 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.shuffle.shfl_item = argcount;
1476 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1477 }
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1480 return FAIL;
1481 isn->isn_arg.bfunc.cbf_idx = func_idx;
1482 isn->isn_arg.bfunc.cbf_argcount = argcount;
1483
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001484 for (i = 0; i < argcount; ++i)
1485 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 stack->ga_len -= argcount; // drop the arguments
1488 if (ga_grow(stack, 1) == FAIL)
1489 return FAIL;
1490 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001491 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 ++stack->ga_len; // add return value
1493
1494 return OK;
1495}
1496
1497/*
1498 * Generate an ISN_DCALL or ISN_UCALL instruction.
1499 * Return FAIL if the number of arguments is wrong.
1500 */
1501 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001502generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
1506 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001507 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if (argcount > regular_args && !has_varargs(ufunc))
1511 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001512 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 return FAIL;
1514 }
1515 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1516 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001517 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 return FAIL;
1519 }
1520
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001521 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001522 {
1523 int i;
1524
1525 for (i = 0; i < argcount; ++i)
1526 {
1527 type_T *expected;
1528 type_T *actual;
1529
1530 if (i < regular_args)
1531 {
1532 if (ufunc->uf_arg_types == NULL)
1533 continue;
1534 expected = ufunc->uf_arg_types[i];
1535 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001536 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1537 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001538 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001539 else
1540 expected = ufunc->uf_va_type->tt_member;
1541 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001542 if (need_type(actual, expected, -argcount + i, cctx,
1543 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001544 {
1545 arg_type_mismatch(expected, actual, i + 1);
1546 return FAIL;
1547 }
1548 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001549 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001550 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1551 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001552 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001553 }
1554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001556 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001557 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001559 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 {
1561 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1562 isn->isn_arg.dfunc.cdf_argcount = argcount;
1563 }
1564 else
1565 {
1566 // A user function may be deleted and redefined later, can't use the
1567 // ufunc pointer, need to look it up again at runtime.
1568 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1569 isn->isn_arg.ufunc.cuf_argcount = argcount;
1570 }
1571
1572 stack->ga_len -= argcount; // drop the arguments
1573 if (ga_grow(stack, 1) == FAIL)
1574 return FAIL;
1575 // add return value
1576 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1577 ++stack->ga_len;
1578
1579 return OK;
1580}
1581
1582/*
1583 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1584 */
1585 static int
1586generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1587{
1588 isn_T *isn;
1589 garray_T *stack = &cctx->ctx_type_stack;
1590
Bram Moolenaar080457c2020-03-03 21:53:32 +01001591 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1593 return FAIL;
1594 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1595 isn->isn_arg.ufunc.cuf_argcount = argcount;
1596
1597 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001598 if (ga_grow(stack, 1) == FAIL)
1599 return FAIL;
1600 // add return value
1601 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1602 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603
1604 return OK;
1605}
1606
1607/*
1608 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001609 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 */
1611 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001612generate_PCALL(
1613 cctx_T *cctx,
1614 int argcount,
1615 char_u *name,
1616 type_T *type,
1617 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618{
1619 isn_T *isn;
1620 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001621 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622
Bram Moolenaar080457c2020-03-03 21:53:32 +01001623 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001624
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001625 if (type->tt_type == VAR_ANY)
1626 ret_type = &t_any;
1627 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001628 {
1629 if (type->tt_argcount != -1)
1630 {
1631 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1632
1633 if (argcount < type->tt_min_argcount - varargs)
1634 {
1635 semsg(_(e_toofewarg), "[reference]");
1636 return FAIL;
1637 }
1638 if (!varargs && argcount > type->tt_argcount)
1639 {
1640 semsg(_(e_toomanyarg), "[reference]");
1641 return FAIL;
1642 }
1643 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001644 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001645 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001646 else
1647 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001648 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001649 return FAIL;
1650 }
1651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1653 return FAIL;
1654 isn->isn_arg.pfunc.cpf_top = at_top;
1655 isn->isn_arg.pfunc.cpf_argcount = argcount;
1656
1657 stack->ga_len -= argcount; // drop the arguments
1658
1659 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001660 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001662 // If partial is above the arguments it must be cleared and replaced with
1663 // the return value.
1664 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1665 return FAIL;
1666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 return OK;
1668}
1669
1670/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001671 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 */
1673 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001674generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675{
1676 isn_T *isn;
1677 garray_T *stack = &cctx->ctx_type_stack;
1678 type_T *type;
1679
Bram Moolenaar080457c2020-03-03 21:53:32 +01001680 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001681 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001683 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001685 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001687 if (type->tt_type != VAR_DICT && type != &t_any)
1688 {
1689 emsg(_(e_dictreq));
1690 return FAIL;
1691 }
1692 // change dict type to dict member type
1693 if (type->tt_type == VAR_DICT)
1694 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
1696 return OK;
1697}
1698
1699/*
1700 * Generate an ISN_ECHO instruction.
1701 */
1702 static int
1703generate_ECHO(cctx_T *cctx, int with_white, int count)
1704{
1705 isn_T *isn;
1706
Bram Moolenaar080457c2020-03-03 21:53:32 +01001707 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1709 return FAIL;
1710 isn->isn_arg.echo.echo_with_white = with_white;
1711 isn->isn_arg.echo.echo_count = count;
1712
1713 return OK;
1714}
1715
Bram Moolenaarad39c092020-02-26 18:23:43 +01001716/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001717 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001718 */
1719 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001720generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001721{
1722 isn_T *isn;
1723
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001724 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001725 return FAIL;
1726 isn->isn_arg.number = count;
1727
1728 return OK;
1729}
1730
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001731/*
1732 * Generate an ISN_PUT instruction.
1733 */
1734 static int
1735generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1736{
1737 isn_T *isn;
1738
1739 RETURN_OK_IF_SKIP(cctx);
1740 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1741 return FAIL;
1742 isn->isn_arg.put.put_regname = regname;
1743 isn->isn_arg.put.put_lnum = lnum;
1744 return OK;
1745}
1746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 static int
1748generate_EXEC(cctx_T *cctx, char_u *line)
1749{
1750 isn_T *isn;
1751
Bram Moolenaar080457c2020-03-03 21:53:32 +01001752 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.string = vim_strsave(line);
1756 return OK;
1757}
1758
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001759 static int
1760generate_EXECCONCAT(cctx_T *cctx, int count)
1761{
1762 isn_T *isn;
1763
1764 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1765 return FAIL;
1766 isn->isn_arg.number = count;
1767 return OK;
1768}
1769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770/*
1771 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001772 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001774 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001775reserve_local(
1776 cctx_T *cctx,
1777 char_u *name,
1778 size_t len,
1779 int isConst,
1780 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 lvar_T *lvar;
1783
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001784 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001786 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001787 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 }
1789
1790 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001791 return NULL;
1792 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001793 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001795 // Every local variable uses the next entry on the stack. We could re-use
1796 // the last ones when leaving a scope, but then variables used in a closure
1797 // might get overwritten. To keep things simple do not re-use stack
1798 // entries. This is less efficient, but memory is cheap these days.
1799 lvar->lv_idx = cctx->ctx_locals_count++;
1800
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001801 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 lvar->lv_const = isConst;
1803 lvar->lv_type = type;
1804
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001805 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806}
1807
1808/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001809 * Remove local variables above "new_top".
1810 */
1811 static void
1812unwind_locals(cctx_T *cctx, int new_top)
1813{
1814 if (cctx->ctx_locals.ga_len > new_top)
1815 {
1816 int idx;
1817 lvar_T *lvar;
1818
1819 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1820 {
1821 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1822 vim_free(lvar->lv_name);
1823 }
1824 }
1825 cctx->ctx_locals.ga_len = new_top;
1826}
1827
1828/*
1829 * Free all local variables.
1830 */
1831 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001832free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001833{
1834 unwind_locals(cctx, 0);
1835 ga_clear(&cctx->ctx_locals);
1836}
1837
1838/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 * Find "name" in script-local items of script "sid".
1840 * Returns the index in "sn_var_vals" if found.
1841 * If found but not in "sn_var_vals" returns -1.
1842 * If not found returns -2.
1843 */
1844 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001845get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846{
1847 hashtab_T *ht;
1848 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001849 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001850 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 int idx;
1852
Bram Moolenaare3d46852020-08-29 13:39:17 +02001853 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001855 if (sid == current_sctx.sc_sid)
1856 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001857 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001858
1859 if (sav == NULL)
1860 return -2;
1861 idx = sav->sav_var_vals_idx;
1862 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1863 if (check_writable && sv->sv_const)
1864 semsg(_(e_readonlyvar), name);
1865 return idx;
1866 }
1867
1868 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 ht = &SCRIPT_VARS(sid);
1870 di = find_var_in_ht(ht, 0, name, TRUE);
1871 if (di == NULL)
1872 return -2;
1873
1874 // Now find the svar_T index in sn_var_vals.
1875 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1876 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001877 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 if (sv->sv_tv == &di->di_tv)
1879 {
1880 if (check_writable && sv->sv_const)
1881 semsg(_(e_readonlyvar), name);
1882 return idx;
1883 }
1884 }
1885 return -1;
1886}
1887
1888/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001889 * Find "name" in imported items of the current script or in "cctx" if not
1890 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 */
1892 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001893find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 int idx;
1896
Bram Moolenaare3d46852020-08-29 13:39:17 +02001897 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001898 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 if (cctx != NULL)
1900 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1901 {
1902 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1903 + idx;
1904
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001905 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1906 : STRLEN(import->imp_name) == len
1907 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 return import;
1909 }
1910
Bram Moolenaarefa94442020-08-08 22:16:00 +02001911 return find_imported_in_script(name, len, current_sctx.sc_sid);
1912}
1913
1914 imported_T *
1915find_imported_in_script(char_u *name, size_t len, int sid)
1916{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001917 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001918 int idx;
1919
Bram Moolenaare3d46852020-08-29 13:39:17 +02001920 if (!SCRIPT_ID_VALID(sid))
1921 return NULL;
1922 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1924 {
1925 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1926
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001927 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1928 : STRLEN(import->imp_name) == len
1929 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 return import;
1931 }
1932 return NULL;
1933}
1934
1935/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001936 * Free all imported variables.
1937 */
1938 static void
1939free_imported(cctx_T *cctx)
1940{
1941 int idx;
1942
1943 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1944 {
1945 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1946
1947 vim_free(import->imp_name);
1948 }
1949 ga_clear(&cctx->ctx_imports);
1950}
1951
1952/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001953 * Return TRUE if "p" points at a "#" but not at "#{".
1954 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001955 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001956vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001957{
1958 return p[0] == '#' && p[1] != '{';
1959}
1960
1961/*
1962 * Return a pointer to the next line that isn't empty or only contains a
1963 * comment. Skips over white space.
1964 * Returns NULL if there is none.
1965 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001966 char_u *
1967peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001968{
1969 int lnum = cctx->ctx_lnum;
1970
1971 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1972 {
1973 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001974 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001975
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001976 // ignore NULLs inserted for continuation lines
1977 if (line != NULL)
1978 {
1979 p = skipwhite(line);
1980 if (*p != NUL && !vim9_comment_start(p))
1981 return p;
1982 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001983 }
1984 return NULL;
1985}
1986
1987/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001988 * Called when checking for a following operator at "arg". When the rest of
1989 * the line is empty or only a comment, peek the next line. If there is a next
1990 * line return a pointer to it and set "nextp".
1991 * Otherwise skip over white space.
1992 */
1993 static char_u *
1994may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1995{
1996 char_u *p = skipwhite(arg);
1997
1998 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001999 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002000 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002001 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002002 if (*nextp != NULL)
2003 return *nextp;
2004 }
2005 return p;
2006}
2007
2008/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002009 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002010 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002011 * Returns NULL when at the end.
2012 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002013 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002014next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002015{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002016 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002017
2018 do
2019 {
2020 ++cctx->ctx_lnum;
2021 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002022 {
2023 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002024 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002025 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002026 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002027 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002028 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002029 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002030 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002031 return line;
2032}
2033
2034/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002035 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002036 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002037 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2038 */
2039 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002040may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002041{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002042 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002043 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002044 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002045
2046 if (next == NULL)
2047 return FAIL;
2048 *arg = skipwhite(next);
2049 }
2050 return OK;
2051}
2052
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002053/*
2054 * Idem, and give an error when failed.
2055 */
2056 static int
2057may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2058{
2059 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2060 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002061 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002062 return FAIL;
2063 }
2064 return OK;
2065}
2066
2067
Bram Moolenaara5565e42020-05-09 15:44:01 +02002068// Structure passed between the compile_expr* functions to keep track of
2069// constants that have been parsed but for which no code was produced yet. If
2070// possible expressions on these constants are applied at compile time. If
2071// that is not possible, the code to push the constants needs to be generated
2072// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002073// Using 50 should be more than enough of 5 levels of ().
2074#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002075typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002076 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002077 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002078 int pp_is_const; // all generated code was constants, used for a
2079 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002080} ppconst_T;
2081
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002082static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002083static int compile_expr0(char_u **arg, cctx_T *cctx);
2084static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2085
Bram Moolenaara5565e42020-05-09 15:44:01 +02002086/*
2087 * Generate a PUSH instruction for "tv".
2088 * "tv" will be consumed or cleared.
2089 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2090 */
2091 static int
2092generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2093{
2094 if (tv != NULL)
2095 {
2096 switch (tv->v_type)
2097 {
2098 case VAR_UNKNOWN:
2099 break;
2100 case VAR_BOOL:
2101 generate_PUSHBOOL(cctx, tv->vval.v_number);
2102 break;
2103 case VAR_SPECIAL:
2104 generate_PUSHSPEC(cctx, tv->vval.v_number);
2105 break;
2106 case VAR_NUMBER:
2107 generate_PUSHNR(cctx, tv->vval.v_number);
2108 break;
2109#ifdef FEAT_FLOAT
2110 case VAR_FLOAT:
2111 generate_PUSHF(cctx, tv->vval.v_float);
2112 break;
2113#endif
2114 case VAR_BLOB:
2115 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2116 tv->vval.v_blob = NULL;
2117 break;
2118 case VAR_STRING:
2119 generate_PUSHS(cctx, tv->vval.v_string);
2120 tv->vval.v_string = NULL;
2121 break;
2122 default:
2123 iemsg("constant type not supported");
2124 clear_tv(tv);
2125 return FAIL;
2126 }
2127 tv->v_type = VAR_UNKNOWN;
2128 }
2129 return OK;
2130}
2131
2132/*
2133 * Generate code for any ppconst entries.
2134 */
2135 static int
2136generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2137{
2138 int i;
2139 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002140 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002141
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002142 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002143 for (i = 0; i < ppconst->pp_used; ++i)
2144 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2145 ret = FAIL;
2146 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002147 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002148 return ret;
2149}
2150
2151/*
2152 * Clear ppconst constants. Used when failing.
2153 */
2154 static void
2155clear_ppconst(ppconst_T *ppconst)
2156{
2157 int i;
2158
2159 for (i = 0; i < ppconst->pp_used; ++i)
2160 clear_tv(&ppconst->pp_tv[i]);
2161 ppconst->pp_used = 0;
2162}
2163
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002164/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002165 * Generate an instruction to load script-local variable "name", without the
2166 * leading "s:".
2167 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 */
2169 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002170compile_load_scriptvar(
2171 cctx_T *cctx,
2172 char_u *name, // variable NUL terminated
2173 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002174 char_u **end, // end of variable
2175 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002177 scriptitem_T *si;
2178 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 imported_T *import;
2180
Bram Moolenaare3d46852020-08-29 13:39:17 +02002181 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2182 return FAIL;
2183 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002184 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002185 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002187 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002188 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2189 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 }
2191 if (idx >= 0)
2192 {
2193 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2194
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002195 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 current_sctx.sc_sid, idx, sv->sv_type);
2197 return OK;
2198 }
2199
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002200 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 if (import != NULL)
2202 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002203 if (import->imp_all)
2204 {
2205 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002206 char_u *exp_name;
2207 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002208 ufunc_T *ufunc;
2209 type_T *type;
2210
2211 // Used "import * as Name", need to lookup the member.
2212 if (*p != '.')
2213 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002214 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002215 return FAIL;
2216 }
2217 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002218 if (VIM_ISWHITE(*p))
2219 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002220 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002221 return FAIL;
2222 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002223
Bram Moolenaar1c991142020-07-04 13:15:31 +02002224 // isolate one name
2225 exp_name = p;
2226 while (eval_isnamec(*p))
2227 ++p;
2228 cc = *p;
2229 *p = NUL;
2230
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002231 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002232 *p = cc;
2233 p = skipwhite(p);
2234
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002235 // TODO: what if it is a function?
2236 if (idx < 0)
2237 return FAIL;
2238 *end = p;
2239
2240 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2241 import->imp_sid,
2242 idx,
2243 type);
2244 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002245 else if (import->imp_funcname != NULL)
2246 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002247 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002248 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2249 import->imp_sid,
2250 import->imp_var_vals_idx,
2251 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 return OK;
2253 }
2254
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002255 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002256 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 return FAIL;
2258}
2259
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002260 static int
2261generate_funcref(cctx_T *cctx, char_u *name)
2262{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002263 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002264
2265 if (ufunc == NULL)
2266 return FAIL;
2267
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002268 // Need to compile any default values to get the argument types.
2269 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2270 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2271 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002272 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002273}
2274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275/*
2276 * Compile a variable name into a load instruction.
2277 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002278 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 * When "error" is FALSE do not give an error when not found.
2280 */
2281 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002282compile_load(
2283 char_u **arg,
2284 char_u *end_arg,
2285 cctx_T *cctx,
2286 int is_expr,
2287 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288{
2289 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002290 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002291 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002293 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294
2295 if (*(*arg + 1) == ':')
2296 {
2297 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002298 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002299 {
2300 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002302 switch (**arg)
2303 {
2304 case 'g': isn_type = ISN_LOADGDICT; break;
2305 case 'w': isn_type = ISN_LOADWDICT; break;
2306 case 't': isn_type = ISN_LOADTDICT; break;
2307 case 'b': isn_type = ISN_LOADBDICT; break;
2308 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002309 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002310 goto theend;
2311 }
2312 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2313 goto theend;
2314 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 else
2317 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002318 isntype_T isn_type = ISN_DROP;
2319
2320 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2321 if (name == NULL)
2322 return FAIL;
2323
2324 switch (**arg)
2325 {
2326 case 'v': res = generate_LOADV(cctx, name, error);
2327 break;
2328 case 's': res = compile_load_scriptvar(cctx, name,
2329 NULL, NULL, error);
2330 break;
2331 case 'g': isn_type = ISN_LOADG; break;
2332 case 'w': isn_type = ISN_LOADW; break;
2333 case 't': isn_type = ISN_LOADT; break;
2334 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002335 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002336 goto theend;
2337 }
2338 if (isn_type != ISN_DROP)
2339 {
2340 // Global, Buffer-local, Window-local and Tabpage-local
2341 // variables can be defined later, thus we don't check if it
2342 // exists, give error at runtime.
2343 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 }
2346 }
2347 else
2348 {
2349 size_t len = end - *arg;
2350 int idx;
2351 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002352 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353
2354 name = vim_strnsave(*arg, end - *arg);
2355 if (name == NULL)
2356 return FAIL;
2357
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002358 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002360 if (!gen_load_outer)
2361 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 }
2363 else
2364 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002365 lvar_T *lvar = lookup_local(*arg, len, cctx);
2366
2367 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002369 type = lvar->lv_type;
2370 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002371 if (lvar->lv_from_outer)
2372 gen_load_outer = TRUE;
2373 else
2374 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 }
2376 else
2377 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002378 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002379 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002380 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002381 || find_imported(name, 0, cctx) != NULL)
2382 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002383
Bram Moolenaar0f769812020-09-12 18:32:34 +02002384 // When evaluating an expression and the name starts with an
2385 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002386 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002387 if (res == FAIL && is_expr
2388 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002389 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 }
2391 }
2392 if (gen_load)
2393 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002394 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002395 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002396 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002397 cctx->ctx_outer_used = TRUE;
2398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 }
2400
2401 *arg = end;
2402
2403theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002404 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002405 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 vim_free(name);
2407 return res;
2408}
2409
2410/*
2411 * Compile the argument expressions.
2412 * "arg" points to just after the "(" and is advanced to after the ")"
2413 */
2414 static int
2415compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2416{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002417 char_u *p = *arg;
2418 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002419 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420
Bram Moolenaare6085c52020-04-12 20:19:16 +02002421 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002423 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2424 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002425 if (*p == ')')
2426 {
2427 *arg = p + 1;
2428 return OK;
2429 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002430 if (must_end)
2431 {
2432 semsg(_(e_missing_comma_before_argument_str), p);
2433 return FAIL;
2434 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002435
Bram Moolenaara5565e42020-05-09 15:44:01 +02002436 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 return FAIL;
2438 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002439
2440 if (*p != ',' && *skipwhite(p) == ',')
2441 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002442 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002443 p = skipwhite(p);
2444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002446 {
2447 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002448 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002449 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002450 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002451 else
2452 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002453 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002454 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002456failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002457 emsg(_(e_missing_close));
2458 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459}
2460
2461/*
2462 * Compile a function call: name(arg1, arg2)
2463 * "arg" points to "name", "arg + varlen" to the "(".
2464 * "argcount_init" is 1 for "value->method()"
2465 * Instructions:
2466 * EVAL arg1
2467 * EVAL arg2
2468 * BCALL / DCALL / UCALL
2469 */
2470 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002471compile_call(
2472 char_u **arg,
2473 size_t varlen,
2474 cctx_T *cctx,
2475 ppconst_T *ppconst,
2476 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477{
2478 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002479 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 int argcount = argcount_init;
2481 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002482 char_u fname_buf[FLEN_FIXED + 1];
2483 char_u *tofree = NULL;
2484 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002486 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002487 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488
Bram Moolenaara5565e42020-05-09 15:44:01 +02002489 // we can evaluate "has('name')" at compile time
2490 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2491 {
2492 char_u *s = skipwhite(*arg + varlen + 1);
2493 typval_T argvars[2];
2494
2495 argvars[0].v_type = VAR_UNKNOWN;
2496 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002497 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002498 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002499 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002500 s = skipwhite(s);
2501 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2502 {
2503 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2504
2505 *arg = s + 1;
2506 argvars[1].v_type = VAR_UNKNOWN;
2507 tv->v_type = VAR_NUMBER;
2508 tv->vval.v_number = 0;
2509 f_has(argvars, tv);
2510 clear_tv(&argvars[0]);
2511 ++ppconst->pp_used;
2512 return OK;
2513 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002514 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002515 }
2516
2517 if (generate_ppconst(cctx, ppconst) == FAIL)
2518 return FAIL;
2519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 if (varlen >= sizeof(namebuf))
2521 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002522 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 return FAIL;
2524 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002525 vim_strncpy(namebuf, *arg, varlen);
2526 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527
2528 *arg = skipwhite(*arg + varlen + 1);
2529 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002530 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531
Bram Moolenaara1773442020-08-12 15:21:22 +02002532 is_autoload = vim_strchr(name, '#') != NULL;
2533 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 {
2535 int idx;
2536
2537 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002538 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002540 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002541 else
2542 semsg(_(e_unknownfunc), namebuf);
2543 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 }
2545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002547 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002548 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002549 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002550 {
2551 res = generate_CALL(cctx, ufunc, argcount);
2552 goto theend;
2553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554
2555 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002556 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002557 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002559 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002560 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002561 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002562 garray_T *stack = &cctx->ctx_type_stack;
2563 type_T *type;
2564
2565 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2566 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002567 goto theend;
2568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569
Bram Moolenaar0f769812020-09-12 18:32:34 +02002570 // If we can find a global function by name generate the right call.
2571 if (ufunc != NULL)
2572 {
2573 res = generate_CALL(cctx, ufunc, argcount);
2574 goto theend;
2575 }
2576
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002577 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002578 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002579 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002580 res = generate_UCALL(cctx, name, argcount);
2581 else
2582 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002583
2584theend:
2585 vim_free(tofree);
2586 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587}
2588
2589// like NAMESPACE_CHAR but with 'a' and 'l'.
2590#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2591
2592/*
2593 * Find the end of a variable or function name. Unlike find_name_end() this
2594 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002595 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 * Return a pointer to just after the name. Equal to "arg" if there is no
2597 * valid name.
2598 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002599 static char_u *
2600to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601{
2602 char_u *p;
2603
2604 // Quick check for valid starting character.
2605 if (!eval_isnamec1(*arg))
2606 return arg;
2607
2608 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2609 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2610 // and can be used in slice "[n:]".
2611 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002612 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2614 break;
2615 return p;
2616}
2617
2618/*
2619 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002620 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 */
2622 char_u *
2623to_name_const_end(char_u *arg)
2624{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002625 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 typval_T rettv;
2627
2628 if (p == arg && *arg == '[')
2629 {
2630
2631 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002632 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 p = arg;
2634 }
2635 else if (p == arg && *arg == '#' && arg[1] == '{')
2636 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002637 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002639 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 p = arg;
2641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642
2643 return p;
2644}
2645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646/*
2647 * parse a list: [expr, expr]
2648 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002649 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 */
2651 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002652compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653{
2654 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002655 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002657 int is_const;
2658 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002660 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002662 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002663 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002664 semsg(_(e_list_end), *arg);
2665 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002666 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002667 if (*p == ',')
2668 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002669 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002670 return FAIL;
2671 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002672 if (*p == ']')
2673 {
2674 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002675 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002676 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002677 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002678 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002679 if (!is_const)
2680 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 ++count;
2682 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002683 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002685 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2686 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002687 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002688 return FAIL;
2689 }
2690 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002691 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 p = skipwhite(p);
2693 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002694 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002696 ppconst->pp_is_const = is_all_const;
2697 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698}
2699
2700/*
2701 * parse a lambda: {arg, arg -> expr}
2702 * "*arg" points to the '{'.
2703 */
2704 static int
2705compile_lambda(char_u **arg, cctx_T *cctx)
2706{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 typval_T rettv;
2708 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002709 evalarg_T evalarg;
2710
2711 CLEAR_FIELD(evalarg);
2712 evalarg.eval_flags = EVAL_EVALUATE;
2713 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714
2715 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002716 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002717 {
2718 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002720 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002723 ++ufunc->uf_refcount;
2724 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002725 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726
2727 // The function will have one line: "return {expr}".
2728 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002729 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002731 clear_evalarg(&evalarg, NULL);
2732
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002733 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002734 {
2735 // The return type will now be known.
2736 set_function_type(ufunc);
2737
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002738 // The function reference count will be 1. When the ISN_FUNCREF
2739 // instruction is deleted the reference count is decremented and the
2740 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002741 return generate_FUNCREF(cctx, ufunc);
2742 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002743
2744 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 return FAIL;
2746}
2747
2748/*
2749 * Compile a lamda call: expr->{lambda}(args)
2750 * "arg" points to the "{".
2751 */
2752 static int
2753compile_lambda_call(char_u **arg, cctx_T *cctx)
2754{
2755 ufunc_T *ufunc;
2756 typval_T rettv;
2757 int argcount = 1;
2758 int ret = FAIL;
2759
2760 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002761 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 return FAIL;
2763
2764 if (**arg != '(')
2765 {
2766 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002767 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 else
2769 semsg(_(e_missing_paren), "lambda");
2770 clear_tv(&rettv);
2771 return FAIL;
2772 }
2773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 ufunc = rettv.vval.v_partial->pt_func;
2775 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002776 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002777 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002778
Bram Moolenaara05e5242020-09-19 18:19:19 +02002779 // The function will have one line: "return {expr}". Compile it into
2780 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002781 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782
2783 // compile the arguments
2784 *arg = skipwhite(*arg + 1);
2785 if (compile_arguments(arg, cctx, &argcount) == OK)
2786 // call the compiled function
2787 ret = generate_CALL(cctx, ufunc, argcount);
2788
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002789 if (ret == FAIL)
2790 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 return ret;
2792}
2793
2794/*
2795 * parse a dict: {'key': val} or #{key: val}
2796 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002797 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 */
2799 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002800compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801{
2802 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002803 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 int count = 0;
2805 dict_T *d = dict_alloc();
2806 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002807 char_u *whitep = *arg;
2808 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002809 int is_const;
2810 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811
2812 if (d == NULL)
2813 return FAIL;
2814 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002815 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 {
2817 char_u *key = NULL;
2818
Bram Moolenaar23c55272020-06-21 16:58:13 +02002819 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002820 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002821 *arg = NULL;
2822 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002823 }
2824
2825 if (**arg == '}')
2826 break;
2827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 if (literal)
2829 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002830 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831
Bram Moolenaar2c330432020-04-13 14:41:35 +02002832 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002834 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 return FAIL;
2836 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002837 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 if (generate_PUSHS(cctx, key) == FAIL)
2839 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002840 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 }
2842 else
2843 {
2844 isn_T *isn;
2845
Bram Moolenaara5565e42020-05-09 15:44:01 +02002846 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2849 if (isn->isn_type == ISN_PUSHS)
2850 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002851 else
2852 {
2853 type_T *keytype = ((type_T **)stack->ga_data)
2854 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002855 if (need_type(keytype, &t_string, -1, cctx,
2856 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002857 return FAIL;
2858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 }
2860
2861 // Check for duplicate keys, if using string keys.
2862 if (key != NULL)
2863 {
2864 item = dict_find(d, key, -1);
2865 if (item != NULL)
2866 {
2867 semsg(_(e_duplicate_key), key);
2868 goto failret;
2869 }
2870 item = dictitem_alloc(key);
2871 if (item != NULL)
2872 {
2873 item->di_tv.v_type = VAR_UNKNOWN;
2874 item->di_tv.v_lock = 0;
2875 if (dict_add(d, item) == FAIL)
2876 dictitem_free(item);
2877 }
2878 }
2879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 if (**arg != ':')
2881 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002882 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002883 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002884 else
2885 semsg(_(e_missing_dict_colon), *arg);
2886 return FAIL;
2887 }
2888 whitep = *arg + 1;
2889 if (!IS_WHITE_OR_NUL(*whitep))
2890 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002891 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 return FAIL;
2893 }
2894
2895 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002896 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002897 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002898 *arg = NULL;
2899 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002900 }
2901
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002902 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002904 if (!is_const)
2905 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 ++count;
2907
Bram Moolenaar2c330432020-04-13 14:41:35 +02002908 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002909 *arg = skipwhite(*arg);
2910 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002911 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002912 *arg = NULL;
2913 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 if (**arg == '}')
2916 break;
2917 if (**arg != ',')
2918 {
2919 semsg(_(e_missing_dict_comma), *arg);
2920 goto failret;
2921 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002922 if (IS_WHITE_OR_NUL(*whitep))
2923 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002924 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002925 return FAIL;
2926 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002927 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 *arg = skipwhite(*arg + 1);
2929 }
2930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 *arg = *arg + 1;
2932
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002933 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002934 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002935 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002936 *arg += STRLEN(*arg);
2937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002939 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 return generate_NEWDICT(cctx, count);
2941
2942failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002943 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002944 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002945 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002946 *arg = (char_u *)"";
2947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 dict_unref(d);
2949 return FAIL;
2950}
2951
2952/*
2953 * Compile "&option".
2954 */
2955 static int
2956compile_get_option(char_u **arg, cctx_T *cctx)
2957{
2958 typval_T rettv;
2959 char_u *start = *arg;
2960 int ret;
2961
2962 // parse the option and get the current value to get the type.
2963 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002964 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 if (ret == OK)
2966 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002967 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 char_u *name = vim_strnsave(start, *arg - start);
2969 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2970
2971 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2972 vim_free(name);
2973 }
2974 clear_tv(&rettv);
2975
2976 return ret;
2977}
2978
2979/*
2980 * Compile "$VAR".
2981 */
2982 static int
2983compile_get_env(char_u **arg, cctx_T *cctx)
2984{
2985 char_u *start = *arg;
2986 int len;
2987 int ret;
2988 char_u *name;
2989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 ++*arg;
2991 len = get_env_len(arg);
2992 if (len == 0)
2993 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002994 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 return FAIL;
2996 }
2997
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002998 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 name = vim_strnsave(start, len + 1);
3000 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3001 vim_free(name);
3002 return ret;
3003}
3004
3005/*
3006 * Compile "@r".
3007 */
3008 static int
3009compile_get_register(char_u **arg, cctx_T *cctx)
3010{
3011 int ret;
3012
3013 ++*arg;
3014 if (**arg == NUL)
3015 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003016 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 return FAIL;
3018 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003019 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 {
3021 emsg_invreg(**arg);
3022 return FAIL;
3023 }
3024 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3025 ++*arg;
3026 return ret;
3027}
3028
3029/*
3030 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003031 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 */
3033 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003034apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003036 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037
3038 // this works from end to start
3039 while (p > start)
3040 {
3041 --p;
3042 if (*p == '-' || *p == '+')
3043 {
3044 // only '-' has an effect, for '+' we only check the type
3045#ifdef FEAT_FLOAT
3046 if (rettv->v_type == VAR_FLOAT)
3047 {
3048 if (*p == '-')
3049 rettv->vval.v_float = -rettv->vval.v_float;
3050 }
3051 else
3052#endif
3053 {
3054 varnumber_T val;
3055 int error = FALSE;
3056
3057 // tv_get_number_chk() accepts a string, but we don't want that
3058 // here
3059 if (check_not_string(rettv) == FAIL)
3060 return FAIL;
3061 val = tv_get_number_chk(rettv, &error);
3062 clear_tv(rettv);
3063 if (error)
3064 return FAIL;
3065 if (*p == '-')
3066 val = -val;
3067 rettv->v_type = VAR_NUMBER;
3068 rettv->vval.v_number = val;
3069 }
3070 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003071 else if (numeric_only)
3072 {
3073 ++p;
3074 break;
3075 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003076 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 {
3078 int v = tv2bool(rettv);
3079
3080 // '!' is permissive in the type.
3081 clear_tv(rettv);
3082 rettv->v_type = VAR_BOOL;
3083 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3084 }
3085 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003086 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 return OK;
3088}
3089
3090/*
3091 * Recognize v: variables that are constants and set "rettv".
3092 */
3093 static void
3094get_vim_constant(char_u **arg, typval_T *rettv)
3095{
3096 if (STRNCMP(*arg, "v:true", 6) == 0)
3097 {
3098 rettv->v_type = VAR_BOOL;
3099 rettv->vval.v_number = VVAL_TRUE;
3100 *arg += 6;
3101 }
3102 else if (STRNCMP(*arg, "v:false", 7) == 0)
3103 {
3104 rettv->v_type = VAR_BOOL;
3105 rettv->vval.v_number = VVAL_FALSE;
3106 *arg += 7;
3107 }
3108 else if (STRNCMP(*arg, "v:null", 6) == 0)
3109 {
3110 rettv->v_type = VAR_SPECIAL;
3111 rettv->vval.v_number = VVAL_NULL;
3112 *arg += 6;
3113 }
3114 else if (STRNCMP(*arg, "v:none", 6) == 0)
3115 {
3116 rettv->v_type = VAR_SPECIAL;
3117 rettv->vval.v_number = VVAL_NONE;
3118 *arg += 6;
3119 }
3120}
3121
Bram Moolenaar696ba232020-07-29 21:20:41 +02003122 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003123get_compare_type(char_u *p, int *len, int *type_is)
3124{
3125 exptype_T type = EXPR_UNKNOWN;
3126 int i;
3127
3128 switch (p[0])
3129 {
3130 case '=': if (p[1] == '=')
3131 type = EXPR_EQUAL;
3132 else if (p[1] == '~')
3133 type = EXPR_MATCH;
3134 break;
3135 case '!': if (p[1] == '=')
3136 type = EXPR_NEQUAL;
3137 else if (p[1] == '~')
3138 type = EXPR_NOMATCH;
3139 break;
3140 case '>': if (p[1] != '=')
3141 {
3142 type = EXPR_GREATER;
3143 *len = 1;
3144 }
3145 else
3146 type = EXPR_GEQUAL;
3147 break;
3148 case '<': if (p[1] != '=')
3149 {
3150 type = EXPR_SMALLER;
3151 *len = 1;
3152 }
3153 else
3154 type = EXPR_SEQUAL;
3155 break;
3156 case 'i': if (p[1] == 's')
3157 {
3158 // "is" and "isnot"; but not a prefix of a name
3159 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3160 *len = 5;
3161 i = p[*len];
3162 if (!isalnum(i) && i != '_')
3163 {
3164 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3165 *type_is = TRUE;
3166 }
3167 }
3168 break;
3169 }
3170 return type;
3171}
3172
3173/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003175 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 */
3177 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003178compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003180 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181
3182 // this works from end to start
3183 while (p > start)
3184 {
3185 --p;
3186 if (*p == '-' || *p == '+')
3187 {
3188 int negate = *p == '-';
3189 isn_T *isn;
3190
3191 // TODO: check type
3192 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3193 {
3194 --p;
3195 if (*p == '-')
3196 negate = !negate;
3197 }
3198 // only '-' has an effect, for '+' we only check the type
3199 if (negate)
3200 isn = generate_instr(cctx, ISN_NEGATENR);
3201 else
3202 isn = generate_instr(cctx, ISN_CHECKNR);
3203 if (isn == NULL)
3204 return FAIL;
3205 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003206 else if (numeric_only)
3207 {
3208 ++p;
3209 break;
3210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 else
3212 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003213 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003215 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003217 if (p[-1] == '!')
3218 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 }
3221 if (generate_2BOOL(cctx, invert) == FAIL)
3222 return FAIL;
3223 }
3224 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003225 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 return OK;
3227}
3228
3229/*
3230 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003231 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 */
3233 static int
3234compile_subscript(
3235 char_u **arg,
3236 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003237 char_u *start_leader,
3238 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003239 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003241 char_u *name_start = *end_leader;
3242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 for (;;)
3244 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003245 char_u *p = skipwhite(*arg);
3246
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003247 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003248 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003249 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003250
3251 // If a following line starts with "->{" or "->X" advance to that
3252 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003253 // Also if a following line starts with ".x".
3254 if (next != NULL &&
3255 ((next[0] == '-' && next[1] == '>'
3256 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003257 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003258 {
3259 next = next_line_from_context(cctx, TRUE);
3260 if (next == NULL)
3261 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003262 *arg = next;
3263 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003264 }
3265 }
3266
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003267 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3268 // not a function call.
3269 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003271 garray_T *stack = &cctx->ctx_type_stack;
3272 type_T *type;
3273 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003275 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003276 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003277 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003280 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3281
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003282 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3284 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003285 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 return FAIL;
3287 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003288 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003290 char_u *pstart = p;
3291
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003292 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003293 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003294 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 // something->method()
3297 // Apply the '!', '-' and '+' first:
3298 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003299 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003302 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003303 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003304 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 if (**arg == '{')
3306 {
3307 // lambda call: list->{lambda}
3308 if (compile_lambda_call(arg, cctx) == FAIL)
3309 return FAIL;
3310 }
3311 else
3312 {
3313 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003314 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003315 if (!eval_isnamec1(*p))
3316 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003317 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003318 return FAIL;
3319 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003320 if (ASCII_ISALPHA(*p) && p[1] == ':')
3321 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003322 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 ;
3324 if (*p != '(')
3325 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003326 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 return FAIL;
3328 }
3329 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003330 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 return FAIL;
3332 }
3333 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003334 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003336 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003337 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003338 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003339 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003340 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003341
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003342 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003343 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003344 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003345 // TODO: blob index
3346 // TODO: more arguments
3347 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003348 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003349 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003350 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003351
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003352 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003353 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003354 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003355 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003356 if (**arg == ':')
3357 // missing first index is equal to zero
3358 generate_PUSHNR(cctx, 0);
3359 else
3360 {
3361 if (compile_expr0(arg, cctx) == FAIL)
3362 return FAIL;
3363 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3364 return FAIL;
3365 *arg = skipwhite(*arg);
3366 }
3367 if (**arg == ':')
3368 {
3369 *arg = skipwhite(*arg + 1);
3370 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3371 return FAIL;
3372 if (**arg == ']')
3373 // missing second index is equal to end of string
3374 generate_PUSHNR(cctx, -1);
3375 else
3376 {
3377 if (compile_expr0(arg, cctx) == FAIL)
3378 return FAIL;
3379 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3380 return FAIL;
3381 *arg = skipwhite(*arg);
3382 }
3383 is_slice = TRUE;
3384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385
3386 if (**arg != ']')
3387 {
3388 emsg(_(e_missbrac));
3389 return FAIL;
3390 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003391 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003393 // We can index a list and a dict. If we don't know the type
3394 // we can use the index value type.
3395 // TODO: If we don't know use an instruction to figure it out at
3396 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003397 typep = ((type_T **)stack->ga_data) + stack->ga_len
3398 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003399 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003400 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3401 // If the index is a string, the variable must be a Dict.
3402 if (*typep == &t_any && valtype == &t_string)
3403 vtype = VAR_DICT;
3404 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003405 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003406 if (need_type(valtype, &t_number, -1, cctx,
3407 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003408 return FAIL;
3409 if (is_slice)
3410 {
3411 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003412 if (need_type(valtype, &t_number, -2, cctx,
3413 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003414 return FAIL;
3415 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003416 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003417
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003418 if (vtype == VAR_DICT)
3419 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003420 if (is_slice)
3421 {
3422 emsg(_(e_cannot_slice_dictionary));
3423 return FAIL;
3424 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003425 if ((*typep)->tt_type == VAR_DICT)
3426 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003427 else
3428 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003429 if (need_type(*typep, &t_dict_any, -2, cctx,
3430 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003431 return FAIL;
3432 *typep = &t_any;
3433 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003434 if (may_generate_2STRING(-1, cctx) == FAIL)
3435 return FAIL;
3436 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3437 return FAIL;
3438 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003439 else if (vtype == VAR_STRING)
3440 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003441 *typep = &t_string;
3442 if ((is_slice
3443 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3444 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003445 return FAIL;
3446 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003447 else if (vtype == VAR_BLOB)
3448 {
3449 emsg("Sorry, blob index and slice not implemented yet");
3450 return FAIL;
3451 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003452 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003453 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003454 if (is_slice)
3455 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003456 if (generate_instr_drop(cctx,
3457 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3458 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003459 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003460 }
Bram Moolenaared591872020-08-15 22:14:53 +02003461 else
3462 {
3463 if ((*typep)->tt_type == VAR_LIST)
3464 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003465 if (generate_instr_drop(cctx,
3466 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3467 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003468 return FAIL;
3469 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003470 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003471 else
3472 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003473 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003474 return FAIL;
3475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003477 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003479 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003480 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003481 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003482 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003483
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003484 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003485 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003486 {
3487 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003488 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003489 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003490 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003491 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 while (eval_isnamec(*p))
3493 MB_PTR_ADV(p);
3494 if (p == *arg)
3495 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003496 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 return FAIL;
3498 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003499 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 return FAIL;
3501 *arg = p;
3502 }
3503 else
3504 break;
3505 }
3506
3507 // TODO - see handle_subscript():
3508 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3509 // Don't do this when "Func" is already a partial that was bound
3510 // explicitly (pt_auto is FALSE).
3511
3512 return OK;
3513}
3514
3515/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003516 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3517 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003519 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003520 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003521 *
3522 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 */
3524
3525/*
3526 * number number constant
3527 * 0zFFFFFFFF Blob constant
3528 * "string" string constant
3529 * 'string' literal string constant
3530 * &option-name option value
3531 * @r register contents
3532 * identifier variable value
3533 * function() function call
3534 * $VAR environment variable
3535 * (expression) nested expression
3536 * [expr, expr] List
3537 * {key: val, key: val} Dictionary
3538 * #{key: val, key: val} Dictionary with literal keys
3539 *
3540 * Also handle:
3541 * ! in front logical NOT
3542 * - in front unary minus
3543 * + in front unary plus (ignored)
3544 * trailing (arg) funcref/partial call
3545 * trailing [] subscript in String or List
3546 * trailing .name entry in Dictionary
3547 * trailing ->name() method call
3548 */
3549 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003550compile_expr7(
3551 char_u **arg,
3552 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003553 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 char_u *start_leader, *end_leader;
3556 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003557 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003558 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003560 ppconst->pp_is_const = FALSE;
3561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 /*
3563 * Skip '!', '-' and '+' characters. They are handled later.
3564 */
3565 start_leader = *arg;
3566 while (**arg == '!' || **arg == '-' || **arg == '+')
3567 *arg = skipwhite(*arg + 1);
3568 end_leader = *arg;
3569
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003570 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 switch (**arg)
3572 {
3573 /*
3574 * Number constant.
3575 */
3576 case '0': // also for blob starting with 0z
3577 case '1':
3578 case '2':
3579 case '3':
3580 case '4':
3581 case '5':
3582 case '6':
3583 case '7':
3584 case '8':
3585 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003588 // Apply "-" and "+" just before the number now, right to
3589 // left. Matters especially when "->" follows. Stops at
3590 // '!'.
3591 if (apply_leader(rettv, TRUE,
3592 start_leader, &end_leader) == FAIL)
3593 {
3594 clear_tv(rettv);
3595 return FAIL;
3596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 break;
3598
3599 /*
3600 * String constant: "string".
3601 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003602 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 return FAIL;
3604 break;
3605
3606 /*
3607 * Literal string constant: 'str''ing'.
3608 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003609 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 return FAIL;
3611 break;
3612
3613 /*
3614 * Constant Vim variable.
3615 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003616 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 ret = NOTDONE;
3618 break;
3619
3620 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003621 * "true" constant
3622 */
3623 case 't': if (STRNCMP(*arg, "true", 4) == 0
3624 && !eval_isnamec((*arg)[4]))
3625 {
3626 *arg += 4;
3627 rettv->v_type = VAR_BOOL;
3628 rettv->vval.v_number = VVAL_TRUE;
3629 }
3630 else
3631 ret = NOTDONE;
3632 break;
3633
3634 /*
3635 * "false" constant
3636 */
3637 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3638 && !eval_isnamec((*arg)[5]))
3639 {
3640 *arg += 5;
3641 rettv->v_type = VAR_BOOL;
3642 rettv->vval.v_number = VVAL_FALSE;
3643 }
3644 else
3645 ret = NOTDONE;
3646 break;
3647
3648 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 * List: [expr, expr]
3650 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003651 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 break;
3653
3654 /*
3655 * Dictionary: #{key: val, key: val}
3656 */
3657 case '#': if ((*arg)[1] == '{')
3658 {
3659 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003660 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662 else
3663 ret = NOTDONE;
3664 break;
3665
3666 /*
3667 * Lambda: {arg, arg -> expr}
3668 * Dictionary: {'key': val, 'key': val}
3669 */
3670 case '{': {
3671 char_u *start = skipwhite(*arg + 1);
3672
3673 // Find out what comes after the arguments.
3674 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003675 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 if (ret != FAIL && *start == '>')
3677 ret = compile_lambda(arg, cctx);
3678 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003679 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 }
3681 break;
3682
3683 /*
3684 * Option value: &name
3685 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003686 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3687 return FAIL;
3688 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 break;
3690
3691 /*
3692 * Environment variable: $VAR.
3693 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003694 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3695 return FAIL;
3696 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 break;
3698
3699 /*
3700 * Register contents: @r.
3701 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003702 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3703 return FAIL;
3704 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 break;
3706 /*
3707 * nested expression: (expression).
3708 */
3709 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003710
3711 // recursive!
3712 if (ppconst->pp_used <= PPSIZE - 10)
3713 {
3714 ret = compile_expr1(arg, cctx, ppconst);
3715 }
3716 else
3717 {
3718 // Not enough space in ppconst, flush constants.
3719 if (generate_ppconst(cctx, ppconst) == FAIL)
3720 return FAIL;
3721 ret = compile_expr0(arg, cctx);
3722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 *arg = skipwhite(*arg);
3724 if (**arg == ')')
3725 ++*arg;
3726 else if (ret == OK)
3727 {
3728 emsg(_(e_missing_close));
3729 ret = FAIL;
3730 }
3731 break;
3732
3733 default: ret = NOTDONE;
3734 break;
3735 }
3736 if (ret == FAIL)
3737 return FAIL;
3738
Bram Moolenaar1c747212020-05-09 18:28:34 +02003739 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003741 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003742 clear_tv(rettv);
3743 else
3744 // A constant expression can possibly be handled compile time,
3745 // return the value instead of generating code.
3746 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 }
3748 else if (ret == NOTDONE)
3749 {
3750 char_u *p;
3751 int r;
3752
3753 if (!eval_isnamec1(**arg))
3754 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003755 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 return FAIL;
3757 }
3758
3759 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003760 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003762 {
3763 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003766 {
3767 if (generate_ppconst(cctx, ppconst) == FAIL)
3768 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003769 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 if (r == FAIL)
3772 return FAIL;
3773 }
3774
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003775 // Handle following "[]", ".member", etc.
3776 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003777 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003778 ppconst) == FAIL)
3779 return FAIL;
3780 if (ppconst->pp_used > 0)
3781 {
3782 // apply the '!', '-' and '+' before the constant
3783 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003784 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003785 return FAIL;
3786 return OK;
3787 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003788 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003790 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791}
3792
3793/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003794 * Give the "white on both sides" error, taking the operator from "p[len]".
3795 */
3796 void
3797error_white_both(char_u *op, int len)
3798{
3799 char_u buf[10];
3800
3801 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003802 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003803}
3804
3805/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003806 * <type>expr7: runtime type check / conversion
3807 */
3808 static int
3809compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3810{
3811 type_T *want_type = NULL;
3812
3813 // Recognize <type>
3814 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3815 {
3816 int called_emsg_before = called_emsg;
3817
3818 ++*arg;
3819 want_type = parse_type(arg, cctx->ctx_type_list);
3820 if (called_emsg != called_emsg_before)
3821 return FAIL;
3822
3823 if (**arg != '>')
3824 {
3825 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003826 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003827 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003828 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003829 return FAIL;
3830 }
3831 ++*arg;
3832 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3833 return FAIL;
3834 }
3835
3836 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3837 return FAIL;
3838
3839 if (want_type != NULL)
3840 {
3841 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003842 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003843
Bram Moolenaard1103582020-08-14 22:44:25 +02003844 generate_ppconst(cctx, ppconst);
3845 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003846 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003847 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003848 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003849 return FAIL;
3850 }
3851 }
3852
3853 return OK;
3854}
3855
3856/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 * * number multiplication
3858 * / number division
3859 * % number modulo
3860 */
3861 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003862compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863{
3864 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003865 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003866 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003868 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003869 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 return FAIL;
3871
3872 /*
3873 * Repeat computing, until no "*", "/" or "%" is following.
3874 */
3875 for (;;)
3876 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003877 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 if (*op != '*' && *op != '/' && *op != '%')
3879 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003880 if (next != NULL)
3881 {
3882 *arg = next_line_from_context(cctx, TRUE);
3883 op = skipwhite(*arg);
3884 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003885
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003886 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003888 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003889 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 }
3891 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003892 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003893 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003895 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003896 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003898
3899 if (ppconst->pp_used == ppconst_used + 2
3900 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3901 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003902 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003903 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3904 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003905 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003907 // both are numbers: compute the result
3908 switch (*op)
3909 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003910 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003911 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003912 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003913 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003914 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003915 break;
3916 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003917 tv1->vval.v_number = res;
3918 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003919 }
3920 else
3921 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003922 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003923 generate_two_op(cctx, op);
3924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 }
3926
3927 return OK;
3928}
3929
3930/*
3931 * + number addition
3932 * - number subtraction
3933 * .. string concatenation
3934 */
3935 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003936compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937{
3938 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003939 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003941 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942
3943 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003944 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 return FAIL;
3946
3947 /*
3948 * Repeat computing, until no "+", "-" or ".." is following.
3949 */
3950 for (;;)
3951 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003952 op = may_peek_next_line(cctx, *arg, &next);
3953 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 break;
3955 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003956 if (next != NULL)
3957 {
3958 *arg = next_line_from_context(cctx, TRUE);
3959 op = skipwhite(*arg);
3960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003962 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003964 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003965 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 }
3967
3968 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003969 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003970 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003972 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003973 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 return FAIL;
3975
Bram Moolenaara5565e42020-05-09 15:44:01 +02003976 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003977 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003978 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3979 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3980 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3981 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003983 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3984 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003985
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003986 // concat/subtract/add constant numbers
3987 if (*op == '+')
3988 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3989 else if (*op == '-')
3990 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3991 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003992 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003993 // concatenate constant strings
3994 char_u *s1 = tv1->vval.v_string;
3995 char_u *s2 = tv2->vval.v_string;
3996 size_t len1 = STRLEN(s1);
3997
3998 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3999 if (tv1->vval.v_string == NULL)
4000 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004001 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004002 return FAIL;
4003 }
4004 mch_memmove(tv1->vval.v_string, s1, len1);
4005 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004006 vim_free(s1);
4007 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004008 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 }
4011 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004012 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004014 if (*op == '.')
4015 {
4016 if (may_generate_2STRING(-2, cctx) == FAIL
4017 || may_generate_2STRING(-1, cctx) == FAIL)
4018 return FAIL;
4019 generate_instr_drop(cctx, ISN_CONCAT, 1);
4020 }
4021 else
4022 generate_two_op(cctx, op);
4023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 }
4025
4026 return OK;
4027}
4028
4029/*
4030 * expr5a == expr5b
4031 * expr5a =~ expr5b
4032 * expr5a != expr5b
4033 * expr5a !~ expr5b
4034 * expr5a > expr5b
4035 * expr5a >= expr5b
4036 * expr5a < expr5b
4037 * expr5a <= expr5b
4038 * expr5a is expr5b
4039 * expr5a isnot expr5b
4040 *
4041 * Produces instructions:
4042 * EVAL expr5a Push result of "expr5a"
4043 * EVAL expr5b Push result of "expr5b"
4044 * COMPARE one of the compare instructions
4045 */
4046 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048{
4049 exptype_T type = EXPR_UNKNOWN;
4050 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004051 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055
4056 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return FAIL;
4059
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004060 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004061 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062
4063 /*
4064 * If there is a comparative operator, use it.
4065 */
4066 if (type != EXPR_UNKNOWN)
4067 {
4068 int ic = FALSE; // Default: do not ignore case
4069
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004070 if (next != NULL)
4071 {
4072 *arg = next_line_from_context(cctx, TRUE);
4073 p = skipwhite(*arg);
4074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 if (type_is && (p[len] == '?' || p[len] == '#'))
4076 {
4077 semsg(_(e_invexpr2), *arg);
4078 return FAIL;
4079 }
4080 // extra question mark appended: ignore case
4081 if (p[len] == '?')
4082 {
4083 ic = TRUE;
4084 ++len;
4085 }
4086 // extra '#' appended: match case (ignored)
4087 else if (p[len] == '#')
4088 ++len;
4089 // nothing appended: match case
4090
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004091 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004093 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004094 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 }
4096
4097 // get the second variable
4098 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004099 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004100 return FAIL;
4101
Bram Moolenaara5565e42020-05-09 15:44:01 +02004102 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 return FAIL;
4104
Bram Moolenaara5565e42020-05-09 15:44:01 +02004105 if (ppconst->pp_used == ppconst_used + 2)
4106 {
4107 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4108 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4109 int ret;
4110
4111 // Both sides are a constant, compute the result now.
4112 // First check for a valid combination of types, this is more
4113 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004114 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004115 ret = FAIL;
4116 else
4117 {
4118 ret = typval_compare(tv1, tv2, type, ic);
4119 tv1->v_type = VAR_BOOL;
4120 tv1->vval.v_number = tv1->vval.v_number
4121 ? VVAL_TRUE : VVAL_FALSE;
4122 clear_tv(tv2);
4123 --ppconst->pp_used;
4124 }
4125 return ret;
4126 }
4127
4128 generate_ppconst(cctx, ppconst);
4129 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 }
4131
4132 return OK;
4133}
4134
Bram Moolenaar7f141552020-05-09 17:35:53 +02004135static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137/*
4138 * Compile || or &&.
4139 */
4140 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141compile_and_or(
4142 char_u **arg,
4143 cctx_T *cctx,
4144 char *op,
4145 ppconst_T *ppconst,
4146 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004148 char_u *next;
4149 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 int opchar = *op;
4151
4152 if (p[0] == opchar && p[1] == opchar)
4153 {
4154 garray_T *instr = &cctx->ctx_instr;
4155 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004156 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004157 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158
4159 /*
4160 * Repeat until there is no following "||" or "&&"
4161 */
4162 ga_init2(&end_ga, sizeof(int), 10);
4163 while (p[0] == opchar && p[1] == opchar)
4164 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004165 if (next != NULL)
4166 {
4167 *arg = next_line_from_context(cctx, TRUE);
4168 p = skipwhite(*arg);
4169 }
4170
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004171 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4172 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004173 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004174 return FAIL;
4175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004177 // TODO: use ppconst if the value is a constant and check
4178 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004179 generate_ppconst(cctx, ppconst);
4180
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004181 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4182 all_bool_values = FALSE;
4183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 if (ga_grow(&end_ga, 1) == FAIL)
4185 {
4186 ga_clear(&end_ga);
4187 return FAIL;
4188 }
4189 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4190 ++end_ga.ga_len;
4191 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004192 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193
4194 // eval the next expression
4195 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004196 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004197 return FAIL;
4198
Bram Moolenaara5565e42020-05-09 15:44:01 +02004199 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4200 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 {
4202 ga_clear(&end_ga);
4203 return FAIL;
4204 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004205
4206 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004208 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209
4210 // Fill in the end label in all jumps.
4211 while (end_ga.ga_len > 0)
4212 {
4213 isn_T *isn;
4214
4215 --end_ga.ga_len;
4216 isn = ((isn_T *)instr->ga_data)
4217 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4218 isn->isn_arg.jump.jump_where = instr->ga_len;
4219 }
4220 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004221
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004222 // The resulting type is converted to bool if needed.
4223 if (!all_bool_values)
4224 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 }
4226
4227 return OK;
4228}
4229
4230/*
4231 * expr4a && expr4a && expr4a logical AND
4232 *
4233 * Produces instructions:
4234 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004235 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004237 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004239 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 * end:
4241 */
4242 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004243compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004245 int ppconst_used = ppconst->pp_used;
4246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004248 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 return FAIL;
4250
4251 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004252 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253}
4254
4255/*
4256 * expr3a || expr3b || expr3c logical OR
4257 *
4258 * Produces instructions:
4259 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004260 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004262 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004264 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 * end:
4266 */
4267 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 int ppconst_used = ppconst->pp_used;
4271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004273 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 return FAIL;
4275
4276 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278}
4279
4280/*
4281 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004283 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 * JUMP_IF_FALSE alt jump if false
4285 * EVAL expr1a
4286 * JUMP_ALWAYS end
4287 * alt: EVAL expr1b
4288 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004289 *
4290 * Toplevel expression: expr2 ?? expr1
4291 * Produces instructions:
4292 * EVAL expr2 Push result of "expr2"
4293 * JUMP_AND_KEEP_IF_TRUE end jump if true
4294 * EVAL expr1
4295 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 */
4297 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004298compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299{
4300 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004302 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
Bram Moolenaar3988f642020-08-27 22:43:03 +02004304 // Ignore all kinds of errors when not producing code.
4305 if (cctx->ctx_skip == SKIP_YES)
4306 {
4307 skip_expr(arg);
4308 return OK;
4309 }
4310
Bram Moolenaar61a89812020-05-07 16:58:17 +02004311 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004312 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 return FAIL;
4314
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004315 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 if (*p == '?')
4317 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004318 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 garray_T *instr = &cctx->ctx_instr;
4320 garray_T *stack = &cctx->ctx_type_stack;
4321 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004322 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004324 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 int has_const_expr = FALSE;
4326 int const_value = FALSE;
4327 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004329 if (next != NULL)
4330 {
4331 *arg = next_line_from_context(cctx, TRUE);
4332 p = skipwhite(*arg);
4333 }
4334
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004335 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004336 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004337 semsg(_(e_white_space_required_before_and_after_str),
4338 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004339 return FAIL;
4340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 if (ppconst->pp_used == ppconst_used + 1)
4343 {
4344 // the condition is a constant, we know whether the ? or the :
4345 // expression is to be evaluated.
4346 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004347 if (op_falsy)
4348 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4349 else
4350 {
4351 int error = FALSE;
4352
4353 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4354 &error);
4355 if (error)
4356 return FAIL;
4357 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004358 cctx->ctx_skip = save_skip == SKIP_YES ||
4359 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4360
4361 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4362 // "left ?? right" and "left" is truthy: produce "left"
4363 generate_ppconst(cctx, ppconst);
4364 else
4365 {
4366 clear_tv(&ppconst->pp_tv[ppconst_used]);
4367 --ppconst->pp_used;
4368 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 }
4370 else
4371 {
4372 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004373 if (op_falsy)
4374 end_idx = instr->ga_len;
4375 generate_JUMP(cctx, op_falsy
4376 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4377 if (op_falsy)
4378 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380
4381 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004382 *arg = skipwhite(p + 1 + op_falsy);
4383 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004384 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004386 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 if (!has_const_expr)
4389 {
4390 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004392 if (!op_falsy)
4393 {
4394 // remember the type and drop it
4395 --stack->ga_len;
4396 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004398 end_idx = instr->ga_len;
4399 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004401 // jump here from JUMP_IF_FALSE
4402 isn = ((isn_T *)instr->ga_data) + alt_idx;
4403 isn->isn_arg.jump.jump_where = instr->ga_len;
4404 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004407 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004409 // Check for the ":".
4410 p = may_peek_next_line(cctx, *arg, &next);
4411 if (*p != ':')
4412 {
4413 emsg(_(e_missing_colon));
4414 return FAIL;
4415 }
4416 if (next != NULL)
4417 {
4418 *arg = next_line_from_context(cctx, TRUE);
4419 p = skipwhite(*arg);
4420 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004421
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004422 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4423 {
4424 semsg(_(e_white_space_required_before_and_after_str), ":");
4425 return FAIL;
4426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004428 // evaluate the third expression
4429 if (has_const_expr)
4430 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004431 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004432 *arg = skipwhite(p + 1);
4433 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4434 return FAIL;
4435 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4436 return FAIL;
4437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438
Bram Moolenaara5565e42020-05-09 15:44:01 +02004439 if (!has_const_expr)
4440 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004441 type_T **typep;
4442
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444
Bram Moolenaara5565e42020-05-09 15:44:01 +02004445 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004446 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4447 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004449 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450 isn = ((isn_T *)instr->ga_data) + end_idx;
4451 isn->isn_arg.jump.jump_where = instr->ga_len;
4452 }
4453
4454 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 }
4456 return OK;
4457}
4458
4459/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004460 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004461 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4462 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 */
4464 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004465compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466{
4467 ppconst_T ppconst;
4468
4469 CLEAR_FIELD(ppconst);
4470 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4471 {
4472 clear_ppconst(&ppconst);
4473 return FAIL;
4474 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004475 if (is_const != NULL)
4476 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004477 if (generate_ppconst(cctx, &ppconst) == FAIL)
4478 return FAIL;
4479 return OK;
4480}
4481
4482/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004483 * Toplevel expression.
4484 */
4485 static int
4486compile_expr0(char_u **arg, cctx_T *cctx)
4487{
4488 return compile_expr0_ext(arg, cctx, NULL);
4489}
4490
4491/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 * compile "return [expr]"
4493 */
4494 static char_u *
4495compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4496{
4497 char_u *p = arg;
4498 garray_T *stack = &cctx->ctx_type_stack;
4499 type_T *stack_type;
4500
4501 if (*p != NUL && *p != '|' && *p != '\n')
4502 {
4503 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 return NULL;
4506
4507 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4508 if (set_return_type)
4509 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004510 else
4511 {
4512 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4513 && stack_type->tt_type != VAR_VOID
4514 && stack_type->tt_type != VAR_UNKNOWN)
4515 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004516 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004517 return NULL;
4518 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004519 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004520 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004521 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 }
4524 else
4525 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004526 // "set_return_type" cannot be TRUE, only used for a lambda which
4527 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004528 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4529 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004531 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 return NULL;
4533 }
4534
4535 // No argument, return zero.
4536 generate_PUSHNR(cctx, 0);
4537 }
4538
4539 if (generate_instr(cctx, ISN_RETURN) == NULL)
4540 return NULL;
4541
4542 // "return val | endif" is possible
4543 return skipwhite(p);
4544}
4545
4546/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004547 * Get a line from the compilation context, compatible with exarg_T getline().
4548 * Return a pointer to the line in allocated memory.
4549 * Return NULL for end-of-file or some error.
4550 */
4551 static char_u *
4552exarg_getline(
4553 int c UNUSED,
4554 void *cookie,
4555 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004556 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004557{
4558 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004559 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004560
Bram Moolenaar66250c92020-08-20 15:02:42 +02004561 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004562 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004563 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004564 return NULL;
4565 ++cctx->ctx_lnum;
4566 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4567 // Comment lines result in NULL pointers, skip them.
4568 if (p != NULL)
4569 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004570 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004571}
4572
4573/*
4574 * Compile a nested :def command.
4575 */
4576 static char_u *
4577compile_nested_function(exarg_T *eap, cctx_T *cctx)
4578{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004579 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004580 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004581 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004582 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004583 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004584 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004585
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004586 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004587 {
4588 emsg(_(e_cannot_use_bang_with_nested_def));
4589 return NULL;
4590 }
4591
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004592 // Only g:Func() can use a namespace.
4593 if (name_start[1] == ':' && !is_global)
4594 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004595 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004596 return NULL;
4597 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004598 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4599 return NULL;
4600
Bram Moolenaar04b12692020-05-04 23:24:44 +02004601 eap->arg = name_end;
4602 eap->getline = exarg_getline;
4603 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004604 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004605 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004606 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004607 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004608
Bram Moolenaar822ba242020-05-24 23:00:18 +02004609 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004610 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004611 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004612 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004613 {
4614 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004615 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004616 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004617
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004618 if (is_global)
4619 {
4620 char_u *func_name = vim_strnsave(name_start + 2,
4621 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004622
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004623 if (func_name == NULL)
4624 r = FAIL;
4625 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004626 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004627 }
4628 else
4629 {
4630 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004631 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004632 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004633 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004634
Bram Moolenaareef21022020-08-01 22:16:43 +02004635 if (lvar == NULL)
4636 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004637 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004638 return NULL;
4639 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004640
4641 // copy over the block scope IDs
4642 if (block_depth > 0)
4643 {
4644 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4645 if (ufunc->uf_block_ids != NULL)
4646 {
4647 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4648 sizeof(int) * block_depth);
4649 ufunc->uf_block_depth = block_depth;
4650 }
4651 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004652 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004653
Bram Moolenaar61a89812020-05-07 16:58:17 +02004654 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004655 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004656}
4657
4658/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 * Return the length of an assignment operator, or zero if there isn't one.
4660 */
4661 int
4662assignment_len(char_u *p, int *heredoc)
4663{
4664 if (*p == '=')
4665 {
4666 if (p[1] == '<' && p[2] == '<')
4667 {
4668 *heredoc = TRUE;
4669 return 3;
4670 }
4671 return 1;
4672 }
4673 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4674 return 2;
4675 if (STRNCMP(p, "..=", 3) == 0)
4676 return 3;
4677 return 0;
4678}
4679
4680// words that cannot be used as a variable
4681static char *reserved[] = {
4682 "true",
4683 "false",
4684 NULL
4685};
4686
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004687typedef enum {
4688 dest_local,
4689 dest_option,
4690 dest_env,
4691 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004692 dest_buffer,
4693 dest_window,
4694 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004695 dest_vimvar,
4696 dest_script,
4697 dest_reg,
4698} assign_dest_T;
4699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004701 * Generate the load instruction for "name".
4702 */
4703 static void
4704generate_loadvar(
4705 cctx_T *cctx,
4706 assign_dest_T dest,
4707 char_u *name,
4708 lvar_T *lvar,
4709 type_T *type)
4710{
4711 switch (dest)
4712 {
4713 case dest_option:
4714 // TODO: check the option exists
4715 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4716 break;
4717 case dest_global:
4718 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4719 break;
4720 case dest_buffer:
4721 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4722 break;
4723 case dest_window:
4724 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4725 break;
4726 case dest_tab:
4727 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4728 break;
4729 case dest_script:
4730 compile_load_scriptvar(cctx,
4731 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4732 break;
4733 case dest_env:
4734 // Include $ in the name here
4735 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4736 break;
4737 case dest_reg:
4738 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4739 break;
4740 case dest_vimvar:
4741 generate_LOADV(cctx, name + 2, TRUE);
4742 break;
4743 case dest_local:
4744 if (lvar->lv_from_outer)
4745 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4746 NULL, type);
4747 else
4748 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4749 break;
4750 }
4751}
4752
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004753 void
4754vim9_declare_error(char_u *name)
4755{
4756 char *scope = "";
4757
4758 switch (*name)
4759 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004760 case 'g': scope = _("global"); break;
4761 case 'b': scope = _("buffer"); break;
4762 case 'w': scope = _("window"); break;
4763 case 't': scope = _("tab"); break;
4764 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004765 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004766 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004767 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004768 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004769 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004770 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004771 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004772 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004773 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004774}
4775
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004776/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004778 * "let name"
4779 * "var name = expr"
4780 * "final name = expr"
4781 * "const name = expr"
4782 * "name = expr"
4783 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 * Return NULL for an error.
4785 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 */
4787 static char_u *
4788compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4789{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004790 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004792 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 char_u *ret = NULL;
4794 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004795 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004796 int scriptvar_sid = 0;
4797 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004800 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 int oplen = 0;
4803 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004804 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004805 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004806 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004808 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4809 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004811 // Skip over the "var" or "[var, var]" to get to any "=".
4812 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4813 if (p == NULL)
4814 return *arg == '[' ? arg : NULL;
4815
4816 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004818 // TODO: should we allow this, and figure out type inference from list
4819 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004820 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 return NULL;
4822 }
4823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 sp = p;
4825 p = skipwhite(p);
4826 op = p;
4827 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828
4829 if (var_count > 0 && oplen == 0)
4830 // can be something like "[1, 2]->func()"
4831 return arg;
4832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4834 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004835 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004836 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004837 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 if (heredoc)
4840 {
4841 list_T *l;
4842 listitem_T *li;
4843
4844 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004845 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004847 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004848 if (l == NULL)
4849 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850
Bram Moolenaar078269b2020-09-21 20:35:55 +02004851 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004853 // Push each line and the create the list.
4854 FOR_ALL_LIST_ITEMS(l, li)
4855 {
4856 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4857 li->li_tv.vval.v_string = NULL;
4858 }
4859 generate_NEWLIST(cctx, l->lv_len);
4860 type = &t_list_string;
4861 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 list_free(l);
4864 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004867 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 // for "[var, var] = expr" evaluate the expression here, loop over the
4870 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004871
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872 p = skipwhite(op + oplen);
4873 if (compile_expr0(&p, cctx) == FAIL)
4874 return NULL;
4875 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004877 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004879 type_T *stacktype;
4880
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004881 stacktype = stack->ga_len == 0 ? &t_void
4882 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004883 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004885 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004886 goto theend;
4887 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004888 if (need_type(stacktype, &t_list_any, -1, cctx,
4889 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004890 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004891 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004892 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4893 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004894 }
4895 }
4896
4897 /*
4898 * Loop over variables in "[var, var] = expr".
4899 * For "var = expr" and "let var: type" this is done only once.
4900 */
4901 if (var_count > 0)
4902 var_start = skipwhite(arg + 1); // skip over the "["
4903 else
4904 var_start = arg;
4905 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4906 {
4907 char_u *var_end = skip_var_one(var_start, FALSE);
4908 size_t varlen;
4909 int new_local = FALSE;
4910 int opt_type;
4911 int opt_flags = 0;
4912 assign_dest_T dest = dest_local;
4913 int vimvaridx = -1;
4914 lvar_T *lvar = NULL;
4915 lvar_T arg_lvar;
4916 int has_type = FALSE;
4917 int has_index = FALSE;
4918 int instr_count = -1;
4919
Bram Moolenaar65821722020-08-02 18:58:54 +02004920 if (*var_start == '@')
4921 p = var_start + 2;
4922 else
4923 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004924 // skip over the leading "&", "&l:", "&g:" and "$"
4925 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004926 p = to_name_end(p, TRUE);
4927 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004928
4929 // "a: type" is declaring variable "a" with a type, not "a:".
4930 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4931 --var_end;
4932 if (is_decl && p == var_start + 2 && p[-1] == ':')
4933 --p;
4934
4935 varlen = p - var_start;
4936 vim_free(name);
4937 name = vim_strnsave(var_start, varlen);
4938 if (name == NULL)
4939 return NULL;
4940 if (!heredoc)
4941 type = &t_any;
4942
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004943 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004944 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004945 int declare_error = FALSE;
4946
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004947 if (*var_start == '&')
4948 {
4949 int cc;
4950 long numval;
4951
4952 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004953 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004955 emsg(_(e_const_option));
4956 goto theend;
4957 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004958 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959 p = var_start;
4960 p = find_option_end(&p, &opt_flags);
4961 if (p == NULL)
4962 {
4963 // cannot happen?
4964 emsg(_(e_letunexp));
4965 goto theend;
4966 }
4967 cc = *p;
4968 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004969 opt_type = get_option_value(skip_option_env_lead(var_start),
4970 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 *p = cc;
4972 if (opt_type == -3)
4973 {
4974 semsg(_(e_unknown_option), var_start);
4975 goto theend;
4976 }
4977 if (opt_type == -2 || opt_type == 0)
4978 type = &t_string;
4979 else
4980 type = &t_number; // both number and boolean option
4981 }
4982 else if (*var_start == '$')
4983 {
4984 dest = dest_env;
4985 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004986 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004987 }
4988 else if (*var_start == '@')
4989 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004990 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991 {
4992 emsg_invreg(var_start[1]);
4993 goto theend;
4994 }
4995 dest = dest_reg;
4996 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004997 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004999 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005000 {
5001 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005002 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005004 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005 {
5006 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005007 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005008 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005009 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005010 {
5011 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005012 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005014 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 {
5016 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005017 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005019 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 {
5021 typval_T *vtv;
5022 int di_flags;
5023
5024 vimvaridx = find_vim_var(name + 2, &di_flags);
5025 if (vimvaridx < 0)
5026 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005027 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 goto theend;
5029 }
5030 // We use the current value of "sandbox" here, is that OK?
5031 if (var_check_ro(di_flags, name, FALSE))
5032 goto theend;
5033 dest = dest_vimvar;
5034 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005035 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005036 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 }
5038 else
5039 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005040 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005041
5042 for (idx = 0; reserved[idx] != NULL; ++idx)
5043 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005044 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005045 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005046 goto theend;
5047 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048
5049 lvar = lookup_local(var_start, varlen, cctx);
5050 if (lvar == NULL)
5051 {
5052 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005053 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005054 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5055 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005056 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 if (is_decl)
5058 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005059 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005060 goto theend;
5061 }
5062 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005063 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005064 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005065 if (lvar != NULL)
5066 {
5067 if (is_decl)
5068 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005069 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005070 goto theend;
5071 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005073 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005075 int script_namespace = varlen > 1
5076 && STRNCMP(var_start, "s:", 2) == 0;
5077 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005078 ? script_var_exists(var_start + 2, varlen - 2,
5079 FALSE, cctx)
5080 : script_var_exists(var_start, varlen,
5081 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005082 imported_T *import =
5083 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005084
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005085 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005087 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5088
5089 if (is_decl)
5090 {
5091 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005092 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005093 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005094 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005095 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005096 name);
5097 goto theend;
5098 }
5099 else if (cctx->ctx_ufunc->uf_script_ctx_version
5100 == SCRIPT_VERSION_VIM9
5101 && script_namespace
5102 && !script_var && import == NULL)
5103 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005104 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005105 goto theend;
5106 }
5107
5108 dest = dest_script;
5109
5110 // existing script-local variables should have a type
5111 scriptvar_sid = current_sctx.sc_sid;
5112 if (import != NULL)
5113 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005114 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005115 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005116 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005117 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005118 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005119 {
5120 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5121 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005122 ((svar_T *)si->sn_var_vals.ga_data)
5123 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005124 type = sv->sv_type;
5125 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005126 }
5127 }
5128 else if (name[1] == ':' && name[2] != NUL)
5129 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005130 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 goto theend;
5132 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005133 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005134 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005135 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005136 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005137 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005138 else if (check_defined(var_start, varlen, cctx) == FAIL)
5139 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005142
5143 if (declare_error)
5144 {
5145 vim9_declare_error(name);
5146 goto theend;
5147 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 }
5149
5150 // handle "a:name" as a name, not index "name" on "a"
5151 if (varlen > 1 || var_start[varlen] != ':')
5152 p = var_end;
5153
5154 if (dest != dest_option)
5155 {
5156 if (is_decl && *p == ':')
5157 {
5158 // parse optional type: "let var: type = expr"
5159 if (!VIM_ISWHITE(p[1]))
5160 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005161 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005162 goto theend;
5163 }
5164 p = skipwhite(p + 1);
5165 type = parse_type(&p, cctx->ctx_type_list);
5166 has_type = TRUE;
5167 }
5168 else if (lvar != NULL)
5169 type = lvar->lv_type;
5170 }
5171
5172 if (oplen == 3 && !heredoc && dest != dest_global
5173 && type->tt_type != VAR_STRING
5174 && type->tt_type != VAR_ANY)
5175 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005176 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005177 goto theend;
5178 }
5179
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005180 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 {
5182 if (oplen > 1 && !heredoc)
5183 {
5184 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005185 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005186 goto theend;
5187 }
5188
5189 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005190 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5191 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 goto theend;
5193 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005194 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 if (lvar == NULL)
5196 goto theend;
5197 new_local = TRUE;
5198 }
5199
5200 member_type = type;
5201 if (var_end > var_start + varlen)
5202 {
5203 // Something follows after the variable: "var[idx]".
5204 if (is_decl)
5205 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005206 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 goto theend;
5208 }
5209
5210 if (var_start[varlen] == '[')
5211 {
5212 has_index = TRUE;
5213 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005214 member_type = &t_any;
5215 else
5216 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 }
5218 else
5219 {
5220 semsg("Not supported yet: %s", var_start);
5221 goto theend;
5222 }
5223 }
5224 else if (lvar == &arg_lvar)
5225 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005226 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005227 goto theend;
5228 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005229 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5230 {
5231 semsg(_(e_cannot_assign_to_constant), name);
5232 goto theend;
5233 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005234
5235 if (!heredoc)
5236 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005237 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005239 if (oplen > 0 && var_count == 0)
5240 {
5241 // skip over the "=" and the expression
5242 p = skipwhite(op + oplen);
5243 compile_expr0(&p, cctx);
5244 }
5245 }
5246 else if (oplen > 0)
5247 {
5248 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005249 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005250
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 // For "var = expr" evaluate the expression.
5252 if (var_count == 0)
5253 {
5254 int r;
5255
5256 // for "+=", "*=", "..=" etc. first load the current value
5257 if (*op != '=')
5258 {
5259 generate_loadvar(cctx, dest, name, lvar, type);
5260
5261 if (has_index)
5262 {
5263 // TODO: get member from list or dict
5264 emsg("Index with operation not supported yet");
5265 goto theend;
5266 }
5267 }
5268
5269 // Compile the expression. Temporarily hide the new local
5270 // variable here, it is not available to this expression.
5271 if (new_local)
5272 --cctx->ctx_locals.ga_len;
5273 instr_count = instr->ga_len;
5274 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005275 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005276 if (new_local)
5277 ++cctx->ctx_locals.ga_len;
5278 if (r == FAIL)
5279 goto theend;
5280 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005281 else if (semicolon && var_idx == var_count - 1)
5282 {
5283 // For "[var; var] = expr" get the rest of the list
5284 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5285 goto theend;
5286 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005287 else
5288 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005289 // For "[var, var] = expr" get the "var_idx" item from the
5290 // list.
5291 if (generate_GETITEM(cctx, var_idx) == FAIL)
5292 return FAIL;
5293 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005294
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005295 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005296 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005297 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005299 if ((stacktype->tt_type == VAR_FUNC
5300 || stacktype->tt_type == VAR_PARTIAL)
5301 && var_wrong_func_name(name, TRUE))
5302 goto theend;
5303
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005304 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005305 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005306 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005308 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005309 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005310 }
5311 else
5312 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005313 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005314 // for a variable that implies &t_any.
5315 if (stacktype == &t_list_empty)
5316 lvar->lv_type = &t_list_any;
5317 else if (stacktype == &t_dict_empty)
5318 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005319 else if (stacktype == &t_unknown)
5320 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005321 else
5322 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005324 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005325 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005326 {
5327 type_T *use_type = lvar->lv_type;
5328
Bram Moolenaar71abe482020-09-14 22:28:30 +02005329 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005330 if (has_index)
5331 {
5332 use_type = use_type->tt_member;
5333 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005334 // could be indexing "any"
5335 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005336 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005337 if (need_type(stacktype, use_type, -1, cctx,
5338 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005339 goto theend;
5340 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005342 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005343 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005344 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005346 else if (cmdidx == CMD_final)
5347 {
5348 emsg(_(e_final_requires_a_value));
5349 goto theend;
5350 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005352 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005353 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005354 goto theend;
5355 }
5356 else if (!has_type || dest == dest_option)
5357 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005358 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005359 goto theend;
5360 }
5361 else
5362 {
5363 // variables are always initialized
5364 if (ga_grow(instr, 1) == FAIL)
5365 goto theend;
5366 switch (member_type->tt_type)
5367 {
5368 case VAR_BOOL:
5369 generate_PUSHBOOL(cctx, VVAL_FALSE);
5370 break;
5371 case VAR_FLOAT:
5372#ifdef FEAT_FLOAT
5373 generate_PUSHF(cctx, 0.0);
5374#endif
5375 break;
5376 case VAR_STRING:
5377 generate_PUSHS(cctx, NULL);
5378 break;
5379 case VAR_BLOB:
5380 generate_PUSHBLOB(cctx, NULL);
5381 break;
5382 case VAR_FUNC:
5383 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5384 break;
5385 case VAR_LIST:
5386 generate_NEWLIST(cctx, 0);
5387 break;
5388 case VAR_DICT:
5389 generate_NEWDICT(cctx, 0);
5390 break;
5391 case VAR_JOB:
5392 generate_PUSHJOB(cctx, NULL);
5393 break;
5394 case VAR_CHANNEL:
5395 generate_PUSHCHANNEL(cctx, NULL);
5396 break;
5397 case VAR_NUMBER:
5398 case VAR_UNKNOWN:
5399 case VAR_ANY:
5400 case VAR_PARTIAL:
5401 case VAR_VOID:
5402 case VAR_SPECIAL: // cannot happen
5403 generate_PUSHNR(cctx, 0);
5404 break;
5405 }
5406 }
5407 if (var_count == 0)
5408 end = p;
5409 }
5410
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005411 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005412 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005413 break;
5414
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 if (oplen > 0 && *op != '=')
5416 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005417 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005418 type_T *stacktype;
5419
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 if (*op == '.')
5421 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005422 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005423 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005425 if (
5426#ifdef FEAT_FLOAT
5427 // If variable is float operation with number is OK.
5428 !(expected == &t_float && stacktype == &t_number) &&
5429#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005430 need_type(stacktype, expected, -1, cctx,
5431 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 goto theend;
5433
5434 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005435 {
5436 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5437 goto theend;
5438 }
5439 else if (*op == '+')
5440 {
5441 if (generate_add_instr(cctx,
5442 operator_type(member_type, stacktype),
5443 member_type, stacktype) == FAIL)
5444 goto theend;
5445 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005446 else if (generate_two_op(cctx, op) == FAIL)
5447 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005450 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005451 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005452 int r;
5453
5454 // Compile the "idx" in "var[idx]".
5455 if (new_local)
5456 --cctx->ctx_locals.ga_len;
5457 p = skipwhite(var_start + varlen + 1);
5458 r = compile_expr0(&p, cctx);
5459 if (new_local)
5460 ++cctx->ctx_locals.ga_len;
5461 if (r == FAIL)
5462 goto theend;
5463 if (*skipwhite(p) != ']')
5464 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005465 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 emsg(_(e_missbrac));
5467 goto theend;
5468 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005469 if (type == &t_any)
5470 {
5471 type_T *idx_type = ((type_T **)stack->ga_data)[
5472 stack->ga_len - 1];
5473 // Index on variable of unknown type: guess the type from the
5474 // index type: number is dict, otherwise dict.
5475 // TODO: should do the assignment at runtime
5476 if (idx_type->tt_type == VAR_NUMBER)
5477 type = &t_list_any;
5478 else
5479 type = &t_dict_any;
5480 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005481 if (type->tt_type == VAR_DICT
5482 && may_generate_2STRING(-1, cctx) == FAIL)
5483 goto theend;
5484 if (type->tt_type == VAR_LIST
5485 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005486 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005487 {
5488 emsg(_(e_number_exp));
5489 goto theend;
5490 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005491
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005492 // Load the dict or list. On the stack we then have:
5493 // - value
5494 // - index
5495 // - variable
5496 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005497
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005498 if (type->tt_type == VAR_LIST)
5499 {
5500 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5501 return FAIL;
5502 }
5503 else if (type->tt_type == VAR_DICT)
5504 {
5505 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5506 return FAIL;
5507 }
5508 else
5509 {
5510 emsg(_(e_listreq));
5511 goto theend;
5512 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005513 }
5514 else
5515 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005516 if (is_decl && cmdidx == CMD_const
5517 && (dest == dest_script || dest == dest_local))
5518 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005519 generate_LOCKCONST(cctx);
5520
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521 switch (dest)
5522 {
5523 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005524 generate_STOREOPT(cctx, skip_option_env_lead(name),
5525 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005526 break;
5527 case dest_global:
5528 // include g: with the name, easier to execute that way
5529 generate_STORE(cctx, ISN_STOREG, 0, name);
5530 break;
5531 case dest_buffer:
5532 // include b: with the name, easier to execute that way
5533 generate_STORE(cctx, ISN_STOREB, 0, name);
5534 break;
5535 case dest_window:
5536 // include w: with the name, easier to execute that way
5537 generate_STORE(cctx, ISN_STOREW, 0, name);
5538 break;
5539 case dest_tab:
5540 // include t: with the name, easier to execute that way
5541 generate_STORE(cctx, ISN_STORET, 0, name);
5542 break;
5543 case dest_env:
5544 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5545 break;
5546 case dest_reg:
5547 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5548 break;
5549 case dest_vimvar:
5550 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5551 break;
5552 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005553 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005554 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555 {
5556 char_u *name_s = name;
5557
5558 // Include s: in the name for store_var()
5559 if (name[1] != ':')
5560 {
5561 int len = (int)STRLEN(name) + 3;
5562
5563 name_s = alloc(len);
5564 if (name_s == NULL)
5565 name_s = name;
5566 else
5567 vim_snprintf((char *)name_s, len,
5568 "s:%s", name);
5569 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005570 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5571 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005572 if (name_s != name)
5573 vim_free(name_s);
5574 }
5575 else
5576 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005577 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005578 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005579 break;
5580 case dest_local:
5581 if (lvar != NULL)
5582 {
5583 isn_T *isn = ((isn_T *)instr->ga_data)
5584 + instr->ga_len - 1;
5585
5586 // optimization: turn "var = 123" from ISN_PUSHNR +
5587 // ISN_STORE into ISN_STORENR
5588 if (!lvar->lv_from_outer
5589 && instr->ga_len == instr_count + 1
5590 && isn->isn_type == ISN_PUSHNR)
5591 {
5592 varnumber_T val = isn->isn_arg.number;
5593
5594 isn->isn_type = ISN_STORENR;
5595 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5596 isn->isn_arg.storenr.stnr_val = val;
5597 if (stack->ga_len > 0)
5598 --stack->ga_len;
5599 }
5600 else if (lvar->lv_from_outer)
5601 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005602 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005603 else
5604 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5605 }
5606 break;
5607 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005608 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005609
5610 if (var_idx + 1 < var_count)
5611 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613
5614 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005615 if (var_count > 0 && !semicolon)
5616 {
5617 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5618 goto theend;
5619 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005620
Bram Moolenaarb2097502020-07-19 17:17:02 +02005621 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622
5623theend:
5624 vim_free(name);
5625 return ret;
5626}
5627
5628/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005629 * Check if "name" can be "unlet".
5630 */
5631 int
5632check_vim9_unlet(char_u *name)
5633{
5634 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5635 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005636 // "unlet s:var" is allowed in legacy script.
5637 if (*name == 's' && !script_is_vim9())
5638 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005639 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005640 return FAIL;
5641 }
5642 return OK;
5643}
5644
5645/*
5646 * Callback passed to ex_unletlock().
5647 */
5648 static int
5649compile_unlet(
5650 lval_T *lvp,
5651 char_u *name_end,
5652 exarg_T *eap,
5653 int deep UNUSED,
5654 void *coookie)
5655{
5656 cctx_T *cctx = coookie;
5657
5658 if (lvp->ll_tv == NULL)
5659 {
5660 char_u *p = lvp->ll_name;
5661 int cc = *name_end;
5662 int ret = OK;
5663
5664 // Normal name. Only supports g:, w:, t: and b: namespaces.
5665 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005666 if (*p == '$')
5667 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5668 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005669 ret = FAIL;
5670 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005671 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005672
5673 *name_end = cc;
5674 return ret;
5675 }
5676
5677 // TODO: unlet {list}[idx]
5678 // TODO: unlet {dict}[key]
5679 emsg("Sorry, :unlet not fully implemented yet");
5680 return FAIL;
5681}
5682
5683/*
5684 * compile "unlet var", "lock var" and "unlock var"
5685 * "arg" points to "var".
5686 */
5687 static char_u *
5688compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5689{
5690 char_u *p = arg;
5691
5692 if (eap->cmdidx != CMD_unlet)
5693 {
5694 emsg("Sorry, :lock and unlock not implemented yet");
5695 return NULL;
5696 }
5697
5698 if (*p == '!')
5699 {
5700 p = skipwhite(p + 1);
5701 eap->forceit = TRUE;
5702 }
5703
5704 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5705 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5706}
5707
5708/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 * Compile an :import command.
5710 */
5711 static char_u *
5712compile_import(char_u *arg, cctx_T *cctx)
5713{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005714 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005715}
5716
5717/*
5718 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5719 */
5720 static int
5721compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5722{
5723 garray_T *instr = &cctx->ctx_instr;
5724 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5725
5726 if (endlabel == NULL)
5727 return FAIL;
5728 endlabel->el_next = *el;
5729 *el = endlabel;
5730 endlabel->el_end_label = instr->ga_len;
5731
5732 generate_JUMP(cctx, when, 0);
5733 return OK;
5734}
5735
5736 static void
5737compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5738{
5739 garray_T *instr = &cctx->ctx_instr;
5740
5741 while (*el != NULL)
5742 {
5743 endlabel_T *cur = (*el);
5744 isn_T *isn;
5745
5746 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5747 isn->isn_arg.jump.jump_where = instr->ga_len;
5748 *el = cur->el_next;
5749 vim_free(cur);
5750 }
5751}
5752
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005753 static void
5754compile_free_jump_to_end(endlabel_T **el)
5755{
5756 while (*el != NULL)
5757 {
5758 endlabel_T *cur = (*el);
5759
5760 *el = cur->el_next;
5761 vim_free(cur);
5762 }
5763}
5764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765/*
5766 * Create a new scope and set up the generic items.
5767 */
5768 static scope_T *
5769new_scope(cctx_T *cctx, scopetype_T type)
5770{
5771 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5772
5773 if (scope == NULL)
5774 return NULL;
5775 scope->se_outer = cctx->ctx_scope;
5776 cctx->ctx_scope = scope;
5777 scope->se_type = type;
5778 scope->se_local_count = cctx->ctx_locals.ga_len;
5779 return scope;
5780}
5781
5782/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005783 * Free the current scope and go back to the outer scope.
5784 */
5785 static void
5786drop_scope(cctx_T *cctx)
5787{
5788 scope_T *scope = cctx->ctx_scope;
5789
5790 if (scope == NULL)
5791 {
5792 iemsg("calling drop_scope() without a scope");
5793 return;
5794 }
5795 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005796 switch (scope->se_type)
5797 {
5798 case IF_SCOPE:
5799 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5800 case FOR_SCOPE:
5801 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5802 case WHILE_SCOPE:
5803 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5804 case TRY_SCOPE:
5805 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5806 case NO_SCOPE:
5807 case BLOCK_SCOPE:
5808 break;
5809 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005810 vim_free(scope);
5811}
5812
5813/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005814 * Check that the top of the type stack has a type that can be used as a
5815 * condition. Give an error and return FAIL if not.
5816 */
5817 static int
5818bool_on_stack(cctx_T *cctx)
5819{
5820 garray_T *stack = &cctx->ctx_type_stack;
5821 type_T *type;
5822
5823 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5824 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005825 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005826 return FAIL;
5827 return OK;
5828}
5829
5830/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005831 * compile "if expr"
5832 *
5833 * "if expr" Produces instructions:
5834 * EVAL expr Push result of "expr"
5835 * JUMP_IF_FALSE end
5836 * ... body ...
5837 * end:
5838 *
5839 * "if expr | else" Produces instructions:
5840 * EVAL expr Push result of "expr"
5841 * JUMP_IF_FALSE else
5842 * ... body ...
5843 * JUMP_ALWAYS end
5844 * else:
5845 * ... body ...
5846 * end:
5847 *
5848 * "if expr1 | elseif expr2 | else" Produces instructions:
5849 * EVAL expr Push result of "expr"
5850 * JUMP_IF_FALSE elseif
5851 * ... body ...
5852 * JUMP_ALWAYS end
5853 * elseif:
5854 * EVAL expr Push result of "expr"
5855 * JUMP_IF_FALSE else
5856 * ... body ...
5857 * JUMP_ALWAYS end
5858 * else:
5859 * ... body ...
5860 * end:
5861 */
5862 static char_u *
5863compile_if(char_u *arg, cctx_T *cctx)
5864{
5865 char_u *p = arg;
5866 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005867 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005868 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005869 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005870 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871
Bram Moolenaara5565e42020-05-09 15:44:01 +02005872 CLEAR_FIELD(ppconst);
5873 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005874 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005875 clear_ppconst(&ppconst);
5876 return NULL;
5877 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005878 if (cctx->ctx_skip == SKIP_YES)
5879 clear_ppconst(&ppconst);
5880 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005881 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005882 int error = FALSE;
5883 int v;
5884
Bram Moolenaara5565e42020-05-09 15:44:01 +02005885 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005886 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005887 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005888 if (error)
5889 return NULL;
5890 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005891 }
5892 else
5893 {
5894 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005895 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005896 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005897 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005898 if (bool_on_stack(cctx) == FAIL)
5899 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005901
5902 scope = new_scope(cctx, IF_SCOPE);
5903 if (scope == NULL)
5904 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005905 scope->se_skip_save = skip_save;
5906 // "is_had_return" will be reset if any block does not end in :return
5907 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005909 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005910 {
5911 // "where" is set when ":elseif", "else" or ":endif" is found
5912 scope->se_u.se_if.is_if_label = instr->ga_len;
5913 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5914 }
5915 else
5916 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917
5918 return p;
5919}
5920
5921 static char_u *
5922compile_elseif(char_u *arg, cctx_T *cctx)
5923{
5924 char_u *p = arg;
5925 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005926 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 isn_T *isn;
5928 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005929 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005930 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931
5932 if (scope == NULL || scope->se_type != IF_SCOPE)
5933 {
5934 emsg(_(e_elseif_without_if));
5935 return NULL;
5936 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005937 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005938 if (!cctx->ctx_had_return)
5939 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005941 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005942 {
5943 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005945 return NULL;
5946 // previous "if" or "elseif" jumps here
5947 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5948 isn->isn_arg.jump.jump_where = instr->ga_len;
5949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005951 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005952 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005953 if (cctx->ctx_skip == SKIP_YES)
5954 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005955 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005956 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005957 clear_ppconst(&ppconst);
5958 return NULL;
5959 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005960 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005961 if (scope->se_skip_save == SKIP_YES)
5962 clear_ppconst(&ppconst);
5963 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005964 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005965 int error = FALSE;
5966 int v;
5967
Bram Moolenaar7f141552020-05-09 17:35:53 +02005968 // The expression results in a constant.
5969 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02005970 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5971 if (error)
5972 return NULL;
5973 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005974 clear_ppconst(&ppconst);
5975 scope->se_u.se_if.is_if_label = -1;
5976 }
5977 else
5978 {
5979 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005980 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005981 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005982 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005983 if (bool_on_stack(cctx) == FAIL)
5984 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005986 // "where" is set when ":elseif", "else" or ":endif" is found
5987 scope->se_u.se_if.is_if_label = instr->ga_len;
5988 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
5991 return p;
5992}
5993
5994 static char_u *
5995compile_else(char_u *arg, cctx_T *cctx)
5996{
5997 char_u *p = arg;
5998 garray_T *instr = &cctx->ctx_instr;
5999 isn_T *isn;
6000 scope_T *scope = cctx->ctx_scope;
6001
6002 if (scope == NULL || scope->se_type != IF_SCOPE)
6003 {
6004 emsg(_(e_else_without_if));
6005 return NULL;
6006 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006007 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006008 if (!cctx->ctx_had_return)
6009 scope->se_u.se_if.is_had_return = FALSE;
6010 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011
Bram Moolenaarefd88552020-06-18 20:50:10 +02006012 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006013 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006014 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006015 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006016 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006017 if (!cctx->ctx_had_return
6018 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6019 JUMP_ALWAYS, cctx) == FAIL)
6020 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006021 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006022
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006023 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006024 {
6025 if (scope->se_u.se_if.is_if_label >= 0)
6026 {
6027 // previous "if" or "elseif" jumps here
6028 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6029 isn->isn_arg.jump.jump_where = instr->ga_len;
6030 scope->se_u.se_if.is_if_label = -1;
6031 }
6032 }
6033
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006034 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006035 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037
6038 return p;
6039}
6040
6041 static char_u *
6042compile_endif(char_u *arg, cctx_T *cctx)
6043{
6044 scope_T *scope = cctx->ctx_scope;
6045 ifscope_T *ifscope;
6046 garray_T *instr = &cctx->ctx_instr;
6047 isn_T *isn;
6048
6049 if (scope == NULL || scope->se_type != IF_SCOPE)
6050 {
6051 emsg(_(e_endif_without_if));
6052 return NULL;
6053 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006054 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006055 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006056 if (!cctx->ctx_had_return)
6057 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006059 if (scope->se_u.se_if.is_if_label >= 0)
6060 {
6061 // previous "if" or "elseif" jumps here
6062 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6063 isn->isn_arg.jump.jump_where = instr->ga_len;
6064 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 // Fill in the "end" label in jumps at the end of the blocks.
6066 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006067 cctx->ctx_skip = scope->se_skip_save;
6068
6069 // If all the blocks end in :return and there is an :else then the
6070 // had_return flag is set.
6071 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006073 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 return arg;
6075}
6076
6077/*
6078 * compile "for var in expr"
6079 *
6080 * Produces instructions:
6081 * PUSHNR -1
6082 * STORE loop-idx Set index to -1
6083 * EVAL expr Push result of "expr"
6084 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6085 * - if beyond end, jump to "end"
6086 * - otherwise get item from list and push it
6087 * STORE var Store item in "var"
6088 * ... body ...
6089 * JUMP top Jump back to repeat
6090 * end: DROP Drop the result of "expr"
6091 *
6092 */
6093 static char_u *
6094compile_for(char_u *arg, cctx_T *cctx)
6095{
6096 char_u *p;
6097 size_t varlen;
6098 garray_T *instr = &cctx->ctx_instr;
6099 garray_T *stack = &cctx->ctx_type_stack;
6100 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006101 lvar_T *loop_lvar; // loop iteration variable
6102 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 type_T *vartype;
6104
6105 // TODO: list of variables: "for [key, value] in dict"
6106 // parse "var"
6107 for (p = arg; eval_isnamec1(*p); ++p)
6108 ;
6109 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006110 var_lvar = lookup_local(arg, varlen, cctx);
6111 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006113 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114 return NULL;
6115 }
6116
6117 // consume "in"
6118 p = skipwhite(p);
6119 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6120 {
6121 emsg(_(e_missing_in));
6122 return NULL;
6123 }
6124 p = skipwhite(p + 2);
6125
6126
6127 scope = new_scope(cctx, FOR_SCOPE);
6128 if (scope == NULL)
6129 return NULL;
6130
6131 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006132 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6133 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006134 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006135 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006136 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139
6140 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006141 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6142 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006143 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006144 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006145 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006149 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150
6151 // compile "expr", it remains on the stack until "endfor"
6152 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006153 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006154 {
6155 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006159 // Now that we know the type of "var", check that it is a list, now or at
6160 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006162 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006164 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 return NULL;
6166 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006167 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006168 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169
6170 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006171 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006173 generate_FOR(cctx, loop_lvar->lv_idx);
6174 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175
6176 return arg;
6177}
6178
6179/*
6180 * compile "endfor"
6181 */
6182 static char_u *
6183compile_endfor(char_u *arg, cctx_T *cctx)
6184{
6185 garray_T *instr = &cctx->ctx_instr;
6186 scope_T *scope = cctx->ctx_scope;
6187 forscope_T *forscope;
6188 isn_T *isn;
6189
6190 if (scope == NULL || scope->se_type != FOR_SCOPE)
6191 {
6192 emsg(_(e_for));
6193 return NULL;
6194 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006195 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006197 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198
6199 // At end of ":for" scope jump back to the FOR instruction.
6200 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6201
6202 // Fill in the "end" label in the FOR statement so it can jump here
6203 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6204 isn->isn_arg.forloop.for_end = instr->ga_len;
6205
6206 // Fill in the "end" label any BREAK statements
6207 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6208
6209 // Below the ":for" scope drop the "expr" list from the stack.
6210 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6211 return NULL;
6212
6213 vim_free(scope);
6214
6215 return arg;
6216}
6217
6218/*
6219 * compile "while expr"
6220 *
6221 * Produces instructions:
6222 * top: EVAL expr Push result of "expr"
6223 * JUMP_IF_FALSE end jump if false
6224 * ... body ...
6225 * JUMP top Jump back to repeat
6226 * end:
6227 *
6228 */
6229 static char_u *
6230compile_while(char_u *arg, cctx_T *cctx)
6231{
6232 char_u *p = arg;
6233 garray_T *instr = &cctx->ctx_instr;
6234 scope_T *scope;
6235
6236 scope = new_scope(cctx, WHILE_SCOPE);
6237 if (scope == NULL)
6238 return NULL;
6239
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006240 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241
6242 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006243 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 return NULL;
6245
Bram Moolenaar13106602020-10-04 16:06:05 +02006246 if (bool_on_stack(cctx) == FAIL)
6247 return FAIL;
6248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006250 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 JUMP_IF_FALSE, cctx) == FAIL)
6252 return FAIL;
6253
6254 return p;
6255}
6256
6257/*
6258 * compile "endwhile"
6259 */
6260 static char_u *
6261compile_endwhile(char_u *arg, cctx_T *cctx)
6262{
6263 scope_T *scope = cctx->ctx_scope;
6264
6265 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6266 {
6267 emsg(_(e_while));
6268 return NULL;
6269 }
6270 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006271 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272
6273 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006274 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275
6276 // Fill in the "end" label in the WHILE statement so it can jump here.
6277 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006278 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
6280 vim_free(scope);
6281
6282 return arg;
6283}
6284
6285/*
6286 * compile "continue"
6287 */
6288 static char_u *
6289compile_continue(char_u *arg, cctx_T *cctx)
6290{
6291 scope_T *scope = cctx->ctx_scope;
6292
6293 for (;;)
6294 {
6295 if (scope == NULL)
6296 {
6297 emsg(_(e_continue));
6298 return NULL;
6299 }
6300 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6301 break;
6302 scope = scope->se_outer;
6303 }
6304
6305 // Jump back to the FOR or WHILE instruction.
6306 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006307 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6308 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 return arg;
6310}
6311
6312/*
6313 * compile "break"
6314 */
6315 static char_u *
6316compile_break(char_u *arg, cctx_T *cctx)
6317{
6318 scope_T *scope = cctx->ctx_scope;
6319 endlabel_T **el;
6320
6321 for (;;)
6322 {
6323 if (scope == NULL)
6324 {
6325 emsg(_(e_break));
6326 return NULL;
6327 }
6328 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6329 break;
6330 scope = scope->se_outer;
6331 }
6332
6333 // Jump to the end of the FOR or WHILE loop.
6334 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006335 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006337 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6339 return FAIL;
6340
6341 return arg;
6342}
6343
6344/*
6345 * compile "{" start of block
6346 */
6347 static char_u *
6348compile_block(char_u *arg, cctx_T *cctx)
6349{
6350 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6351 return NULL;
6352 return skipwhite(arg + 1);
6353}
6354
6355/*
6356 * compile end of block: drop one scope
6357 */
6358 static void
6359compile_endblock(cctx_T *cctx)
6360{
6361 scope_T *scope = cctx->ctx_scope;
6362
6363 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006364 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365 vim_free(scope);
6366}
6367
6368/*
6369 * compile "try"
6370 * Creates a new scope for the try-endtry, pointing to the first catch and
6371 * finally.
6372 * Creates another scope for the "try" block itself.
6373 * TRY instruction sets up exception handling at runtime.
6374 *
6375 * "try"
6376 * TRY -> catch1, -> finally push trystack entry
6377 * ... try block
6378 * "throw {exception}"
6379 * EVAL {exception}
6380 * THROW create exception
6381 * ... try block
6382 * " catch {expr}"
6383 * JUMP -> finally
6384 * catch1: PUSH exeception
6385 * EVAL {expr}
6386 * MATCH
6387 * JUMP nomatch -> catch2
6388 * CATCH remove exception
6389 * ... catch block
6390 * " catch"
6391 * JUMP -> finally
6392 * catch2: CATCH remove exception
6393 * ... catch block
6394 * " finally"
6395 * finally:
6396 * ... finally block
6397 * " endtry"
6398 * ENDTRY pop trystack entry, may rethrow
6399 */
6400 static char_u *
6401compile_try(char_u *arg, cctx_T *cctx)
6402{
6403 garray_T *instr = &cctx->ctx_instr;
6404 scope_T *try_scope;
6405 scope_T *scope;
6406
6407 // scope that holds the jumps that go to catch/finally/endtry
6408 try_scope = new_scope(cctx, TRY_SCOPE);
6409 if (try_scope == NULL)
6410 return NULL;
6411
6412 // "catch" is set when the first ":catch" is found.
6413 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006414 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 if (generate_instr(cctx, ISN_TRY) == NULL)
6416 return NULL;
6417
6418 // scope for the try block itself
6419 scope = new_scope(cctx, BLOCK_SCOPE);
6420 if (scope == NULL)
6421 return NULL;
6422
6423 return arg;
6424}
6425
6426/*
6427 * compile "catch {expr}"
6428 */
6429 static char_u *
6430compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6431{
6432 scope_T *scope = cctx->ctx_scope;
6433 garray_T *instr = &cctx->ctx_instr;
6434 char_u *p;
6435 isn_T *isn;
6436
6437 // end block scope from :try or :catch
6438 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6439 compile_endblock(cctx);
6440 scope = cctx->ctx_scope;
6441
6442 // Error if not in a :try scope
6443 if (scope == NULL || scope->se_type != TRY_SCOPE)
6444 {
6445 emsg(_(e_catch));
6446 return NULL;
6447 }
6448
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006449 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006451 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 return NULL;
6453 }
6454
6455 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006456 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 JUMP_ALWAYS, cctx) == FAIL)
6458 return NULL;
6459
6460 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006461 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462 if (isn->isn_arg.try.try_catch == 0)
6463 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006464 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006465 {
6466 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006467 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 isn->isn_arg.jump.jump_where = instr->ga_len;
6469 }
6470
6471 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006472 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006473 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006474 scope->se_u.se_try.ts_caught_all = TRUE;
6475 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006476 }
6477 else
6478 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006479 char_u *end;
6480 char_u *pat;
6481 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006482 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006483 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 // Push v:exception, push {expr} and MATCH
6486 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6487
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006488 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006489 if (*end != *p)
6490 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006491 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006492 vim_free(tofree);
6493 return FAIL;
6494 }
6495 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006496 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006497 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006498 len = (int)(end - tofree);
6499 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006500 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006501 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006502 if (pat == NULL)
6503 return FAIL;
6504 if (generate_PUSHS(cctx, pat) == FAIL)
6505 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6508 return NULL;
6509
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006510 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6512 return NULL;
6513 }
6514
6515 if (generate_instr(cctx, ISN_CATCH) == NULL)
6516 return NULL;
6517
6518 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6519 return NULL;
6520 return p;
6521}
6522
6523 static char_u *
6524compile_finally(char_u *arg, cctx_T *cctx)
6525{
6526 scope_T *scope = cctx->ctx_scope;
6527 garray_T *instr = &cctx->ctx_instr;
6528 isn_T *isn;
6529
6530 // end block scope from :try or :catch
6531 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6532 compile_endblock(cctx);
6533 scope = cctx->ctx_scope;
6534
6535 // Error if not in a :try scope
6536 if (scope == NULL || scope->se_type != TRY_SCOPE)
6537 {
6538 emsg(_(e_finally));
6539 return NULL;
6540 }
6541
6542 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006543 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 if (isn->isn_arg.try.try_finally != 0)
6545 {
6546 emsg(_(e_finally_dup));
6547 return NULL;
6548 }
6549
6550 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006551 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552
Bram Moolenaar585fea72020-04-02 22:33:21 +02006553 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006554 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 {
6556 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006557 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006559 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 }
6561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 // TODO: set index in ts_finally_label jumps
6563
6564 return arg;
6565}
6566
6567 static char_u *
6568compile_endtry(char_u *arg, cctx_T *cctx)
6569{
6570 scope_T *scope = cctx->ctx_scope;
6571 garray_T *instr = &cctx->ctx_instr;
6572 isn_T *isn;
6573
6574 // end block scope from :catch or :finally
6575 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6576 compile_endblock(cctx);
6577 scope = cctx->ctx_scope;
6578
6579 // Error if not in a :try scope
6580 if (scope == NULL || scope->se_type != TRY_SCOPE)
6581 {
6582 if (scope == NULL)
6583 emsg(_(e_no_endtry));
6584 else if (scope->se_type == WHILE_SCOPE)
6585 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006586 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 emsg(_(e_endfor));
6588 else
6589 emsg(_(e_endif));
6590 return NULL;
6591 }
6592
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006593 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6595 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006596 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 return NULL;
6598 }
6599
6600 // Fill in the "end" label in jumps at the end of the blocks, if not done
6601 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006602 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603
6604 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006605 if (isn->isn_arg.try.try_catch == 0)
6606 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 if (isn->isn_arg.try.try_finally == 0)
6608 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006609
6610 if (scope->se_u.se_try.ts_catch_label != 0)
6611 {
6612 // Last catch without match jumps here
6613 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6614 isn->isn_arg.jump.jump_where = instr->ga_len;
6615 }
6616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 compile_endblock(cctx);
6618
6619 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6620 return NULL;
6621 return arg;
6622}
6623
6624/*
6625 * compile "throw {expr}"
6626 */
6627 static char_u *
6628compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6629{
6630 char_u *p = skipwhite(arg);
6631
Bram Moolenaara5565e42020-05-09 15:44:01 +02006632 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 return NULL;
6634 if (may_generate_2STRING(-1, cctx) == FAIL)
6635 return NULL;
6636 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6637 return NULL;
6638
6639 return p;
6640}
6641
6642/*
6643 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006644 * compile "echomsg expr"
6645 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006646 * compile "execute expr"
6647 */
6648 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006649compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006650{
6651 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006652 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006653 int count = 0;
6654
6655 for (;;)
6656 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006657 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006658 return NULL;
6659 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006660 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006661 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006662 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006663 break;
6664 }
6665
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006666 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6667 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6668 else if (cmdidx == CMD_execute)
6669 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6670 else if (cmdidx == CMD_echomsg)
6671 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6672 else
6673 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 return p;
6675}
6676
6677/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006678 * :put r
6679 * :put ={expr}
6680 */
6681 static char_u *
6682compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6683{
6684 char_u *line = arg;
6685 linenr_T lnum;
6686 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006687 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006688
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006689 eap->regname = *line;
6690
6691 if (eap->regname == '=')
6692 {
6693 char_u *p = line + 1;
6694
6695 if (compile_expr0(&p, cctx) == FAIL)
6696 return NULL;
6697 line = p;
6698 }
6699 else if (eap->regname != NUL)
6700 ++line;
6701
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006702 // "errormsg" will not be set because the range is ADDR_LINES.
6703 // TODO: if the range contains something like "$" or "." need to evaluate
6704 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006705 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6706 return NULL;
6707 if (eap->addr_count == 0)
6708 lnum = -1;
6709 else
6710 lnum = eap->line2;
6711 if (above)
6712 --lnum;
6713
6714 generate_PUT(cctx, eap->regname, lnum);
6715 return line;
6716}
6717
6718/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006719 * A command that is not compiled, execute with legacy code.
6720 */
6721 static char_u *
6722compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6723{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006724 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006725 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006726 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006727
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006728 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006729 goto theend;
6730
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006731 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006732 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006733 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006734 int usefilter = FALSE;
6735
6736 has_expr = argt & (EX_XFILE | EX_EXPAND);
6737
6738 // If the command can be followed by a bar, find the bar and truncate
6739 // it, so that the following command can be compiled.
6740 // The '|' is overwritten with a NUL, it is put back below.
6741 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6742 && *eap->arg == '!')
6743 // :w !filter or :r !filter or :r! filter
6744 usefilter = TRUE;
6745 if ((argt & EX_TRLBAR) && !usefilter)
6746 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006747 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006748 separate_nextcmd(eap);
6749 if (eap->nextcmd != NULL)
6750 nextcmd = eap->nextcmd;
6751 }
6752 }
6753
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006754 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6755 {
6756 // expand filename in "syntax include [@group] filename"
6757 has_expr = TRUE;
6758 eap->arg = skipwhite(eap->arg + 7);
6759 if (*eap->arg == '@')
6760 eap->arg = skiptowhite(eap->arg);
6761 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006762
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006763 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006764 {
6765 int count = 0;
6766 char_u *start = skipwhite(line);
6767
6768 // :cmd xxx`=expr1`yyy`=expr2`zzz
6769 // PUSHS ":cmd xxx"
6770 // eval expr1
6771 // PUSHS "yyy"
6772 // eval expr2
6773 // PUSHS "zzz"
6774 // EXECCONCAT 5
6775 for (;;)
6776 {
6777 if (p > start)
6778 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006779 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006780 ++count;
6781 }
6782 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006783 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006784 return NULL;
6785 may_generate_2STRING(-1, cctx);
6786 ++count;
6787 p = skipwhite(p);
6788 if (*p != '`')
6789 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006790 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006791 return NULL;
6792 }
6793 start = p + 1;
6794
6795 p = (char_u *)strstr((char *)start, "`=");
6796 if (p == NULL)
6797 {
6798 if (*skipwhite(start) != NUL)
6799 {
6800 generate_PUSHS(cctx, vim_strsave(start));
6801 ++count;
6802 }
6803 break;
6804 }
6805 }
6806 generate_EXECCONCAT(cctx, count);
6807 }
6808 else
6809 generate_EXEC(cctx, line);
6810
6811theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006812 if (*nextcmd != NUL)
6813 {
6814 // the parser expects a pointer to the bar, put it back
6815 --nextcmd;
6816 *nextcmd = '|';
6817 }
6818
6819 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006820}
6821
6822/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006823 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006824 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006825 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006826 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006827add_def_function(ufunc_T *ufunc)
6828{
6829 dfunc_T *dfunc;
6830
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006831 if (def_functions.ga_len == 0)
6832 {
6833 // The first position is not used, so that a zero uf_dfunc_idx means it
6834 // wasn't set.
6835 if (ga_grow(&def_functions, 1) == FAIL)
6836 return FAIL;
6837 ++def_functions.ga_len;
6838 }
6839
Bram Moolenaar09689a02020-05-09 22:50:08 +02006840 // Add the function to "def_functions".
6841 if (ga_grow(&def_functions, 1) == FAIL)
6842 return FAIL;
6843 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6844 CLEAR_POINTER(dfunc);
6845 dfunc->df_idx = def_functions.ga_len;
6846 ufunc->uf_dfunc_idx = dfunc->df_idx;
6847 dfunc->df_ufunc = ufunc;
6848 ++def_functions.ga_len;
6849 return OK;
6850}
6851
6852/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 * After ex_function() has collected all the function lines: parse and compile
6854 * the lines into instructions.
6855 * Adds the function to "def_functions".
6856 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6857 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006858 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006859 * This can be used recursively through compile_lambda(), which may reallocate
6860 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006861 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006863 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006864compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 char_u *line = NULL;
6867 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 cctx_T cctx;
6870 garray_T *instr;
6871 int called_emsg_before = called_emsg;
6872 int ret = FAIL;
6873 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006874 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006875 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006876 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006877 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006879 // When using a function that was compiled before: Free old instructions.
6880 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006881 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006883 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6884 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006885 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006887 else
6888 {
6889 if (add_def_function(ufunc) == FAIL)
6890 return FAIL;
6891 new_def_function = TRUE;
6892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893
Bram Moolenaar985116a2020-07-12 17:31:09 +02006894 ufunc->uf_def_status = UF_COMPILING;
6895
Bram Moolenaara80faa82020-04-12 19:37:17 +02006896 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 cctx.ctx_ufunc = ufunc;
6898 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006899 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6901 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6902 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6903 cctx.ctx_type_list = &ufunc->uf_type_list;
6904 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6905 instr = &cctx.ctx_instr;
6906
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006907 // Set the context to the function, it may be compiled when called from
6908 // another script. Set the script version to the most modern one.
6909 // The line number will be set in next_line_from_context().
6910 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6912
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006913 // Make sure error messages are OK.
6914 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6915 if (do_estack_push)
6916 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006917 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006918
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006919 if (ufunc->uf_def_args.ga_len > 0)
6920 {
6921 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006922 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006923 int i;
6924 char_u *arg;
6925 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006926 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006927
6928 // Produce instructions for the default values of optional arguments.
6929 // Store the instruction index in uf_def_arg_idx[] so that we know
6930 // where to start when the function is called, depending on the number
6931 // of arguments.
6932 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6933 if (ufunc->uf_def_arg_idx == NULL)
6934 goto erret;
6935 for (i = 0; i < count; ++i)
6936 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006937 garray_T *stack = &cctx.ctx_type_stack;
6938 type_T *val_type;
6939 int arg_idx = first_def_arg + i;
6940
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006941 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6942 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006943 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006944 goto erret;
6945
6946 // If no type specified use the type of the default value.
6947 // Otherwise check that the default value type matches the
6948 // specified type.
6949 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6950 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006951 {
6952 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006953 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006954 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006955 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6956 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006957 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006958
6959 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006960 goto erret;
6961 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006962 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006963
6964 if (did_set_arg_type)
6965 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006966 }
6967
6968 /*
6969 * Loop over all the lines of the function and generate instructions.
6970 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 for (;;)
6972 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006973 exarg_T ea;
6974 cmdmod_T save_cmdmod;
6975 int starts_with_colon = FALSE;
6976 char_u *cmd;
6977 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006978
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006979 // Bail out on the first error to avoid a flood of errors and report
6980 // the right line number when inside try/catch.
6981 if (emsg_before != called_emsg)
6982 goto erret;
6983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 if (line != NULL && *line == '|')
6985 // the line continues after a '|'
6986 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006987 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006988 && !(*line == '#' && (line == cctx.ctx_line_start
6989 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006991 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992 goto erret;
6993 }
6994 else
6995 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006996 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006997 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006998 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007001 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002
Bram Moolenaara80faa82020-04-12 19:37:17 +02007003 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 ea.cmdlinep = &line;
7005 ea.cmd = skipwhite(line);
7006
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007007 // Some things can be recognized by the first character.
7008 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007010 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007011 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007012 if (ea.cmd[1] != '{')
7013 {
7014 line = (char_u *)"";
7015 continue;
7016 }
7017 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007019 case '}':
7020 {
7021 // "}" ends a block scope
7022 scopetype_T stype = cctx.ctx_scope == NULL
7023 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007025 if (stype == BLOCK_SCOPE)
7026 {
7027 compile_endblock(&cctx);
7028 line = ea.cmd;
7029 }
7030 else
7031 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007032 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007033 goto erret;
7034 }
7035 if (line != NULL)
7036 line = skipwhite(ea.cmd + 1);
7037 continue;
7038 }
7039
7040 case '{':
7041 // "{" starts a block scope
7042 // "{'a': 1}->func() is something else
7043 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7044 {
7045 line = compile_block(ea.cmd, &cctx);
7046 continue;
7047 }
7048 break;
7049
7050 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007051 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007052 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 }
7054
7055 /*
7056 * COMMAND MODIFIERS
7057 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007058 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7060 {
7061 if (errormsg != NULL)
7062 goto erret;
7063 // empty line or comment
7064 line = (char_u *)"";
7065 continue;
7066 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007067 // TODO: use modifiers in the command
7068 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007069 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070
7071 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007072 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007074 {
7075 if (*ea.cmd == '(')
7076 // not for "call()"
7077 ea.cmd = p;
7078 else
7079 ea.cmd = skipwhite(ea.cmd);
7080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007082 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007084 char_u *pskip;
7085
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007086 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007087 // find what follows.
7088 // Skip over "var.member", "var[idx]" and the like.
7089 // Also "&opt = val", "$ENV = val" and "@r = val".
7090 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007091 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007092 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007093 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007095 char_u *var_end;
7096 int oplen;
7097 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098
Bram Moolenaar65821722020-08-02 18:58:54 +02007099 if (ea.cmd[0] == '@')
7100 var_end = ea.cmd + 2;
7101 else
7102 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007103 FNE_CHECK_START | FNE_INCL_BR);
7104 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007105 if (oplen > 0)
7106 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007107 size_t len = p - ea.cmd;
7108
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007109 // Recognize an assignment if we recognize the variable
7110 // name:
7111 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007112 // "local = expr" where "local" is a local var.
7113 // "script = expr" where "script" is a script-local var.
7114 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007115 // "&opt = expr"
7116 // "$ENV = expr"
7117 // "@r = expr"
7118 if (*ea.cmd == '&'
7119 || *ea.cmd == '$'
7120 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007121 || ((len) > 2 && ea.cmd[1] == ':')
7122 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007123 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007124 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007125 || script_var_exists(ea.cmd, len,
7126 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007127 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007128 {
7129 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007130 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007131 goto erret;
7132 continue;
7133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134 }
7135 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007136
7137 if (*ea.cmd == '[')
7138 {
7139 // [var, var] = expr
7140 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7141 if (line == NULL)
7142 goto erret;
7143 if (line != ea.cmd)
7144 continue;
7145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 }
7147
7148 /*
7149 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007150 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007152 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007153 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007154 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007155 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007156 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007157 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007158 if (!starts_with_colon)
7159 {
7160 emsg(_(e_colon_required_before_a_range));
7161 goto erret;
7162 }
7163 if (ends_excmd2(line, ea.cmd))
7164 {
7165 // A range without a command: jump to the line.
7166 // TODO: compile to a more efficient command, possibly
7167 // calling parse_cmd_address().
7168 ea.cmdidx = CMD_SIZE;
7169 line = compile_exec(line, &ea, &cctx);
7170 goto nextline;
7171 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007172 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007173 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007174 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007175 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007176 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
7178 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7179 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007180 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007181 {
7182 line += STRLEN(line);
7183 continue;
7184 }
7185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007187 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007189 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007190 iemsg("Command from find_ex_command() not handled");
7191 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193 }
7194
Bram Moolenaar3988f642020-08-27 22:43:03 +02007195 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007196 && ea.cmdidx != CMD_elseif
7197 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007198 && ea.cmdidx != CMD_endif
7199 && ea.cmdidx != CMD_endfor
7200 && ea.cmdidx != CMD_endwhile
7201 && ea.cmdidx != CMD_catch
7202 && ea.cmdidx != CMD_finally
7203 && ea.cmdidx != CMD_endtry)
7204 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007205 emsg(_(e_unreachable_code_after_return));
7206 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007207 }
7208
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007209 p = skipwhite(p);
7210 if (ea.cmdidx != CMD_SIZE
7211 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7212 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007213 if (ea.cmdidx >= 0)
7214 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007215 if ((ea.argt & EX_BANG) && *p == '!')
7216 {
7217 ea.forceit = TRUE;
7218 p = skipwhite(p + 1);
7219 }
7220 }
7221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 switch (ea.cmdidx)
7223 {
7224 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007225 ea.arg = p;
7226 line = compile_nested_function(&ea, &cctx);
7227 break;
7228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007230 // TODO: should we allow this, e.g. to declare a global
7231 // function?
7232 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233 goto erret;
7234
7235 case CMD_return:
7236 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007237 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238 break;
7239
7240 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007241 if (get_vim_var_nr(VV_DISALLOW_LET))
7242 {
7243 emsg(_(e_cannot_use_let_in_vim9_script));
7244 break;
7245 }
7246 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007247 case CMD_var:
7248 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 case CMD_const:
7250 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007251 if (line == p)
7252 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 break;
7254
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007255 case CMD_unlet:
7256 case CMD_unlockvar:
7257 case CMD_lockvar:
7258 line = compile_unletlock(p, &ea, &cctx);
7259 break;
7260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 case CMD_import:
7262 line = compile_import(p, &cctx);
7263 break;
7264
7265 case CMD_if:
7266 line = compile_if(p, &cctx);
7267 break;
7268 case CMD_elseif:
7269 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007270 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 break;
7272 case CMD_else:
7273 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007274 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 break;
7276 case CMD_endif:
7277 line = compile_endif(p, &cctx);
7278 break;
7279
7280 case CMD_while:
7281 line = compile_while(p, &cctx);
7282 break;
7283 case CMD_endwhile:
7284 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007285 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 break;
7287
7288 case CMD_for:
7289 line = compile_for(p, &cctx);
7290 break;
7291 case CMD_endfor:
7292 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007293 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 break;
7295 case CMD_continue:
7296 line = compile_continue(p, &cctx);
7297 break;
7298 case CMD_break:
7299 line = compile_break(p, &cctx);
7300 break;
7301
7302 case CMD_try:
7303 line = compile_try(p, &cctx);
7304 break;
7305 case CMD_catch:
7306 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007307 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 break;
7309 case CMD_finally:
7310 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007311 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 break;
7313 case CMD_endtry:
7314 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007315 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 break;
7317 case CMD_throw:
7318 line = compile_throw(p, &cctx);
7319 break;
7320
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007321 case CMD_eval:
7322 if (compile_expr0(&p, &cctx) == FAIL)
7323 goto erret;
7324
Bram Moolenaar3988f642020-08-27 22:43:03 +02007325 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007326 generate_instr_drop(&cctx, ISN_DROP, 1);
7327
7328 line = skipwhite(p);
7329 break;
7330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007333 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007334 case CMD_echomsg:
7335 case CMD_echoerr:
7336 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007337 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007339 case CMD_put:
7340 ea.cmd = cmd;
7341 line = compile_put(p, &ea, &cctx);
7342 break;
7343
Bram Moolenaar3988f642020-08-27 22:43:03 +02007344 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007345
Bram Moolenaarae616492020-07-28 20:07:27 +02007346 case CMD_append:
7347 case CMD_change:
7348 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007349 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007350 case CMD_xit:
7351 not_in_vim9(&ea);
7352 goto erret;
7353
Bram Moolenaar002262f2020-07-08 17:47:57 +02007354 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007355 if (cctx.ctx_skip != SKIP_YES)
7356 {
7357 semsg(_(e_invalid_command_str), ea.cmd);
7358 goto erret;
7359 }
7360 // We don't check for a next command here.
7361 line = (char_u *)"";
7362 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007365 if (cctx.ctx_skip == SKIP_YES)
7366 {
7367 // We don't check for a next command here.
7368 line = (char_u *)"";
7369 }
7370 else
7371 {
7372 // Not recognized, execute with do_cmdline_cmd().
7373 ea.arg = p;
7374 line = compile_exec(line, &ea, &cctx);
7375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 break;
7377 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007378nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 if (line == NULL)
7380 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007381 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382
7383 if (cctx.ctx_type_stack.ga_len < 0)
7384 {
7385 iemsg("Type stack underflow");
7386 goto erret;
7387 }
7388 }
7389
7390 if (cctx.ctx_scope != NULL)
7391 {
7392 if (cctx.ctx_scope->se_type == IF_SCOPE)
7393 emsg(_(e_endif));
7394 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7395 emsg(_(e_endwhile));
7396 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7397 emsg(_(e_endfor));
7398 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007399 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 goto erret;
7401 }
7402
Bram Moolenaarefd88552020-06-18 20:50:10 +02007403 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 {
7405 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7406 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007407 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 goto erret;
7409 }
7410
7411 // Return zero if there is no return at the end.
7412 generate_PUSHNR(&cctx, 0);
7413 generate_instr(&cctx, ISN_RETURN);
7414 }
7415
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007416 {
7417 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7418 + ufunc->uf_dfunc_idx;
7419 dfunc->df_deleted = FALSE;
7420 dfunc->df_instr = instr->ga_data;
7421 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007422 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007423 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007424 if (cctx.ctx_outer_used)
7425 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007426 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428
7429 ret = OK;
7430
7431erret:
7432 if (ret == FAIL)
7433 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007434 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007435 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7436 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007437
7438 for (idx = 0; idx < instr->ga_len; ++idx)
7439 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007441
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007442 // If using the last entry in the table and it was added above, we
7443 // might as well remove it.
7444 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007445 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007446 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007447 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007448 ufunc->uf_dfunc_idx = 0;
7449 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007450 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007451
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007452 while (cctx.ctx_scope != NULL)
7453 drop_scope(&cctx);
7454
Bram Moolenaar20431c92020-03-20 18:39:46 +01007455 // Don't execute this function body.
7456 ga_clear_strings(&ufunc->uf_lines);
7457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 if (errormsg != NULL)
7459 emsg(errormsg);
7460 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007461 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 }
7463
7464 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007465 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007466 if (do_estack_push)
7467 estack_pop();
7468
Bram Moolenaar20431c92020-03-20 18:39:46 +01007469 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007470 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007472 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473}
7474
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007475 void
7476set_function_type(ufunc_T *ufunc)
7477{
7478 int varargs = ufunc->uf_va_name != NULL;
7479 int argcount = ufunc->uf_args.ga_len;
7480
7481 // Create a type for the function, with the return type and any
7482 // argument types.
7483 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7484 // The type is included in "tt_args".
7485 if (argcount > 0 || varargs)
7486 {
7487 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7488 argcount, &ufunc->uf_type_list);
7489 // Add argument types to the function type.
7490 if (func_type_add_arg_types(ufunc->uf_func_type,
7491 argcount + varargs,
7492 &ufunc->uf_type_list) == FAIL)
7493 return;
7494 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7495 ufunc->uf_func_type->tt_min_argcount =
7496 argcount - ufunc->uf_def_args.ga_len;
7497 if (ufunc->uf_arg_types == NULL)
7498 {
7499 int i;
7500
7501 // lambda does not have argument types.
7502 for (i = 0; i < argcount; ++i)
7503 ufunc->uf_func_type->tt_args[i] = &t_any;
7504 }
7505 else
7506 mch_memmove(ufunc->uf_func_type->tt_args,
7507 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7508 if (varargs)
7509 {
7510 ufunc->uf_func_type->tt_args[argcount] =
7511 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7512 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7513 }
7514 }
7515 else
7516 // No arguments, can use a predefined type.
7517 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7518 argcount, &ufunc->uf_type_list);
7519}
7520
7521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522/*
7523 * Delete an instruction, free what it contains.
7524 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007525 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526delete_instr(isn_T *isn)
7527{
7528 switch (isn->isn_type)
7529 {
7530 case ISN_EXEC:
7531 case ISN_LOADENV:
7532 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007533 case ISN_LOADB:
7534 case ISN_LOADW:
7535 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007537 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007538 case ISN_PUSHEXC:
7539 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007540 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007542 case ISN_STOREB:
7543 case ISN_STOREW:
7544 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007545 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546 vim_free(isn->isn_arg.string);
7547 break;
7548
7549 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007550 case ISN_STORES:
7551 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 break;
7553
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007554 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007555 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007556 vim_free(isn->isn_arg.unlet.ul_name);
7557 break;
7558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559 case ISN_STOREOPT:
7560 vim_free(isn->isn_arg.storeopt.so_name);
7561 break;
7562
7563 case ISN_PUSHBLOB: // push blob isn_arg.blob
7564 blob_unref(isn->isn_arg.blob);
7565 break;
7566
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007567 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007568#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007569 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007570#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007571 break;
7572
7573 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007574#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007575 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007576#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007577 break;
7578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 case ISN_UCALL:
7580 vim_free(isn->isn_arg.ufunc.cuf_name);
7581 break;
7582
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007583 case ISN_FUNCREF:
7584 {
7585 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7586 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007587
7588 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7589 func_ptr_unref(dfunc->df_ufunc);
7590 }
7591 break;
7592
7593 case ISN_DCALL:
7594 {
7595 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7596 + isn->isn_arg.dfunc.cdf_idx;
7597
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007598 if (dfunc->df_ufunc != NULL
7599 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007600 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007601 }
7602 break;
7603
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007604 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007605 {
7606 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7607 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7608
7609 if (ufunc != NULL)
7610 {
7611 // Clear uf_dfunc_idx so that the function is deleted.
7612 clear_def_function(ufunc);
7613 ufunc->uf_dfunc_idx = 0;
7614 func_ptr_unref(ufunc);
7615 }
7616
7617 vim_free(lambda);
7618 vim_free(isn->isn_arg.newfunc.nf_global);
7619 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007620 break;
7621
Bram Moolenaar5e654232020-09-16 15:22:00 +02007622 case ISN_CHECKTYPE:
7623 free_type(isn->isn_arg.type.ct_type);
7624 break;
7625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 case ISN_2BOOL:
7627 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007628 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 case ISN_ADDBLOB:
7630 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007631 case ISN_ANYINDEX:
7632 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633 case ISN_BCALL:
7634 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007635 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637 case ISN_COMPAREANY:
7638 case ISN_COMPAREBLOB:
7639 case ISN_COMPAREBOOL:
7640 case ISN_COMPAREDICT:
7641 case ISN_COMPAREFLOAT:
7642 case ISN_COMPAREFUNC:
7643 case ISN_COMPARELIST:
7644 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 case ISN_COMPARESPECIAL:
7646 case ISN_COMPARESTRING:
7647 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007648 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 case ISN_DROP:
7650 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007651 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007652 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007654 case ISN_EXECCONCAT:
7655 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007656 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007657 case ISN_GETITEM:
7658 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007659 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007660 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007662 case ISN_LOADBDICT:
7663 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007664 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007666 case ISN_LOADSCRIPT:
7667 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007669 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007670 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007671 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672 case ISN_NEGATENR:
7673 case ISN_NEWDICT:
7674 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007676 case ISN_OPFLOAT:
7677 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007679 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007680 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 case ISN_PUSHF:
7682 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007684 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007686 case ISN_SHUFFLE:
7687 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007689 case ISN_STOREDICT:
7690 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007691 case ISN_STORENR:
7692 case ISN_STOREOUTER:
7693 case ISN_STOREREG:
7694 case ISN_STORESCRIPT:
7695 case ISN_STOREV:
7696 case ISN_STRINDEX:
7697 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007698 case ISN_THROW:
7699 case ISN_TRY:
7700 // nothing allocated
7701 break;
7702 }
7703}
7704
7705/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007706 * Free all instructions for "dfunc".
7707 */
7708 static void
7709delete_def_function_contents(dfunc_T *dfunc)
7710{
7711 int idx;
7712
7713 ga_clear(&dfunc->df_def_args_isn);
7714
7715 if (dfunc->df_instr != NULL)
7716 {
7717 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7718 delete_instr(dfunc->df_instr + idx);
7719 VIM_CLEAR(dfunc->df_instr);
7720 }
7721
7722 dfunc->df_deleted = TRUE;
7723}
7724
7725/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007726 * When a user function is deleted, clear the contents of any associated def
7727 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728 */
7729 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007730clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007732 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007733 {
7734 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7735 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736
Bram Moolenaar20431c92020-03-20 18:39:46 +01007737 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007738 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 }
7740}
7741
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007742/*
7743 * Used when a user function is about to be deleted: remove the pointer to it.
7744 * The entry in def_functions is then unused.
7745 */
7746 void
7747unlink_def_function(ufunc_T *ufunc)
7748{
7749 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7750
7751 dfunc->df_ufunc = NULL;
7752}
7753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007755/*
7756 * Free all functions defined with ":def".
7757 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 void
7759free_def_functions(void)
7760{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007761 int idx;
7762
7763 for (idx = 0; idx < def_functions.ga_len; ++idx)
7764 {
7765 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7766
7767 delete_def_function_contents(dfunc);
7768 }
7769
7770 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771}
7772#endif
7773
7774
7775#endif // FEAT_EVAL