blob: d9908d30bec0c3b58acae4621e9bf0fb5f88d377 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200198arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200250 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200262 * Lookup a script-local variable in the current script, possibly defined in a
263 * block that contains the function "cctx->ctx_ufunc".
264 * "cctx" is NULL at the script level.
265 * if "len" is <= 0 "name" must be NUL terminated.
266 * Return NULL when not found.
267 */
268 static sallvar_T *
269find_script_var(char_u *name, size_t len, cctx_T *cctx)
270{
271 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
272 hashitem_T *hi;
273 int cc;
274 sallvar_T *sav;
275 ufunc_T *ufunc;
276
277 // Find the list of all script variables with the right name.
278 if (len > 0)
279 {
280 cc = name[len];
281 name[len] = NUL;
282 }
283 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
284 if (len > 0)
285 name[len] = cc;
286 if (HASHITEM_EMPTY(hi))
287 return NULL;
288
289 sav = HI2SAV(hi);
290 if (sav->sav_block_id == 0 || cctx == NULL)
291 // variable defined in the script scope or not in a function.
292 return sav;
293
294 // Go over the variables with this name and find one that was visible
295 // from the function.
296 ufunc = cctx->ctx_ufunc;
297 while (sav != NULL)
298 {
299 int idx;
300
301 // Go over the blocks that this function was defined in. If the
302 // variable block ID matches it was visible to the function.
303 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
304 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
305 return sav;
306 sav = sav->sav_next;
307 }
308
309 return NULL;
310}
311
312/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200313 * Returnd TRUE if the script context is Vim9 script.
314 */
315 static int
316script_is_vim9()
317{
318 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
319}
320
321/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200322 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200323 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
324 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200325 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 * Returns OK or FAIL.
327 */
328 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200333 if (current_sctx.sc_sid <= 0)
334 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 is_vim9_script = script_is_vim9();
336 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200337 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200338 if (is_vim9_script)
339 {
340 // Check script variables that were visible where the function was
341 // defined.
342 if (find_script_var(name, len, cctx) != NULL)
343 return OK;
344 }
345 else
346 {
347 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
348 dictitem_T *di;
349 int cc;
350
351 // Check script variables that are currently visible
352 cc = name[len];
353 name[len] = NUL;
354 di = find_var_in_ht(ht, 0, name, TRUE);
355 name[len] = cc;
356 if (di != NULL)
357 return OK;
358 }
359
360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361}
362
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100363/*
364 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200365 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200366 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100367 * Return FAIL and give an error if it defined.
368 */
369 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200370check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200372 int c = p[len];
373 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200374
375 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200376 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100377 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200378 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200379 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200380 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200381 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100382 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 // A local or script-local function can shadow a global function.
384 if (ufunc == NULL || !func_is_global(ufunc)
385 || (p[0] == 'g' && p[1] == ':'))
386 {
387 p[len] = c;
388 semsg(_(e_name_already_defined_str), p);
389 return FAIL;
390 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100391 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200392 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 return OK;
394}
395
Bram Moolenaar65b95452020-07-19 14:03:09 +0200396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397/////////////////////////////////////////////////////////////////////
398// Following generate_ functions expect the caller to call ga_grow().
399
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200400#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
401#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403/*
404 * Generate an instruction without arguments.
405 * Returns a pointer to the new instruction, NULL if failed.
406 */
407 static isn_T *
408generate_instr(cctx_T *cctx, isntype_T isn_type)
409{
410 garray_T *instr = &cctx->ctx_instr;
411 isn_T *isn;
412
Bram Moolenaar080457c2020-03-03 21:53:32 +0100413 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (ga_grow(instr, 1) == FAIL)
415 return NULL;
416 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
417 isn->isn_type = isn_type;
418 isn->isn_lnum = cctx->ctx_lnum + 1;
419 ++instr->ga_len;
420
421 return isn;
422}
423
424/*
425 * Generate an instruction without arguments.
426 * "drop" will be removed from the stack.
427 * Returns a pointer to the new instruction, NULL if failed.
428 */
429 static isn_T *
430generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
431{
432 garray_T *stack = &cctx->ctx_type_stack;
433
Bram Moolenaar080457c2020-03-03 21:53:32 +0100434 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 stack->ga_len -= drop;
436 return generate_instr(cctx, isn_type);
437}
438
439/*
440 * Generate instruction "isn_type" and put "type" on the type stack.
441 */
442 static isn_T *
443generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
444{
445 isn_T *isn;
446 garray_T *stack = &cctx->ctx_type_stack;
447
448 if ((isn = generate_instr(cctx, isn_type)) == NULL)
449 return NULL;
450
451 if (ga_grow(stack, 1) == FAIL)
452 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200453 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 ++stack->ga_len;
455
456 return isn;
457}
458
459/*
460 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200461 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 */
463 static int
464may_generate_2STRING(int offset, cctx_T *cctx)
465{
466 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200467 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 garray_T *stack = &cctx->ctx_type_stack;
469 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
470
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200471 switch ((*type)->tt_type)
472 {
473 // nothing to be done
474 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200476 // conversion possible
477 case VAR_SPECIAL:
478 case VAR_BOOL:
479 case VAR_NUMBER:
480 case VAR_FLOAT:
481 break;
482
483 // conversion possible (with runtime check)
484 case VAR_ANY:
485 case VAR_UNKNOWN:
486 isntype = ISN_2STRING_ANY;
487 break;
488
489 // conversion not possible
490 case VAR_VOID:
491 case VAR_BLOB:
492 case VAR_FUNC:
493 case VAR_PARTIAL:
494 case VAR_LIST:
495 case VAR_DICT:
496 case VAR_JOB:
497 case VAR_CHANNEL:
498 to_string_error((*type)->tt_type);
499 return FAIL;
500 }
501
502 *type = &t_string;
503 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 return FAIL;
505 isn->isn_arg.number = offset;
506
507 return OK;
508}
509
510 static int
511check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
512{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200513 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 {
517 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200518 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 return FAIL;
522 }
523 return OK;
524}
525
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200526 static int
527generate_add_instr(
528 cctx_T *cctx,
529 vartype_T vartype,
530 type_T *type1,
531 type_T *type2)
532{
533 isn_T *isn = generate_instr_drop(cctx,
534 vartype == VAR_NUMBER ? ISN_OPNR
535 : vartype == VAR_LIST ? ISN_ADDLIST
536 : vartype == VAR_BLOB ? ISN_ADDBLOB
537#ifdef FEAT_FLOAT
538 : vartype == VAR_FLOAT ? ISN_OPFLOAT
539#endif
540 : ISN_OPANY, 1);
541
542 if (vartype != VAR_LIST && vartype != VAR_BLOB
543 && type1->tt_type != VAR_ANY
544 && type2->tt_type != VAR_ANY
545 && check_number_or_float(
546 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
547 return FAIL;
548
549 if (isn != NULL)
550 isn->isn_arg.op.op_type = EXPR_ADD;
551 return isn == NULL ? FAIL : OK;
552}
553
554/*
555 * Get the type to use for an instruction for an operation on "type1" and
556 * "type2". If they are matching use a type-specific instruction. Otherwise
557 * fall back to runtime type checking.
558 */
559 static vartype_T
560operator_type(type_T *type1, type_T *type2)
561{
562 if (type1->tt_type == type2->tt_type
563 && (type1->tt_type == VAR_NUMBER
564 || type1->tt_type == VAR_LIST
565#ifdef FEAT_FLOAT
566 || type1->tt_type == VAR_FLOAT
567#endif
568 || type1->tt_type == VAR_BLOB))
569 return type1->tt_type;
570 return VAR_ANY;
571}
572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573/*
574 * Generate an instruction with two arguments. The instruction depends on the
575 * type of the arguments.
576 */
577 static int
578generate_two_op(cctx_T *cctx, char_u *op)
579{
580 garray_T *stack = &cctx->ctx_type_stack;
581 type_T *type1;
582 type_T *type2;
583 vartype_T vartype;
584 isn_T *isn;
585
Bram Moolenaar080457c2020-03-03 21:53:32 +0100586 RETURN_OK_IF_SKIP(cctx);
587
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200588 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
590 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200591 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592
593 switch (*op)
594 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200595 case '+':
596 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 break;
599
600 case '-':
601 case '*':
602 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
603 op) == FAIL)
604 return FAIL;
605 if (vartype == VAR_NUMBER)
606 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
607#ifdef FEAT_FLOAT
608 else if (vartype == VAR_FLOAT)
609 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
610#endif
611 else
612 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = *op == '*'
615 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
616 break;
617
Bram Moolenaar4c683752020-04-05 21:38:23 +0200618 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type2->tt_type != VAR_NUMBER))
622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200623 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 }
626 isn = generate_instr_drop(cctx,
627 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
628 if (isn != NULL)
629 isn->isn_arg.op.op_type = EXPR_REM;
630 break;
631 }
632
633 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200634 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 {
636 type_T *type = &t_any;
637
638#ifdef FEAT_FLOAT
639 // float+number and number+float results in float
640 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
641 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
642 type = &t_float;
643#endif
644 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
645 }
646
647 return OK;
648}
649
650/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200651 * Get the instruction to use for comparing "type1" with "type2"
652 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200654 static isntype_T
655get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656{
657 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658
Bram Moolenaar4c683752020-04-05 21:38:23 +0200659 if (type1 == VAR_UNKNOWN)
660 type1 = VAR_ANY;
661 if (type2 == VAR_UNKNOWN)
662 type2 = VAR_ANY;
663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 if (type1 == type2)
665 {
666 switch (type1)
667 {
668 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
669 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
670 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
671 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
672 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
673 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
674 case VAR_LIST: isntype = ISN_COMPARELIST; break;
675 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
676 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 default: isntype = ISN_COMPAREANY; break;
678 }
679 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
682 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
683 isntype = ISN_COMPAREANY;
684
685 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
686 && (isntype == ISN_COMPAREBOOL
687 || isntype == ISN_COMPARESPECIAL
688 || isntype == ISN_COMPARENR
689 || isntype == ISN_COMPAREFLOAT))
690 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200691 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200693 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 }
695 if (isntype == ISN_DROP
696 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
697 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
698 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
699 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
700 && exptype != EXPR_IS && exptype != EXPR_ISNOT
701 && (type1 == VAR_BLOB || type2 == VAR_BLOB
702 || type1 == VAR_LIST || type2 == VAR_LIST))))
703 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200704 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200706 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return isntype;
709}
710
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200711 int
712check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
713{
714 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
715 return FAIL;
716 return OK;
717}
718
Bram Moolenaara5565e42020-05-09 15:44:01 +0200719/*
720 * Generate an ISN_COMPARE* instruction with a boolean result.
721 */
722 static int
723generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
724{
725 isntype_T isntype;
726 isn_T *isn;
727 garray_T *stack = &cctx->ctx_type_stack;
728 vartype_T type1;
729 vartype_T type2;
730
731 RETURN_OK_IF_SKIP(cctx);
732
733 // Get the known type of the two items on the stack. If they are matching
734 // use a type-specific instruction. Otherwise fall back to runtime type
735 // checking.
736 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
737 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
738 isntype = get_compare_isn(exptype, type1, type2);
739 if (isntype == ISN_DROP)
740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
742 if ((isn = generate_instr(cctx, isntype)) == NULL)
743 return FAIL;
744 isn->isn_arg.op.op_type = exptype;
745 isn->isn_arg.op.op_ic = ic;
746
747 // takes two arguments, puts one bool back
748 if (stack->ga_len >= 2)
749 {
750 --stack->ga_len;
751 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
752 }
753
754 return OK;
755}
756
757/*
758 * Generate an ISN_2BOOL instruction.
759 */
760 static int
761generate_2BOOL(cctx_T *cctx, int invert)
762{
763 isn_T *isn;
764 garray_T *stack = &cctx->ctx_type_stack;
765
Bram Moolenaar080457c2020-03-03 21:53:32 +0100766 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
768 return FAIL;
769 isn->isn_arg.number = invert;
770
771 // type becomes bool
772 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
773
774 return OK;
775}
776
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200777/*
778 * Generate an ISN_COND2BOOL instruction.
779 */
780 static int
781generate_COND2BOOL(cctx_T *cctx)
782{
783 isn_T *isn;
784 garray_T *stack = &cctx->ctx_type_stack;
785
786 RETURN_OK_IF_SKIP(cctx);
787 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
788 return FAIL;
789
790 // type becomes bool
791 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
792
793 return OK;
794}
795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200797generate_TYPECHECK(
798 cctx_T *cctx,
799 type_T *expected,
800 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801{
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804
Bram Moolenaar080457c2020-03-03 21:53:32 +0100805 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
807 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200808 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 isn->isn_arg.type.ct_off = offset;
810
Bram Moolenaar5e654232020-09-16 15:22:00 +0200811 // type becomes expected
812 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 return OK;
815}
816
817/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200818 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
819 * used. Return FALSE if the types will never match.
820 */
821 static int
822use_typecheck(type_T *actual, type_T *expected)
823{
824 if (actual->tt_type == VAR_ANY
825 || actual->tt_type == VAR_UNKNOWN
826 || (actual->tt_type == VAR_FUNC
827 && (expected->tt_type == VAR_FUNC
828 || expected->tt_type == VAR_PARTIAL)
829 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
830 return TRUE;
831 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
832 && actual->tt_type == expected->tt_type)
833 // This takes care of a nested list or dict.
834 return use_typecheck(actual->tt_member, expected->tt_member);
835 return FALSE;
836}
837
838/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200839 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200840 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * - "actual" is a type that can be "expected" type: add a runtime check; or
842 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200843 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
844 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200845 */
846 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200847need_type(
848 type_T *actual,
849 type_T *expected,
850 int offset,
851 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200852 int silent,
853 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200854{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200855 if (expected == &t_bool && actual != &t_bool
856 && (actual->tt_flags & TTFLAG_BOOL_OK))
857 {
858 // Using "0", "1" or the result of an expression with "&&" or "||" as a
859 // boolean is OK but requires a conversion.
860 generate_2BOOL(cctx, FALSE);
861 return OK;
862 }
863
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200864 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200865 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200866
867 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200868 // If it's a constant a runtime check makes no sense.
869 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200871 generate_TYPECHECK(cctx, expected, offset);
872 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200873 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200874
875 if (!silent)
876 type_mismatch(expected, actual);
877 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878}
879
880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 * Generate an ISN_PUSHNR instruction.
882 */
883 static int
884generate_PUSHNR(cctx_T *cctx, varnumber_T number)
885{
886 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200887 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar080457c2020-03-03 21:53:32 +0100889 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
891 return FAIL;
892 isn->isn_arg.number = number;
893
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200894 if (number == 0 || number == 1)
895 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200896 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200897
898 // A 0 or 1 number can also be used as a bool.
899 if (type != NULL)
900 {
901 type->tt_type = VAR_NUMBER;
902 type->tt_flags = TTFLAG_BOOL_OK;
903 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
904 }
905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 return OK;
907}
908
909/*
910 * Generate an ISN_PUSHBOOL instruction.
911 */
912 static int
913generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
914{
915 isn_T *isn;
916
Bram Moolenaar080457c2020-03-03 21:53:32 +0100917 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
919 return FAIL;
920 isn->isn_arg.number = number;
921
922 return OK;
923}
924
925/*
926 * Generate an ISN_PUSHSPEC instruction.
927 */
928 static int
929generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
930{
931 isn_T *isn;
932
Bram Moolenaar080457c2020-03-03 21:53:32 +0100933 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
935 return FAIL;
936 isn->isn_arg.number = number;
937
938 return OK;
939}
940
941#ifdef FEAT_FLOAT
942/*
943 * Generate an ISN_PUSHF instruction.
944 */
945 static int
946generate_PUSHF(cctx_T *cctx, float_T fnumber)
947{
948 isn_T *isn;
949
Bram Moolenaar080457c2020-03-03 21:53:32 +0100950 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
952 return FAIL;
953 isn->isn_arg.fnumber = fnumber;
954
955 return OK;
956}
957#endif
958
959/*
960 * Generate an ISN_PUSHS instruction.
961 * Consumes "str".
962 */
963 static int
964generate_PUSHS(cctx_T *cctx, char_u *str)
965{
966 isn_T *isn;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
970 return FAIL;
971 isn->isn_arg.string = str;
972
973 return OK;
974}
975
976/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100977 * Generate an ISN_PUSHCHANNEL instruction.
978 * Consumes "channel".
979 */
980 static int
981generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
982{
983 isn_T *isn;
984
Bram Moolenaar080457c2020-03-03 21:53:32 +0100985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100986 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
987 return FAIL;
988 isn->isn_arg.channel = channel;
989
990 return OK;
991}
992
993/*
994 * Generate an ISN_PUSHJOB instruction.
995 * Consumes "job".
996 */
997 static int
998generate_PUSHJOB(cctx_T *cctx, job_T *job)
999{
1000 isn_T *isn;
1001
Bram Moolenaar080457c2020-03-03 21:53:32 +01001002 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001003 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001004 return FAIL;
1005 isn->isn_arg.job = job;
1006
1007 return OK;
1008}
1009
1010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 * Generate an ISN_PUSHBLOB instruction.
1012 * Consumes "blob".
1013 */
1014 static int
1015generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1016{
1017 isn_T *isn;
1018
Bram Moolenaar080457c2020-03-03 21:53:32 +01001019 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.blob = blob;
1023
1024 return OK;
1025}
1026
1027/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 * Generate an ISN_PUSHFUNC instruction with name "name".
1029 * Consumes "name".
1030 */
1031 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001032generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001037 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001038 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001039 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040
1041 return OK;
1042}
1043
1044/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 * Generate an ISN_GETITEM instruction with "index".
1046 */
1047 static int
1048generate_GETITEM(cctx_T *cctx, int index)
1049{
1050 isn_T *isn;
1051 garray_T *stack = &cctx->ctx_type_stack;
1052 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1053 type_T *item_type = &t_any;
1054
1055 RETURN_OK_IF_SKIP(cctx);
1056
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001057 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001059 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 emsg(_(e_listreq));
1061 return FAIL;
1062 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001063 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001064 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = index;
1067
1068 // add the item type to the type stack
1069 if (ga_grow(stack, 1) == FAIL)
1070 return FAIL;
1071 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1072 ++stack->ga_len;
1073 return OK;
1074}
1075
1076/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001077 * Generate an ISN_SLICE instruction with "count".
1078 */
1079 static int
1080generate_SLICE(cctx_T *cctx, int count)
1081{
1082 isn_T *isn;
1083
1084 RETURN_OK_IF_SKIP(cctx);
1085 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.number = count;
1088 return OK;
1089}
1090
1091/*
1092 * Generate an ISN_CHECKLEN instruction with "min_len".
1093 */
1094 static int
1095generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1096{
1097 isn_T *isn;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100
1101 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.checklen.cl_min_len = min_len;
1104 isn->isn_arg.checklen.cl_more_OK = more_OK;
1105
1106 return OK;
1107}
1108
1109/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 * Generate an ISN_STORE instruction.
1111 */
1112 static int
1113generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1114{
1115 isn_T *isn;
1116
Bram Moolenaar080457c2020-03-03 21:53:32 +01001117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1119 return FAIL;
1120 if (name != NULL)
1121 isn->isn_arg.string = vim_strsave(name);
1122 else
1123 isn->isn_arg.number = idx;
1124
1125 return OK;
1126}
1127
1128/*
1129 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1130 */
1131 static int
1132generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1133{
1134 isn_T *isn;
1135
Bram Moolenaar080457c2020-03-03 21:53:32 +01001136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1138 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001139 isn->isn_arg.storenr.stnr_idx = idx;
1140 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141
1142 return OK;
1143}
1144
1145/*
1146 * Generate an ISN_STOREOPT instruction
1147 */
1148 static int
1149generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1150{
1151 isn_T *isn;
1152
Bram Moolenaar080457c2020-03-03 21:53:32 +01001153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001154 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 return FAIL;
1156 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1157 isn->isn_arg.storeopt.so_flags = opt_flags;
1158
1159 return OK;
1160}
1161
1162/*
1163 * Generate an ISN_LOAD or similar instruction.
1164 */
1165 static int
1166generate_LOAD(
1167 cctx_T *cctx,
1168 isntype_T isn_type,
1169 int idx,
1170 char_u *name,
1171 type_T *type)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1177 return FAIL;
1178 if (name != NULL)
1179 isn->isn_arg.string = vim_strsave(name);
1180 else
1181 isn->isn_arg.number = idx;
1182
1183 return OK;
1184}
1185
1186/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001187 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188 */
1189 static int
1190generate_LOADV(
1191 cctx_T *cctx,
1192 char_u *name,
1193 int error)
1194{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001195 int di_flags;
1196 int vidx = find_vim_var(name, &di_flags);
1197 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200 if (vidx < 0)
1201 {
1202 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001203 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001204 return FAIL;
1205 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001206 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001207
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001208 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209}
1210
1211/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001212 * Generate an ISN_UNLET instruction.
1213 */
1214 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001215generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001216{
1217 isn_T *isn;
1218
1219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001220 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001221 return FAIL;
1222 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1223 isn->isn_arg.unlet.ul_forceit = forceit;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001229 * Generate an ISN_LOCKCONST instruction.
1230 */
1231 static int
1232generate_LOCKCONST(cctx_T *cctx)
1233{
1234 isn_T *isn;
1235
1236 RETURN_OK_IF_SKIP(cctx);
1237 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1238 return FAIL;
1239 return OK;
1240}
1241
1242/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 * Generate an ISN_LOADS instruction.
1244 */
1245 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250 int sid,
1251 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252{
1253 isn_T *isn;
1254
Bram Moolenaar080457c2020-03-03 21:53:32 +01001255 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001256 if (isn_type == ISN_LOADS)
1257 isn = generate_instr_type(cctx, isn_type, type);
1258 else
1259 isn = generate_instr_drop(cctx, isn_type, 1);
1260 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001262 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1263 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 return OK;
1266}
1267
1268/*
1269 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1270 */
1271 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 cctx_T *cctx,
1274 isntype_T isn_type,
1275 int sid,
1276 int idx,
1277 type_T *type)
1278{
1279 isn_T *isn;
1280
Bram Moolenaar080457c2020-03-03 21:53:32 +01001281 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 if (isn_type == ISN_LOADSCRIPT)
1283 isn = generate_instr_type(cctx, isn_type, type);
1284 else
1285 isn = generate_instr_drop(cctx, isn_type, 1);
1286 if (isn == NULL)
1287 return FAIL;
1288 isn->isn_arg.script.script_sid = sid;
1289 isn->isn_arg.script.script_idx = idx;
1290 return OK;
1291}
1292
1293/*
1294 * Generate an ISN_NEWLIST instruction.
1295 */
1296 static int
1297generate_NEWLIST(cctx_T *cctx, int count)
1298{
1299 isn_T *isn;
1300 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 type_T *type;
1302 type_T *member;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1306 return FAIL;
1307 isn->isn_arg.number = count;
1308
Bram Moolenaar127542b2020-08-09 17:22:04 +02001309 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001310 if (count == 0)
1311 member = &t_void;
1312 else
1313 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001314 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1315 cctx->ctx_type_list);
1316 type = get_list_type(member, cctx->ctx_type_list);
1317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 // drop the value types
1319 stack->ga_len -= count;
1320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 // add the list type to the type stack
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1325 ++stack->ga_len;
1326
1327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_NEWDICT instruction.
1332 */
1333 static int
1334generate_NEWDICT(cctx_T *cctx, int count)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 type_T *type;
1339 type_T *member;
1340
Bram Moolenaar080457c2020-03-03 21:53:32 +01001341 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.number = count;
1345
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001346 if (count == 0)
1347 member = &t_void;
1348 else
1349 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001350 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1351 cctx->ctx_type_list);
1352 type = get_dict_type(member, cctx->ctx_type_list);
1353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 // drop the key and value types
1355 stack->ga_len -= 2 * count;
1356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 // add the dict type to the type stack
1358 if (ga_grow(stack, 1) == FAIL)
1359 return FAIL;
1360 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1361 ++stack->ga_len;
1362
1363 return OK;
1364}
1365
1366/*
1367 * Generate an ISN_FUNCREF instruction.
1368 */
1369 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001370generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371{
1372 isn_T *isn;
1373 garray_T *stack = &cctx->ctx_type_stack;
1374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1377 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001378 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001379 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 if (ga_grow(stack, 1) == FAIL)
1382 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001383 ((type_T **)stack->ga_data)[stack->ga_len] =
1384 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001391 * Generate an ISN_NEWFUNC instruction.
1392 */
1393 static int
1394generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1395{
1396 isn_T *isn;
1397 char_u *name;
1398
1399 RETURN_OK_IF_SKIP(cctx);
1400 name = vim_strsave(lambda_name);
1401 if (name == NULL)
1402 return FAIL;
1403 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1404 return FAIL;
1405 isn->isn_arg.newfunc.nf_lambda = name;
1406 isn->isn_arg.newfunc.nf_global = func_name;
1407
1408 return OK;
1409}
1410
1411/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 * Generate an ISN_JUMP instruction.
1413 */
1414 static int
1415generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1416{
1417 isn_T *isn;
1418 garray_T *stack = &cctx->ctx_type_stack;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.jump.jump_when = when;
1424 isn->isn_arg.jump.jump_where = where;
1425
1426 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1427 --stack->ga_len;
1428
1429 return OK;
1430}
1431
1432 static int
1433generate_FOR(cctx_T *cctx, int loop_idx)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
1437
Bram Moolenaar080457c2020-03-03 21:53:32 +01001438 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1440 return FAIL;
1441 isn->isn_arg.forloop.for_idx = loop_idx;
1442
1443 if (ga_grow(stack, 1) == FAIL)
1444 return FAIL;
1445 // type doesn't matter, will be stored next
1446 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1447 ++stack->ga_len;
1448
1449 return OK;
1450}
1451
1452/*
1453 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001454 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 * Return FAIL if the number of arguments is wrong.
1456 */
1457 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001458generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001462 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001463 type_T *argtypes[MAX_FUNC_ARGS];
1464 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465
Bram Moolenaar080457c2020-03-03 21:53:32 +01001466 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001467 argoff = check_internal_func(func_idx, argcount);
1468 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 return FAIL;
1470
Bram Moolenaar389df252020-07-09 21:20:47 +02001471 if (method_call && argoff > 1)
1472 {
1473 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.shuffle.shfl_item = argcount;
1476 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1477 }
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1480 return FAIL;
1481 isn->isn_arg.bfunc.cbf_idx = func_idx;
1482 isn->isn_arg.bfunc.cbf_argcount = argcount;
1483
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001484 for (i = 0; i < argcount; ++i)
1485 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 stack->ga_len -= argcount; // drop the arguments
1488 if (ga_grow(stack, 1) == FAIL)
1489 return FAIL;
1490 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001491 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 ++stack->ga_len; // add return value
1493
1494 return OK;
1495}
1496
1497/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001498 * Generate an ISN_LISTAPPEND instruction. Works like add().
1499 * Argument count is already checked.
1500 */
1501 static int
1502generate_LISTAPPEND(cctx_T *cctx)
1503{
1504 garray_T *stack = &cctx->ctx_type_stack;
1505 type_T *list_type;
1506 type_T *item_type;
1507 type_T *expected;
1508
1509 // Caller already checked that list_type is a list.
1510 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1511 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1512 expected = list_type->tt_member;
1513 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1514 return FAIL;
1515
1516 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1517 return FAIL;
1518
1519 --stack->ga_len; // drop the argument
1520 return OK;
1521}
1522
1523/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001524 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1525 * Argument count is already checked.
1526 */
1527 static int
1528generate_BLOBAPPEND(cctx_T *cctx)
1529{
1530 garray_T *stack = &cctx->ctx_type_stack;
1531 type_T *item_type;
1532
1533 // Caller already checked that blob_type is a blob.
1534 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1535 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1536 return FAIL;
1537
1538 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1539 return FAIL;
1540
1541 --stack->ga_len; // drop the argument
1542 return OK;
1543}
1544
1545/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 * Generate an ISN_DCALL or ISN_UCALL instruction.
1547 * Return FAIL if the number of arguments is wrong.
1548 */
1549 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001550generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551{
1552 isn_T *isn;
1553 garray_T *stack = &cctx->ctx_type_stack;
1554 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001555 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556
Bram Moolenaar080457c2020-03-03 21:53:32 +01001557 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 if (argcount > regular_args && !has_varargs(ufunc))
1559 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001560 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 return FAIL;
1562 }
1563 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1564 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001565 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 return FAIL;
1567 }
1568
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001569 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001570 {
1571 int i;
1572
1573 for (i = 0; i < argcount; ++i)
1574 {
1575 type_T *expected;
1576 type_T *actual;
1577
1578 if (i < regular_args)
1579 {
1580 if (ufunc->uf_arg_types == NULL)
1581 continue;
1582 expected = ufunc->uf_arg_types[i];
1583 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001584 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1585 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001586 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001587 else
1588 expected = ufunc->uf_va_type->tt_member;
1589 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001590 if (need_type(actual, expected, -argcount + i, cctx,
1591 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001592 {
1593 arg_type_mismatch(expected, actual, i + 1);
1594 return FAIL;
1595 }
1596 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001597 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001598 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1599 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001600 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001601 }
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001604 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001605 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001607 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 {
1609 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1610 isn->isn_arg.dfunc.cdf_argcount = argcount;
1611 }
1612 else
1613 {
1614 // A user function may be deleted and redefined later, can't use the
1615 // ufunc pointer, need to look it up again at runtime.
1616 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1617 isn->isn_arg.ufunc.cuf_argcount = argcount;
1618 }
1619
1620 stack->ga_len -= argcount; // drop the arguments
1621 if (ga_grow(stack, 1) == FAIL)
1622 return FAIL;
1623 // add return value
1624 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1625 ++stack->ga_len;
1626
1627 return OK;
1628}
1629
1630/*
1631 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1632 */
1633 static int
1634generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1635{
1636 isn_T *isn;
1637 garray_T *stack = &cctx->ctx_type_stack;
1638
Bram Moolenaar080457c2020-03-03 21:53:32 +01001639 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1641 return FAIL;
1642 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1643 isn->isn_arg.ufunc.cuf_argcount = argcount;
1644
1645 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001646 if (ga_grow(stack, 1) == FAIL)
1647 return FAIL;
1648 // add return value
1649 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1650 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651
1652 return OK;
1653}
1654
1655/*
1656 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001657 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 */
1659 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001660generate_PCALL(
1661 cctx_T *cctx,
1662 int argcount,
1663 char_u *name,
1664 type_T *type,
1665 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666{
1667 isn_T *isn;
1668 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001669 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaar080457c2020-03-03 21:53:32 +01001671 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001672
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001673 if (type->tt_type == VAR_ANY)
1674 ret_type = &t_any;
1675 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001676 {
1677 if (type->tt_argcount != -1)
1678 {
1679 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1680
1681 if (argcount < type->tt_min_argcount - varargs)
1682 {
1683 semsg(_(e_toofewarg), "[reference]");
1684 return FAIL;
1685 }
1686 if (!varargs && argcount > type->tt_argcount)
1687 {
1688 semsg(_(e_toomanyarg), "[reference]");
1689 return FAIL;
1690 }
1691 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001692 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001693 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001694 else
1695 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001696 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001697 return FAIL;
1698 }
1699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1701 return FAIL;
1702 isn->isn_arg.pfunc.cpf_top = at_top;
1703 isn->isn_arg.pfunc.cpf_argcount = argcount;
1704
1705 stack->ga_len -= argcount; // drop the arguments
1706
1707 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001708 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001710 // If partial is above the arguments it must be cleared and replaced with
1711 // the return value.
1712 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1713 return FAIL;
1714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 return OK;
1716}
1717
1718/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001719 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 */
1721 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001722generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723{
1724 isn_T *isn;
1725 garray_T *stack = &cctx->ctx_type_stack;
1726 type_T *type;
1727
Bram Moolenaar080457c2020-03-03 21:53:32 +01001728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001729 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001731 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001733 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001735 if (type->tt_type != VAR_DICT && type != &t_any)
1736 {
1737 emsg(_(e_dictreq));
1738 return FAIL;
1739 }
1740 // change dict type to dict member type
1741 if (type->tt_type == VAR_DICT)
1742 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
1744 return OK;
1745}
1746
1747/*
1748 * Generate an ISN_ECHO instruction.
1749 */
1750 static int
1751generate_ECHO(cctx_T *cctx, int with_white, int count)
1752{
1753 isn_T *isn;
1754
Bram Moolenaar080457c2020-03-03 21:53:32 +01001755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1757 return FAIL;
1758 isn->isn_arg.echo.echo_with_white = with_white;
1759 isn->isn_arg.echo.echo_count = count;
1760
1761 return OK;
1762}
1763
Bram Moolenaarad39c092020-02-26 18:23:43 +01001764/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001765 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001766 */
1767 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001768generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001769{
1770 isn_T *isn;
1771
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001772 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001773 return FAIL;
1774 isn->isn_arg.number = count;
1775
1776 return OK;
1777}
1778
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001779/*
1780 * Generate an ISN_PUT instruction.
1781 */
1782 static int
1783generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1784{
1785 isn_T *isn;
1786
1787 RETURN_OK_IF_SKIP(cctx);
1788 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1789 return FAIL;
1790 isn->isn_arg.put.put_regname = regname;
1791 isn->isn_arg.put.put_lnum = lnum;
1792 return OK;
1793}
1794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 static int
1796generate_EXEC(cctx_T *cctx, char_u *line)
1797{
1798 isn_T *isn;
1799
Bram Moolenaar080457c2020-03-03 21:53:32 +01001800 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1802 return FAIL;
1803 isn->isn_arg.string = vim_strsave(line);
1804 return OK;
1805}
1806
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001807 static int
1808generate_EXECCONCAT(cctx_T *cctx, int count)
1809{
1810 isn_T *isn;
1811
1812 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1813 return FAIL;
1814 isn->isn_arg.number = count;
1815 return OK;
1816}
1817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818/*
1819 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001820 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001822 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001823reserve_local(
1824 cctx_T *cctx,
1825 char_u *name,
1826 size_t len,
1827 int isConst,
1828 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 lvar_T *lvar;
1831
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001832 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001834 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001835 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 }
1837
1838 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001839 return NULL;
1840 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001841 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001843 // Every local variable uses the next entry on the stack. We could re-use
1844 // the last ones when leaving a scope, but then variables used in a closure
1845 // might get overwritten. To keep things simple do not re-use stack
1846 // entries. This is less efficient, but memory is cheap these days.
1847 lvar->lv_idx = cctx->ctx_locals_count++;
1848
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001849 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 lvar->lv_const = isConst;
1851 lvar->lv_type = type;
1852
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001853 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854}
1855
1856/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001857 * Remove local variables above "new_top".
1858 */
1859 static void
1860unwind_locals(cctx_T *cctx, int new_top)
1861{
1862 if (cctx->ctx_locals.ga_len > new_top)
1863 {
1864 int idx;
1865 lvar_T *lvar;
1866
1867 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1868 {
1869 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1870 vim_free(lvar->lv_name);
1871 }
1872 }
1873 cctx->ctx_locals.ga_len = new_top;
1874}
1875
1876/*
1877 * Free all local variables.
1878 */
1879 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001880free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001881{
1882 unwind_locals(cctx, 0);
1883 ga_clear(&cctx->ctx_locals);
1884}
1885
1886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 * Find "name" in script-local items of script "sid".
1888 * Returns the index in "sn_var_vals" if found.
1889 * If found but not in "sn_var_vals" returns -1.
1890 * If not found returns -2.
1891 */
1892 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001893get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894{
1895 hashtab_T *ht;
1896 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001897 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001898 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 int idx;
1900
Bram Moolenaare3d46852020-08-29 13:39:17 +02001901 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001903 if (sid == current_sctx.sc_sid)
1904 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001905 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001906
1907 if (sav == NULL)
1908 return -2;
1909 idx = sav->sav_var_vals_idx;
1910 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1911 if (check_writable && sv->sv_const)
1912 semsg(_(e_readonlyvar), name);
1913 return idx;
1914 }
1915
1916 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 ht = &SCRIPT_VARS(sid);
1918 di = find_var_in_ht(ht, 0, name, TRUE);
1919 if (di == NULL)
1920 return -2;
1921
1922 // Now find the svar_T index in sn_var_vals.
1923 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1924 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001925 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 if (sv->sv_tv == &di->di_tv)
1927 {
1928 if (check_writable && sv->sv_const)
1929 semsg(_(e_readonlyvar), name);
1930 return idx;
1931 }
1932 }
1933 return -1;
1934}
1935
1936/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001937 * Find "name" in imported items of the current script or in "cctx" if not
1938 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 */
1940 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001941find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 int idx;
1944
Bram Moolenaare3d46852020-08-29 13:39:17 +02001945 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001946 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 if (cctx != NULL)
1948 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1949 {
1950 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1951 + idx;
1952
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001953 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1954 : STRLEN(import->imp_name) == len
1955 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 return import;
1957 }
1958
Bram Moolenaarefa94442020-08-08 22:16:00 +02001959 return find_imported_in_script(name, len, current_sctx.sc_sid);
1960}
1961
1962 imported_T *
1963find_imported_in_script(char_u *name, size_t len, int sid)
1964{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001965 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001966 int idx;
1967
Bram Moolenaare3d46852020-08-29 13:39:17 +02001968 if (!SCRIPT_ID_VALID(sid))
1969 return NULL;
1970 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1972 {
1973 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1974
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001975 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1976 : STRLEN(import->imp_name) == len
1977 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 return import;
1979 }
1980 return NULL;
1981}
1982
1983/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001984 * Free all imported variables.
1985 */
1986 static void
1987free_imported(cctx_T *cctx)
1988{
1989 int idx;
1990
1991 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1992 {
1993 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1994
1995 vim_free(import->imp_name);
1996 }
1997 ga_clear(&cctx->ctx_imports);
1998}
1999
2000/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002001 * Return TRUE if "p" points at a "#" but not at "#{".
2002 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002003 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002004vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002005{
2006 return p[0] == '#' && p[1] != '{';
2007}
2008
2009/*
2010 * Return a pointer to the next line that isn't empty or only contains a
2011 * comment. Skips over white space.
2012 * Returns NULL if there is none.
2013 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002014 char_u *
2015peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002016{
2017 int lnum = cctx->ctx_lnum;
2018
2019 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2020 {
2021 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002022 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002023
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002024 // ignore NULLs inserted for continuation lines
2025 if (line != NULL)
2026 {
2027 p = skipwhite(line);
2028 if (*p != NUL && !vim9_comment_start(p))
2029 return p;
2030 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002031 }
2032 return NULL;
2033}
2034
2035/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002036 * Called when checking for a following operator at "arg". When the rest of
2037 * the line is empty or only a comment, peek the next line. If there is a next
2038 * line return a pointer to it and set "nextp".
2039 * Otherwise skip over white space.
2040 */
2041 static char_u *
2042may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2043{
2044 char_u *p = skipwhite(arg);
2045
2046 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002047 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002048 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002049 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002050 if (*nextp != NULL)
2051 return *nextp;
2052 }
2053 return p;
2054}
2055
2056/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002057 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002058 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002059 * Returns NULL when at the end.
2060 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002061 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002062next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002063{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002064 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002065
2066 do
2067 {
2068 ++cctx->ctx_lnum;
2069 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002070 {
2071 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002072 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002073 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002074 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002075 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002076 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002077 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002078 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002079 return line;
2080}
2081
2082/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002083 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002084 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002085 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2086 */
2087 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002088may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002089{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002090 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002091 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002092 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002093
2094 if (next == NULL)
2095 return FAIL;
2096 *arg = skipwhite(next);
2097 }
2098 return OK;
2099}
2100
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002101/*
2102 * Idem, and give an error when failed.
2103 */
2104 static int
2105may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2106{
2107 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2108 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002109 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002110 return FAIL;
2111 }
2112 return OK;
2113}
2114
2115
Bram Moolenaara5565e42020-05-09 15:44:01 +02002116// Structure passed between the compile_expr* functions to keep track of
2117// constants that have been parsed but for which no code was produced yet. If
2118// possible expressions on these constants are applied at compile time. If
2119// that is not possible, the code to push the constants needs to be generated
2120// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002121// Using 50 should be more than enough of 5 levels of ().
2122#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002123typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002124 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002125 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002126 int pp_is_const; // all generated code was constants, used for a
2127 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002128} ppconst_T;
2129
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002130static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002131static int compile_expr0(char_u **arg, cctx_T *cctx);
2132static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2133
Bram Moolenaara5565e42020-05-09 15:44:01 +02002134/*
2135 * Generate a PUSH instruction for "tv".
2136 * "tv" will be consumed or cleared.
2137 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2138 */
2139 static int
2140generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2141{
2142 if (tv != NULL)
2143 {
2144 switch (tv->v_type)
2145 {
2146 case VAR_UNKNOWN:
2147 break;
2148 case VAR_BOOL:
2149 generate_PUSHBOOL(cctx, tv->vval.v_number);
2150 break;
2151 case VAR_SPECIAL:
2152 generate_PUSHSPEC(cctx, tv->vval.v_number);
2153 break;
2154 case VAR_NUMBER:
2155 generate_PUSHNR(cctx, tv->vval.v_number);
2156 break;
2157#ifdef FEAT_FLOAT
2158 case VAR_FLOAT:
2159 generate_PUSHF(cctx, tv->vval.v_float);
2160 break;
2161#endif
2162 case VAR_BLOB:
2163 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2164 tv->vval.v_blob = NULL;
2165 break;
2166 case VAR_STRING:
2167 generate_PUSHS(cctx, tv->vval.v_string);
2168 tv->vval.v_string = NULL;
2169 break;
2170 default:
2171 iemsg("constant type not supported");
2172 clear_tv(tv);
2173 return FAIL;
2174 }
2175 tv->v_type = VAR_UNKNOWN;
2176 }
2177 return OK;
2178}
2179
2180/*
2181 * Generate code for any ppconst entries.
2182 */
2183 static int
2184generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2185{
2186 int i;
2187 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002188 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002189
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002190 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002191 for (i = 0; i < ppconst->pp_used; ++i)
2192 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2193 ret = FAIL;
2194 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002195 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002196 return ret;
2197}
2198
2199/*
2200 * Clear ppconst constants. Used when failing.
2201 */
2202 static void
2203clear_ppconst(ppconst_T *ppconst)
2204{
2205 int i;
2206
2207 for (i = 0; i < ppconst->pp_used; ++i)
2208 clear_tv(&ppconst->pp_tv[i]);
2209 ppconst->pp_used = 0;
2210}
2211
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002212/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002213 * Generate an instruction to load script-local variable "name", without the
2214 * leading "s:".
2215 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 */
2217 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002218compile_load_scriptvar(
2219 cctx_T *cctx,
2220 char_u *name, // variable NUL terminated
2221 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002222 char_u **end, // end of variable
2223 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002225 scriptitem_T *si;
2226 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 imported_T *import;
2228
Bram Moolenaare3d46852020-08-29 13:39:17 +02002229 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2230 return FAIL;
2231 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002232 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002233 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002235 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002236 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2237 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 }
2239 if (idx >= 0)
2240 {
2241 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2242
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002243 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 current_sctx.sc_sid, idx, sv->sv_type);
2245 return OK;
2246 }
2247
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002248 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 if (import != NULL)
2250 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002251 if (import->imp_all)
2252 {
2253 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002254 char_u *exp_name;
2255 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002256 ufunc_T *ufunc;
2257 type_T *type;
2258
2259 // Used "import * as Name", need to lookup the member.
2260 if (*p != '.')
2261 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002262 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002263 return FAIL;
2264 }
2265 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002266 if (VIM_ISWHITE(*p))
2267 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002268 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002269 return FAIL;
2270 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002271
Bram Moolenaar1c991142020-07-04 13:15:31 +02002272 // isolate one name
2273 exp_name = p;
2274 while (eval_isnamec(*p))
2275 ++p;
2276 cc = *p;
2277 *p = NUL;
2278
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002279 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002280 *p = cc;
2281 p = skipwhite(p);
2282
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002283 // TODO: what if it is a function?
2284 if (idx < 0)
2285 return FAIL;
2286 *end = p;
2287
2288 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2289 import->imp_sid,
2290 idx,
2291 type);
2292 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002293 else if (import->imp_funcname != NULL)
2294 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002295 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002296 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2297 import->imp_sid,
2298 import->imp_var_vals_idx,
2299 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 return OK;
2301 }
2302
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002303 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002304 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 return FAIL;
2306}
2307
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002308 static int
2309generate_funcref(cctx_T *cctx, char_u *name)
2310{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002311 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002312
2313 if (ufunc == NULL)
2314 return FAIL;
2315
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002316 // Need to compile any default values to get the argument types.
2317 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2318 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2319 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002320 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002321}
2322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323/*
2324 * Compile a variable name into a load instruction.
2325 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002326 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 * When "error" is FALSE do not give an error when not found.
2328 */
2329 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002330compile_load(
2331 char_u **arg,
2332 char_u *end_arg,
2333 cctx_T *cctx,
2334 int is_expr,
2335 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336{
2337 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002338 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002339 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002341 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342
2343 if (*(*arg + 1) == ':')
2344 {
2345 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002346 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002347 {
2348 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002350 switch (**arg)
2351 {
2352 case 'g': isn_type = ISN_LOADGDICT; break;
2353 case 'w': isn_type = ISN_LOADWDICT; break;
2354 case 't': isn_type = ISN_LOADTDICT; break;
2355 case 'b': isn_type = ISN_LOADBDICT; break;
2356 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002357 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002358 goto theend;
2359 }
2360 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2361 goto theend;
2362 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 else
2365 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002366 isntype_T isn_type = ISN_DROP;
2367
2368 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2369 if (name == NULL)
2370 return FAIL;
2371
2372 switch (**arg)
2373 {
2374 case 'v': res = generate_LOADV(cctx, name, error);
2375 break;
2376 case 's': res = compile_load_scriptvar(cctx, name,
2377 NULL, NULL, error);
2378 break;
2379 case 'g': isn_type = ISN_LOADG; break;
2380 case 'w': isn_type = ISN_LOADW; break;
2381 case 't': isn_type = ISN_LOADT; break;
2382 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002383 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002384 goto theend;
2385 }
2386 if (isn_type != ISN_DROP)
2387 {
2388 // Global, Buffer-local, Window-local and Tabpage-local
2389 // variables can be defined later, thus we don't check if it
2390 // exists, give error at runtime.
2391 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 }
2394 }
2395 else
2396 {
2397 size_t len = end - *arg;
2398 int idx;
2399 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002400 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
2402 name = vim_strnsave(*arg, end - *arg);
2403 if (name == NULL)
2404 return FAIL;
2405
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002406 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002408 if (!gen_load_outer)
2409 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
2411 else
2412 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002413 lvar_T *lvar = lookup_local(*arg, len, cctx);
2414
2415 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002417 type = lvar->lv_type;
2418 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002419 if (lvar->lv_from_outer)
2420 gen_load_outer = TRUE;
2421 else
2422 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 }
2424 else
2425 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002426 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002427 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002428 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002429 || find_imported(name, 0, cctx) != NULL)
2430 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002431
Bram Moolenaar0f769812020-09-12 18:32:34 +02002432 // When evaluating an expression and the name starts with an
2433 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002434 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002435 if (res == FAIL && is_expr
2436 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002437 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 }
2439 }
2440 if (gen_load)
2441 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002442 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002443 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002444 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002445 cctx->ctx_outer_used = TRUE;
2446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 }
2448
2449 *arg = end;
2450
2451theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002452 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002453 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 vim_free(name);
2455 return res;
2456}
2457
2458/*
2459 * Compile the argument expressions.
2460 * "arg" points to just after the "(" and is advanced to after the ")"
2461 */
2462 static int
2463compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2464{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002465 char_u *p = *arg;
2466 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002467 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468
Bram Moolenaare6085c52020-04-12 20:19:16 +02002469 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002471 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2472 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002473 if (*p == ')')
2474 {
2475 *arg = p + 1;
2476 return OK;
2477 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002478 if (must_end)
2479 {
2480 semsg(_(e_missing_comma_before_argument_str), p);
2481 return FAIL;
2482 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002483
Bram Moolenaara5565e42020-05-09 15:44:01 +02002484 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 return FAIL;
2486 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002487
2488 if (*p != ',' && *skipwhite(p) == ',')
2489 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002490 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002491 p = skipwhite(p);
2492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002494 {
2495 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002497 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002498 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002499 else
2500 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002501 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002502 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002504failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002505 emsg(_(e_missing_close));
2506 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507}
2508
2509/*
2510 * Compile a function call: name(arg1, arg2)
2511 * "arg" points to "name", "arg + varlen" to the "(".
2512 * "argcount_init" is 1 for "value->method()"
2513 * Instructions:
2514 * EVAL arg1
2515 * EVAL arg2
2516 * BCALL / DCALL / UCALL
2517 */
2518 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002519compile_call(
2520 char_u **arg,
2521 size_t varlen,
2522 cctx_T *cctx,
2523 ppconst_T *ppconst,
2524 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525{
2526 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002527 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 int argcount = argcount_init;
2529 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002530 char_u fname_buf[FLEN_FIXED + 1];
2531 char_u *tofree = NULL;
2532 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002534 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002535 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536
Bram Moolenaara5565e42020-05-09 15:44:01 +02002537 // we can evaluate "has('name')" at compile time
2538 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2539 {
2540 char_u *s = skipwhite(*arg + varlen + 1);
2541 typval_T argvars[2];
2542
2543 argvars[0].v_type = VAR_UNKNOWN;
2544 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002545 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002546 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002547 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002548 s = skipwhite(s);
2549 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2550 {
2551 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2552
2553 *arg = s + 1;
2554 argvars[1].v_type = VAR_UNKNOWN;
2555 tv->v_type = VAR_NUMBER;
2556 tv->vval.v_number = 0;
2557 f_has(argvars, tv);
2558 clear_tv(&argvars[0]);
2559 ++ppconst->pp_used;
2560 return OK;
2561 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002562 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002563 }
2564
2565 if (generate_ppconst(cctx, ppconst) == FAIL)
2566 return FAIL;
2567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (varlen >= sizeof(namebuf))
2569 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002570 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 return FAIL;
2572 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002573 vim_strncpy(namebuf, *arg, varlen);
2574 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
2576 *arg = skipwhite(*arg + varlen + 1);
2577 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002578 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579
Bram Moolenaara1773442020-08-12 15:21:22 +02002580 is_autoload = vim_strchr(name, '#') != NULL;
2581 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 {
2583 int idx;
2584
2585 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002586 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002588 {
2589 if (STRCMP(name, "add") == 0 && argcount == 2)
2590 {
2591 garray_T *stack = &cctx->ctx_type_stack;
2592 type_T *type = ((type_T **)stack->ga_data)[
2593 stack->ga_len - 2];
2594
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002595 if (type->tt_type == VAR_LIST)
2596 {
2597 // inline "add(list, item)" so that the type can be checked
2598 res = generate_LISTAPPEND(cctx);
2599 idx = -1;
2600 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002601 else if (type->tt_type == VAR_BLOB)
2602 {
2603 // inline "add(blob, nr)" so that the type can be checked
2604 res = generate_BLOBAPPEND(cctx);
2605 idx = -1;
2606 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002607 }
2608
2609 if (idx >= 0)
2610 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2611 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002612 else
2613 semsg(_(e_unknownfunc), namebuf);
2614 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 }
2616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002618 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002619 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002620 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002621 {
2622 res = generate_CALL(cctx, ufunc, argcount);
2623 goto theend;
2624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625
2626 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002627 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002628 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002630 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002631 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002632 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002633 garray_T *stack = &cctx->ctx_type_stack;
2634 type_T *type;
2635
2636 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2637 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002638 goto theend;
2639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640
Bram Moolenaar0f769812020-09-12 18:32:34 +02002641 // If we can find a global function by name generate the right call.
2642 if (ufunc != NULL)
2643 {
2644 res = generate_CALL(cctx, ufunc, argcount);
2645 goto theend;
2646 }
2647
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002648 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002649 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002650 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002651 res = generate_UCALL(cctx, name, argcount);
2652 else
2653 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002654
2655theend:
2656 vim_free(tofree);
2657 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658}
2659
2660// like NAMESPACE_CHAR but with 'a' and 'l'.
2661#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2662
2663/*
2664 * Find the end of a variable or function name. Unlike find_name_end() this
2665 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002666 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 * Return a pointer to just after the name. Equal to "arg" if there is no
2668 * valid name.
2669 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002670 static char_u *
2671to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672{
2673 char_u *p;
2674
2675 // Quick check for valid starting character.
2676 if (!eval_isnamec1(*arg))
2677 return arg;
2678
2679 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2680 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2681 // and can be used in slice "[n:]".
2682 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002683 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2685 break;
2686 return p;
2687}
2688
2689/*
2690 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002691 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 */
2693 char_u *
2694to_name_const_end(char_u *arg)
2695{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002696 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 typval_T rettv;
2698
2699 if (p == arg && *arg == '[')
2700 {
2701
2702 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002703 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 p = arg;
2705 }
2706 else if (p == arg && *arg == '#' && arg[1] == '{')
2707 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002708 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002710 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 p = arg;
2712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
2714 return p;
2715}
2716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717/*
2718 * parse a list: [expr, expr]
2719 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002720 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 */
2722 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002723compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724{
2725 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002726 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002728 int is_const;
2729 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002731 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002733 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002734 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002735 semsg(_(e_list_end), *arg);
2736 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002737 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002738 if (*p == ',')
2739 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002740 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002741 return FAIL;
2742 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002743 if (*p == ']')
2744 {
2745 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002746 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002747 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002748 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002749 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002750 if (!is_const)
2751 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 ++count;
2753 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002754 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002756 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2757 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002758 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002759 return FAIL;
2760 }
2761 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002762 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 p = skipwhite(p);
2764 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002765 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002767 ppconst->pp_is_const = is_all_const;
2768 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769}
2770
2771/*
2772 * parse a lambda: {arg, arg -> expr}
2773 * "*arg" points to the '{'.
2774 */
2775 static int
2776compile_lambda(char_u **arg, cctx_T *cctx)
2777{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 typval_T rettv;
2779 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002780 evalarg_T evalarg;
2781
2782 CLEAR_FIELD(evalarg);
2783 evalarg.eval_flags = EVAL_EVALUATE;
2784 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785
2786 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002787 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002788 {
2789 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002791 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002794 ++ufunc->uf_refcount;
2795 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002796 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797
2798 // The function will have one line: "return {expr}".
2799 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002800 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002802 clear_evalarg(&evalarg, NULL);
2803
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002804 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002805 {
2806 // The return type will now be known.
2807 set_function_type(ufunc);
2808
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002809 // The function reference count will be 1. When the ISN_FUNCREF
2810 // instruction is deleted the reference count is decremented and the
2811 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002812 return generate_FUNCREF(cctx, ufunc);
2813 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002814
2815 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 return FAIL;
2817}
2818
2819/*
2820 * Compile a lamda call: expr->{lambda}(args)
2821 * "arg" points to the "{".
2822 */
2823 static int
2824compile_lambda_call(char_u **arg, cctx_T *cctx)
2825{
2826 ufunc_T *ufunc;
2827 typval_T rettv;
2828 int argcount = 1;
2829 int ret = FAIL;
2830
2831 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002832 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 return FAIL;
2834
2835 if (**arg != '(')
2836 {
2837 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002838 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 else
2840 semsg(_(e_missing_paren), "lambda");
2841 clear_tv(&rettv);
2842 return FAIL;
2843 }
2844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 ufunc = rettv.vval.v_partial->pt_func;
2846 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002847 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002848 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002849
Bram Moolenaara05e5242020-09-19 18:19:19 +02002850 // The function will have one line: "return {expr}". Compile it into
2851 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002852 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
2854 // compile the arguments
2855 *arg = skipwhite(*arg + 1);
2856 if (compile_arguments(arg, cctx, &argcount) == OK)
2857 // call the compiled function
2858 ret = generate_CALL(cctx, ufunc, argcount);
2859
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002860 if (ret == FAIL)
2861 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 return ret;
2863}
2864
2865/*
2866 * parse a dict: {'key': val} or #{key: val}
2867 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002868 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 */
2870 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872{
2873 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002874 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 int count = 0;
2876 dict_T *d = dict_alloc();
2877 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002878 char_u *whitep = *arg;
2879 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002880 int is_const;
2881 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882
2883 if (d == NULL)
2884 return FAIL;
2885 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002886 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 {
2888 char_u *key = NULL;
2889
Bram Moolenaar23c55272020-06-21 16:58:13 +02002890 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002891 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002892 *arg = NULL;
2893 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002894 }
2895
2896 if (**arg == '}')
2897 break;
2898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 if (literal)
2900 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002901 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902
Bram Moolenaar2c330432020-04-13 14:41:35 +02002903 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002905 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 return FAIL;
2907 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002908 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 if (generate_PUSHS(cctx, key) == FAIL)
2910 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002911 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 }
2913 else
2914 {
2915 isn_T *isn;
2916
Bram Moolenaara5565e42020-05-09 15:44:01 +02002917 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2920 if (isn->isn_type == ISN_PUSHS)
2921 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002922 else
2923 {
2924 type_T *keytype = ((type_T **)stack->ga_data)
2925 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002926 if (need_type(keytype, &t_string, -1, cctx,
2927 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002928 return FAIL;
2929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 }
2931
2932 // Check for duplicate keys, if using string keys.
2933 if (key != NULL)
2934 {
2935 item = dict_find(d, key, -1);
2936 if (item != NULL)
2937 {
2938 semsg(_(e_duplicate_key), key);
2939 goto failret;
2940 }
2941 item = dictitem_alloc(key);
2942 if (item != NULL)
2943 {
2944 item->di_tv.v_type = VAR_UNKNOWN;
2945 item->di_tv.v_lock = 0;
2946 if (dict_add(d, item) == FAIL)
2947 dictitem_free(item);
2948 }
2949 }
2950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 if (**arg != ':')
2952 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002953 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002954 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002955 else
2956 semsg(_(e_missing_dict_colon), *arg);
2957 return FAIL;
2958 }
2959 whitep = *arg + 1;
2960 if (!IS_WHITE_OR_NUL(*whitep))
2961 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002962 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 return FAIL;
2964 }
2965
2966 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002967 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002968 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002969 *arg = NULL;
2970 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002971 }
2972
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002973 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002975 if (!is_const)
2976 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 ++count;
2978
Bram Moolenaar2c330432020-04-13 14:41:35 +02002979 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002980 *arg = skipwhite(*arg);
2981 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002982 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002983 *arg = NULL;
2984 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 if (**arg == '}')
2987 break;
2988 if (**arg != ',')
2989 {
2990 semsg(_(e_missing_dict_comma), *arg);
2991 goto failret;
2992 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002993 if (IS_WHITE_OR_NUL(*whitep))
2994 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002995 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002996 return FAIL;
2997 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002998 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 *arg = skipwhite(*arg + 1);
3000 }
3001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 *arg = *arg + 1;
3003
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003004 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003005 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003006 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003007 *arg += STRLEN(*arg);
3008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003010 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 return generate_NEWDICT(cctx, count);
3012
3013failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003014 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003015 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003016 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003017 *arg = (char_u *)"";
3018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 dict_unref(d);
3020 return FAIL;
3021}
3022
3023/*
3024 * Compile "&option".
3025 */
3026 static int
3027compile_get_option(char_u **arg, cctx_T *cctx)
3028{
3029 typval_T rettv;
3030 char_u *start = *arg;
3031 int ret;
3032
3033 // parse the option and get the current value to get the type.
3034 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003035 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 if (ret == OK)
3037 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003038 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 char_u *name = vim_strnsave(start, *arg - start);
3040 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3041
3042 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3043 vim_free(name);
3044 }
3045 clear_tv(&rettv);
3046
3047 return ret;
3048}
3049
3050/*
3051 * Compile "$VAR".
3052 */
3053 static int
3054compile_get_env(char_u **arg, cctx_T *cctx)
3055{
3056 char_u *start = *arg;
3057 int len;
3058 int ret;
3059 char_u *name;
3060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 ++*arg;
3062 len = get_env_len(arg);
3063 if (len == 0)
3064 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003065 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return FAIL;
3067 }
3068
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003069 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 name = vim_strnsave(start, len + 1);
3071 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3072 vim_free(name);
3073 return ret;
3074}
3075
3076/*
3077 * Compile "@r".
3078 */
3079 static int
3080compile_get_register(char_u **arg, cctx_T *cctx)
3081{
3082 int ret;
3083
3084 ++*arg;
3085 if (**arg == NUL)
3086 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003087 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 return FAIL;
3089 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003090 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 {
3092 emsg_invreg(**arg);
3093 return FAIL;
3094 }
3095 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3096 ++*arg;
3097 return ret;
3098}
3099
3100/*
3101 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003102 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 */
3104 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003105apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003107 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108
3109 // this works from end to start
3110 while (p > start)
3111 {
3112 --p;
3113 if (*p == '-' || *p == '+')
3114 {
3115 // only '-' has an effect, for '+' we only check the type
3116#ifdef FEAT_FLOAT
3117 if (rettv->v_type == VAR_FLOAT)
3118 {
3119 if (*p == '-')
3120 rettv->vval.v_float = -rettv->vval.v_float;
3121 }
3122 else
3123#endif
3124 {
3125 varnumber_T val;
3126 int error = FALSE;
3127
3128 // tv_get_number_chk() accepts a string, but we don't want that
3129 // here
3130 if (check_not_string(rettv) == FAIL)
3131 return FAIL;
3132 val = tv_get_number_chk(rettv, &error);
3133 clear_tv(rettv);
3134 if (error)
3135 return FAIL;
3136 if (*p == '-')
3137 val = -val;
3138 rettv->v_type = VAR_NUMBER;
3139 rettv->vval.v_number = val;
3140 }
3141 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003142 else if (numeric_only)
3143 {
3144 ++p;
3145 break;
3146 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003147 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 {
3149 int v = tv2bool(rettv);
3150
3151 // '!' is permissive in the type.
3152 clear_tv(rettv);
3153 rettv->v_type = VAR_BOOL;
3154 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3155 }
3156 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003157 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 return OK;
3159}
3160
3161/*
3162 * Recognize v: variables that are constants and set "rettv".
3163 */
3164 static void
3165get_vim_constant(char_u **arg, typval_T *rettv)
3166{
3167 if (STRNCMP(*arg, "v:true", 6) == 0)
3168 {
3169 rettv->v_type = VAR_BOOL;
3170 rettv->vval.v_number = VVAL_TRUE;
3171 *arg += 6;
3172 }
3173 else if (STRNCMP(*arg, "v:false", 7) == 0)
3174 {
3175 rettv->v_type = VAR_BOOL;
3176 rettv->vval.v_number = VVAL_FALSE;
3177 *arg += 7;
3178 }
3179 else if (STRNCMP(*arg, "v:null", 6) == 0)
3180 {
3181 rettv->v_type = VAR_SPECIAL;
3182 rettv->vval.v_number = VVAL_NULL;
3183 *arg += 6;
3184 }
3185 else if (STRNCMP(*arg, "v:none", 6) == 0)
3186 {
3187 rettv->v_type = VAR_SPECIAL;
3188 rettv->vval.v_number = VVAL_NONE;
3189 *arg += 6;
3190 }
3191}
3192
Bram Moolenaar696ba232020-07-29 21:20:41 +02003193 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003194get_compare_type(char_u *p, int *len, int *type_is)
3195{
3196 exptype_T type = EXPR_UNKNOWN;
3197 int i;
3198
3199 switch (p[0])
3200 {
3201 case '=': if (p[1] == '=')
3202 type = EXPR_EQUAL;
3203 else if (p[1] == '~')
3204 type = EXPR_MATCH;
3205 break;
3206 case '!': if (p[1] == '=')
3207 type = EXPR_NEQUAL;
3208 else if (p[1] == '~')
3209 type = EXPR_NOMATCH;
3210 break;
3211 case '>': if (p[1] != '=')
3212 {
3213 type = EXPR_GREATER;
3214 *len = 1;
3215 }
3216 else
3217 type = EXPR_GEQUAL;
3218 break;
3219 case '<': if (p[1] != '=')
3220 {
3221 type = EXPR_SMALLER;
3222 *len = 1;
3223 }
3224 else
3225 type = EXPR_SEQUAL;
3226 break;
3227 case 'i': if (p[1] == 's')
3228 {
3229 // "is" and "isnot"; but not a prefix of a name
3230 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3231 *len = 5;
3232 i = p[*len];
3233 if (!isalnum(i) && i != '_')
3234 {
3235 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3236 *type_is = TRUE;
3237 }
3238 }
3239 break;
3240 }
3241 return type;
3242}
3243
3244/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003246 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 */
3248 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003249compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003251 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252
3253 // this works from end to start
3254 while (p > start)
3255 {
3256 --p;
3257 if (*p == '-' || *p == '+')
3258 {
3259 int negate = *p == '-';
3260 isn_T *isn;
3261
3262 // TODO: check type
3263 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3264 {
3265 --p;
3266 if (*p == '-')
3267 negate = !negate;
3268 }
3269 // only '-' has an effect, for '+' we only check the type
3270 if (negate)
3271 isn = generate_instr(cctx, ISN_NEGATENR);
3272 else
3273 isn = generate_instr(cctx, ISN_CHECKNR);
3274 if (isn == NULL)
3275 return FAIL;
3276 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003277 else if (numeric_only)
3278 {
3279 ++p;
3280 break;
3281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 else
3283 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003284 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003286 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003288 if (p[-1] == '!')
3289 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 }
3292 if (generate_2BOOL(cctx, invert) == FAIL)
3293 return FAIL;
3294 }
3295 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003296 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 return OK;
3298}
3299
3300/*
3301 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003302 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 */
3304 static int
3305compile_subscript(
3306 char_u **arg,
3307 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003308 char_u *start_leader,
3309 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003310 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003312 char_u *name_start = *end_leader;
3313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 for (;;)
3315 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003316 char_u *p = skipwhite(*arg);
3317
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003318 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003319 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003320 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003321
3322 // If a following line starts with "->{" or "->X" advance to that
3323 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003324 // Also if a following line starts with ".x".
3325 if (next != NULL &&
3326 ((next[0] == '-' && next[1] == '>'
3327 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003328 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003329 {
3330 next = next_line_from_context(cctx, TRUE);
3331 if (next == NULL)
3332 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003333 *arg = next;
3334 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003335 }
3336 }
3337
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003338 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3339 // not a function call.
3340 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003342 garray_T *stack = &cctx->ctx_type_stack;
3343 type_T *type;
3344 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003346 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003347 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003348 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003351 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3352
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003353 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3355 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003356 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 return FAIL;
3358 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003359 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003361 char_u *pstart = p;
3362
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003363 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003364 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003365 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 // something->method()
3368 // Apply the '!', '-' and '+' first:
3369 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003370 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003373 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003374 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003375 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 if (**arg == '{')
3377 {
3378 // lambda call: list->{lambda}
3379 if (compile_lambda_call(arg, cctx) == FAIL)
3380 return FAIL;
3381 }
3382 else
3383 {
3384 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003385 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003386 if (!eval_isnamec1(*p))
3387 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003388 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003389 return FAIL;
3390 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003391 if (ASCII_ISALPHA(*p) && p[1] == ':')
3392 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003393 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 ;
3395 if (*p != '(')
3396 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003397 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 return FAIL;
3399 }
3400 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003401 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 return FAIL;
3403 }
3404 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003405 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003407 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003408 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003409 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003410 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003411 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003412
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003413 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003414 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003415 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003416 // TODO: blob index
3417 // TODO: more arguments
3418 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003419 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003420 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003421 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003422
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003423 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003424 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003425 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003426 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003427 if (**arg == ':')
3428 // missing first index is equal to zero
3429 generate_PUSHNR(cctx, 0);
3430 else
3431 {
3432 if (compile_expr0(arg, cctx) == FAIL)
3433 return FAIL;
3434 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3435 return FAIL;
3436 *arg = skipwhite(*arg);
3437 }
3438 if (**arg == ':')
3439 {
3440 *arg = skipwhite(*arg + 1);
3441 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3442 return FAIL;
3443 if (**arg == ']')
3444 // missing second index is equal to end of string
3445 generate_PUSHNR(cctx, -1);
3446 else
3447 {
3448 if (compile_expr0(arg, cctx) == FAIL)
3449 return FAIL;
3450 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3451 return FAIL;
3452 *arg = skipwhite(*arg);
3453 }
3454 is_slice = TRUE;
3455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456
3457 if (**arg != ']')
3458 {
3459 emsg(_(e_missbrac));
3460 return FAIL;
3461 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003462 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003464 // We can index a list and a dict. If we don't know the type
3465 // we can use the index value type.
3466 // TODO: If we don't know use an instruction to figure it out at
3467 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003468 typep = ((type_T **)stack->ga_data) + stack->ga_len
3469 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003470 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003471 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3472 // If the index is a string, the variable must be a Dict.
3473 if (*typep == &t_any && valtype == &t_string)
3474 vtype = VAR_DICT;
3475 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003476 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003477 if (need_type(valtype, &t_number, -1, cctx,
3478 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003479 return FAIL;
3480 if (is_slice)
3481 {
3482 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003483 if (need_type(valtype, &t_number, -2, cctx,
3484 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003485 return FAIL;
3486 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003487 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003488
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003489 if (vtype == VAR_DICT)
3490 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003491 if (is_slice)
3492 {
3493 emsg(_(e_cannot_slice_dictionary));
3494 return FAIL;
3495 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003496 if ((*typep)->tt_type == VAR_DICT)
3497 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003498 else
3499 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003500 if (need_type(*typep, &t_dict_any, -2, cctx,
3501 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003502 return FAIL;
3503 *typep = &t_any;
3504 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003505 if (may_generate_2STRING(-1, cctx) == FAIL)
3506 return FAIL;
3507 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3508 return FAIL;
3509 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003510 else if (vtype == VAR_STRING)
3511 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003512 *typep = &t_string;
3513 if ((is_slice
3514 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3515 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003516 return FAIL;
3517 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003518 else if (vtype == VAR_BLOB)
3519 {
3520 emsg("Sorry, blob index and slice not implemented yet");
3521 return FAIL;
3522 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003523 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003524 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003525 if (is_slice)
3526 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003527 if (generate_instr_drop(cctx,
3528 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3529 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003530 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003531 }
Bram Moolenaared591872020-08-15 22:14:53 +02003532 else
3533 {
3534 if ((*typep)->tt_type == VAR_LIST)
3535 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003536 if (generate_instr_drop(cctx,
3537 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3538 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003539 return FAIL;
3540 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003541 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003542 else
3543 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003544 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003545 return FAIL;
3546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003548 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003550 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003551 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003552 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003553 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003554
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003555 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003556 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003557 {
3558 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003559 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003560 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003561 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003562 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 while (eval_isnamec(*p))
3564 MB_PTR_ADV(p);
3565 if (p == *arg)
3566 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003567 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 return FAIL;
3569 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003570 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 return FAIL;
3572 *arg = p;
3573 }
3574 else
3575 break;
3576 }
3577
3578 // TODO - see handle_subscript():
3579 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3580 // Don't do this when "Func" is already a partial that was bound
3581 // explicitly (pt_auto is FALSE).
3582
3583 return OK;
3584}
3585
3586/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003587 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3588 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003590 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003591 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003592 *
3593 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 */
3595
3596/*
3597 * number number constant
3598 * 0zFFFFFFFF Blob constant
3599 * "string" string constant
3600 * 'string' literal string constant
3601 * &option-name option value
3602 * @r register contents
3603 * identifier variable value
3604 * function() function call
3605 * $VAR environment variable
3606 * (expression) nested expression
3607 * [expr, expr] List
3608 * {key: val, key: val} Dictionary
3609 * #{key: val, key: val} Dictionary with literal keys
3610 *
3611 * Also handle:
3612 * ! in front logical NOT
3613 * - in front unary minus
3614 * + in front unary plus (ignored)
3615 * trailing (arg) funcref/partial call
3616 * trailing [] subscript in String or List
3617 * trailing .name entry in Dictionary
3618 * trailing ->name() method call
3619 */
3620 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003621compile_expr7(
3622 char_u **arg,
3623 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003624 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 char_u *start_leader, *end_leader;
3627 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003628 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003629 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003631 ppconst->pp_is_const = FALSE;
3632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 /*
3634 * Skip '!', '-' and '+' characters. They are handled later.
3635 */
3636 start_leader = *arg;
3637 while (**arg == '!' || **arg == '-' || **arg == '+')
3638 *arg = skipwhite(*arg + 1);
3639 end_leader = *arg;
3640
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003641 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 switch (**arg)
3643 {
3644 /*
3645 * Number constant.
3646 */
3647 case '0': // also for blob starting with 0z
3648 case '1':
3649 case '2':
3650 case '3':
3651 case '4':
3652 case '5':
3653 case '6':
3654 case '7':
3655 case '8':
3656 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003657 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003659 // Apply "-" and "+" just before the number now, right to
3660 // left. Matters especially when "->" follows. Stops at
3661 // '!'.
3662 if (apply_leader(rettv, TRUE,
3663 start_leader, &end_leader) == FAIL)
3664 {
3665 clear_tv(rettv);
3666 return FAIL;
3667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 break;
3669
3670 /*
3671 * String constant: "string".
3672 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003673 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 return FAIL;
3675 break;
3676
3677 /*
3678 * Literal string constant: 'str''ing'.
3679 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003680 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 return FAIL;
3682 break;
3683
3684 /*
3685 * Constant Vim variable.
3686 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003687 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 ret = NOTDONE;
3689 break;
3690
3691 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003692 * "true" constant
3693 */
3694 case 't': if (STRNCMP(*arg, "true", 4) == 0
3695 && !eval_isnamec((*arg)[4]))
3696 {
3697 *arg += 4;
3698 rettv->v_type = VAR_BOOL;
3699 rettv->vval.v_number = VVAL_TRUE;
3700 }
3701 else
3702 ret = NOTDONE;
3703 break;
3704
3705 /*
3706 * "false" constant
3707 */
3708 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3709 && !eval_isnamec((*arg)[5]))
3710 {
3711 *arg += 5;
3712 rettv->v_type = VAR_BOOL;
3713 rettv->vval.v_number = VVAL_FALSE;
3714 }
3715 else
3716 ret = NOTDONE;
3717 break;
3718
3719 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 * List: [expr, expr]
3721 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003722 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 break;
3724
3725 /*
3726 * Dictionary: #{key: val, key: val}
3727 */
3728 case '#': if ((*arg)[1] == '{')
3729 {
3730 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003731 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 }
3733 else
3734 ret = NOTDONE;
3735 break;
3736
3737 /*
3738 * Lambda: {arg, arg -> expr}
3739 * Dictionary: {'key': val, 'key': val}
3740 */
3741 case '{': {
3742 char_u *start = skipwhite(*arg + 1);
3743
3744 // Find out what comes after the arguments.
3745 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003746 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 if (ret != FAIL && *start == '>')
3748 ret = compile_lambda(arg, cctx);
3749 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003750 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 }
3752 break;
3753
3754 /*
3755 * Option value: &name
3756 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003757 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3758 return FAIL;
3759 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 break;
3761
3762 /*
3763 * Environment variable: $VAR.
3764 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003765 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3766 return FAIL;
3767 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 break;
3769
3770 /*
3771 * Register contents: @r.
3772 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003773 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3774 return FAIL;
3775 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 break;
3777 /*
3778 * nested expression: (expression).
3779 */
3780 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003781
3782 // recursive!
3783 if (ppconst->pp_used <= PPSIZE - 10)
3784 {
3785 ret = compile_expr1(arg, cctx, ppconst);
3786 }
3787 else
3788 {
3789 // Not enough space in ppconst, flush constants.
3790 if (generate_ppconst(cctx, ppconst) == FAIL)
3791 return FAIL;
3792 ret = compile_expr0(arg, cctx);
3793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 *arg = skipwhite(*arg);
3795 if (**arg == ')')
3796 ++*arg;
3797 else if (ret == OK)
3798 {
3799 emsg(_(e_missing_close));
3800 ret = FAIL;
3801 }
3802 break;
3803
3804 default: ret = NOTDONE;
3805 break;
3806 }
3807 if (ret == FAIL)
3808 return FAIL;
3809
Bram Moolenaar1c747212020-05-09 18:28:34 +02003810 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003812 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003813 clear_tv(rettv);
3814 else
3815 // A constant expression can possibly be handled compile time,
3816 // return the value instead of generating code.
3817 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 }
3819 else if (ret == NOTDONE)
3820 {
3821 char_u *p;
3822 int r;
3823
3824 if (!eval_isnamec1(**arg))
3825 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003826 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 return FAIL;
3828 }
3829
3830 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003831 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003833 {
3834 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003837 {
3838 if (generate_ppconst(cctx, ppconst) == FAIL)
3839 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003840 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 if (r == FAIL)
3843 return FAIL;
3844 }
3845
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003846 // Handle following "[]", ".member", etc.
3847 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003848 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003849 ppconst) == FAIL)
3850 return FAIL;
3851 if (ppconst->pp_used > 0)
3852 {
3853 // apply the '!', '-' and '+' before the constant
3854 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003855 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003856 return FAIL;
3857 return OK;
3858 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003859 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003861 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862}
3863
3864/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003865 * Give the "white on both sides" error, taking the operator from "p[len]".
3866 */
3867 void
3868error_white_both(char_u *op, int len)
3869{
3870 char_u buf[10];
3871
3872 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003873 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003874}
3875
3876/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003877 * <type>expr7: runtime type check / conversion
3878 */
3879 static int
3880compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3881{
3882 type_T *want_type = NULL;
3883
3884 // Recognize <type>
3885 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3886 {
3887 int called_emsg_before = called_emsg;
3888
3889 ++*arg;
3890 want_type = parse_type(arg, cctx->ctx_type_list);
3891 if (called_emsg != called_emsg_before)
3892 return FAIL;
3893
3894 if (**arg != '>')
3895 {
3896 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003897 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003898 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003899 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003900 return FAIL;
3901 }
3902 ++*arg;
3903 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3904 return FAIL;
3905 }
3906
3907 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3908 return FAIL;
3909
3910 if (want_type != NULL)
3911 {
3912 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003913 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003914
Bram Moolenaard1103582020-08-14 22:44:25 +02003915 generate_ppconst(cctx, ppconst);
3916 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003917 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003918 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003919 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003920 return FAIL;
3921 }
3922 }
3923
3924 return OK;
3925}
3926
3927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 * * number multiplication
3929 * / number division
3930 * % number modulo
3931 */
3932 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003933compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934{
3935 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003936 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003937 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003939 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003940 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 return FAIL;
3942
3943 /*
3944 * Repeat computing, until no "*", "/" or "%" is following.
3945 */
3946 for (;;)
3947 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003948 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 if (*op != '*' && *op != '/' && *op != '%')
3950 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003951 if (next != NULL)
3952 {
3953 *arg = next_line_from_context(cctx, TRUE);
3954 op = skipwhite(*arg);
3955 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003956
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003957 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003959 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003960 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 }
3962 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003963 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003964 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003966 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003967 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003969
3970 if (ppconst->pp_used == ppconst_used + 2
3971 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3972 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003973 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003974 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3975 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003976 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003978 // both are numbers: compute the result
3979 switch (*op)
3980 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003981 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003982 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003983 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003984 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003986 break;
3987 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003988 tv1->vval.v_number = res;
3989 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003990 }
3991 else
3992 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003993 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003994 generate_two_op(cctx, op);
3995 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 }
3997
3998 return OK;
3999}
4000
4001/*
4002 * + number addition
4003 * - number subtraction
4004 * .. string concatenation
4005 */
4006 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004007compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008{
4009 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004010 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004012 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013
4014 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 return FAIL;
4017
4018 /*
4019 * Repeat computing, until no "+", "-" or ".." is following.
4020 */
4021 for (;;)
4022 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004023 op = may_peek_next_line(cctx, *arg, &next);
4024 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 break;
4026 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004027 if (next != NULL)
4028 {
4029 *arg = next_line_from_context(cctx, TRUE);
4030 op = skipwhite(*arg);
4031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004033 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004035 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004036 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 }
4038
4039 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004040 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004041 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004043 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004044 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 return FAIL;
4046
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004048 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004049 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4050 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4051 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4052 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4055 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004056
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004057 // concat/subtract/add constant numbers
4058 if (*op == '+')
4059 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4060 else if (*op == '-')
4061 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4062 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004063 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004064 // concatenate constant strings
4065 char_u *s1 = tv1->vval.v_string;
4066 char_u *s2 = tv2->vval.v_string;
4067 size_t len1 = STRLEN(s1);
4068
4069 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4070 if (tv1->vval.v_string == NULL)
4071 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004072 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004073 return FAIL;
4074 }
4075 mch_memmove(tv1->vval.v_string, s1, len1);
4076 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004077 vim_free(s1);
4078 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004079 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 }
4082 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004083 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004084 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004085 if (*op == '.')
4086 {
4087 if (may_generate_2STRING(-2, cctx) == FAIL
4088 || may_generate_2STRING(-1, cctx) == FAIL)
4089 return FAIL;
4090 generate_instr_drop(cctx, ISN_CONCAT, 1);
4091 }
4092 else
4093 generate_two_op(cctx, op);
4094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 }
4096
4097 return OK;
4098}
4099
4100/*
4101 * expr5a == expr5b
4102 * expr5a =~ expr5b
4103 * expr5a != expr5b
4104 * expr5a !~ expr5b
4105 * expr5a > expr5b
4106 * expr5a >= expr5b
4107 * expr5a < expr5b
4108 * expr5a <= expr5b
4109 * expr5a is expr5b
4110 * expr5a isnot expr5b
4111 *
4112 * Produces instructions:
4113 * EVAL expr5a Push result of "expr5a"
4114 * EVAL expr5b Push result of "expr5b"
4115 * COMPARE one of the compare instructions
4116 */
4117 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004118compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119{
4120 exptype_T type = EXPR_UNKNOWN;
4121 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004122 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126
4127 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 return FAIL;
4130
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004131 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004132 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
4134 /*
4135 * If there is a comparative operator, use it.
4136 */
4137 if (type != EXPR_UNKNOWN)
4138 {
4139 int ic = FALSE; // Default: do not ignore case
4140
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004141 if (next != NULL)
4142 {
4143 *arg = next_line_from_context(cctx, TRUE);
4144 p = skipwhite(*arg);
4145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 if (type_is && (p[len] == '?' || p[len] == '#'))
4147 {
4148 semsg(_(e_invexpr2), *arg);
4149 return FAIL;
4150 }
4151 // extra question mark appended: ignore case
4152 if (p[len] == '?')
4153 {
4154 ic = TRUE;
4155 ++len;
4156 }
4157 // extra '#' appended: match case (ignored)
4158 else if (p[len] == '#')
4159 ++len;
4160 // nothing appended: match case
4161
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004162 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004164 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004165 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 }
4167
4168 // get the second variable
4169 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004170 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004171 return FAIL;
4172
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 return FAIL;
4175
Bram Moolenaara5565e42020-05-09 15:44:01 +02004176 if (ppconst->pp_used == ppconst_used + 2)
4177 {
4178 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4179 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4180 int ret;
4181
4182 // Both sides are a constant, compute the result now.
4183 // First check for a valid combination of types, this is more
4184 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004185 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004186 ret = FAIL;
4187 else
4188 {
4189 ret = typval_compare(tv1, tv2, type, ic);
4190 tv1->v_type = VAR_BOOL;
4191 tv1->vval.v_number = tv1->vval.v_number
4192 ? VVAL_TRUE : VVAL_FALSE;
4193 clear_tv(tv2);
4194 --ppconst->pp_used;
4195 }
4196 return ret;
4197 }
4198
4199 generate_ppconst(cctx, ppconst);
4200 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 }
4202
4203 return OK;
4204}
4205
Bram Moolenaar7f141552020-05-09 17:35:53 +02004206static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208/*
4209 * Compile || or &&.
4210 */
4211 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004212compile_and_or(
4213 char_u **arg,
4214 cctx_T *cctx,
4215 char *op,
4216 ppconst_T *ppconst,
4217 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004219 char_u *next;
4220 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 int opchar = *op;
4222
4223 if (p[0] == opchar && p[1] == opchar)
4224 {
4225 garray_T *instr = &cctx->ctx_instr;
4226 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004227 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004228 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229
4230 /*
4231 * Repeat until there is no following "||" or "&&"
4232 */
4233 ga_init2(&end_ga, sizeof(int), 10);
4234 while (p[0] == opchar && p[1] == opchar)
4235 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004236 if (next != NULL)
4237 {
4238 *arg = next_line_from_context(cctx, TRUE);
4239 p = skipwhite(*arg);
4240 }
4241
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004242 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4243 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004244 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004245 return FAIL;
4246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004248 // TODO: use ppconst if the value is a constant and check
4249 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004250 generate_ppconst(cctx, ppconst);
4251
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004252 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4253 all_bool_values = FALSE;
4254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 if (ga_grow(&end_ga, 1) == FAIL)
4256 {
4257 ga_clear(&end_ga);
4258 return FAIL;
4259 }
4260 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4261 ++end_ga.ga_len;
4262 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004263 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
4265 // eval the next expression
4266 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004267 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004268 return FAIL;
4269
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4271 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 {
4273 ga_clear(&end_ga);
4274 return FAIL;
4275 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004276
4277 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004279 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280
4281 // Fill in the end label in all jumps.
4282 while (end_ga.ga_len > 0)
4283 {
4284 isn_T *isn;
4285
4286 --end_ga.ga_len;
4287 isn = ((isn_T *)instr->ga_data)
4288 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4289 isn->isn_arg.jump.jump_where = instr->ga_len;
4290 }
4291 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004292
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004293 // The resulting type is converted to bool if needed.
4294 if (!all_bool_values)
4295 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 }
4297
4298 return OK;
4299}
4300
4301/*
4302 * expr4a && expr4a && expr4a logical AND
4303 *
4304 * Produces instructions:
4305 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004306 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004308 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004310 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 * end:
4312 */
4313 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004314compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 int ppconst_used = ppconst->pp_used;
4317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 return FAIL;
4321
4322 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004323 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324}
4325
4326/*
4327 * expr3a || expr3b || expr3c logical OR
4328 *
4329 * Produces instructions:
4330 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004331 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004333 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004335 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 * end:
4337 */
4338 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004341 int ppconst_used = ppconst->pp_used;
4342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 return FAIL;
4346
4347 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004348 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349}
4350
4351/*
4352 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004354 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 * JUMP_IF_FALSE alt jump if false
4356 * EVAL expr1a
4357 * JUMP_ALWAYS end
4358 * alt: EVAL expr1b
4359 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004360 *
4361 * Toplevel expression: expr2 ?? expr1
4362 * Produces instructions:
4363 * EVAL expr2 Push result of "expr2"
4364 * JUMP_AND_KEEP_IF_TRUE end jump if true
4365 * EVAL expr1
4366 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 */
4368 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370{
4371 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004372 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004373 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374
Bram Moolenaar3988f642020-08-27 22:43:03 +02004375 // Ignore all kinds of errors when not producing code.
4376 if (cctx->ctx_skip == SKIP_YES)
4377 {
4378 skip_expr(arg);
4379 return OK;
4380 }
4381
Bram Moolenaar61a89812020-05-07 16:58:17 +02004382 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004383 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 return FAIL;
4385
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004386 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 if (*p == '?')
4388 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004389 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 garray_T *instr = &cctx->ctx_instr;
4391 garray_T *stack = &cctx->ctx_type_stack;
4392 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004393 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004395 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396 int has_const_expr = FALSE;
4397 int const_value = FALSE;
4398 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004400 if (next != NULL)
4401 {
4402 *arg = next_line_from_context(cctx, TRUE);
4403 p = skipwhite(*arg);
4404 }
4405
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004406 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004407 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004408 semsg(_(e_white_space_required_before_and_after_str),
4409 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004410 return FAIL;
4411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413 if (ppconst->pp_used == ppconst_used + 1)
4414 {
4415 // the condition is a constant, we know whether the ? or the :
4416 // expression is to be evaluated.
4417 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004418 if (op_falsy)
4419 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4420 else
4421 {
4422 int error = FALSE;
4423
4424 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4425 &error);
4426 if (error)
4427 return FAIL;
4428 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004429 cctx->ctx_skip = save_skip == SKIP_YES ||
4430 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4431
4432 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4433 // "left ?? right" and "left" is truthy: produce "left"
4434 generate_ppconst(cctx, ppconst);
4435 else
4436 {
4437 clear_tv(&ppconst->pp_tv[ppconst_used]);
4438 --ppconst->pp_used;
4439 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 }
4441 else
4442 {
4443 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004444 if (op_falsy)
4445 end_idx = instr->ga_len;
4446 generate_JUMP(cctx, op_falsy
4447 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4448 if (op_falsy)
4449 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451
4452 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004453 *arg = skipwhite(p + 1 + op_falsy);
4454 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004455 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004457 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459 if (!has_const_expr)
4460 {
4461 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004463 if (!op_falsy)
4464 {
4465 // remember the type and drop it
4466 --stack->ga_len;
4467 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004469 end_idx = instr->ga_len;
4470 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004472 // jump here from JUMP_IF_FALSE
4473 isn = ((isn_T *)instr->ga_data) + alt_idx;
4474 isn->isn_arg.jump.jump_where = instr->ga_len;
4475 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004478 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004480 // Check for the ":".
4481 p = may_peek_next_line(cctx, *arg, &next);
4482 if (*p != ':')
4483 {
4484 emsg(_(e_missing_colon));
4485 return FAIL;
4486 }
4487 if (next != NULL)
4488 {
4489 *arg = next_line_from_context(cctx, TRUE);
4490 p = skipwhite(*arg);
4491 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004492
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004493 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4494 {
4495 semsg(_(e_white_space_required_before_and_after_str), ":");
4496 return FAIL;
4497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004499 // evaluate the third expression
4500 if (has_const_expr)
4501 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004502 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004503 *arg = skipwhite(p + 1);
4504 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4505 return FAIL;
4506 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4507 return FAIL;
4508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510 if (!has_const_expr)
4511 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004512 type_T **typep;
4513
Bram Moolenaara5565e42020-05-09 15:44:01 +02004514 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515
Bram Moolenaara5565e42020-05-09 15:44:01 +02004516 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004517 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4518 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004520 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521 isn = ((isn_T *)instr->ga_data) + end_idx;
4522 isn->isn_arg.jump.jump_where = instr->ga_len;
4523 }
4524
4525 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 }
4527 return OK;
4528}
4529
4530/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004532 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4533 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534 */
4535 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004536compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537{
4538 ppconst_T ppconst;
4539
4540 CLEAR_FIELD(ppconst);
4541 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4542 {
4543 clear_ppconst(&ppconst);
4544 return FAIL;
4545 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004546 if (is_const != NULL)
4547 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004548 if (generate_ppconst(cctx, &ppconst) == FAIL)
4549 return FAIL;
4550 return OK;
4551}
4552
4553/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004554 * Toplevel expression.
4555 */
4556 static int
4557compile_expr0(char_u **arg, cctx_T *cctx)
4558{
4559 return compile_expr0_ext(arg, cctx, NULL);
4560}
4561
4562/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 * compile "return [expr]"
4564 */
4565 static char_u *
4566compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4567{
4568 char_u *p = arg;
4569 garray_T *stack = &cctx->ctx_type_stack;
4570 type_T *stack_type;
4571
4572 if (*p != NUL && *p != '|' && *p != '\n')
4573 {
4574 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 return NULL;
4577
4578 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4579 if (set_return_type)
4580 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004581 else
4582 {
4583 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4584 && stack_type->tt_type != VAR_VOID
4585 && stack_type->tt_type != VAR_UNKNOWN)
4586 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004587 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004588 return NULL;
4589 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004590 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004591 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004592 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 }
4595 else
4596 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004597 // "set_return_type" cannot be TRUE, only used for a lambda which
4598 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004599 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4600 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004602 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 return NULL;
4604 }
4605
4606 // No argument, return zero.
4607 generate_PUSHNR(cctx, 0);
4608 }
4609
4610 if (generate_instr(cctx, ISN_RETURN) == NULL)
4611 return NULL;
4612
4613 // "return val | endif" is possible
4614 return skipwhite(p);
4615}
4616
4617/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004618 * Get a line from the compilation context, compatible with exarg_T getline().
4619 * Return a pointer to the line in allocated memory.
4620 * Return NULL for end-of-file or some error.
4621 */
4622 static char_u *
4623exarg_getline(
4624 int c UNUSED,
4625 void *cookie,
4626 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004627 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004628{
4629 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004630 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004631
Bram Moolenaar66250c92020-08-20 15:02:42 +02004632 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004633 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004634 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004635 return NULL;
4636 ++cctx->ctx_lnum;
4637 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4638 // Comment lines result in NULL pointers, skip them.
4639 if (p != NULL)
4640 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004641 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004642}
4643
4644/*
4645 * Compile a nested :def command.
4646 */
4647 static char_u *
4648compile_nested_function(exarg_T *eap, cctx_T *cctx)
4649{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004650 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004651 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004652 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004653 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004654 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004655 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004656
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004657 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004658 {
4659 emsg(_(e_cannot_use_bang_with_nested_def));
4660 return NULL;
4661 }
4662
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004663 // Only g:Func() can use a namespace.
4664 if (name_start[1] == ':' && !is_global)
4665 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004666 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004667 return NULL;
4668 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004669 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4670 return NULL;
4671
Bram Moolenaar04b12692020-05-04 23:24:44 +02004672 eap->arg = name_end;
4673 eap->getline = exarg_getline;
4674 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004675 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004676 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004677 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004678 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004679
Bram Moolenaar822ba242020-05-24 23:00:18 +02004680 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004681 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004682 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004683 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004684 {
4685 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004686 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004687 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004688
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004689 if (is_global)
4690 {
4691 char_u *func_name = vim_strnsave(name_start + 2,
4692 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004693
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004694 if (func_name == NULL)
4695 r = FAIL;
4696 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004697 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004698 }
4699 else
4700 {
4701 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004702 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004703 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004704 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004705
Bram Moolenaareef21022020-08-01 22:16:43 +02004706 if (lvar == NULL)
4707 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004708 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004709 return NULL;
4710 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004711
4712 // copy over the block scope IDs
4713 if (block_depth > 0)
4714 {
4715 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4716 if (ufunc->uf_block_ids != NULL)
4717 {
4718 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4719 sizeof(int) * block_depth);
4720 ufunc->uf_block_depth = block_depth;
4721 }
4722 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004723 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004724
Bram Moolenaar61a89812020-05-07 16:58:17 +02004725 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004726 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004727}
4728
4729/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 * Return the length of an assignment operator, or zero if there isn't one.
4731 */
4732 int
4733assignment_len(char_u *p, int *heredoc)
4734{
4735 if (*p == '=')
4736 {
4737 if (p[1] == '<' && p[2] == '<')
4738 {
4739 *heredoc = TRUE;
4740 return 3;
4741 }
4742 return 1;
4743 }
4744 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4745 return 2;
4746 if (STRNCMP(p, "..=", 3) == 0)
4747 return 3;
4748 return 0;
4749}
4750
4751// words that cannot be used as a variable
4752static char *reserved[] = {
4753 "true",
4754 "false",
4755 NULL
4756};
4757
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004758typedef enum {
4759 dest_local,
4760 dest_option,
4761 dest_env,
4762 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004763 dest_buffer,
4764 dest_window,
4765 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004766 dest_vimvar,
4767 dest_script,
4768 dest_reg,
4769} assign_dest_T;
4770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004772 * Generate the load instruction for "name".
4773 */
4774 static void
4775generate_loadvar(
4776 cctx_T *cctx,
4777 assign_dest_T dest,
4778 char_u *name,
4779 lvar_T *lvar,
4780 type_T *type)
4781{
4782 switch (dest)
4783 {
4784 case dest_option:
4785 // TODO: check the option exists
4786 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4787 break;
4788 case dest_global:
4789 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4790 break;
4791 case dest_buffer:
4792 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4793 break;
4794 case dest_window:
4795 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4796 break;
4797 case dest_tab:
4798 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4799 break;
4800 case dest_script:
4801 compile_load_scriptvar(cctx,
4802 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4803 break;
4804 case dest_env:
4805 // Include $ in the name here
4806 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4807 break;
4808 case dest_reg:
4809 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4810 break;
4811 case dest_vimvar:
4812 generate_LOADV(cctx, name + 2, TRUE);
4813 break;
4814 case dest_local:
4815 if (lvar->lv_from_outer)
4816 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4817 NULL, type);
4818 else
4819 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4820 break;
4821 }
4822}
4823
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004824 void
4825vim9_declare_error(char_u *name)
4826{
4827 char *scope = "";
4828
4829 switch (*name)
4830 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004831 case 'g': scope = _("global"); break;
4832 case 'b': scope = _("buffer"); break;
4833 case 'w': scope = _("window"); break;
4834 case 't': scope = _("tab"); break;
4835 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004836 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004837 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004838 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004839 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004840 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004841 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004842 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004843 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004844 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004845}
4846
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004847/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004849 * "let name"
4850 * "var name = expr"
4851 * "final name = expr"
4852 * "const name = expr"
4853 * "name = expr"
4854 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004855 * Return NULL for an error.
4856 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 */
4858 static char_u *
4859compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4860{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004861 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004863 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 char_u *ret = NULL;
4865 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004866 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004867 int scriptvar_sid = 0;
4868 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004871 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 int oplen = 0;
4874 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004875 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004876 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004877 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004879 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4880 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004882 // Skip over the "var" or "[var, var]" to get to any "=".
4883 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4884 if (p == NULL)
4885 return *arg == '[' ? arg : NULL;
4886
4887 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004889 // TODO: should we allow this, and figure out type inference from list
4890 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004891 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 return NULL;
4893 }
4894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 sp = p;
4896 p = skipwhite(p);
4897 op = p;
4898 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004899
4900 if (var_count > 0 && oplen == 0)
4901 // can be something like "[1, 2]->func()"
4902 return arg;
4903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4905 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004906 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004907 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004908 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 if (heredoc)
4911 {
4912 list_T *l;
4913 listitem_T *li;
4914
4915 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004916 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004918 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004919 if (l == NULL)
4920 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921
Bram Moolenaar078269b2020-09-21 20:35:55 +02004922 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004924 // Push each line and the create the list.
4925 FOR_ALL_LIST_ITEMS(l, li)
4926 {
4927 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4928 li->li_tv.vval.v_string = NULL;
4929 }
4930 generate_NEWLIST(cctx, l->lv_len);
4931 type = &t_list_string;
4932 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 list_free(l);
4935 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004936 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004940 // for "[var, var] = expr" evaluate the expression here, loop over the
4941 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004942
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004943 p = skipwhite(op + oplen);
4944 if (compile_expr0(&p, cctx) == FAIL)
4945 return NULL;
4946 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004948 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004950 type_T *stacktype;
4951
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004952 stacktype = stack->ga_len == 0 ? &t_void
4953 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004954 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004956 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 goto theend;
4958 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004959 if (need_type(stacktype, &t_list_any, -1, cctx,
4960 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004962 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004963 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4964 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004965 }
4966 }
4967
4968 /*
4969 * Loop over variables in "[var, var] = expr".
4970 * For "var = expr" and "let var: type" this is done only once.
4971 */
4972 if (var_count > 0)
4973 var_start = skipwhite(arg + 1); // skip over the "["
4974 else
4975 var_start = arg;
4976 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4977 {
4978 char_u *var_end = skip_var_one(var_start, FALSE);
4979 size_t varlen;
4980 int new_local = FALSE;
4981 int opt_type;
4982 int opt_flags = 0;
4983 assign_dest_T dest = dest_local;
4984 int vimvaridx = -1;
4985 lvar_T *lvar = NULL;
4986 lvar_T arg_lvar;
4987 int has_type = FALSE;
4988 int has_index = FALSE;
4989 int instr_count = -1;
4990
Bram Moolenaar65821722020-08-02 18:58:54 +02004991 if (*var_start == '@')
4992 p = var_start + 2;
4993 else
4994 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004995 // skip over the leading "&", "&l:", "&g:" and "$"
4996 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004997 p = to_name_end(p, TRUE);
4998 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004999
5000 // "a: type" is declaring variable "a" with a type, not "a:".
5001 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5002 --var_end;
5003 if (is_decl && p == var_start + 2 && p[-1] == ':')
5004 --p;
5005
5006 varlen = p - var_start;
5007 vim_free(name);
5008 name = vim_strnsave(var_start, varlen);
5009 if (name == NULL)
5010 return NULL;
5011 if (!heredoc)
5012 type = &t_any;
5013
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005014 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005016 int declare_error = FALSE;
5017
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 if (*var_start == '&')
5019 {
5020 int cc;
5021 long numval;
5022
5023 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005024 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 emsg(_(e_const_option));
5027 goto theend;
5028 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005029 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 p = var_start;
5031 p = find_option_end(&p, &opt_flags);
5032 if (p == NULL)
5033 {
5034 // cannot happen?
5035 emsg(_(e_letunexp));
5036 goto theend;
5037 }
5038 cc = *p;
5039 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005040 opt_type = get_option_value(skip_option_env_lead(var_start),
5041 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 *p = cc;
5043 if (opt_type == -3)
5044 {
5045 semsg(_(e_unknown_option), var_start);
5046 goto theend;
5047 }
5048 if (opt_type == -2 || opt_type == 0)
5049 type = &t_string;
5050 else
5051 type = &t_number; // both number and boolean option
5052 }
5053 else if (*var_start == '$')
5054 {
5055 dest = dest_env;
5056 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005057 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005058 }
5059 else if (*var_start == '@')
5060 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005061 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005062 {
5063 emsg_invreg(var_start[1]);
5064 goto theend;
5065 }
5066 dest = dest_reg;
5067 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005068 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005069 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005070 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 {
5072 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005073 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005075 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 {
5077 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005078 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005080 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 {
5082 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005083 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005085 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 {
5087 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005088 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005090 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 {
5092 typval_T *vtv;
5093 int di_flags;
5094
5095 vimvaridx = find_vim_var(name + 2, &di_flags);
5096 if (vimvaridx < 0)
5097 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005098 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 goto theend;
5100 }
5101 // We use the current value of "sandbox" here, is that OK?
5102 if (var_check_ro(di_flags, name, FALSE))
5103 goto theend;
5104 dest = dest_vimvar;
5105 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005106 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005107 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005108 }
5109 else
5110 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005111 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005112
5113 for (idx = 0; reserved[idx] != NULL; ++idx)
5114 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005116 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005117 goto theend;
5118 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119
5120 lvar = lookup_local(var_start, varlen, cctx);
5121 if (lvar == NULL)
5122 {
5123 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005124 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005125 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5126 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005127 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 if (is_decl)
5129 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005130 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 goto theend;
5132 }
5133 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005134 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005135 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 if (lvar != NULL)
5137 {
5138 if (is_decl)
5139 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005140 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 goto theend;
5142 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005144 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005146 int script_namespace = varlen > 1
5147 && STRNCMP(var_start, "s:", 2) == 0;
5148 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005149 ? script_var_exists(var_start + 2, varlen - 2,
5150 FALSE, cctx)
5151 : script_var_exists(var_start, varlen,
5152 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005153 imported_T *import =
5154 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005155
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005156 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005157 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005158 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5159
5160 if (is_decl)
5161 {
5162 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005163 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005164 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005165 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005166 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005167 name);
5168 goto theend;
5169 }
5170 else if (cctx->ctx_ufunc->uf_script_ctx_version
5171 == SCRIPT_VERSION_VIM9
5172 && script_namespace
5173 && !script_var && import == NULL)
5174 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005175 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005176 goto theend;
5177 }
5178
5179 dest = dest_script;
5180
5181 // existing script-local variables should have a type
5182 scriptvar_sid = current_sctx.sc_sid;
5183 if (import != NULL)
5184 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005185 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005186 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005187 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005188 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005189 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005190 {
5191 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5192 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005193 ((svar_T *)si->sn_var_vals.ga_data)
5194 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005195 type = sv->sv_type;
5196 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005197 }
5198 }
5199 else if (name[1] == ':' && name[2] != NUL)
5200 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005201 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005202 goto theend;
5203 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005204 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005205 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005206 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005207 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005208 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005209 else if (check_defined(var_start, varlen, cctx) == FAIL)
5210 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005211 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005213
5214 if (declare_error)
5215 {
5216 vim9_declare_error(name);
5217 goto theend;
5218 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219 }
5220
5221 // handle "a:name" as a name, not index "name" on "a"
5222 if (varlen > 1 || var_start[varlen] != ':')
5223 p = var_end;
5224
5225 if (dest != dest_option)
5226 {
5227 if (is_decl && *p == ':')
5228 {
5229 // parse optional type: "let var: type = expr"
5230 if (!VIM_ISWHITE(p[1]))
5231 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005232 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 goto theend;
5234 }
5235 p = skipwhite(p + 1);
5236 type = parse_type(&p, cctx->ctx_type_list);
5237 has_type = TRUE;
5238 }
5239 else if (lvar != NULL)
5240 type = lvar->lv_type;
5241 }
5242
5243 if (oplen == 3 && !heredoc && dest != dest_global
5244 && type->tt_type != VAR_STRING
5245 && type->tt_type != VAR_ANY)
5246 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005247 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 goto theend;
5249 }
5250
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005251 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005252 {
5253 if (oplen > 1 && !heredoc)
5254 {
5255 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005256 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005257 goto theend;
5258 }
5259
5260 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005261 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5262 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005263 goto theend;
5264 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005265 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 if (lvar == NULL)
5267 goto theend;
5268 new_local = TRUE;
5269 }
5270
5271 member_type = type;
5272 if (var_end > var_start + varlen)
5273 {
5274 // Something follows after the variable: "var[idx]".
5275 if (is_decl)
5276 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005277 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 goto theend;
5279 }
5280
5281 if (var_start[varlen] == '[')
5282 {
5283 has_index = TRUE;
5284 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005285 member_type = &t_any;
5286 else
5287 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 }
5289 else
5290 {
5291 semsg("Not supported yet: %s", var_start);
5292 goto theend;
5293 }
5294 }
5295 else if (lvar == &arg_lvar)
5296 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005297 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 goto theend;
5299 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005300 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5301 {
5302 semsg(_(e_cannot_assign_to_constant), name);
5303 goto theend;
5304 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005305
5306 if (!heredoc)
5307 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005308 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005309 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005310 if (oplen > 0 && var_count == 0)
5311 {
5312 // skip over the "=" and the expression
5313 p = skipwhite(op + oplen);
5314 compile_expr0(&p, cctx);
5315 }
5316 }
5317 else if (oplen > 0)
5318 {
5319 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005320 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005321
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005322 // For "var = expr" evaluate the expression.
5323 if (var_count == 0)
5324 {
5325 int r;
5326
5327 // for "+=", "*=", "..=" etc. first load the current value
5328 if (*op != '=')
5329 {
5330 generate_loadvar(cctx, dest, name, lvar, type);
5331
5332 if (has_index)
5333 {
5334 // TODO: get member from list or dict
5335 emsg("Index with operation not supported yet");
5336 goto theend;
5337 }
5338 }
5339
5340 // Compile the expression. Temporarily hide the new local
5341 // variable here, it is not available to this expression.
5342 if (new_local)
5343 --cctx->ctx_locals.ga_len;
5344 instr_count = instr->ga_len;
5345 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005346 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347 if (new_local)
5348 ++cctx->ctx_locals.ga_len;
5349 if (r == FAIL)
5350 goto theend;
5351 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005352 else if (semicolon && var_idx == var_count - 1)
5353 {
5354 // For "[var; var] = expr" get the rest of the list
5355 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5356 goto theend;
5357 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005358 else
5359 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005360 // For "[var, var] = expr" get the "var_idx" item from the
5361 // list.
5362 if (generate_GETITEM(cctx, var_idx) == FAIL)
5363 return FAIL;
5364 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005365
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005366 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005367 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005368 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005369 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005370 if ((stacktype->tt_type == VAR_FUNC
5371 || stacktype->tt_type == VAR_PARTIAL)
5372 && var_wrong_func_name(name, TRUE))
5373 goto theend;
5374
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005375 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005376 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005377 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005379 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005380 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 }
5382 else
5383 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005384 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005385 // for a variable that implies &t_any.
5386 if (stacktype == &t_list_empty)
5387 lvar->lv_type = &t_list_any;
5388 else if (stacktype == &t_dict_empty)
5389 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005390 else if (stacktype == &t_unknown)
5391 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005392 else
5393 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005394 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005395 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005396 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005397 {
5398 type_T *use_type = lvar->lv_type;
5399
Bram Moolenaar71abe482020-09-14 22:28:30 +02005400 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005401 if (has_index)
5402 {
5403 use_type = use_type->tt_member;
5404 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005405 // could be indexing "any"
5406 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005407 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005408 if (need_type(stacktype, use_type, -1, cctx,
5409 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005410 goto theend;
5411 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005412 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005413 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005414 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005415 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005417 else if (cmdidx == CMD_final)
5418 {
5419 emsg(_(e_final_requires_a_value));
5420 goto theend;
5421 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005422 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005424 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005425 goto theend;
5426 }
5427 else if (!has_type || dest == dest_option)
5428 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005429 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 goto theend;
5431 }
5432 else
5433 {
5434 // variables are always initialized
5435 if (ga_grow(instr, 1) == FAIL)
5436 goto theend;
5437 switch (member_type->tt_type)
5438 {
5439 case VAR_BOOL:
5440 generate_PUSHBOOL(cctx, VVAL_FALSE);
5441 break;
5442 case VAR_FLOAT:
5443#ifdef FEAT_FLOAT
5444 generate_PUSHF(cctx, 0.0);
5445#endif
5446 break;
5447 case VAR_STRING:
5448 generate_PUSHS(cctx, NULL);
5449 break;
5450 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005451 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005452 break;
5453 case VAR_FUNC:
5454 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5455 break;
5456 case VAR_LIST:
5457 generate_NEWLIST(cctx, 0);
5458 break;
5459 case VAR_DICT:
5460 generate_NEWDICT(cctx, 0);
5461 break;
5462 case VAR_JOB:
5463 generate_PUSHJOB(cctx, NULL);
5464 break;
5465 case VAR_CHANNEL:
5466 generate_PUSHCHANNEL(cctx, NULL);
5467 break;
5468 case VAR_NUMBER:
5469 case VAR_UNKNOWN:
5470 case VAR_ANY:
5471 case VAR_PARTIAL:
5472 case VAR_VOID:
5473 case VAR_SPECIAL: // cannot happen
5474 generate_PUSHNR(cctx, 0);
5475 break;
5476 }
5477 }
5478 if (var_count == 0)
5479 end = p;
5480 }
5481
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005482 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005483 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005484 break;
5485
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005486 if (oplen > 0 && *op != '=')
5487 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005488 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005489 type_T *stacktype;
5490
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005491 if (*op == '.')
5492 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005493 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005494 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005495 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005496 if (
5497#ifdef FEAT_FLOAT
5498 // If variable is float operation with number is OK.
5499 !(expected == &t_float && stacktype == &t_number) &&
5500#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005501 need_type(stacktype, expected, -1, cctx,
5502 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005503 goto theend;
5504
5505 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005506 {
5507 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5508 goto theend;
5509 }
5510 else if (*op == '+')
5511 {
5512 if (generate_add_instr(cctx,
5513 operator_type(member_type, stacktype),
5514 member_type, stacktype) == FAIL)
5515 goto theend;
5516 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005517 else if (generate_two_op(cctx, op) == FAIL)
5518 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005520
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005522 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005523 int r;
5524
5525 // Compile the "idx" in "var[idx]".
5526 if (new_local)
5527 --cctx->ctx_locals.ga_len;
5528 p = skipwhite(var_start + varlen + 1);
5529 r = compile_expr0(&p, cctx);
5530 if (new_local)
5531 ++cctx->ctx_locals.ga_len;
5532 if (r == FAIL)
5533 goto theend;
5534 if (*skipwhite(p) != ']')
5535 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005536 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005537 emsg(_(e_missbrac));
5538 goto theend;
5539 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005540 if (type == &t_any)
5541 {
5542 type_T *idx_type = ((type_T **)stack->ga_data)[
5543 stack->ga_len - 1];
5544 // Index on variable of unknown type: guess the type from the
5545 // index type: number is dict, otherwise dict.
5546 // TODO: should do the assignment at runtime
5547 if (idx_type->tt_type == VAR_NUMBER)
5548 type = &t_list_any;
5549 else
5550 type = &t_dict_any;
5551 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005552 if (type->tt_type == VAR_DICT
5553 && may_generate_2STRING(-1, cctx) == FAIL)
5554 goto theend;
5555 if (type->tt_type == VAR_LIST
5556 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005557 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005558 {
5559 emsg(_(e_number_exp));
5560 goto theend;
5561 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005562
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563 // Load the dict or list. On the stack we then have:
5564 // - value
5565 // - index
5566 // - variable
5567 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005568
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005569 if (type->tt_type == VAR_LIST)
5570 {
5571 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5572 return FAIL;
5573 }
5574 else if (type->tt_type == VAR_DICT)
5575 {
5576 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5577 return FAIL;
5578 }
5579 else
5580 {
5581 emsg(_(e_listreq));
5582 goto theend;
5583 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005584 }
5585 else
5586 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005587 if (is_decl && cmdidx == CMD_const
5588 && (dest == dest_script || dest == dest_local))
5589 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005590 generate_LOCKCONST(cctx);
5591
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005592 switch (dest)
5593 {
5594 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005595 generate_STOREOPT(cctx, skip_option_env_lead(name),
5596 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005597 break;
5598 case dest_global:
5599 // include g: with the name, easier to execute that way
5600 generate_STORE(cctx, ISN_STOREG, 0, name);
5601 break;
5602 case dest_buffer:
5603 // include b: with the name, easier to execute that way
5604 generate_STORE(cctx, ISN_STOREB, 0, name);
5605 break;
5606 case dest_window:
5607 // include w: with the name, easier to execute that way
5608 generate_STORE(cctx, ISN_STOREW, 0, name);
5609 break;
5610 case dest_tab:
5611 // include t: with the name, easier to execute that way
5612 generate_STORE(cctx, ISN_STORET, 0, name);
5613 break;
5614 case dest_env:
5615 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5616 break;
5617 case dest_reg:
5618 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5619 break;
5620 case dest_vimvar:
5621 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5622 break;
5623 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005624 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005625 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005626 {
5627 char_u *name_s = name;
5628
5629 // Include s: in the name for store_var()
5630 if (name[1] != ':')
5631 {
5632 int len = (int)STRLEN(name) + 3;
5633
5634 name_s = alloc(len);
5635 if (name_s == NULL)
5636 name_s = name;
5637 else
5638 vim_snprintf((char *)name_s, len,
5639 "s:%s", name);
5640 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005641 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5642 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005643 if (name_s != name)
5644 vim_free(name_s);
5645 }
5646 else
5647 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005648 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005649 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005650 break;
5651 case dest_local:
5652 if (lvar != NULL)
5653 {
5654 isn_T *isn = ((isn_T *)instr->ga_data)
5655 + instr->ga_len - 1;
5656
5657 // optimization: turn "var = 123" from ISN_PUSHNR +
5658 // ISN_STORE into ISN_STORENR
5659 if (!lvar->lv_from_outer
5660 && instr->ga_len == instr_count + 1
5661 && isn->isn_type == ISN_PUSHNR)
5662 {
5663 varnumber_T val = isn->isn_arg.number;
5664
5665 isn->isn_type = ISN_STORENR;
5666 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5667 isn->isn_arg.storenr.stnr_val = val;
5668 if (stack->ga_len > 0)
5669 --stack->ga_len;
5670 }
5671 else if (lvar->lv_from_outer)
5672 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005673 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005674 else
5675 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5676 }
5677 break;
5678 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005679 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005680
5681 if (var_idx + 1 < var_count)
5682 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005684
5685 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005686 if (var_count > 0 && !semicolon)
5687 {
5688 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5689 goto theend;
5690 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005691
Bram Moolenaarb2097502020-07-19 17:17:02 +02005692 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693
5694theend:
5695 vim_free(name);
5696 return ret;
5697}
5698
5699/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005700 * Check if "name" can be "unlet".
5701 */
5702 int
5703check_vim9_unlet(char_u *name)
5704{
5705 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5706 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005707 // "unlet s:var" is allowed in legacy script.
5708 if (*name == 's' && !script_is_vim9())
5709 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005710 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005711 return FAIL;
5712 }
5713 return OK;
5714}
5715
5716/*
5717 * Callback passed to ex_unletlock().
5718 */
5719 static int
5720compile_unlet(
5721 lval_T *lvp,
5722 char_u *name_end,
5723 exarg_T *eap,
5724 int deep UNUSED,
5725 void *coookie)
5726{
5727 cctx_T *cctx = coookie;
5728
5729 if (lvp->ll_tv == NULL)
5730 {
5731 char_u *p = lvp->ll_name;
5732 int cc = *name_end;
5733 int ret = OK;
5734
5735 // Normal name. Only supports g:, w:, t: and b: namespaces.
5736 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005737 if (*p == '$')
5738 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5739 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005740 ret = FAIL;
5741 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005742 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005743
5744 *name_end = cc;
5745 return ret;
5746 }
5747
5748 // TODO: unlet {list}[idx]
5749 // TODO: unlet {dict}[key]
5750 emsg("Sorry, :unlet not fully implemented yet");
5751 return FAIL;
5752}
5753
5754/*
5755 * compile "unlet var", "lock var" and "unlock var"
5756 * "arg" points to "var".
5757 */
5758 static char_u *
5759compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5760{
5761 char_u *p = arg;
5762
5763 if (eap->cmdidx != CMD_unlet)
5764 {
5765 emsg("Sorry, :lock and unlock not implemented yet");
5766 return NULL;
5767 }
5768
5769 if (*p == '!')
5770 {
5771 p = skipwhite(p + 1);
5772 eap->forceit = TRUE;
5773 }
5774
5775 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5776 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5777}
5778
5779/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005780 * Compile an :import command.
5781 */
5782 static char_u *
5783compile_import(char_u *arg, cctx_T *cctx)
5784{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005785 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786}
5787
5788/*
5789 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5790 */
5791 static int
5792compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5793{
5794 garray_T *instr = &cctx->ctx_instr;
5795 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5796
5797 if (endlabel == NULL)
5798 return FAIL;
5799 endlabel->el_next = *el;
5800 *el = endlabel;
5801 endlabel->el_end_label = instr->ga_len;
5802
5803 generate_JUMP(cctx, when, 0);
5804 return OK;
5805}
5806
5807 static void
5808compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5809{
5810 garray_T *instr = &cctx->ctx_instr;
5811
5812 while (*el != NULL)
5813 {
5814 endlabel_T *cur = (*el);
5815 isn_T *isn;
5816
5817 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5818 isn->isn_arg.jump.jump_where = instr->ga_len;
5819 *el = cur->el_next;
5820 vim_free(cur);
5821 }
5822}
5823
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005824 static void
5825compile_free_jump_to_end(endlabel_T **el)
5826{
5827 while (*el != NULL)
5828 {
5829 endlabel_T *cur = (*el);
5830
5831 *el = cur->el_next;
5832 vim_free(cur);
5833 }
5834}
5835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836/*
5837 * Create a new scope and set up the generic items.
5838 */
5839 static scope_T *
5840new_scope(cctx_T *cctx, scopetype_T type)
5841{
5842 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5843
5844 if (scope == NULL)
5845 return NULL;
5846 scope->se_outer = cctx->ctx_scope;
5847 cctx->ctx_scope = scope;
5848 scope->se_type = type;
5849 scope->se_local_count = cctx->ctx_locals.ga_len;
5850 return scope;
5851}
5852
5853/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005854 * Free the current scope and go back to the outer scope.
5855 */
5856 static void
5857drop_scope(cctx_T *cctx)
5858{
5859 scope_T *scope = cctx->ctx_scope;
5860
5861 if (scope == NULL)
5862 {
5863 iemsg("calling drop_scope() without a scope");
5864 return;
5865 }
5866 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005867 switch (scope->se_type)
5868 {
5869 case IF_SCOPE:
5870 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5871 case FOR_SCOPE:
5872 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5873 case WHILE_SCOPE:
5874 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5875 case TRY_SCOPE:
5876 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5877 case NO_SCOPE:
5878 case BLOCK_SCOPE:
5879 break;
5880 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005881 vim_free(scope);
5882}
5883
5884/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005885 * Check that the top of the type stack has a type that can be used as a
5886 * condition. Give an error and return FAIL if not.
5887 */
5888 static int
5889bool_on_stack(cctx_T *cctx)
5890{
5891 garray_T *stack = &cctx->ctx_type_stack;
5892 type_T *type;
5893
5894 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5895 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005896 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005897 return FAIL;
5898 return OK;
5899}
5900
5901/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902 * compile "if expr"
5903 *
5904 * "if expr" Produces instructions:
5905 * EVAL expr Push result of "expr"
5906 * JUMP_IF_FALSE end
5907 * ... body ...
5908 * end:
5909 *
5910 * "if expr | else" Produces instructions:
5911 * EVAL expr Push result of "expr"
5912 * JUMP_IF_FALSE else
5913 * ... body ...
5914 * JUMP_ALWAYS end
5915 * else:
5916 * ... body ...
5917 * end:
5918 *
5919 * "if expr1 | elseif expr2 | else" Produces instructions:
5920 * EVAL expr Push result of "expr"
5921 * JUMP_IF_FALSE elseif
5922 * ... body ...
5923 * JUMP_ALWAYS end
5924 * elseif:
5925 * EVAL expr Push result of "expr"
5926 * JUMP_IF_FALSE else
5927 * ... body ...
5928 * JUMP_ALWAYS end
5929 * else:
5930 * ... body ...
5931 * end:
5932 */
5933 static char_u *
5934compile_if(char_u *arg, cctx_T *cctx)
5935{
5936 char_u *p = arg;
5937 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005938 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005940 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005941 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942
Bram Moolenaara5565e42020-05-09 15:44:01 +02005943 CLEAR_FIELD(ppconst);
5944 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005945 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005946 clear_ppconst(&ppconst);
5947 return NULL;
5948 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005949 if (cctx->ctx_skip == SKIP_YES)
5950 clear_ppconst(&ppconst);
5951 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005952 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005953 int error = FALSE;
5954 int v;
5955
Bram Moolenaara5565e42020-05-09 15:44:01 +02005956 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005957 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005958 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005959 if (error)
5960 return NULL;
5961 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005962 }
5963 else
5964 {
5965 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005966 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005967 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005968 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005969 if (bool_on_stack(cctx) == FAIL)
5970 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972
5973 scope = new_scope(cctx, IF_SCOPE);
5974 if (scope == NULL)
5975 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005976 scope->se_skip_save = skip_save;
5977 // "is_had_return" will be reset if any block does not end in :return
5978 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005980 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005981 {
5982 // "where" is set when ":elseif", "else" or ":endif" is found
5983 scope->se_u.se_if.is_if_label = instr->ga_len;
5984 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5985 }
5986 else
5987 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988
5989 return p;
5990}
5991
5992 static char_u *
5993compile_elseif(char_u *arg, cctx_T *cctx)
5994{
5995 char_u *p = arg;
5996 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005997 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 isn_T *isn;
5999 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006000 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006001 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
6003 if (scope == NULL || scope->se_type != IF_SCOPE)
6004 {
6005 emsg(_(e_elseif_without_if));
6006 return NULL;
6007 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006008 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006009 if (!cctx->ctx_had_return)
6010 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006012 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006013 {
6014 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006016 return NULL;
6017 // previous "if" or "elseif" jumps here
6018 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6019 isn->isn_arg.jump.jump_where = instr->ga_len;
6020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006022 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006023 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006024 if (cctx->ctx_skip == SKIP_YES)
6025 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006026 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006027 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006028 clear_ppconst(&ppconst);
6029 return NULL;
6030 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006031 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006032 if (scope->se_skip_save == SKIP_YES)
6033 clear_ppconst(&ppconst);
6034 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006035 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006036 int error = FALSE;
6037 int v;
6038
Bram Moolenaar7f141552020-05-09 17:35:53 +02006039 // The expression results in a constant.
6040 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006041 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6042 if (error)
6043 return NULL;
6044 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006045 clear_ppconst(&ppconst);
6046 scope->se_u.se_if.is_if_label = -1;
6047 }
6048 else
6049 {
6050 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006051 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006052 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006053 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006054 if (bool_on_stack(cctx) == FAIL)
6055 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006057 // "where" is set when ":elseif", "else" or ":endif" is found
6058 scope->se_u.se_if.is_if_label = instr->ga_len;
6059 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
6062 return p;
6063}
6064
6065 static char_u *
6066compile_else(char_u *arg, cctx_T *cctx)
6067{
6068 char_u *p = arg;
6069 garray_T *instr = &cctx->ctx_instr;
6070 isn_T *isn;
6071 scope_T *scope = cctx->ctx_scope;
6072
6073 if (scope == NULL || scope->se_type != IF_SCOPE)
6074 {
6075 emsg(_(e_else_without_if));
6076 return NULL;
6077 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006078 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006079 if (!cctx->ctx_had_return)
6080 scope->se_u.se_if.is_had_return = FALSE;
6081 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082
Bram Moolenaarefd88552020-06-18 20:50:10 +02006083 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006084 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006085 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006086 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006087 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006088 if (!cctx->ctx_had_return
6089 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6090 JUMP_ALWAYS, cctx) == FAIL)
6091 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006092 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006093
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006094 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006095 {
6096 if (scope->se_u.se_if.is_if_label >= 0)
6097 {
6098 // previous "if" or "elseif" jumps here
6099 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6100 isn->isn_arg.jump.jump_where = instr->ga_len;
6101 scope->se_u.se_if.is_if_label = -1;
6102 }
6103 }
6104
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006105 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006106 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108
6109 return p;
6110}
6111
6112 static char_u *
6113compile_endif(char_u *arg, cctx_T *cctx)
6114{
6115 scope_T *scope = cctx->ctx_scope;
6116 ifscope_T *ifscope;
6117 garray_T *instr = &cctx->ctx_instr;
6118 isn_T *isn;
6119
6120 if (scope == NULL || scope->se_type != IF_SCOPE)
6121 {
6122 emsg(_(e_endif_without_if));
6123 return NULL;
6124 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006125 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006126 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006127 if (!cctx->ctx_had_return)
6128 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006130 if (scope->se_u.se_if.is_if_label >= 0)
6131 {
6132 // previous "if" or "elseif" jumps here
6133 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6134 isn->isn_arg.jump.jump_where = instr->ga_len;
6135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136 // Fill in the "end" label in jumps at the end of the blocks.
6137 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006138 cctx->ctx_skip = scope->se_skip_save;
6139
6140 // If all the blocks end in :return and there is an :else then the
6141 // had_return flag is set.
6142 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006144 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 return arg;
6146}
6147
6148/*
6149 * compile "for var in expr"
6150 *
6151 * Produces instructions:
6152 * PUSHNR -1
6153 * STORE loop-idx Set index to -1
6154 * EVAL expr Push result of "expr"
6155 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6156 * - if beyond end, jump to "end"
6157 * - otherwise get item from list and push it
6158 * STORE var Store item in "var"
6159 * ... body ...
6160 * JUMP top Jump back to repeat
6161 * end: DROP Drop the result of "expr"
6162 *
6163 */
6164 static char_u *
6165compile_for(char_u *arg, cctx_T *cctx)
6166{
6167 char_u *p;
6168 size_t varlen;
6169 garray_T *instr = &cctx->ctx_instr;
6170 garray_T *stack = &cctx->ctx_type_stack;
6171 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006172 lvar_T *loop_lvar; // loop iteration variable
6173 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 type_T *vartype;
6175
6176 // TODO: list of variables: "for [key, value] in dict"
6177 // parse "var"
6178 for (p = arg; eval_isnamec1(*p); ++p)
6179 ;
6180 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006181 var_lvar = lookup_local(arg, varlen, cctx);
6182 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006184 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 return NULL;
6186 }
6187
6188 // consume "in"
6189 p = skipwhite(p);
6190 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6191 {
6192 emsg(_(e_missing_in));
6193 return NULL;
6194 }
6195 p = skipwhite(p + 2);
6196
6197
6198 scope = new_scope(cctx, FOR_SCOPE);
6199 if (scope == NULL)
6200 return NULL;
6201
6202 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006203 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6204 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006205 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006206 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006207 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006210
6211 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006212 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6213 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006214 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006215 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006216 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006220 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221
6222 // compile "expr", it remains on the stack until "endfor"
6223 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006224 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006225 {
6226 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006230 // Now that we know the type of "var", check that it is a list, now or at
6231 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006233 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006235 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 return NULL;
6237 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006238 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006239 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240
6241 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006242 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006244 generate_FOR(cctx, loop_lvar->lv_idx);
6245 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246
6247 return arg;
6248}
6249
6250/*
6251 * compile "endfor"
6252 */
6253 static char_u *
6254compile_endfor(char_u *arg, cctx_T *cctx)
6255{
6256 garray_T *instr = &cctx->ctx_instr;
6257 scope_T *scope = cctx->ctx_scope;
6258 forscope_T *forscope;
6259 isn_T *isn;
6260
6261 if (scope == NULL || scope->se_type != FOR_SCOPE)
6262 {
6263 emsg(_(e_for));
6264 return NULL;
6265 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006266 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006268 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269
6270 // At end of ":for" scope jump back to the FOR instruction.
6271 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6272
6273 // Fill in the "end" label in the FOR statement so it can jump here
6274 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6275 isn->isn_arg.forloop.for_end = instr->ga_len;
6276
6277 // Fill in the "end" label any BREAK statements
6278 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6279
6280 // Below the ":for" scope drop the "expr" list from the stack.
6281 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6282 return NULL;
6283
6284 vim_free(scope);
6285
6286 return arg;
6287}
6288
6289/*
6290 * compile "while expr"
6291 *
6292 * Produces instructions:
6293 * top: EVAL expr Push result of "expr"
6294 * JUMP_IF_FALSE end jump if false
6295 * ... body ...
6296 * JUMP top Jump back to repeat
6297 * end:
6298 *
6299 */
6300 static char_u *
6301compile_while(char_u *arg, cctx_T *cctx)
6302{
6303 char_u *p = arg;
6304 garray_T *instr = &cctx->ctx_instr;
6305 scope_T *scope;
6306
6307 scope = new_scope(cctx, WHILE_SCOPE);
6308 if (scope == NULL)
6309 return NULL;
6310
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006311 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312
6313 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006314 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 return NULL;
6316
Bram Moolenaar13106602020-10-04 16:06:05 +02006317 if (bool_on_stack(cctx) == FAIL)
6318 return FAIL;
6319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006321 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 JUMP_IF_FALSE, cctx) == FAIL)
6323 return FAIL;
6324
6325 return p;
6326}
6327
6328/*
6329 * compile "endwhile"
6330 */
6331 static char_u *
6332compile_endwhile(char_u *arg, cctx_T *cctx)
6333{
6334 scope_T *scope = cctx->ctx_scope;
6335
6336 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6337 {
6338 emsg(_(e_while));
6339 return NULL;
6340 }
6341 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006342 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343
6344 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006345 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
6347 // Fill in the "end" label in the WHILE statement so it can jump here.
6348 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006349 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350
6351 vim_free(scope);
6352
6353 return arg;
6354}
6355
6356/*
6357 * compile "continue"
6358 */
6359 static char_u *
6360compile_continue(char_u *arg, cctx_T *cctx)
6361{
6362 scope_T *scope = cctx->ctx_scope;
6363
6364 for (;;)
6365 {
6366 if (scope == NULL)
6367 {
6368 emsg(_(e_continue));
6369 return NULL;
6370 }
6371 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6372 break;
6373 scope = scope->se_outer;
6374 }
6375
6376 // Jump back to the FOR or WHILE instruction.
6377 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006378 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6379 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 return arg;
6381}
6382
6383/*
6384 * compile "break"
6385 */
6386 static char_u *
6387compile_break(char_u *arg, cctx_T *cctx)
6388{
6389 scope_T *scope = cctx->ctx_scope;
6390 endlabel_T **el;
6391
6392 for (;;)
6393 {
6394 if (scope == NULL)
6395 {
6396 emsg(_(e_break));
6397 return NULL;
6398 }
6399 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6400 break;
6401 scope = scope->se_outer;
6402 }
6403
6404 // Jump to the end of the FOR or WHILE loop.
6405 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006406 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006408 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6410 return FAIL;
6411
6412 return arg;
6413}
6414
6415/*
6416 * compile "{" start of block
6417 */
6418 static char_u *
6419compile_block(char_u *arg, cctx_T *cctx)
6420{
6421 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6422 return NULL;
6423 return skipwhite(arg + 1);
6424}
6425
6426/*
6427 * compile end of block: drop one scope
6428 */
6429 static void
6430compile_endblock(cctx_T *cctx)
6431{
6432 scope_T *scope = cctx->ctx_scope;
6433
6434 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006435 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 vim_free(scope);
6437}
6438
6439/*
6440 * compile "try"
6441 * Creates a new scope for the try-endtry, pointing to the first catch and
6442 * finally.
6443 * Creates another scope for the "try" block itself.
6444 * TRY instruction sets up exception handling at runtime.
6445 *
6446 * "try"
6447 * TRY -> catch1, -> finally push trystack entry
6448 * ... try block
6449 * "throw {exception}"
6450 * EVAL {exception}
6451 * THROW create exception
6452 * ... try block
6453 * " catch {expr}"
6454 * JUMP -> finally
6455 * catch1: PUSH exeception
6456 * EVAL {expr}
6457 * MATCH
6458 * JUMP nomatch -> catch2
6459 * CATCH remove exception
6460 * ... catch block
6461 * " catch"
6462 * JUMP -> finally
6463 * catch2: CATCH remove exception
6464 * ... catch block
6465 * " finally"
6466 * finally:
6467 * ... finally block
6468 * " endtry"
6469 * ENDTRY pop trystack entry, may rethrow
6470 */
6471 static char_u *
6472compile_try(char_u *arg, cctx_T *cctx)
6473{
6474 garray_T *instr = &cctx->ctx_instr;
6475 scope_T *try_scope;
6476 scope_T *scope;
6477
6478 // scope that holds the jumps that go to catch/finally/endtry
6479 try_scope = new_scope(cctx, TRY_SCOPE);
6480 if (try_scope == NULL)
6481 return NULL;
6482
6483 // "catch" is set when the first ":catch" is found.
6484 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006485 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 if (generate_instr(cctx, ISN_TRY) == NULL)
6487 return NULL;
6488
6489 // scope for the try block itself
6490 scope = new_scope(cctx, BLOCK_SCOPE);
6491 if (scope == NULL)
6492 return NULL;
6493
6494 return arg;
6495}
6496
6497/*
6498 * compile "catch {expr}"
6499 */
6500 static char_u *
6501compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6502{
6503 scope_T *scope = cctx->ctx_scope;
6504 garray_T *instr = &cctx->ctx_instr;
6505 char_u *p;
6506 isn_T *isn;
6507
6508 // end block scope from :try or :catch
6509 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6510 compile_endblock(cctx);
6511 scope = cctx->ctx_scope;
6512
6513 // Error if not in a :try scope
6514 if (scope == NULL || scope->se_type != TRY_SCOPE)
6515 {
6516 emsg(_(e_catch));
6517 return NULL;
6518 }
6519
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006520 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006522 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 return NULL;
6524 }
6525
6526 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006527 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 JUMP_ALWAYS, cctx) == FAIL)
6529 return NULL;
6530
6531 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006532 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 if (isn->isn_arg.try.try_catch == 0)
6534 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006535 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 {
6537 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006538 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 isn->isn_arg.jump.jump_where = instr->ga_len;
6540 }
6541
6542 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006543 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006545 scope->se_u.se_try.ts_caught_all = TRUE;
6546 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 }
6548 else
6549 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006550 char_u *end;
6551 char_u *pat;
6552 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006553 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006554 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 // Push v:exception, push {expr} and MATCH
6557 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6558
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006559 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006560 if (*end != *p)
6561 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006562 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006563 vim_free(tofree);
6564 return FAIL;
6565 }
6566 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006567 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006568 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006569 len = (int)(end - tofree);
6570 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006571 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006572 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006573 if (pat == NULL)
6574 return FAIL;
6575 if (generate_PUSHS(cctx, pat) == FAIL)
6576 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6579 return NULL;
6580
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006581 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6583 return NULL;
6584 }
6585
6586 if (generate_instr(cctx, ISN_CATCH) == NULL)
6587 return NULL;
6588
6589 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6590 return NULL;
6591 return p;
6592}
6593
6594 static char_u *
6595compile_finally(char_u *arg, cctx_T *cctx)
6596{
6597 scope_T *scope = cctx->ctx_scope;
6598 garray_T *instr = &cctx->ctx_instr;
6599 isn_T *isn;
6600
6601 // end block scope from :try or :catch
6602 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6603 compile_endblock(cctx);
6604 scope = cctx->ctx_scope;
6605
6606 // Error if not in a :try scope
6607 if (scope == NULL || scope->se_type != TRY_SCOPE)
6608 {
6609 emsg(_(e_finally));
6610 return NULL;
6611 }
6612
6613 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006614 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 if (isn->isn_arg.try.try_finally != 0)
6616 {
6617 emsg(_(e_finally_dup));
6618 return NULL;
6619 }
6620
6621 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006622 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623
Bram Moolenaar585fea72020-04-02 22:33:21 +02006624 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006625 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 {
6627 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006628 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006630 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 }
6632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 // TODO: set index in ts_finally_label jumps
6634
6635 return arg;
6636}
6637
6638 static char_u *
6639compile_endtry(char_u *arg, cctx_T *cctx)
6640{
6641 scope_T *scope = cctx->ctx_scope;
6642 garray_T *instr = &cctx->ctx_instr;
6643 isn_T *isn;
6644
6645 // end block scope from :catch or :finally
6646 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6647 compile_endblock(cctx);
6648 scope = cctx->ctx_scope;
6649
6650 // Error if not in a :try scope
6651 if (scope == NULL || scope->se_type != TRY_SCOPE)
6652 {
6653 if (scope == NULL)
6654 emsg(_(e_no_endtry));
6655 else if (scope->se_type == WHILE_SCOPE)
6656 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006657 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 emsg(_(e_endfor));
6659 else
6660 emsg(_(e_endif));
6661 return NULL;
6662 }
6663
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006664 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6666 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006667 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 return NULL;
6669 }
6670
6671 // Fill in the "end" label in jumps at the end of the blocks, if not done
6672 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006673 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674
6675 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006676 if (isn->isn_arg.try.try_catch == 0)
6677 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 if (isn->isn_arg.try.try_finally == 0)
6679 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006680
6681 if (scope->se_u.se_try.ts_catch_label != 0)
6682 {
6683 // Last catch without match jumps here
6684 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6685 isn->isn_arg.jump.jump_where = instr->ga_len;
6686 }
6687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 compile_endblock(cctx);
6689
6690 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6691 return NULL;
6692 return arg;
6693}
6694
6695/*
6696 * compile "throw {expr}"
6697 */
6698 static char_u *
6699compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6700{
6701 char_u *p = skipwhite(arg);
6702
Bram Moolenaara5565e42020-05-09 15:44:01 +02006703 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 return NULL;
6705 if (may_generate_2STRING(-1, cctx) == FAIL)
6706 return NULL;
6707 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6708 return NULL;
6709
6710 return p;
6711}
6712
6713/*
6714 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006715 * compile "echomsg expr"
6716 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006717 * compile "execute expr"
6718 */
6719 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006720compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006721{
6722 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006723 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006724 int count = 0;
6725
6726 for (;;)
6727 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006728 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006729 return NULL;
6730 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006731 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006732 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006733 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006734 break;
6735 }
6736
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006737 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6738 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6739 else if (cmdidx == CMD_execute)
6740 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6741 else if (cmdidx == CMD_echomsg)
6742 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6743 else
6744 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 return p;
6746}
6747
6748/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006749 * :put r
6750 * :put ={expr}
6751 */
6752 static char_u *
6753compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6754{
6755 char_u *line = arg;
6756 linenr_T lnum;
6757 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006758 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006759
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006760 eap->regname = *line;
6761
6762 if (eap->regname == '=')
6763 {
6764 char_u *p = line + 1;
6765
6766 if (compile_expr0(&p, cctx) == FAIL)
6767 return NULL;
6768 line = p;
6769 }
6770 else if (eap->regname != NUL)
6771 ++line;
6772
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006773 // "errormsg" will not be set because the range is ADDR_LINES.
6774 // TODO: if the range contains something like "$" or "." need to evaluate
6775 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006776 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6777 return NULL;
6778 if (eap->addr_count == 0)
6779 lnum = -1;
6780 else
6781 lnum = eap->line2;
6782 if (above)
6783 --lnum;
6784
6785 generate_PUT(cctx, eap->regname, lnum);
6786 return line;
6787}
6788
6789/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006790 * A command that is not compiled, execute with legacy code.
6791 */
6792 static char_u *
6793compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6794{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006795 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006796 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006797 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006798
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006799 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006800 goto theend;
6801
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006802 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006803 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006804 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006805 int usefilter = FALSE;
6806
6807 has_expr = argt & (EX_XFILE | EX_EXPAND);
6808
6809 // If the command can be followed by a bar, find the bar and truncate
6810 // it, so that the following command can be compiled.
6811 // The '|' is overwritten with a NUL, it is put back below.
6812 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6813 && *eap->arg == '!')
6814 // :w !filter or :r !filter or :r! filter
6815 usefilter = TRUE;
6816 if ((argt & EX_TRLBAR) && !usefilter)
6817 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006818 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006819 separate_nextcmd(eap);
6820 if (eap->nextcmd != NULL)
6821 nextcmd = eap->nextcmd;
6822 }
6823 }
6824
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006825 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6826 {
6827 // expand filename in "syntax include [@group] filename"
6828 has_expr = TRUE;
6829 eap->arg = skipwhite(eap->arg + 7);
6830 if (*eap->arg == '@')
6831 eap->arg = skiptowhite(eap->arg);
6832 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006833
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006834 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006835 {
6836 int count = 0;
6837 char_u *start = skipwhite(line);
6838
6839 // :cmd xxx`=expr1`yyy`=expr2`zzz
6840 // PUSHS ":cmd xxx"
6841 // eval expr1
6842 // PUSHS "yyy"
6843 // eval expr2
6844 // PUSHS "zzz"
6845 // EXECCONCAT 5
6846 for (;;)
6847 {
6848 if (p > start)
6849 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006850 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006851 ++count;
6852 }
6853 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006854 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006855 return NULL;
6856 may_generate_2STRING(-1, cctx);
6857 ++count;
6858 p = skipwhite(p);
6859 if (*p != '`')
6860 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006861 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006862 return NULL;
6863 }
6864 start = p + 1;
6865
6866 p = (char_u *)strstr((char *)start, "`=");
6867 if (p == NULL)
6868 {
6869 if (*skipwhite(start) != NUL)
6870 {
6871 generate_PUSHS(cctx, vim_strsave(start));
6872 ++count;
6873 }
6874 break;
6875 }
6876 }
6877 generate_EXECCONCAT(cctx, count);
6878 }
6879 else
6880 generate_EXEC(cctx, line);
6881
6882theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006883 if (*nextcmd != NUL)
6884 {
6885 // the parser expects a pointer to the bar, put it back
6886 --nextcmd;
6887 *nextcmd = '|';
6888 }
6889
6890 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006891}
6892
6893/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006894 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006895 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006896 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006897 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006898add_def_function(ufunc_T *ufunc)
6899{
6900 dfunc_T *dfunc;
6901
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006902 if (def_functions.ga_len == 0)
6903 {
6904 // The first position is not used, so that a zero uf_dfunc_idx means it
6905 // wasn't set.
6906 if (ga_grow(&def_functions, 1) == FAIL)
6907 return FAIL;
6908 ++def_functions.ga_len;
6909 }
6910
Bram Moolenaar09689a02020-05-09 22:50:08 +02006911 // Add the function to "def_functions".
6912 if (ga_grow(&def_functions, 1) == FAIL)
6913 return FAIL;
6914 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6915 CLEAR_POINTER(dfunc);
6916 dfunc->df_idx = def_functions.ga_len;
6917 ufunc->uf_dfunc_idx = dfunc->df_idx;
6918 dfunc->df_ufunc = ufunc;
6919 ++def_functions.ga_len;
6920 return OK;
6921}
6922
6923/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 * After ex_function() has collected all the function lines: parse and compile
6925 * the lines into instructions.
6926 * Adds the function to "def_functions".
6927 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6928 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006929 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006930 * This can be used recursively through compile_lambda(), which may reallocate
6931 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006932 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006934 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006935compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 char_u *line = NULL;
6938 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 cctx_T cctx;
6941 garray_T *instr;
6942 int called_emsg_before = called_emsg;
6943 int ret = FAIL;
6944 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006945 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006946 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006947 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006948 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006950 // When using a function that was compiled before: Free old instructions.
6951 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006952 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006954 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6955 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006956 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006958 else
6959 {
6960 if (add_def_function(ufunc) == FAIL)
6961 return FAIL;
6962 new_def_function = TRUE;
6963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964
Bram Moolenaar985116a2020-07-12 17:31:09 +02006965 ufunc->uf_def_status = UF_COMPILING;
6966
Bram Moolenaara80faa82020-04-12 19:37:17 +02006967 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968 cctx.ctx_ufunc = ufunc;
6969 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006970 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6972 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6973 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6974 cctx.ctx_type_list = &ufunc->uf_type_list;
6975 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6976 instr = &cctx.ctx_instr;
6977
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006978 // Set the context to the function, it may be compiled when called from
6979 // another script. Set the script version to the most modern one.
6980 // The line number will be set in next_line_from_context().
6981 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6983
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006984 // Make sure error messages are OK.
6985 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6986 if (do_estack_push)
6987 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006988 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006989
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006990 if (ufunc->uf_def_args.ga_len > 0)
6991 {
6992 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006993 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006994 int i;
6995 char_u *arg;
6996 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006997 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006998
6999 // Produce instructions for the default values of optional arguments.
7000 // Store the instruction index in uf_def_arg_idx[] so that we know
7001 // where to start when the function is called, depending on the number
7002 // of arguments.
7003 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7004 if (ufunc->uf_def_arg_idx == NULL)
7005 goto erret;
7006 for (i = 0; i < count; ++i)
7007 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007008 garray_T *stack = &cctx.ctx_type_stack;
7009 type_T *val_type;
7010 int arg_idx = first_def_arg + i;
7011
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007012 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7013 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007014 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007015 goto erret;
7016
7017 // If no type specified use the type of the default value.
7018 // Otherwise check that the default value type matches the
7019 // specified type.
7020 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7021 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007022 {
7023 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007024 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007025 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007026 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7027 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007028 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007029
7030 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007031 goto erret;
7032 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007033 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007034
7035 if (did_set_arg_type)
7036 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007037 }
7038
7039 /*
7040 * Loop over all the lines of the function and generate instructions.
7041 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 for (;;)
7043 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007044 exarg_T ea;
7045 cmdmod_T save_cmdmod;
7046 int starts_with_colon = FALSE;
7047 char_u *cmd;
7048 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007049
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007050 // Bail out on the first error to avoid a flood of errors and report
7051 // the right line number when inside try/catch.
7052 if (emsg_before != called_emsg)
7053 goto erret;
7054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 if (line != NULL && *line == '|')
7056 // the line continues after a '|'
7057 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007058 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007059 && !(*line == '#' && (line == cctx.ctx_line_start
7060 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007062 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 goto erret;
7064 }
7065 else
7066 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007067 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007068 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007069 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007072 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073
Bram Moolenaara80faa82020-04-12 19:37:17 +02007074 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 ea.cmdlinep = &line;
7076 ea.cmd = skipwhite(line);
7077
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007078 // Some things can be recognized by the first character.
7079 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007081 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007082 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007083 if (ea.cmd[1] != '{')
7084 {
7085 line = (char_u *)"";
7086 continue;
7087 }
7088 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007090 case '}':
7091 {
7092 // "}" ends a block scope
7093 scopetype_T stype = cctx.ctx_scope == NULL
7094 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007096 if (stype == BLOCK_SCOPE)
7097 {
7098 compile_endblock(&cctx);
7099 line = ea.cmd;
7100 }
7101 else
7102 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007103 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007104 goto erret;
7105 }
7106 if (line != NULL)
7107 line = skipwhite(ea.cmd + 1);
7108 continue;
7109 }
7110
7111 case '{':
7112 // "{" starts a block scope
7113 // "{'a': 1}->func() is something else
7114 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7115 {
7116 line = compile_block(ea.cmd, &cctx);
7117 continue;
7118 }
7119 break;
7120
7121 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007122 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007123 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124 }
7125
7126 /*
7127 * COMMAND MODIFIERS
7128 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007129 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7131 {
7132 if (errormsg != NULL)
7133 goto erret;
7134 // empty line or comment
7135 line = (char_u *)"";
7136 continue;
7137 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007138 // TODO: use modifiers in the command
7139 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007140 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141
7142 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007143 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007145 {
7146 if (*ea.cmd == '(')
7147 // not for "call()"
7148 ea.cmd = p;
7149 else
7150 ea.cmd = skipwhite(ea.cmd);
7151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007153 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007155 char_u *pskip;
7156
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007157 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007158 // find what follows.
7159 // Skip over "var.member", "var[idx]" and the like.
7160 // Also "&opt = val", "$ENV = val" and "@r = val".
7161 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007162 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007163 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007164 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007166 char_u *var_end;
7167 int oplen;
7168 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169
Bram Moolenaar65821722020-08-02 18:58:54 +02007170 if (ea.cmd[0] == '@')
7171 var_end = ea.cmd + 2;
7172 else
7173 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007174 FNE_CHECK_START | FNE_INCL_BR);
7175 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007176 if (oplen > 0)
7177 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007178 size_t len = p - ea.cmd;
7179
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007180 // Recognize an assignment if we recognize the variable
7181 // name:
7182 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007183 // "local = expr" where "local" is a local var.
7184 // "script = expr" where "script" is a script-local var.
7185 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007186 // "&opt = expr"
7187 // "$ENV = expr"
7188 // "@r = expr"
7189 if (*ea.cmd == '&'
7190 || *ea.cmd == '$'
7191 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007192 || ((len) > 2 && ea.cmd[1] == ':')
7193 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007194 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007195 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007196 || script_var_exists(ea.cmd, len,
7197 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007198 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007199 {
7200 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007201 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007202 goto erret;
7203 continue;
7204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205 }
7206 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007207
7208 if (*ea.cmd == '[')
7209 {
7210 // [var, var] = expr
7211 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7212 if (line == NULL)
7213 goto erret;
7214 if (line != ea.cmd)
7215 continue;
7216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 }
7218
7219 /*
7220 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007221 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007223 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007224 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007225 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007226 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007227 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007228 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007229 if (!starts_with_colon)
7230 {
7231 emsg(_(e_colon_required_before_a_range));
7232 goto erret;
7233 }
7234 if (ends_excmd2(line, ea.cmd))
7235 {
7236 // A range without a command: jump to the line.
7237 // TODO: compile to a more efficient command, possibly
7238 // calling parse_cmd_address().
7239 ea.cmdidx = CMD_SIZE;
7240 line = compile_exec(line, &ea, &cctx);
7241 goto nextline;
7242 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007243 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007244 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007245 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007246 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007247 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248
7249 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7250 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007251 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007252 {
7253 line += STRLEN(line);
7254 continue;
7255 }
7256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007258 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007259 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007260 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007261 iemsg("Command from find_ex_command() not handled");
7262 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 }
7265
Bram Moolenaar3988f642020-08-27 22:43:03 +02007266 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007267 && ea.cmdidx != CMD_elseif
7268 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007269 && ea.cmdidx != CMD_endif
7270 && ea.cmdidx != CMD_endfor
7271 && ea.cmdidx != CMD_endwhile
7272 && ea.cmdidx != CMD_catch
7273 && ea.cmdidx != CMD_finally
7274 && ea.cmdidx != CMD_endtry)
7275 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007276 emsg(_(e_unreachable_code_after_return));
7277 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007278 }
7279
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007280 p = skipwhite(p);
7281 if (ea.cmdidx != CMD_SIZE
7282 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7283 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007284 if (ea.cmdidx >= 0)
7285 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007286 if ((ea.argt & EX_BANG) && *p == '!')
7287 {
7288 ea.forceit = TRUE;
7289 p = skipwhite(p + 1);
7290 }
7291 }
7292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 switch (ea.cmdidx)
7294 {
7295 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007296 ea.arg = p;
7297 line = compile_nested_function(&ea, &cctx);
7298 break;
7299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007301 // TODO: should we allow this, e.g. to declare a global
7302 // function?
7303 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304 goto erret;
7305
7306 case CMD_return:
7307 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007308 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309 break;
7310
7311 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007312 if (get_vim_var_nr(VV_DISALLOW_LET))
7313 {
7314 emsg(_(e_cannot_use_let_in_vim9_script));
7315 break;
7316 }
7317 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007318 case CMD_var:
7319 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320 case CMD_const:
7321 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007322 if (line == p)
7323 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 break;
7325
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007326 case CMD_unlet:
7327 case CMD_unlockvar:
7328 case CMD_lockvar:
7329 line = compile_unletlock(p, &ea, &cctx);
7330 break;
7331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 case CMD_import:
7333 line = compile_import(p, &cctx);
7334 break;
7335
7336 case CMD_if:
7337 line = compile_if(p, &cctx);
7338 break;
7339 case CMD_elseif:
7340 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007341 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 break;
7343 case CMD_else:
7344 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007345 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 break;
7347 case CMD_endif:
7348 line = compile_endif(p, &cctx);
7349 break;
7350
7351 case CMD_while:
7352 line = compile_while(p, &cctx);
7353 break;
7354 case CMD_endwhile:
7355 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007356 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 break;
7358
7359 case CMD_for:
7360 line = compile_for(p, &cctx);
7361 break;
7362 case CMD_endfor:
7363 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007364 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 break;
7366 case CMD_continue:
7367 line = compile_continue(p, &cctx);
7368 break;
7369 case CMD_break:
7370 line = compile_break(p, &cctx);
7371 break;
7372
7373 case CMD_try:
7374 line = compile_try(p, &cctx);
7375 break;
7376 case CMD_catch:
7377 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007378 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 break;
7380 case CMD_finally:
7381 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007382 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 break;
7384 case CMD_endtry:
7385 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007386 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 break;
7388 case CMD_throw:
7389 line = compile_throw(p, &cctx);
7390 break;
7391
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007392 case CMD_eval:
7393 if (compile_expr0(&p, &cctx) == FAIL)
7394 goto erret;
7395
Bram Moolenaar3988f642020-08-27 22:43:03 +02007396 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007397 generate_instr_drop(&cctx, ISN_DROP, 1);
7398
7399 line = skipwhite(p);
7400 break;
7401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007404 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007405 case CMD_echomsg:
7406 case CMD_echoerr:
7407 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007408 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007410 case CMD_put:
7411 ea.cmd = cmd;
7412 line = compile_put(p, &ea, &cctx);
7413 break;
7414
Bram Moolenaar3988f642020-08-27 22:43:03 +02007415 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007416
Bram Moolenaarae616492020-07-28 20:07:27 +02007417 case CMD_append:
7418 case CMD_change:
7419 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007420 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007421 case CMD_xit:
7422 not_in_vim9(&ea);
7423 goto erret;
7424
Bram Moolenaar002262f2020-07-08 17:47:57 +02007425 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007426 if (cctx.ctx_skip != SKIP_YES)
7427 {
7428 semsg(_(e_invalid_command_str), ea.cmd);
7429 goto erret;
7430 }
7431 // We don't check for a next command here.
7432 line = (char_u *)"";
7433 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007436 if (cctx.ctx_skip == SKIP_YES)
7437 {
7438 // We don't check for a next command here.
7439 line = (char_u *)"";
7440 }
7441 else
7442 {
7443 // Not recognized, execute with do_cmdline_cmd().
7444 ea.arg = p;
7445 line = compile_exec(line, &ea, &cctx);
7446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 break;
7448 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007449nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 if (line == NULL)
7451 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007452 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453
7454 if (cctx.ctx_type_stack.ga_len < 0)
7455 {
7456 iemsg("Type stack underflow");
7457 goto erret;
7458 }
7459 }
7460
7461 if (cctx.ctx_scope != NULL)
7462 {
7463 if (cctx.ctx_scope->se_type == IF_SCOPE)
7464 emsg(_(e_endif));
7465 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7466 emsg(_(e_endwhile));
7467 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7468 emsg(_(e_endfor));
7469 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007470 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 goto erret;
7472 }
7473
Bram Moolenaarefd88552020-06-18 20:50:10 +02007474 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 {
7476 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7477 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007478 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 goto erret;
7480 }
7481
7482 // Return zero if there is no return at the end.
7483 generate_PUSHNR(&cctx, 0);
7484 generate_instr(&cctx, ISN_RETURN);
7485 }
7486
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007487 {
7488 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7489 + ufunc->uf_dfunc_idx;
7490 dfunc->df_deleted = FALSE;
7491 dfunc->df_instr = instr->ga_data;
7492 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007493 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007494 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007495 if (cctx.ctx_outer_used)
7496 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007497 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007498 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499
7500 ret = OK;
7501
7502erret:
7503 if (ret == FAIL)
7504 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007505 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7507 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007508
7509 for (idx = 0; idx < instr->ga_len; ++idx)
7510 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007512
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007513 // If using the last entry in the table and it was added above, we
7514 // might as well remove it.
7515 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007516 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007517 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007518 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007519 ufunc->uf_dfunc_idx = 0;
7520 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007521 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007522
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007523 while (cctx.ctx_scope != NULL)
7524 drop_scope(&cctx);
7525
Bram Moolenaar20431c92020-03-20 18:39:46 +01007526 // Don't execute this function body.
7527 ga_clear_strings(&ufunc->uf_lines);
7528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 if (errormsg != NULL)
7530 emsg(errormsg);
7531 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007532 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 }
7534
7535 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007536 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007537 if (do_estack_push)
7538 estack_pop();
7539
Bram Moolenaar20431c92020-03-20 18:39:46 +01007540 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007541 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007543 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007544}
7545
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007546 void
7547set_function_type(ufunc_T *ufunc)
7548{
7549 int varargs = ufunc->uf_va_name != NULL;
7550 int argcount = ufunc->uf_args.ga_len;
7551
7552 // Create a type for the function, with the return type and any
7553 // argument types.
7554 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7555 // The type is included in "tt_args".
7556 if (argcount > 0 || varargs)
7557 {
7558 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7559 argcount, &ufunc->uf_type_list);
7560 // Add argument types to the function type.
7561 if (func_type_add_arg_types(ufunc->uf_func_type,
7562 argcount + varargs,
7563 &ufunc->uf_type_list) == FAIL)
7564 return;
7565 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7566 ufunc->uf_func_type->tt_min_argcount =
7567 argcount - ufunc->uf_def_args.ga_len;
7568 if (ufunc->uf_arg_types == NULL)
7569 {
7570 int i;
7571
7572 // lambda does not have argument types.
7573 for (i = 0; i < argcount; ++i)
7574 ufunc->uf_func_type->tt_args[i] = &t_any;
7575 }
7576 else
7577 mch_memmove(ufunc->uf_func_type->tt_args,
7578 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7579 if (varargs)
7580 {
7581 ufunc->uf_func_type->tt_args[argcount] =
7582 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7583 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7584 }
7585 }
7586 else
7587 // No arguments, can use a predefined type.
7588 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7589 argcount, &ufunc->uf_type_list);
7590}
7591
7592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593/*
7594 * Delete an instruction, free what it contains.
7595 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007596 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597delete_instr(isn_T *isn)
7598{
7599 switch (isn->isn_type)
7600 {
7601 case ISN_EXEC:
7602 case ISN_LOADENV:
7603 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007604 case ISN_LOADB:
7605 case ISN_LOADW:
7606 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007608 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609 case ISN_PUSHEXC:
7610 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007611 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007613 case ISN_STOREB:
7614 case ISN_STOREW:
7615 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007616 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 vim_free(isn->isn_arg.string);
7618 break;
7619
7620 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007621 case ISN_STORES:
7622 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 break;
7624
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007625 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007626 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007627 vim_free(isn->isn_arg.unlet.ul_name);
7628 break;
7629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630 case ISN_STOREOPT:
7631 vim_free(isn->isn_arg.storeopt.so_name);
7632 break;
7633
7634 case ISN_PUSHBLOB: // push blob isn_arg.blob
7635 blob_unref(isn->isn_arg.blob);
7636 break;
7637
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007638 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007639#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007640 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007641#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007642 break;
7643
7644 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007646 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007647#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007648 break;
7649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650 case ISN_UCALL:
7651 vim_free(isn->isn_arg.ufunc.cuf_name);
7652 break;
7653
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007654 case ISN_FUNCREF:
7655 {
7656 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7657 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007658
7659 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7660 func_ptr_unref(dfunc->df_ufunc);
7661 }
7662 break;
7663
7664 case ISN_DCALL:
7665 {
7666 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7667 + isn->isn_arg.dfunc.cdf_idx;
7668
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007669 if (dfunc->df_ufunc != NULL
7670 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007671 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007672 }
7673 break;
7674
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007675 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007676 {
7677 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7678 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7679
7680 if (ufunc != NULL)
7681 {
7682 // Clear uf_dfunc_idx so that the function is deleted.
7683 clear_def_function(ufunc);
7684 ufunc->uf_dfunc_idx = 0;
7685 func_ptr_unref(ufunc);
7686 }
7687
7688 vim_free(lambda);
7689 vim_free(isn->isn_arg.newfunc.nf_global);
7690 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007691 break;
7692
Bram Moolenaar5e654232020-09-16 15:22:00 +02007693 case ISN_CHECKTYPE:
7694 free_type(isn->isn_arg.type.ct_type);
7695 break;
7696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 case ISN_2BOOL:
7698 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007699 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 case ISN_ADDBLOB:
7701 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007702 case ISN_ANYINDEX:
7703 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007705 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007707 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709 case ISN_COMPAREANY:
7710 case ISN_COMPAREBLOB:
7711 case ISN_COMPAREBOOL:
7712 case ISN_COMPAREDICT:
7713 case ISN_COMPAREFLOAT:
7714 case ISN_COMPAREFUNC:
7715 case ISN_COMPARELIST:
7716 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717 case ISN_COMPARESPECIAL:
7718 case ISN_COMPARESTRING:
7719 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007720 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007721 case ISN_DROP:
7722 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007723 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007724 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007726 case ISN_EXECCONCAT:
7727 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007729 case ISN_GETITEM:
7730 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007731 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007732 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007733 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007735 case ISN_LOADBDICT:
7736 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007737 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007739 case ISN_LOADSCRIPT:
7740 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007742 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007743 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007744 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 case ISN_NEGATENR:
7746 case ISN_NEWDICT:
7747 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007749 case ISN_OPFLOAT:
7750 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007752 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007753 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 case ISN_PUSHF:
7755 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007757 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007759 case ISN_SHUFFLE:
7760 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007762 case ISN_STOREDICT:
7763 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007764 case ISN_STORENR:
7765 case ISN_STOREOUTER:
7766 case ISN_STOREREG:
7767 case ISN_STORESCRIPT:
7768 case ISN_STOREV:
7769 case ISN_STRINDEX:
7770 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 case ISN_THROW:
7772 case ISN_TRY:
7773 // nothing allocated
7774 break;
7775 }
7776}
7777
7778/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007779 * Free all instructions for "dfunc".
7780 */
7781 static void
7782delete_def_function_contents(dfunc_T *dfunc)
7783{
7784 int idx;
7785
7786 ga_clear(&dfunc->df_def_args_isn);
7787
7788 if (dfunc->df_instr != NULL)
7789 {
7790 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7791 delete_instr(dfunc->df_instr + idx);
7792 VIM_CLEAR(dfunc->df_instr);
7793 }
7794
7795 dfunc->df_deleted = TRUE;
7796}
7797
7798/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007799 * When a user function is deleted, clear the contents of any associated def
7800 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801 */
7802 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007803clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007804{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007805 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 {
7807 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7808 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809
Bram Moolenaar20431c92020-03-20 18:39:46 +01007810 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007811 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 }
7813}
7814
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007815/*
7816 * Used when a user function is about to be deleted: remove the pointer to it.
7817 * The entry in def_functions is then unused.
7818 */
7819 void
7820unlink_def_function(ufunc_T *ufunc)
7821{
7822 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7823
7824 dfunc->df_ufunc = NULL;
7825}
7826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007828/*
7829 * Free all functions defined with ":def".
7830 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007831 void
7832free_def_functions(void)
7833{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007834 int idx;
7835
7836 for (idx = 0; idx < def_functions.ga_len; ++idx)
7837 {
7838 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7839
7840 delete_def_function_contents(dfunc);
7841 }
7842
7843 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844}
7845#endif
7846
7847
7848#endif // FEAT_EVAL