blob: 421957708632d88446ba60f1966902b3dd8a1e2c [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 Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
145 int ctx_silent; // set when ISN_SILENT was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200151 * Lookup variable "name" in the local scope and return it.
152 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200154 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155lookup_local(char_u *name, size_t len, cctx_T *cctx)
156{
157 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100160 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200161 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162
163 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
165 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 if (STRNCMP(name, lvar->lv_name, len) == 0
168 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200169 {
170 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200171 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174
175 // Find local in outer function scope.
176 if (cctx->ctx_outer != NULL)
177 {
178 lvar = lookup_local(name, len, cctx->ctx_outer);
179 if (lvar != NULL)
180 {
181 // TODO: are there situations we should not mark the outer scope as
182 // used?
183 cctx->ctx_outer_used = TRUE;
184 lvar->lv_from_outer = TRUE;
185 return lvar;
186 }
187 }
188
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190}
191
192/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200193 * Lookup an argument in the current function and an enclosing function.
194 * Returns the argument index in "idxp"
195 * Returns the argument type in "type"
196 * Sets "gen_load_outer" to TRUE if found in outer scope.
197 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 */
199 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200200arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200201 char_u *name,
202 size_t len,
203 int *idxp,
204 type_T **type,
205 int *gen_load_outer,
206 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207{
208 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200209 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100211 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200212 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
214 {
215 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
216
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
218 {
219 if (idxp != NULL)
220 {
221 // Arguments are located above the frame pointer. One further
222 // if there is a vararg argument
223 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
224 + STACK_FRAME_SIZE)
225 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
226
227 if (cctx->ctx_ufunc->uf_arg_types != NULL)
228 *type = cctx->ctx_ufunc->uf_arg_types[idx];
229 else
230 *type = &t_any;
231 }
232 return OK;
233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200236 va_name = cctx->ctx_ufunc->uf_va_name;
237 if (va_name != NULL
238 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
239 {
240 if (idxp != NULL)
241 {
242 // varargs is always the last argument
243 *idxp = -STACK_FRAME_SIZE - 1;
244 *type = cctx->ctx_ufunc->uf_va_type;
245 }
246 return OK;
247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200249 if (cctx->ctx_outer != NULL)
250 {
251 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200252 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 == OK)
254 {
255 *gen_load_outer = TRUE;
256 return OK;
257 }
258 }
259
260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261}
262
263/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200264 * Lookup a script-local variable in the current script, possibly defined in a
265 * block that contains the function "cctx->ctx_ufunc".
266 * "cctx" is NULL at the script level.
267 * if "len" is <= 0 "name" must be NUL terminated.
268 * Return NULL when not found.
269 */
270 static sallvar_T *
271find_script_var(char_u *name, size_t len, cctx_T *cctx)
272{
273 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
274 hashitem_T *hi;
275 int cc;
276 sallvar_T *sav;
277 ufunc_T *ufunc;
278
279 // Find the list of all script variables with the right name.
280 if (len > 0)
281 {
282 cc = name[len];
283 name[len] = NUL;
284 }
285 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
286 if (len > 0)
287 name[len] = cc;
288 if (HASHITEM_EMPTY(hi))
289 return NULL;
290
291 sav = HI2SAV(hi);
292 if (sav->sav_block_id == 0 || cctx == NULL)
293 // variable defined in the script scope or not in a function.
294 return sav;
295
296 // Go over the variables with this name and find one that was visible
297 // from the function.
298 ufunc = cctx->ctx_ufunc;
299 while (sav != NULL)
300 {
301 int idx;
302
303 // Go over the blocks that this function was defined in. If the
304 // variable block ID matches it was visible to the function.
305 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
306 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
307 return sav;
308 sav = sav->sav_next;
309 }
310
311 return NULL;
312}
313
314/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200315 * Returnd TRUE if the script context is Vim9 script.
316 */
317 static int
318script_is_vim9()
319{
320 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
321}
322
323/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200324 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200325 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
326 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200327 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 * Returns OK or FAIL.
329 */
330 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200331script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200335 if (current_sctx.sc_sid <= 0)
336 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 is_vim9_script = script_is_vim9();
338 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200339 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200340 if (is_vim9_script)
341 {
342 // Check script variables that were visible where the function was
343 // defined.
344 if (find_script_var(name, len, cctx) != NULL)
345 return OK;
346 }
347 else
348 {
349 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
350 dictitem_T *di;
351 int cc;
352
353 // Check script variables that are currently visible
354 cc = name[len];
355 name[len] = NUL;
356 di = find_var_in_ht(ht, 0, name, TRUE);
357 name[len] = cc;
358 if (di != NULL)
359 return OK;
360 }
361
362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363}
364
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100365/*
366 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200367 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200368 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100369 * Return FAIL and give an error if it defined.
370 */
371 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200372check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100373{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 int c = p[len];
375 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200376
377 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200378 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200380 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200381 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100384 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200385 // A local or script-local function can shadow a global function.
386 if (ufunc == NULL || !func_is_global(ufunc)
387 || (p[0] == 'g' && p[1] == ':'))
388 {
389 p[len] = c;
390 semsg(_(e_name_already_defined_str), p);
391 return FAIL;
392 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200394 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100395 return OK;
396}
397
Bram Moolenaar65b95452020-07-19 14:03:09 +0200398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399/////////////////////////////////////////////////////////////////////
400// Following generate_ functions expect the caller to call ga_grow().
401
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200402#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
403#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/*
406 * Generate an instruction without arguments.
407 * Returns a pointer to the new instruction, NULL if failed.
408 */
409 static isn_T *
410generate_instr(cctx_T *cctx, isntype_T isn_type)
411{
412 garray_T *instr = &cctx->ctx_instr;
413 isn_T *isn;
414
Bram Moolenaar080457c2020-03-03 21:53:32 +0100415 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 if (ga_grow(instr, 1) == FAIL)
417 return NULL;
418 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
419 isn->isn_type = isn_type;
420 isn->isn_lnum = cctx->ctx_lnum + 1;
421 ++instr->ga_len;
422
423 return isn;
424}
425
426/*
427 * Generate an instruction without arguments.
428 * "drop" will be removed from the stack.
429 * Returns a pointer to the new instruction, NULL if failed.
430 */
431 static isn_T *
432generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
433{
434 garray_T *stack = &cctx->ctx_type_stack;
435
Bram Moolenaar080457c2020-03-03 21:53:32 +0100436 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 stack->ga_len -= drop;
438 return generate_instr(cctx, isn_type);
439}
440
441/*
442 * Generate instruction "isn_type" and put "type" on the type stack.
443 */
444 static isn_T *
445generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
446{
447 isn_T *isn;
448 garray_T *stack = &cctx->ctx_type_stack;
449
450 if ((isn = generate_instr(cctx, isn_type)) == NULL)
451 return NULL;
452
453 if (ga_grow(stack, 1) == FAIL)
454 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200455 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 ++stack->ga_len;
457
458 return isn;
459}
460
461/*
462 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200463 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 */
465 static int
466may_generate_2STRING(int offset, cctx_T *cctx)
467{
468 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 garray_T *stack = &cctx->ctx_type_stack;
471 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
472
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200473 switch ((*type)->tt_type)
474 {
475 // nothing to be done
476 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200478 // conversion possible
479 case VAR_SPECIAL:
480 case VAR_BOOL:
481 case VAR_NUMBER:
482 case VAR_FLOAT:
483 break;
484
485 // conversion possible (with runtime check)
486 case VAR_ANY:
487 case VAR_UNKNOWN:
488 isntype = ISN_2STRING_ANY;
489 break;
490
491 // conversion not possible
492 case VAR_VOID:
493 case VAR_BLOB:
494 case VAR_FUNC:
495 case VAR_PARTIAL:
496 case VAR_LIST:
497 case VAR_DICT:
498 case VAR_JOB:
499 case VAR_CHANNEL:
500 to_string_error((*type)->tt_type);
501 return FAIL;
502 }
503
504 *type = &t_string;
505 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507 isn->isn_arg.number = offset;
508
509 return OK;
510}
511
512 static int
513check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
514{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200515 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200517 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 {
519 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200520 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200522 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return FAIL;
524 }
525 return OK;
526}
527
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200528 static int
529generate_add_instr(
530 cctx_T *cctx,
531 vartype_T vartype,
532 type_T *type1,
533 type_T *type2)
534{
535 isn_T *isn = generate_instr_drop(cctx,
536 vartype == VAR_NUMBER ? ISN_OPNR
537 : vartype == VAR_LIST ? ISN_ADDLIST
538 : vartype == VAR_BLOB ? ISN_ADDBLOB
539#ifdef FEAT_FLOAT
540 : vartype == VAR_FLOAT ? ISN_OPFLOAT
541#endif
542 : ISN_OPANY, 1);
543
544 if (vartype != VAR_LIST && vartype != VAR_BLOB
545 && type1->tt_type != VAR_ANY
546 && type2->tt_type != VAR_ANY
547 && check_number_or_float(
548 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
549 return FAIL;
550
551 if (isn != NULL)
552 isn->isn_arg.op.op_type = EXPR_ADD;
553 return isn == NULL ? FAIL : OK;
554}
555
556/*
557 * Get the type to use for an instruction for an operation on "type1" and
558 * "type2". If they are matching use a type-specific instruction. Otherwise
559 * fall back to runtime type checking.
560 */
561 static vartype_T
562operator_type(type_T *type1, type_T *type2)
563{
564 if (type1->tt_type == type2->tt_type
565 && (type1->tt_type == VAR_NUMBER
566 || type1->tt_type == VAR_LIST
567#ifdef FEAT_FLOAT
568 || type1->tt_type == VAR_FLOAT
569#endif
570 || type1->tt_type == VAR_BLOB))
571 return type1->tt_type;
572 return VAR_ANY;
573}
574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575/*
576 * Generate an instruction with two arguments. The instruction depends on the
577 * type of the arguments.
578 */
579 static int
580generate_two_op(cctx_T *cctx, char_u *op)
581{
582 garray_T *stack = &cctx->ctx_type_stack;
583 type_T *type1;
584 type_T *type2;
585 vartype_T vartype;
586 isn_T *isn;
587
Bram Moolenaar080457c2020-03-03 21:53:32 +0100588 RETURN_OK_IF_SKIP(cctx);
589
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200590 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
592 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
595 switch (*op)
596 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200597 case '+':
598 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 break;
601
602 case '-':
603 case '*':
604 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
605 op) == FAIL)
606 return FAIL;
607 if (vartype == VAR_NUMBER)
608 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
609#ifdef FEAT_FLOAT
610 else if (vartype == VAR_FLOAT)
611 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
612#endif
613 else
614 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
615 if (isn != NULL)
616 isn->isn_arg.op.op_type = *op == '*'
617 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
618 break;
619
Bram Moolenaar4c683752020-04-05 21:38:23 +0200620 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200622 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 && type2->tt_type != VAR_NUMBER))
624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200625 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return FAIL;
627 }
628 isn = generate_instr_drop(cctx,
629 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = EXPR_REM;
632 break;
633 }
634
635 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200636 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 {
638 type_T *type = &t_any;
639
640#ifdef FEAT_FLOAT
641 // float+number and number+float results in float
642 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
643 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
644 type = &t_float;
645#endif
646 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
647 }
648
649 return OK;
650}
651
652/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200653 * Get the instruction to use for comparing "type1" with "type2"
654 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200656 static isntype_T
657get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658{
659 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
Bram Moolenaar4c683752020-04-05 21:38:23 +0200661 if (type1 == VAR_UNKNOWN)
662 type1 = VAR_ANY;
663 if (type2 == VAR_UNKNOWN)
664 type2 = VAR_ANY;
665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 if (type1 == type2)
667 {
668 switch (type1)
669 {
670 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
671 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
672 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
673 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
674 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
675 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
676 case VAR_LIST: isntype = ISN_COMPARELIST; break;
677 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
678 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 default: isntype = ISN_COMPAREANY; break;
680 }
681 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
684 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
685 isntype = ISN_COMPAREANY;
686
687 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
688 && (isntype == ISN_COMPAREBOOL
689 || isntype == ISN_COMPARESPECIAL
690 || isntype == ISN_COMPARENR
691 || isntype == ISN_COMPAREFLOAT))
692 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200693 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200695 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 }
697 if (isntype == ISN_DROP
698 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
699 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
700 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
701 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
702 && exptype != EXPR_IS && exptype != EXPR_ISNOT
703 && (type1 == VAR_BLOB || type2 == VAR_BLOB
704 || type1 == VAR_LIST || type2 == VAR_LIST))))
705 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200706 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200708 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return isntype;
711}
712
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200713 int
714check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
715{
716 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
717 return FAIL;
718 return OK;
719}
720
Bram Moolenaara5565e42020-05-09 15:44:01 +0200721/*
722 * Generate an ISN_COMPARE* instruction with a boolean result.
723 */
724 static int
725generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
726{
727 isntype_T isntype;
728 isn_T *isn;
729 garray_T *stack = &cctx->ctx_type_stack;
730 vartype_T type1;
731 vartype_T type2;
732
733 RETURN_OK_IF_SKIP(cctx);
734
735 // Get the known type of the two items on the stack. If they are matching
736 // use a type-specific instruction. Otherwise fall back to runtime type
737 // checking.
738 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
739 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
740 isntype = get_compare_isn(exptype, type1, type2);
741 if (isntype == ISN_DROP)
742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if ((isn = generate_instr(cctx, isntype)) == NULL)
745 return FAIL;
746 isn->isn_arg.op.op_type = exptype;
747 isn->isn_arg.op.op_ic = ic;
748
749 // takes two arguments, puts one bool back
750 if (stack->ga_len >= 2)
751 {
752 --stack->ga_len;
753 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
754 }
755
756 return OK;
757}
758
759/*
760 * Generate an ISN_2BOOL instruction.
761 */
762 static int
763generate_2BOOL(cctx_T *cctx, int invert)
764{
765 isn_T *isn;
766 garray_T *stack = &cctx->ctx_type_stack;
767
Bram Moolenaar080457c2020-03-03 21:53:32 +0100768 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
770 return FAIL;
771 isn->isn_arg.number = invert;
772
773 // type becomes bool
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
775
776 return OK;
777}
778
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200779/*
780 * Generate an ISN_COND2BOOL instruction.
781 */
782 static int
783generate_COND2BOOL(cctx_T *cctx)
784{
785 isn_T *isn;
786 garray_T *stack = &cctx->ctx_type_stack;
787
788 RETURN_OK_IF_SKIP(cctx);
789 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
790 return FAIL;
791
792 // type becomes bool
793 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
794
795 return OK;
796}
797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200799generate_TYPECHECK(
800 cctx_T *cctx,
801 type_T *expected,
802 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803{
804 isn_T *isn;
805 garray_T *stack = &cctx->ctx_type_stack;
806
Bram Moolenaar080457c2020-03-03 21:53:32 +0100807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
809 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200810 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 isn->isn_arg.type.ct_off = offset;
812
Bram Moolenaar5e654232020-09-16 15:22:00 +0200813 // type becomes expected
814 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
816 return OK;
817}
818
819/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200820 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
821 * used. Return FALSE if the types will never match.
822 */
823 static int
824use_typecheck(type_T *actual, type_T *expected)
825{
826 if (actual->tt_type == VAR_ANY
827 || actual->tt_type == VAR_UNKNOWN
828 || (actual->tt_type == VAR_FUNC
829 && (expected->tt_type == VAR_FUNC
830 || expected->tt_type == VAR_PARTIAL)
831 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
832 return TRUE;
833 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
834 && actual->tt_type == expected->tt_type)
835 // This takes care of a nested list or dict.
836 return use_typecheck(actual->tt_member, expected->tt_member);
837 return FALSE;
838}
839
840/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200841 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200842 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200843 * - "actual" is a type that can be "expected" type: add a runtime check; or
844 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200845 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
846 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200847 */
848 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200849need_type(
850 type_T *actual,
851 type_T *expected,
852 int offset,
853 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200854 int silent,
855 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200857 if (expected == &t_bool && actual != &t_bool
858 && (actual->tt_flags & TTFLAG_BOOL_OK))
859 {
860 // Using "0", "1" or the result of an expression with "&&" or "||" as a
861 // boolean is OK but requires a conversion.
862 generate_2BOOL(cctx, FALSE);
863 return OK;
864 }
865
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200866 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200867 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200868
869 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200870 // If it's a constant a runtime check makes no sense.
871 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873 generate_TYPECHECK(cctx, expected, offset);
874 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200875 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876
877 if (!silent)
878 type_mismatch(expected, actual);
879 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880}
881
882/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 * Generate an ISN_PUSHNR instruction.
884 */
885 static int
886generate_PUSHNR(cctx_T *cctx, varnumber_T number)
887{
888 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200889 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaar080457c2020-03-03 21:53:32 +0100891 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
893 return FAIL;
894 isn->isn_arg.number = number;
895
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200896 if (number == 0 || number == 1)
897 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200898 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200899
900 // A 0 or 1 number can also be used as a bool.
901 if (type != NULL)
902 {
903 type->tt_type = VAR_NUMBER;
904 type->tt_flags = TTFLAG_BOOL_OK;
905 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
906 }
907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 return OK;
909}
910
911/*
912 * Generate an ISN_PUSHBOOL instruction.
913 */
914 static int
915generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
916{
917 isn_T *isn;
918
Bram Moolenaar080457c2020-03-03 21:53:32 +0100919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
921 return FAIL;
922 isn->isn_arg.number = number;
923
924 return OK;
925}
926
927/*
928 * Generate an ISN_PUSHSPEC instruction.
929 */
930 static int
931generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
932{
933 isn_T *isn;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
937 return FAIL;
938 isn->isn_arg.number = number;
939
940 return OK;
941}
942
943#ifdef FEAT_FLOAT
944/*
945 * Generate an ISN_PUSHF instruction.
946 */
947 static int
948generate_PUSHF(cctx_T *cctx, float_T fnumber)
949{
950 isn_T *isn;
951
Bram Moolenaar080457c2020-03-03 21:53:32 +0100952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
954 return FAIL;
955 isn->isn_arg.fnumber = fnumber;
956
957 return OK;
958}
959#endif
960
961/*
962 * Generate an ISN_PUSHS instruction.
963 * Consumes "str".
964 */
965 static int
966generate_PUSHS(cctx_T *cctx, char_u *str)
967{
968 isn_T *isn;
969
Bram Moolenaar080457c2020-03-03 21:53:32 +0100970 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
972 return FAIL;
973 isn->isn_arg.string = str;
974
975 return OK;
976}
977
978/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100979 * Generate an ISN_PUSHCHANNEL instruction.
980 * Consumes "channel".
981 */
982 static int
983generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
984{
985 isn_T *isn;
986
Bram Moolenaar080457c2020-03-03 21:53:32 +0100987 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100988 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
989 return FAIL;
990 isn->isn_arg.channel = channel;
991
992 return OK;
993}
994
995/*
996 * Generate an ISN_PUSHJOB instruction.
997 * Consumes "job".
998 */
999 static int
1000generate_PUSHJOB(cctx_T *cctx, job_T *job)
1001{
1002 isn_T *isn;
1003
Bram Moolenaar080457c2020-03-03 21:53:32 +01001004 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001005 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001006 return FAIL;
1007 isn->isn_arg.job = job;
1008
1009 return OK;
1010}
1011
1012/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 * Generate an ISN_PUSHBLOB instruction.
1014 * Consumes "blob".
1015 */
1016 static int
1017generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1018{
1019 isn_T *isn;
1020
Bram Moolenaar080457c2020-03-03 21:53:32 +01001021 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.blob = blob;
1025
1026 return OK;
1027}
1028
1029/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001030 * Generate an ISN_PUSHFUNC instruction with name "name".
1031 * Consumes "name".
1032 */
1033 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001034generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001035{
1036 isn_T *isn;
1037
Bram Moolenaar080457c2020-03-03 21:53:32 +01001038 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001039 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001041 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001042
1043 return OK;
1044}
1045
1046/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001047 * Generate an ISN_GETITEM instruction with "index".
1048 */
1049 static int
1050generate_GETITEM(cctx_T *cctx, int index)
1051{
1052 isn_T *isn;
1053 garray_T *stack = &cctx->ctx_type_stack;
1054 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1055 type_T *item_type = &t_any;
1056
1057 RETURN_OK_IF_SKIP(cctx);
1058
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001059 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001061 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001062 emsg(_(e_listreq));
1063 return FAIL;
1064 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001065 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001066 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1067 return FAIL;
1068 isn->isn_arg.number = index;
1069
1070 // add the item type to the type stack
1071 if (ga_grow(stack, 1) == FAIL)
1072 return FAIL;
1073 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1074 ++stack->ga_len;
1075 return OK;
1076}
1077
1078/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001079 * Generate an ISN_SLICE instruction with "count".
1080 */
1081 static int
1082generate_SLICE(cctx_T *cctx, int count)
1083{
1084 isn_T *isn;
1085
1086 RETURN_OK_IF_SKIP(cctx);
1087 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1088 return FAIL;
1089 isn->isn_arg.number = count;
1090 return OK;
1091}
1092
1093/*
1094 * Generate an ISN_CHECKLEN instruction with "min_len".
1095 */
1096 static int
1097generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1098{
1099 isn_T *isn;
1100
1101 RETURN_OK_IF_SKIP(cctx);
1102
1103 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.checklen.cl_min_len = min_len;
1106 isn->isn_arg.checklen.cl_more_OK = more_OK;
1107
1108 return OK;
1109}
1110
1111/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 * Generate an ISN_STORE instruction.
1113 */
1114 static int
1115generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1116{
1117 isn_T *isn;
1118
Bram Moolenaar080457c2020-03-03 21:53:32 +01001119 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1121 return FAIL;
1122 if (name != NULL)
1123 isn->isn_arg.string = vim_strsave(name);
1124 else
1125 isn->isn_arg.number = idx;
1126
1127 return OK;
1128}
1129
1130/*
1131 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1132 */
1133 static int
1134generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1140 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001141 isn->isn_arg.storenr.stnr_idx = idx;
1142 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143
1144 return OK;
1145}
1146
1147/*
1148 * Generate an ISN_STOREOPT instruction
1149 */
1150 static int
1151generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001156 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 return FAIL;
1158 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1159 isn->isn_arg.storeopt.so_flags = opt_flags;
1160
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_LOAD or similar instruction.
1166 */
1167 static int
1168generate_LOAD(
1169 cctx_T *cctx,
1170 isntype_T isn_type,
1171 int idx,
1172 char_u *name,
1173 type_T *type)
1174{
1175 isn_T *isn;
1176
Bram Moolenaar080457c2020-03-03 21:53:32 +01001177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1179 return FAIL;
1180 if (name != NULL)
1181 isn->isn_arg.string = vim_strsave(name);
1182 else
1183 isn->isn_arg.number = idx;
1184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001189 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001190 */
1191 static int
1192generate_LOADV(
1193 cctx_T *cctx,
1194 char_u *name,
1195 int error)
1196{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001197 int di_flags;
1198 int vidx = find_vim_var(name, &di_flags);
1199 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001202 if (vidx < 0)
1203 {
1204 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001205 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001206 return FAIL;
1207 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001208 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001210 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001211}
1212
1213/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001214 * Generate an ISN_UNLET instruction.
1215 */
1216 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001217generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001218{
1219 isn_T *isn;
1220
1221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001222 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001223 return FAIL;
1224 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1225 isn->isn_arg.unlet.ul_forceit = forceit;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001231 * Generate an ISN_LOCKCONST instruction.
1232 */
1233 static int
1234generate_LOCKCONST(cctx_T *cctx)
1235{
1236 isn_T *isn;
1237
1238 RETURN_OK_IF_SKIP(cctx);
1239 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1240 return FAIL;
1241 return OK;
1242}
1243
1244/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 * Generate an ISN_LOADS instruction.
1246 */
1247 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001252 int sid,
1253 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254{
1255 isn_T *isn;
1256
Bram Moolenaar080457c2020-03-03 21:53:32 +01001257 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001258 if (isn_type == ISN_LOADS)
1259 isn = generate_instr_type(cctx, isn_type, type);
1260 else
1261 isn = generate_instr_drop(cctx, isn_type, 1);
1262 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1265 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266
1267 return OK;
1268}
1269
1270/*
1271 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1272 */
1273 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 cctx_T *cctx,
1276 isntype_T isn_type,
1277 int sid,
1278 int idx,
1279 type_T *type)
1280{
1281 isn_T *isn;
1282
Bram Moolenaar080457c2020-03-03 21:53:32 +01001283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 if (isn_type == ISN_LOADSCRIPT)
1285 isn = generate_instr_type(cctx, isn_type, type);
1286 else
1287 isn = generate_instr_drop(cctx, isn_type, 1);
1288 if (isn == NULL)
1289 return FAIL;
1290 isn->isn_arg.script.script_sid = sid;
1291 isn->isn_arg.script.script_idx = idx;
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_NEWLIST instruction.
1297 */
1298 static int
1299generate_NEWLIST(cctx_T *cctx, int count)
1300{
1301 isn_T *isn;
1302 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 type_T *type;
1304 type_T *member;
1305
Bram Moolenaar080457c2020-03-03 21:53:32 +01001306 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.number = count;
1310
Bram Moolenaar127542b2020-08-09 17:22:04 +02001311 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001312 if (count == 0)
1313 member = &t_void;
1314 else
1315 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001316 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1317 cctx->ctx_type_list);
1318 type = get_list_type(member, cctx->ctx_type_list);
1319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 // drop the value types
1321 stack->ga_len -= count;
1322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 // add the list type to the type stack
1324 if (ga_grow(stack, 1) == FAIL)
1325 return FAIL;
1326 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1327 ++stack->ga_len;
1328
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_NEWDICT instruction.
1334 */
1335 static int
1336generate_NEWDICT(cctx_T *cctx, int count)
1337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 type_T *type;
1341 type_T *member;
1342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1345 return FAIL;
1346 isn->isn_arg.number = count;
1347
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001348 if (count == 0)
1349 member = &t_void;
1350 else
1351 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001352 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1353 cctx->ctx_type_list);
1354 type = get_dict_type(member, cctx->ctx_type_list);
1355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 // drop the key and value types
1357 stack->ga_len -= 2 * count;
1358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 // add the dict type to the type stack
1360 if (ga_grow(stack, 1) == FAIL)
1361 return FAIL;
1362 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1363 ++stack->ga_len;
1364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_FUNCREF instruction.
1370 */
1371 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001372generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373{
1374 isn_T *isn;
1375 garray_T *stack = &cctx->ctx_type_stack;
1376
Bram Moolenaar080457c2020-03-03 21:53:32 +01001377 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1379 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001380 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001381 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382
1383 if (ga_grow(stack, 1) == FAIL)
1384 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001385 ((type_T **)stack->ga_data)[stack->ga_len] =
1386 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 ++stack->ga_len;
1388
1389 return OK;
1390}
1391
1392/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001393 * Generate an ISN_NEWFUNC instruction.
1394 */
1395 static int
1396generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1397{
1398 isn_T *isn;
1399 char_u *name;
1400
1401 RETURN_OK_IF_SKIP(cctx);
1402 name = vim_strsave(lambda_name);
1403 if (name == NULL)
1404 return FAIL;
1405 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1406 return FAIL;
1407 isn->isn_arg.newfunc.nf_lambda = name;
1408 isn->isn_arg.newfunc.nf_global = func_name;
1409
1410 return OK;
1411}
1412
1413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 * Generate an ISN_JUMP instruction.
1415 */
1416 static int
1417generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1418{
1419 isn_T *isn;
1420 garray_T *stack = &cctx->ctx_type_stack;
1421
Bram Moolenaar080457c2020-03-03 21:53:32 +01001422 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1424 return FAIL;
1425 isn->isn_arg.jump.jump_when = when;
1426 isn->isn_arg.jump.jump_where = where;
1427
1428 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1429 --stack->ga_len;
1430
1431 return OK;
1432}
1433
1434 static int
1435generate_FOR(cctx_T *cctx, int loop_idx)
1436{
1437 isn_T *isn;
1438 garray_T *stack = &cctx->ctx_type_stack;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.forloop.for_idx = loop_idx;
1444
1445 if (ga_grow(stack, 1) == FAIL)
1446 return FAIL;
1447 // type doesn't matter, will be stored next
1448 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1449 ++stack->ga_len;
1450
1451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001456 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 * Return FAIL if the number of arguments is wrong.
1458 */
1459 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001460generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461{
1462 isn_T *isn;
1463 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001464 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001465 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466
Bram Moolenaar080457c2020-03-03 21:53:32 +01001467 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001468 argoff = check_internal_func(func_idx, argcount);
1469 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 return FAIL;
1471
Bram Moolenaar389df252020-07-09 21:20:47 +02001472 if (method_call && argoff > 1)
1473 {
1474 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1475 return FAIL;
1476 isn->isn_arg.shuffle.shfl_item = argcount;
1477 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1478 }
1479
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001480 if (argcount > 0)
1481 {
1482 // Check the types of the arguments.
1483 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1484 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001486 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.bfunc.cbf_idx = func_idx;
1491 isn->isn_arg.bfunc.cbf_argcount = argcount;
1492
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 // Drop the argument types and push the return type.
1494 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 if (ga_grow(stack, 1) == FAIL)
1496 return FAIL;
1497 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001498 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001499 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500
1501 return OK;
1502}
1503
1504/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001505 * Generate an ISN_LISTAPPEND instruction. Works like add().
1506 * Argument count is already checked.
1507 */
1508 static int
1509generate_LISTAPPEND(cctx_T *cctx)
1510{
1511 garray_T *stack = &cctx->ctx_type_stack;
1512 type_T *list_type;
1513 type_T *item_type;
1514 type_T *expected;
1515
1516 // Caller already checked that list_type is a list.
1517 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1518 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1519 expected = list_type->tt_member;
1520 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1521 return FAIL;
1522
1523 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1524 return FAIL;
1525
1526 --stack->ga_len; // drop the argument
1527 return OK;
1528}
1529
1530/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001531 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1532 * Argument count is already checked.
1533 */
1534 static int
1535generate_BLOBAPPEND(cctx_T *cctx)
1536{
1537 garray_T *stack = &cctx->ctx_type_stack;
1538 type_T *item_type;
1539
1540 // Caller already checked that blob_type is a blob.
1541 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1542 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1543 return FAIL;
1544
1545 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1546 return FAIL;
1547
1548 --stack->ga_len; // drop the argument
1549 return OK;
1550}
1551
1552/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 * Generate an ISN_DCALL or ISN_UCALL instruction.
1554 * Return FAIL if the number of arguments is wrong.
1555 */
1556 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001557generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558{
1559 isn_T *isn;
1560 garray_T *stack = &cctx->ctx_type_stack;
1561 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001562 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
Bram Moolenaar080457c2020-03-03 21:53:32 +01001564 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 if (argcount > regular_args && !has_varargs(ufunc))
1566 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001567 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 return FAIL;
1569 }
1570 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1571 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001572 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 return FAIL;
1574 }
1575
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001576 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001577 {
1578 int i;
1579
1580 for (i = 0; i < argcount; ++i)
1581 {
1582 type_T *expected;
1583 type_T *actual;
1584
1585 if (i < regular_args)
1586 {
1587 if (ufunc->uf_arg_types == NULL)
1588 continue;
1589 expected = ufunc->uf_arg_types[i];
1590 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001591 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1592 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001593 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001594 else
1595 expected = ufunc->uf_va_type->tt_member;
1596 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001597 if (need_type(actual, expected, -argcount + i, cctx,
1598 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001599 {
1600 arg_type_mismatch(expected, actual, i + 1);
1601 return FAIL;
1602 }
1603 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001604 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001605 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1606 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001607 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001608 }
1609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001611 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001612 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001614 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 {
1616 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1617 isn->isn_arg.dfunc.cdf_argcount = argcount;
1618 }
1619 else
1620 {
1621 // A user function may be deleted and redefined later, can't use the
1622 // ufunc pointer, need to look it up again at runtime.
1623 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1624 isn->isn_arg.ufunc.cuf_argcount = argcount;
1625 }
1626
1627 stack->ga_len -= argcount; // drop the arguments
1628 if (ga_grow(stack, 1) == FAIL)
1629 return FAIL;
1630 // add return value
1631 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1632 ++stack->ga_len;
1633
1634 return OK;
1635}
1636
1637/*
1638 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1639 */
1640 static int
1641generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1642{
1643 isn_T *isn;
1644 garray_T *stack = &cctx->ctx_type_stack;
1645
Bram Moolenaar080457c2020-03-03 21:53:32 +01001646 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1648 return FAIL;
1649 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1650 isn->isn_arg.ufunc.cuf_argcount = argcount;
1651
1652 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001653 if (ga_grow(stack, 1) == FAIL)
1654 return FAIL;
1655 // add return value
1656 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1657 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658
1659 return OK;
1660}
1661
1662/*
1663 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001664 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 */
1666 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001667generate_PCALL(
1668 cctx_T *cctx,
1669 int argcount,
1670 char_u *name,
1671 type_T *type,
1672 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673{
1674 isn_T *isn;
1675 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001676 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
Bram Moolenaar080457c2020-03-03 21:53:32 +01001678 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001679
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001680 if (type->tt_type == VAR_ANY)
1681 ret_type = &t_any;
1682 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001683 {
1684 if (type->tt_argcount != -1)
1685 {
1686 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1687
1688 if (argcount < type->tt_min_argcount - varargs)
1689 {
1690 semsg(_(e_toofewarg), "[reference]");
1691 return FAIL;
1692 }
1693 if (!varargs && argcount > type->tt_argcount)
1694 {
1695 semsg(_(e_toomanyarg), "[reference]");
1696 return FAIL;
1697 }
1698 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001699 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001700 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001701 else
1702 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001703 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001704 return FAIL;
1705 }
1706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.pfunc.cpf_top = at_top;
1710 isn->isn_arg.pfunc.cpf_argcount = argcount;
1711
1712 stack->ga_len -= argcount; // drop the arguments
1713
1714 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001715 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001717 // If partial is above the arguments it must be cleared and replaced with
1718 // the return value.
1719 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1720 return FAIL;
1721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 return OK;
1723}
1724
1725/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001726 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 */
1728 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001729generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730{
1731 isn_T *isn;
1732 garray_T *stack = &cctx->ctx_type_stack;
1733 type_T *type;
1734
Bram Moolenaar080457c2020-03-03 21:53:32 +01001735 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001736 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001738 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001740 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001742 if (type->tt_type != VAR_DICT && type != &t_any)
1743 {
1744 emsg(_(e_dictreq));
1745 return FAIL;
1746 }
1747 // change dict type to dict member type
1748 if (type->tt_type == VAR_DICT)
1749 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
1751 return OK;
1752}
1753
1754/*
1755 * Generate an ISN_ECHO instruction.
1756 */
1757 static int
1758generate_ECHO(cctx_T *cctx, int with_white, int count)
1759{
1760 isn_T *isn;
1761
Bram Moolenaar080457c2020-03-03 21:53:32 +01001762 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1764 return FAIL;
1765 isn->isn_arg.echo.echo_with_white = with_white;
1766 isn->isn_arg.echo.echo_count = count;
1767
1768 return OK;
1769}
1770
Bram Moolenaarad39c092020-02-26 18:23:43 +01001771/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001772 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001773 */
1774 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001775generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001776{
1777 isn_T *isn;
1778
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001779 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001780 return FAIL;
1781 isn->isn_arg.number = count;
1782
1783 return OK;
1784}
1785
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001786/*
1787 * Generate an ISN_PUT instruction.
1788 */
1789 static int
1790generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1791{
1792 isn_T *isn;
1793
1794 RETURN_OK_IF_SKIP(cctx);
1795 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1796 return FAIL;
1797 isn->isn_arg.put.put_regname = regname;
1798 isn->isn_arg.put.put_lnum = lnum;
1799 return OK;
1800}
1801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 static int
1803generate_EXEC(cctx_T *cctx, char_u *line)
1804{
1805 isn_T *isn;
1806
Bram Moolenaar080457c2020-03-03 21:53:32 +01001807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1809 return FAIL;
1810 isn->isn_arg.string = vim_strsave(line);
1811 return OK;
1812}
1813
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001814 static int
1815generate_EXECCONCAT(cctx_T *cctx, int count)
1816{
1817 isn_T *isn;
1818
1819 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1820 return FAIL;
1821 isn->isn_arg.number = count;
1822 return OK;
1823}
1824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825/*
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001826 * Generate any instructions for side effects of "cmdmod".
1827 */
1828 static int
1829generate_cmdmods(cctx_T *cctx)
1830{
1831 isn_T *isn;
1832
1833 // TODO: use more modifiers in the command
1834 if (cmdmod.msg_silent || cmdmod.emsg_silent)
1835 {
1836 if ((isn = generate_instr(cctx, ISN_SILENT)) == NULL)
1837 return FAIL;
1838 isn->isn_arg.number = cmdmod.emsg_silent;
1839 cctx->ctx_silent = cmdmod.emsg_silent ? 2 : 1;
1840 }
1841 return OK;
1842}
1843
1844 static int
1845generate_restore_cmdmods(cctx_T *cctx)
1846{
1847 isn_T *isn;
1848
1849 if (cctx->ctx_silent > 0)
1850 {
1851 if ((isn = generate_instr(cctx, ISN_UNSILENT)) == NULL)
1852 return FAIL;
1853 isn->isn_arg.number = cctx->ctx_silent == 2;
1854 cctx->ctx_silent = 0;
1855 }
1856 return OK;
1857}
1858
1859/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001861 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001863 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001864reserve_local(
1865 cctx_T *cctx,
1866 char_u *name,
1867 size_t len,
1868 int isConst,
1869 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 lvar_T *lvar;
1872
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001873 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001875 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001876 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 }
1878
1879 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001880 return NULL;
1881 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001882 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001884 // Every local variable uses the next entry on the stack. We could re-use
1885 // the last ones when leaving a scope, but then variables used in a closure
1886 // might get overwritten. To keep things simple do not re-use stack
1887 // entries. This is less efficient, but memory is cheap these days.
1888 lvar->lv_idx = cctx->ctx_locals_count++;
1889
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001890 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 lvar->lv_const = isConst;
1892 lvar->lv_type = type;
1893
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001894 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895}
1896
1897/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001898 * Remove local variables above "new_top".
1899 */
1900 static void
1901unwind_locals(cctx_T *cctx, int new_top)
1902{
1903 if (cctx->ctx_locals.ga_len > new_top)
1904 {
1905 int idx;
1906 lvar_T *lvar;
1907
1908 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1909 {
1910 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1911 vim_free(lvar->lv_name);
1912 }
1913 }
1914 cctx->ctx_locals.ga_len = new_top;
1915}
1916
1917/*
1918 * Free all local variables.
1919 */
1920 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001921free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001922{
1923 unwind_locals(cctx, 0);
1924 ga_clear(&cctx->ctx_locals);
1925}
1926
1927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 * Find "name" in script-local items of script "sid".
1929 * Returns the index in "sn_var_vals" if found.
1930 * If found but not in "sn_var_vals" returns -1.
1931 * If not found returns -2.
1932 */
1933 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001934get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935{
1936 hashtab_T *ht;
1937 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001938 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001939 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 int idx;
1941
Bram Moolenaare3d46852020-08-29 13:39:17 +02001942 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001944 if (sid == current_sctx.sc_sid)
1945 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02001946 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001947
1948 if (sav == NULL)
1949 return -2;
1950 idx = sav->sav_var_vals_idx;
1951 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1952 if (check_writable && sv->sv_const)
1953 semsg(_(e_readonlyvar), name);
1954 return idx;
1955 }
1956
1957 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 ht = &SCRIPT_VARS(sid);
1959 di = find_var_in_ht(ht, 0, name, TRUE);
1960 if (di == NULL)
1961 return -2;
1962
1963 // Now find the svar_T index in sn_var_vals.
1964 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1965 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001966 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 if (sv->sv_tv == &di->di_tv)
1968 {
1969 if (check_writable && sv->sv_const)
1970 semsg(_(e_readonlyvar), name);
1971 return idx;
1972 }
1973 }
1974 return -1;
1975}
1976
1977/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001978 * Find "name" in imported items of the current script or in "cctx" if not
1979 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 */
1981 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001982find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 int idx;
1985
Bram Moolenaare3d46852020-08-29 13:39:17 +02001986 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001987 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 if (cctx != NULL)
1989 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1990 {
1991 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1992 + idx;
1993
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001994 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1995 : STRLEN(import->imp_name) == len
1996 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 return import;
1998 }
1999
Bram Moolenaarefa94442020-08-08 22:16:00 +02002000 return find_imported_in_script(name, len, current_sctx.sc_sid);
2001}
2002
2003 imported_T *
2004find_imported_in_script(char_u *name, size_t len, int sid)
2005{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002006 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002007 int idx;
2008
Bram Moolenaare3d46852020-08-29 13:39:17 +02002009 if (!SCRIPT_ID_VALID(sid))
2010 return NULL;
2011 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2013 {
2014 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2015
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002016 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2017 : STRLEN(import->imp_name) == len
2018 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 return import;
2020 }
2021 return NULL;
2022}
2023
2024/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002025 * Free all imported variables.
2026 */
2027 static void
2028free_imported(cctx_T *cctx)
2029{
2030 int idx;
2031
2032 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2033 {
2034 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2035
2036 vim_free(import->imp_name);
2037 }
2038 ga_clear(&cctx->ctx_imports);
2039}
2040
2041/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002042 * Return TRUE if "p" points at a "#" but not at "#{".
2043 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002044 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002045vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002046{
2047 return p[0] == '#' && p[1] != '{';
2048}
2049
2050/*
2051 * Return a pointer to the next line that isn't empty or only contains a
2052 * comment. Skips over white space.
2053 * Returns NULL if there is none.
2054 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002055 char_u *
2056peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002057{
2058 int lnum = cctx->ctx_lnum;
2059
2060 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2061 {
2062 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002063 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002064
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002065 // ignore NULLs inserted for continuation lines
2066 if (line != NULL)
2067 {
2068 p = skipwhite(line);
2069 if (*p != NUL && !vim9_comment_start(p))
2070 return p;
2071 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002072 }
2073 return NULL;
2074}
2075
2076/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002077 * Called when checking for a following operator at "arg". When the rest of
2078 * the line is empty or only a comment, peek the next line. If there is a next
2079 * line return a pointer to it and set "nextp".
2080 * Otherwise skip over white space.
2081 */
2082 static char_u *
2083may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2084{
2085 char_u *p = skipwhite(arg);
2086
2087 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002088 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002089 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002090 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002091 if (*nextp != NULL)
2092 return *nextp;
2093 }
2094 return p;
2095}
2096
2097/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002098 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002099 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002100 * Returns NULL when at the end.
2101 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002102 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002103next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002104{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002105 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002106
2107 do
2108 {
2109 ++cctx->ctx_lnum;
2110 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002111 {
2112 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002113 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002114 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002115 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002116 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002117 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002118 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002119 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002120 return line;
2121}
2122
2123/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002124 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002125 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002126 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2127 */
2128 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002129may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002130{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002131 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002132 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002133 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002134
2135 if (next == NULL)
2136 return FAIL;
2137 *arg = skipwhite(next);
2138 }
2139 return OK;
2140}
2141
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002142/*
2143 * Idem, and give an error when failed.
2144 */
2145 static int
2146may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2147{
2148 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002150 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002151 return FAIL;
2152 }
2153 return OK;
2154}
2155
2156
Bram Moolenaara5565e42020-05-09 15:44:01 +02002157// Structure passed between the compile_expr* functions to keep track of
2158// constants that have been parsed but for which no code was produced yet. If
2159// possible expressions on these constants are applied at compile time. If
2160// that is not possible, the code to push the constants needs to be generated
2161// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002162// Using 50 should be more than enough of 5 levels of ().
2163#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002164typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002165 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002166 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002167 int pp_is_const; // all generated code was constants, used for a
2168 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002169} ppconst_T;
2170
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002171static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002172static int compile_expr0(char_u **arg, cctx_T *cctx);
2173static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2174
Bram Moolenaara5565e42020-05-09 15:44:01 +02002175/*
2176 * Generate a PUSH instruction for "tv".
2177 * "tv" will be consumed or cleared.
2178 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2179 */
2180 static int
2181generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2182{
2183 if (tv != NULL)
2184 {
2185 switch (tv->v_type)
2186 {
2187 case VAR_UNKNOWN:
2188 break;
2189 case VAR_BOOL:
2190 generate_PUSHBOOL(cctx, tv->vval.v_number);
2191 break;
2192 case VAR_SPECIAL:
2193 generate_PUSHSPEC(cctx, tv->vval.v_number);
2194 break;
2195 case VAR_NUMBER:
2196 generate_PUSHNR(cctx, tv->vval.v_number);
2197 break;
2198#ifdef FEAT_FLOAT
2199 case VAR_FLOAT:
2200 generate_PUSHF(cctx, tv->vval.v_float);
2201 break;
2202#endif
2203 case VAR_BLOB:
2204 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2205 tv->vval.v_blob = NULL;
2206 break;
2207 case VAR_STRING:
2208 generate_PUSHS(cctx, tv->vval.v_string);
2209 tv->vval.v_string = NULL;
2210 break;
2211 default:
2212 iemsg("constant type not supported");
2213 clear_tv(tv);
2214 return FAIL;
2215 }
2216 tv->v_type = VAR_UNKNOWN;
2217 }
2218 return OK;
2219}
2220
2221/*
2222 * Generate code for any ppconst entries.
2223 */
2224 static int
2225generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2226{
2227 int i;
2228 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002229 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002230
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002231 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002232 for (i = 0; i < ppconst->pp_used; ++i)
2233 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2234 ret = FAIL;
2235 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002236 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002237 return ret;
2238}
2239
2240/*
2241 * Clear ppconst constants. Used when failing.
2242 */
2243 static void
2244clear_ppconst(ppconst_T *ppconst)
2245{
2246 int i;
2247
2248 for (i = 0; i < ppconst->pp_used; ++i)
2249 clear_tv(&ppconst->pp_tv[i]);
2250 ppconst->pp_used = 0;
2251}
2252
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002253/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002254 * Generate an instruction to load script-local variable "name", without the
2255 * leading "s:".
2256 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 */
2258 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002259compile_load_scriptvar(
2260 cctx_T *cctx,
2261 char_u *name, // variable NUL terminated
2262 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002263 char_u **end, // end of variable
2264 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002266 scriptitem_T *si;
2267 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 imported_T *import;
2269
Bram Moolenaare3d46852020-08-29 13:39:17 +02002270 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2271 return FAIL;
2272 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002273 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002274 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002276 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002277 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2278 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 }
2280 if (idx >= 0)
2281 {
2282 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2283
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002284 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 current_sctx.sc_sid, idx, sv->sv_type);
2286 return OK;
2287 }
2288
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002289 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 if (import != NULL)
2291 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002292 if (import->imp_all)
2293 {
2294 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002295 char_u *exp_name;
2296 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002297 ufunc_T *ufunc;
2298 type_T *type;
2299
2300 // Used "import * as Name", need to lookup the member.
2301 if (*p != '.')
2302 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002303 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002304 return FAIL;
2305 }
2306 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002307 if (VIM_ISWHITE(*p))
2308 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002309 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002310 return FAIL;
2311 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002312
Bram Moolenaar1c991142020-07-04 13:15:31 +02002313 // isolate one name
2314 exp_name = p;
2315 while (eval_isnamec(*p))
2316 ++p;
2317 cc = *p;
2318 *p = NUL;
2319
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002320 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002321 *p = cc;
2322 p = skipwhite(p);
2323
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002324 // TODO: what if it is a function?
2325 if (idx < 0)
2326 return FAIL;
2327 *end = p;
2328
2329 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2330 import->imp_sid,
2331 idx,
2332 type);
2333 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002334 else if (import->imp_funcname != NULL)
2335 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002336 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002337 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2338 import->imp_sid,
2339 import->imp_var_vals_idx,
2340 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 return OK;
2342 }
2343
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002344 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002345 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 return FAIL;
2347}
2348
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002349 static int
2350generate_funcref(cctx_T *cctx, char_u *name)
2351{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002352 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002353
2354 if (ufunc == NULL)
2355 return FAIL;
2356
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002357 // Need to compile any default values to get the argument types.
2358 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2359 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2360 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002361 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002362}
2363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364/*
2365 * Compile a variable name into a load instruction.
2366 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002367 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 * When "error" is FALSE do not give an error when not found.
2369 */
2370 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002371compile_load(
2372 char_u **arg,
2373 char_u *end_arg,
2374 cctx_T *cctx,
2375 int is_expr,
2376 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377{
2378 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002379 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002380 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002382 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383
2384 if (*(*arg + 1) == ':')
2385 {
2386 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002387 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002388 {
2389 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002391 switch (**arg)
2392 {
2393 case 'g': isn_type = ISN_LOADGDICT; break;
2394 case 'w': isn_type = ISN_LOADWDICT; break;
2395 case 't': isn_type = ISN_LOADTDICT; break;
2396 case 'b': isn_type = ISN_LOADBDICT; break;
2397 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002398 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002399 goto theend;
2400 }
2401 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2402 goto theend;
2403 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 else
2406 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002407 isntype_T isn_type = ISN_DROP;
2408
2409 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2410 if (name == NULL)
2411 return FAIL;
2412
2413 switch (**arg)
2414 {
2415 case 'v': res = generate_LOADV(cctx, name, error);
2416 break;
2417 case 's': res = compile_load_scriptvar(cctx, name,
2418 NULL, NULL, error);
2419 break;
2420 case 'g': isn_type = ISN_LOADG; break;
2421 case 'w': isn_type = ISN_LOADW; break;
2422 case 't': isn_type = ISN_LOADT; break;
2423 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002424 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002425 goto theend;
2426 }
2427 if (isn_type != ISN_DROP)
2428 {
2429 // Global, Buffer-local, Window-local and Tabpage-local
2430 // variables can be defined later, thus we don't check if it
2431 // exists, give error at runtime.
2432 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
2435 }
2436 else
2437 {
2438 size_t len = end - *arg;
2439 int idx;
2440 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002441 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442
2443 name = vim_strnsave(*arg, end - *arg);
2444 if (name == NULL)
2445 return FAIL;
2446
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002447 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002449 if (!gen_load_outer)
2450 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 }
2452 else
2453 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002454 lvar_T *lvar = lookup_local(*arg, len, cctx);
2455
2456 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002458 type = lvar->lv_type;
2459 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002460 if (lvar->lv_from_outer)
2461 gen_load_outer = TRUE;
2462 else
2463 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 }
2465 else
2466 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002467 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002468 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002469 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002470 || find_imported(name, 0, cctx) != NULL)
2471 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002472
Bram Moolenaar0f769812020-09-12 18:32:34 +02002473 // When evaluating an expression and the name starts with an
2474 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002475 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002476 if (res == FAIL && is_expr
2477 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002478 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 }
2480 }
2481 if (gen_load)
2482 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002483 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002484 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002485 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002486 cctx->ctx_outer_used = TRUE;
2487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 }
2489
2490 *arg = end;
2491
2492theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002493 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002494 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 vim_free(name);
2496 return res;
2497}
2498
2499/*
2500 * Compile the argument expressions.
2501 * "arg" points to just after the "(" and is advanced to after the ")"
2502 */
2503 static int
2504compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2505{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002506 char_u *p = *arg;
2507 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002508 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509
Bram Moolenaare6085c52020-04-12 20:19:16 +02002510 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002512 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2513 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002514 if (*p == ')')
2515 {
2516 *arg = p + 1;
2517 return OK;
2518 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002519 if (must_end)
2520 {
2521 semsg(_(e_missing_comma_before_argument_str), p);
2522 return FAIL;
2523 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002524
Bram Moolenaara5565e42020-05-09 15:44:01 +02002525 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 return FAIL;
2527 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002528
2529 if (*p != ',' && *skipwhite(p) == ',')
2530 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002531 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002532 p = skipwhite(p);
2533 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002535 {
2536 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002537 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002538 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002539 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002540 else
2541 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002542 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002543 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002545failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002546 emsg(_(e_missing_close));
2547 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548}
2549
2550/*
2551 * Compile a function call: name(arg1, arg2)
2552 * "arg" points to "name", "arg + varlen" to the "(".
2553 * "argcount_init" is 1 for "value->method()"
2554 * Instructions:
2555 * EVAL arg1
2556 * EVAL arg2
2557 * BCALL / DCALL / UCALL
2558 */
2559 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002560compile_call(
2561 char_u **arg,
2562 size_t varlen,
2563 cctx_T *cctx,
2564 ppconst_T *ppconst,
2565 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566{
2567 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002568 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 int argcount = argcount_init;
2570 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002571 char_u fname_buf[FLEN_FIXED + 1];
2572 char_u *tofree = NULL;
2573 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002575 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002576 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577
Bram Moolenaara5565e42020-05-09 15:44:01 +02002578 // we can evaluate "has('name')" at compile time
2579 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2580 {
2581 char_u *s = skipwhite(*arg + varlen + 1);
2582 typval_T argvars[2];
2583
2584 argvars[0].v_type = VAR_UNKNOWN;
2585 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002586 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002587 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002588 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002589 s = skipwhite(s);
2590 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2591 {
2592 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2593
2594 *arg = s + 1;
2595 argvars[1].v_type = VAR_UNKNOWN;
2596 tv->v_type = VAR_NUMBER;
2597 tv->vval.v_number = 0;
2598 f_has(argvars, tv);
2599 clear_tv(&argvars[0]);
2600 ++ppconst->pp_used;
2601 return OK;
2602 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002603 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002604 }
2605
2606 if (generate_ppconst(cctx, ppconst) == FAIL)
2607 return FAIL;
2608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 if (varlen >= sizeof(namebuf))
2610 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002611 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 return FAIL;
2613 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002614 vim_strncpy(namebuf, *arg, varlen);
2615 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616
2617 *arg = skipwhite(*arg + varlen + 1);
2618 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002619 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620
Bram Moolenaara1773442020-08-12 15:21:22 +02002621 is_autoload = vim_strchr(name, '#') != NULL;
2622 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 {
2624 int idx;
2625
2626 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002627 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002629 {
2630 if (STRCMP(name, "add") == 0 && argcount == 2)
2631 {
2632 garray_T *stack = &cctx->ctx_type_stack;
2633 type_T *type = ((type_T **)stack->ga_data)[
2634 stack->ga_len - 2];
2635
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002636 if (type->tt_type == VAR_LIST)
2637 {
2638 // inline "add(list, item)" so that the type can be checked
2639 res = generate_LISTAPPEND(cctx);
2640 idx = -1;
2641 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002642 else if (type->tt_type == VAR_BLOB)
2643 {
2644 // inline "add(blob, nr)" so that the type can be checked
2645 res = generate_BLOBAPPEND(cctx);
2646 idx = -1;
2647 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002648 }
2649
2650 if (idx >= 0)
2651 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2652 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002653 else
2654 semsg(_(e_unknownfunc), namebuf);
2655 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 }
2657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002659 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002660 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002661 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002662 {
2663 res = generate_CALL(cctx, ufunc, argcount);
2664 goto theend;
2665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666
2667 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002668 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002669 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002671 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002672 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002673 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002674 garray_T *stack = &cctx->ctx_type_stack;
2675 type_T *type;
2676
2677 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2678 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002679 goto theend;
2680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681
Bram Moolenaar0f769812020-09-12 18:32:34 +02002682 // If we can find a global function by name generate the right call.
2683 if (ufunc != NULL)
2684 {
2685 res = generate_CALL(cctx, ufunc, argcount);
2686 goto theend;
2687 }
2688
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002689 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002690 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002691 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002692 res = generate_UCALL(cctx, name, argcount);
2693 else
2694 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002695
2696theend:
2697 vim_free(tofree);
2698 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699}
2700
2701// like NAMESPACE_CHAR but with 'a' and 'l'.
2702#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2703
2704/*
2705 * Find the end of a variable or function name. Unlike find_name_end() this
2706 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002707 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 * Return a pointer to just after the name. Equal to "arg" if there is no
2709 * valid name.
2710 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002711 static char_u *
2712to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713{
2714 char_u *p;
2715
2716 // Quick check for valid starting character.
2717 if (!eval_isnamec1(*arg))
2718 return arg;
2719
2720 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2721 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2722 // and can be used in slice "[n:]".
2723 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002724 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2726 break;
2727 return p;
2728}
2729
2730/*
2731 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002732 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 */
2734 char_u *
2735to_name_const_end(char_u *arg)
2736{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002737 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 typval_T rettv;
2739
2740 if (p == arg && *arg == '[')
2741 {
2742
2743 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002744 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 p = arg;
2746 }
2747 else if (p == arg && *arg == '#' && arg[1] == '{')
2748 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002749 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002751 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 p = arg;
2753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754
2755 return p;
2756}
2757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758/*
2759 * parse a list: [expr, expr]
2760 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002761 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 */
2763 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002764compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765{
2766 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002767 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002769 int is_const;
2770 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002772 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002774 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002775 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002776 semsg(_(e_list_end), *arg);
2777 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002778 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002779 if (*p == ',')
2780 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002781 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002782 return FAIL;
2783 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002784 if (*p == ']')
2785 {
2786 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002787 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002788 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002789 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002790 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002791 if (!is_const)
2792 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 ++count;
2794 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002795 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002797 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2798 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002799 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002800 return FAIL;
2801 }
2802 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002803 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 p = skipwhite(p);
2805 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002806 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002808 ppconst->pp_is_const = is_all_const;
2809 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810}
2811
2812/*
2813 * parse a lambda: {arg, arg -> expr}
2814 * "*arg" points to the '{'.
2815 */
2816 static int
2817compile_lambda(char_u **arg, cctx_T *cctx)
2818{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 typval_T rettv;
2820 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002821 evalarg_T evalarg;
2822
2823 CLEAR_FIELD(evalarg);
2824 evalarg.eval_flags = EVAL_EVALUATE;
2825 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826
2827 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002828 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002829 {
2830 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002832 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002835 ++ufunc->uf_refcount;
2836 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002837 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838
2839 // The function will have one line: "return {expr}".
2840 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002841 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002843 clear_evalarg(&evalarg, NULL);
2844
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002845 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002846 {
2847 // The return type will now be known.
2848 set_function_type(ufunc);
2849
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002850 // The function reference count will be 1. When the ISN_FUNCREF
2851 // instruction is deleted the reference count is decremented and the
2852 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002853 return generate_FUNCREF(cctx, ufunc);
2854 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002855
2856 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 return FAIL;
2858}
2859
2860/*
2861 * Compile a lamda call: expr->{lambda}(args)
2862 * "arg" points to the "{".
2863 */
2864 static int
2865compile_lambda_call(char_u **arg, cctx_T *cctx)
2866{
2867 ufunc_T *ufunc;
2868 typval_T rettv;
2869 int argcount = 1;
2870 int ret = FAIL;
2871
2872 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002873 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 return FAIL;
2875
2876 if (**arg != '(')
2877 {
2878 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002879 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 else
2881 semsg(_(e_missing_paren), "lambda");
2882 clear_tv(&rettv);
2883 return FAIL;
2884 }
2885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 ufunc = rettv.vval.v_partial->pt_func;
2887 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002888 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002889 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002890
Bram Moolenaara05e5242020-09-19 18:19:19 +02002891 // The function will have one line: "return {expr}". Compile it into
2892 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002893 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894
2895 // compile the arguments
2896 *arg = skipwhite(*arg + 1);
2897 if (compile_arguments(arg, cctx, &argcount) == OK)
2898 // call the compiled function
2899 ret = generate_CALL(cctx, ufunc, argcount);
2900
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002901 if (ret == FAIL)
2902 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 return ret;
2904}
2905
2906/*
2907 * parse a dict: {'key': val} or #{key: val}
2908 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002909 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 */
2911 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002912compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913{
2914 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002915 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 int count = 0;
2917 dict_T *d = dict_alloc();
2918 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002919 char_u *whitep = *arg;
2920 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002921 int is_const;
2922 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923
2924 if (d == NULL)
2925 return FAIL;
2926 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002927 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 {
2929 char_u *key = NULL;
2930
Bram Moolenaar23c55272020-06-21 16:58:13 +02002931 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002932 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002933 *arg = NULL;
2934 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002935 }
2936
2937 if (**arg == '}')
2938 break;
2939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 if (literal)
2941 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002942 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943
Bram Moolenaar2c330432020-04-13 14:41:35 +02002944 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002946 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 return FAIL;
2948 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002949 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 if (generate_PUSHS(cctx, key) == FAIL)
2951 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002952 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 }
2954 else
2955 {
2956 isn_T *isn;
2957
Bram Moolenaara5565e42020-05-09 15:44:01 +02002958 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2961 if (isn->isn_type == ISN_PUSHS)
2962 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002963 else
2964 {
2965 type_T *keytype = ((type_T **)stack->ga_data)
2966 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002967 if (need_type(keytype, &t_string, -1, cctx,
2968 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002969 return FAIL;
2970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 }
2972
2973 // Check for duplicate keys, if using string keys.
2974 if (key != NULL)
2975 {
2976 item = dict_find(d, key, -1);
2977 if (item != NULL)
2978 {
2979 semsg(_(e_duplicate_key), key);
2980 goto failret;
2981 }
2982 item = dictitem_alloc(key);
2983 if (item != NULL)
2984 {
2985 item->di_tv.v_type = VAR_UNKNOWN;
2986 item->di_tv.v_lock = 0;
2987 if (dict_add(d, item) == FAIL)
2988 dictitem_free(item);
2989 }
2990 }
2991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 if (**arg != ':')
2993 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002994 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002995 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002996 else
2997 semsg(_(e_missing_dict_colon), *arg);
2998 return FAIL;
2999 }
3000 whitep = *arg + 1;
3001 if (!IS_WHITE_OR_NUL(*whitep))
3002 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003003 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 return FAIL;
3005 }
3006
3007 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003008 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003009 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003010 *arg = NULL;
3011 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003012 }
3013
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003014 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003016 if (!is_const)
3017 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 ++count;
3019
Bram Moolenaar2c330432020-04-13 14:41:35 +02003020 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003021 *arg = skipwhite(*arg);
3022 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003023 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003024 *arg = NULL;
3025 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 if (**arg == '}')
3028 break;
3029 if (**arg != ',')
3030 {
3031 semsg(_(e_missing_dict_comma), *arg);
3032 goto failret;
3033 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003034 if (IS_WHITE_OR_NUL(*whitep))
3035 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003036 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003037 return FAIL;
3038 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003039 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003040 if (!IS_WHITE_OR_NUL(*whitep))
3041 {
3042 semsg(_(e_white_space_required_after_str), ",");
3043 return FAIL;
3044 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 *arg = skipwhite(*arg + 1);
3046 }
3047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 *arg = *arg + 1;
3049
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003050 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003051 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003052 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003053 *arg += STRLEN(*arg);
3054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003056 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 return generate_NEWDICT(cctx, count);
3058
3059failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003060 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003061 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003062 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003063 *arg = (char_u *)"";
3064 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 dict_unref(d);
3066 return FAIL;
3067}
3068
3069/*
3070 * Compile "&option".
3071 */
3072 static int
3073compile_get_option(char_u **arg, cctx_T *cctx)
3074{
3075 typval_T rettv;
3076 char_u *start = *arg;
3077 int ret;
3078
3079 // parse the option and get the current value to get the type.
3080 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003081 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 if (ret == OK)
3083 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003084 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 char_u *name = vim_strnsave(start, *arg - start);
3086 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3087
3088 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3089 vim_free(name);
3090 }
3091 clear_tv(&rettv);
3092
3093 return ret;
3094}
3095
3096/*
3097 * Compile "$VAR".
3098 */
3099 static int
3100compile_get_env(char_u **arg, cctx_T *cctx)
3101{
3102 char_u *start = *arg;
3103 int len;
3104 int ret;
3105 char_u *name;
3106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 ++*arg;
3108 len = get_env_len(arg);
3109 if (len == 0)
3110 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003111 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 return FAIL;
3113 }
3114
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003115 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 name = vim_strnsave(start, len + 1);
3117 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3118 vim_free(name);
3119 return ret;
3120}
3121
3122/*
3123 * Compile "@r".
3124 */
3125 static int
3126compile_get_register(char_u **arg, cctx_T *cctx)
3127{
3128 int ret;
3129
3130 ++*arg;
3131 if (**arg == NUL)
3132 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003133 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 return FAIL;
3135 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003136 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
3138 emsg_invreg(**arg);
3139 return FAIL;
3140 }
3141 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3142 ++*arg;
3143 return ret;
3144}
3145
3146/*
3147 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003148 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 */
3150 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003151apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003153 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
3155 // this works from end to start
3156 while (p > start)
3157 {
3158 --p;
3159 if (*p == '-' || *p == '+')
3160 {
3161 // only '-' has an effect, for '+' we only check the type
3162#ifdef FEAT_FLOAT
3163 if (rettv->v_type == VAR_FLOAT)
3164 {
3165 if (*p == '-')
3166 rettv->vval.v_float = -rettv->vval.v_float;
3167 }
3168 else
3169#endif
3170 {
3171 varnumber_T val;
3172 int error = FALSE;
3173
3174 // tv_get_number_chk() accepts a string, but we don't want that
3175 // here
3176 if (check_not_string(rettv) == FAIL)
3177 return FAIL;
3178 val = tv_get_number_chk(rettv, &error);
3179 clear_tv(rettv);
3180 if (error)
3181 return FAIL;
3182 if (*p == '-')
3183 val = -val;
3184 rettv->v_type = VAR_NUMBER;
3185 rettv->vval.v_number = val;
3186 }
3187 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003188 else if (numeric_only)
3189 {
3190 ++p;
3191 break;
3192 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003193 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 {
3195 int v = tv2bool(rettv);
3196
3197 // '!' is permissive in the type.
3198 clear_tv(rettv);
3199 rettv->v_type = VAR_BOOL;
3200 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3201 }
3202 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003203 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 return OK;
3205}
3206
3207/*
3208 * Recognize v: variables that are constants and set "rettv".
3209 */
3210 static void
3211get_vim_constant(char_u **arg, typval_T *rettv)
3212{
3213 if (STRNCMP(*arg, "v:true", 6) == 0)
3214 {
3215 rettv->v_type = VAR_BOOL;
3216 rettv->vval.v_number = VVAL_TRUE;
3217 *arg += 6;
3218 }
3219 else if (STRNCMP(*arg, "v:false", 7) == 0)
3220 {
3221 rettv->v_type = VAR_BOOL;
3222 rettv->vval.v_number = VVAL_FALSE;
3223 *arg += 7;
3224 }
3225 else if (STRNCMP(*arg, "v:null", 6) == 0)
3226 {
3227 rettv->v_type = VAR_SPECIAL;
3228 rettv->vval.v_number = VVAL_NULL;
3229 *arg += 6;
3230 }
3231 else if (STRNCMP(*arg, "v:none", 6) == 0)
3232 {
3233 rettv->v_type = VAR_SPECIAL;
3234 rettv->vval.v_number = VVAL_NONE;
3235 *arg += 6;
3236 }
3237}
3238
Bram Moolenaar696ba232020-07-29 21:20:41 +02003239 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003240get_compare_type(char_u *p, int *len, int *type_is)
3241{
3242 exptype_T type = EXPR_UNKNOWN;
3243 int i;
3244
3245 switch (p[0])
3246 {
3247 case '=': if (p[1] == '=')
3248 type = EXPR_EQUAL;
3249 else if (p[1] == '~')
3250 type = EXPR_MATCH;
3251 break;
3252 case '!': if (p[1] == '=')
3253 type = EXPR_NEQUAL;
3254 else if (p[1] == '~')
3255 type = EXPR_NOMATCH;
3256 break;
3257 case '>': if (p[1] != '=')
3258 {
3259 type = EXPR_GREATER;
3260 *len = 1;
3261 }
3262 else
3263 type = EXPR_GEQUAL;
3264 break;
3265 case '<': if (p[1] != '=')
3266 {
3267 type = EXPR_SMALLER;
3268 *len = 1;
3269 }
3270 else
3271 type = EXPR_SEQUAL;
3272 break;
3273 case 'i': if (p[1] == 's')
3274 {
3275 // "is" and "isnot"; but not a prefix of a name
3276 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3277 *len = 5;
3278 i = p[*len];
3279 if (!isalnum(i) && i != '_')
3280 {
3281 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3282 *type_is = TRUE;
3283 }
3284 }
3285 break;
3286 }
3287 return type;
3288}
3289
3290/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003292 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 */
3294 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003295compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003297 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
3299 // this works from end to start
3300 while (p > start)
3301 {
3302 --p;
3303 if (*p == '-' || *p == '+')
3304 {
3305 int negate = *p == '-';
3306 isn_T *isn;
3307
3308 // TODO: check type
3309 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3310 {
3311 --p;
3312 if (*p == '-')
3313 negate = !negate;
3314 }
3315 // only '-' has an effect, for '+' we only check the type
3316 if (negate)
3317 isn = generate_instr(cctx, ISN_NEGATENR);
3318 else
3319 isn = generate_instr(cctx, ISN_CHECKNR);
3320 if (isn == NULL)
3321 return FAIL;
3322 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003323 else if (numeric_only)
3324 {
3325 ++p;
3326 break;
3327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 else
3329 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003330 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003332 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003334 if (p[-1] == '!')
3335 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 }
3338 if (generate_2BOOL(cctx, invert) == FAIL)
3339 return FAIL;
3340 }
3341 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003342 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 return OK;
3344}
3345
3346/*
3347 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003348 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 */
3350 static int
3351compile_subscript(
3352 char_u **arg,
3353 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003354 char_u *start_leader,
3355 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003356 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003358 char_u *name_start = *end_leader;
3359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 for (;;)
3361 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003362 char_u *p = skipwhite(*arg);
3363
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003364 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003365 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003366 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003367
3368 // If a following line starts with "->{" or "->X" advance to that
3369 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003370 // Also if a following line starts with ".x".
3371 if (next != NULL &&
3372 ((next[0] == '-' && next[1] == '>'
3373 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003374 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003375 {
3376 next = next_line_from_context(cctx, TRUE);
3377 if (next == NULL)
3378 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003379 *arg = next;
3380 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003381 }
3382 }
3383
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003384 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3385 // not a function call.
3386 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003388 garray_T *stack = &cctx->ctx_type_stack;
3389 type_T *type;
3390 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003392 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003393 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003394 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003397 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3398
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003399 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3401 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003402 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 return FAIL;
3404 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003405 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003407 char_u *pstart = p;
3408
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003409 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003410 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003411 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 // something->method()
3414 // Apply the '!', '-' and '+' first:
3415 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003416 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003419 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003420 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003421 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 if (**arg == '{')
3423 {
3424 // lambda call: list->{lambda}
3425 if (compile_lambda_call(arg, cctx) == FAIL)
3426 return FAIL;
3427 }
3428 else
3429 {
3430 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003431 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003432 if (!eval_isnamec1(*p))
3433 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003434 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003435 return FAIL;
3436 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003437 if (ASCII_ISALPHA(*p) && p[1] == ':')
3438 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003439 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 ;
3441 if (*p != '(')
3442 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003443 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 return FAIL;
3445 }
3446 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003447 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 return FAIL;
3449 }
3450 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003451 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003453 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003454 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003455 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003456 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003457 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003458
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003459 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003460 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003461 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003462 // TODO: blob index
3463 // TODO: more arguments
3464 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003465 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003466 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003467 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003468
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003469 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003470 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003471 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003472 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003473 if (**arg == ':')
3474 // missing first index is equal to zero
3475 generate_PUSHNR(cctx, 0);
3476 else
3477 {
3478 if (compile_expr0(arg, cctx) == FAIL)
3479 return FAIL;
3480 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3481 return FAIL;
3482 *arg = skipwhite(*arg);
3483 }
3484 if (**arg == ':')
3485 {
3486 *arg = skipwhite(*arg + 1);
3487 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3488 return FAIL;
3489 if (**arg == ']')
3490 // missing second index is equal to end of string
3491 generate_PUSHNR(cctx, -1);
3492 else
3493 {
3494 if (compile_expr0(arg, cctx) == FAIL)
3495 return FAIL;
3496 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3497 return FAIL;
3498 *arg = skipwhite(*arg);
3499 }
3500 is_slice = TRUE;
3501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502
3503 if (**arg != ']')
3504 {
3505 emsg(_(e_missbrac));
3506 return FAIL;
3507 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003508 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003510 // We can index a list and a dict. If we don't know the type
3511 // we can use the index value type.
3512 // TODO: If we don't know use an instruction to figure it out at
3513 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003514 typep = ((type_T **)stack->ga_data) + stack->ga_len
3515 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003516 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003517 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3518 // If the index is a string, the variable must be a Dict.
3519 if (*typep == &t_any && valtype == &t_string)
3520 vtype = VAR_DICT;
3521 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003522 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003523 if (need_type(valtype, &t_number, -1, cctx,
3524 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003525 return FAIL;
3526 if (is_slice)
3527 {
3528 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003529 if (need_type(valtype, &t_number, -2, cctx,
3530 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003531 return FAIL;
3532 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003533 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003534
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003535 if (vtype == VAR_DICT)
3536 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003537 if (is_slice)
3538 {
3539 emsg(_(e_cannot_slice_dictionary));
3540 return FAIL;
3541 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003542 if ((*typep)->tt_type == VAR_DICT)
3543 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003544 else
3545 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003546 if (need_type(*typep, &t_dict_any, -2, cctx,
3547 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003548 return FAIL;
3549 *typep = &t_any;
3550 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003551 if (may_generate_2STRING(-1, cctx) == FAIL)
3552 return FAIL;
3553 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3554 return FAIL;
3555 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003556 else if (vtype == VAR_STRING)
3557 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003558 *typep = &t_string;
3559 if ((is_slice
3560 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3561 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003562 return FAIL;
3563 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003564 else if (vtype == VAR_BLOB)
3565 {
3566 emsg("Sorry, blob index and slice not implemented yet");
3567 return FAIL;
3568 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003569 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003570 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003571 if (is_slice)
3572 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003573 if (generate_instr_drop(cctx,
3574 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3575 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003576 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003577 }
Bram Moolenaared591872020-08-15 22:14:53 +02003578 else
3579 {
3580 if ((*typep)->tt_type == VAR_LIST)
3581 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003582 if (generate_instr_drop(cctx,
3583 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3584 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003585 return FAIL;
3586 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003587 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003588 else
3589 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003590 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003591 return FAIL;
3592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003594 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003596 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003597 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003598 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003599 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003600
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003601 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003602 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003603 {
3604 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003605 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003606 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003607 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003608 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 while (eval_isnamec(*p))
3610 MB_PTR_ADV(p);
3611 if (p == *arg)
3612 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003613 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 return FAIL;
3615 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003616 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 return FAIL;
3618 *arg = p;
3619 }
3620 else
3621 break;
3622 }
3623
3624 // TODO - see handle_subscript():
3625 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3626 // Don't do this when "Func" is already a partial that was bound
3627 // explicitly (pt_auto is FALSE).
3628
3629 return OK;
3630}
3631
3632/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003633 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3634 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003636 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003637 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003638 *
3639 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 */
3641
3642/*
3643 * number number constant
3644 * 0zFFFFFFFF Blob constant
3645 * "string" string constant
3646 * 'string' literal string constant
3647 * &option-name option value
3648 * @r register contents
3649 * identifier variable value
3650 * function() function call
3651 * $VAR environment variable
3652 * (expression) nested expression
3653 * [expr, expr] List
3654 * {key: val, key: val} Dictionary
3655 * #{key: val, key: val} Dictionary with literal keys
3656 *
3657 * Also handle:
3658 * ! in front logical NOT
3659 * - in front unary minus
3660 * + in front unary plus (ignored)
3661 * trailing (arg) funcref/partial call
3662 * trailing [] subscript in String or List
3663 * trailing .name entry in Dictionary
3664 * trailing ->name() method call
3665 */
3666 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003667compile_expr7(
3668 char_u **arg,
3669 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003670 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 char_u *start_leader, *end_leader;
3673 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003674 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003675 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003677 ppconst->pp_is_const = FALSE;
3678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 /*
3680 * Skip '!', '-' and '+' characters. They are handled later.
3681 */
3682 start_leader = *arg;
3683 while (**arg == '!' || **arg == '-' || **arg == '+')
3684 *arg = skipwhite(*arg + 1);
3685 end_leader = *arg;
3686
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003687 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 switch (**arg)
3689 {
3690 /*
3691 * Number constant.
3692 */
3693 case '0': // also for blob starting with 0z
3694 case '1':
3695 case '2':
3696 case '3':
3697 case '4':
3698 case '5':
3699 case '6':
3700 case '7':
3701 case '8':
3702 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003703 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003705 // Apply "-" and "+" just before the number now, right to
3706 // left. Matters especially when "->" follows. Stops at
3707 // '!'.
3708 if (apply_leader(rettv, TRUE,
3709 start_leader, &end_leader) == FAIL)
3710 {
3711 clear_tv(rettv);
3712 return FAIL;
3713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 break;
3715
3716 /*
3717 * String constant: "string".
3718 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003719 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 return FAIL;
3721 break;
3722
3723 /*
3724 * Literal string constant: 'str''ing'.
3725 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003726 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 return FAIL;
3728 break;
3729
3730 /*
3731 * Constant Vim variable.
3732 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003733 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 ret = NOTDONE;
3735 break;
3736
3737 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003738 * "true" constant
3739 */
3740 case 't': if (STRNCMP(*arg, "true", 4) == 0
3741 && !eval_isnamec((*arg)[4]))
3742 {
3743 *arg += 4;
3744 rettv->v_type = VAR_BOOL;
3745 rettv->vval.v_number = VVAL_TRUE;
3746 }
3747 else
3748 ret = NOTDONE;
3749 break;
3750
3751 /*
3752 * "false" constant
3753 */
3754 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3755 && !eval_isnamec((*arg)[5]))
3756 {
3757 *arg += 5;
3758 rettv->v_type = VAR_BOOL;
3759 rettv->vval.v_number = VVAL_FALSE;
3760 }
3761 else
3762 ret = NOTDONE;
3763 break;
3764
3765 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 * List: [expr, expr]
3767 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003768 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 break;
3770
3771 /*
3772 * Dictionary: #{key: val, key: val}
3773 */
3774 case '#': if ((*arg)[1] == '{')
3775 {
3776 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003777 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 }
3779 else
3780 ret = NOTDONE;
3781 break;
3782
3783 /*
3784 * Lambda: {arg, arg -> expr}
3785 * Dictionary: {'key': val, 'key': val}
3786 */
3787 case '{': {
3788 char_u *start = skipwhite(*arg + 1);
3789
3790 // Find out what comes after the arguments.
3791 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003792 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 if (ret != FAIL && *start == '>')
3794 ret = compile_lambda(arg, cctx);
3795 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003796 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 }
3798 break;
3799
3800 /*
3801 * Option value: &name
3802 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003803 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3804 return FAIL;
3805 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 break;
3807
3808 /*
3809 * Environment variable: $VAR.
3810 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003811 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3812 return FAIL;
3813 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 break;
3815
3816 /*
3817 * Register contents: @r.
3818 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003819 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3820 return FAIL;
3821 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 break;
3823 /*
3824 * nested expression: (expression).
3825 */
3826 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003827
3828 // recursive!
3829 if (ppconst->pp_used <= PPSIZE - 10)
3830 {
3831 ret = compile_expr1(arg, cctx, ppconst);
3832 }
3833 else
3834 {
3835 // Not enough space in ppconst, flush constants.
3836 if (generate_ppconst(cctx, ppconst) == FAIL)
3837 return FAIL;
3838 ret = compile_expr0(arg, cctx);
3839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 *arg = skipwhite(*arg);
3841 if (**arg == ')')
3842 ++*arg;
3843 else if (ret == OK)
3844 {
3845 emsg(_(e_missing_close));
3846 ret = FAIL;
3847 }
3848 break;
3849
3850 default: ret = NOTDONE;
3851 break;
3852 }
3853 if (ret == FAIL)
3854 return FAIL;
3855
Bram Moolenaar1c747212020-05-09 18:28:34 +02003856 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003858 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003859 clear_tv(rettv);
3860 else
3861 // A constant expression can possibly be handled compile time,
3862 // return the value instead of generating code.
3863 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 }
3865 else if (ret == NOTDONE)
3866 {
3867 char_u *p;
3868 int r;
3869
3870 if (!eval_isnamec1(**arg))
3871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003872 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 return FAIL;
3874 }
3875
3876 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003877 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003879 {
3880 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003883 {
3884 if (generate_ppconst(cctx, ppconst) == FAIL)
3885 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003886 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 if (r == FAIL)
3889 return FAIL;
3890 }
3891
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003892 // Handle following "[]", ".member", etc.
3893 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003894 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003895 ppconst) == FAIL)
3896 return FAIL;
3897 if (ppconst->pp_used > 0)
3898 {
3899 // apply the '!', '-' and '+' before the constant
3900 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003901 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003902 return FAIL;
3903 return OK;
3904 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003905 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003907 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908}
3909
3910/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003911 * Give the "white on both sides" error, taking the operator from "p[len]".
3912 */
3913 void
3914error_white_both(char_u *op, int len)
3915{
3916 char_u buf[10];
3917
3918 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003919 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003920}
3921
3922/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003923 * <type>expr7: runtime type check / conversion
3924 */
3925 static int
3926compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3927{
3928 type_T *want_type = NULL;
3929
3930 // Recognize <type>
3931 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3932 {
3933 int called_emsg_before = called_emsg;
3934
3935 ++*arg;
3936 want_type = parse_type(arg, cctx->ctx_type_list);
3937 if (called_emsg != called_emsg_before)
3938 return FAIL;
3939
3940 if (**arg != '>')
3941 {
3942 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003943 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003944 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003945 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003946 return FAIL;
3947 }
3948 ++*arg;
3949 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3950 return FAIL;
3951 }
3952
3953 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3954 return FAIL;
3955
3956 if (want_type != NULL)
3957 {
3958 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003959 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003960
Bram Moolenaard1103582020-08-14 22:44:25 +02003961 generate_ppconst(cctx, ppconst);
3962 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003963 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003964 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003965 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003966 return FAIL;
3967 }
3968 }
3969
3970 return OK;
3971}
3972
3973/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 * * number multiplication
3975 * / number division
3976 * % number modulo
3977 */
3978 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003979compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980{
3981 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003982 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003983 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003985 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003986 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 return FAIL;
3988
3989 /*
3990 * Repeat computing, until no "*", "/" or "%" is following.
3991 */
3992 for (;;)
3993 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003994 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 if (*op != '*' && *op != '/' && *op != '%')
3996 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003997 if (next != NULL)
3998 {
3999 *arg = next_line_from_context(cctx, TRUE);
4000 op = skipwhite(*arg);
4001 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004002
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004003 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004005 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004006 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 }
4008 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004009 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004010 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004012 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004013 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004015
4016 if (ppconst->pp_used == ppconst_used + 2
4017 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4018 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004019 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004020 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4021 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004022 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004024 // both are numbers: compute the result
4025 switch (*op)
4026 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004027 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004028 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004029 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004030 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004031 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004032 break;
4033 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004034 tv1->vval.v_number = res;
4035 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004036 }
4037 else
4038 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004039 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004040 generate_two_op(cctx, op);
4041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
4043
4044 return OK;
4045}
4046
4047/*
4048 * + number addition
4049 * - number subtraction
4050 * .. string concatenation
4051 */
4052 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004053compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054{
4055 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004056 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004058 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059
4060 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004061 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 return FAIL;
4063
4064 /*
4065 * Repeat computing, until no "+", "-" or ".." is following.
4066 */
4067 for (;;)
4068 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004069 op = may_peek_next_line(cctx, *arg, &next);
4070 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 break;
4072 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004073 if (next != NULL)
4074 {
4075 *arg = next_line_from_context(cctx, TRUE);
4076 op = skipwhite(*arg);
4077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004079 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004081 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004082 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 }
4084
4085 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004086 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004087 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004089 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004090 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 return FAIL;
4092
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004094 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004095 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4096 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4097 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4098 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4101 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 // concat/subtract/add constant numbers
4104 if (*op == '+')
4105 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4106 else if (*op == '-')
4107 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4108 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004109 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004110 // concatenate constant strings
4111 char_u *s1 = tv1->vval.v_string;
4112 char_u *s2 = tv2->vval.v_string;
4113 size_t len1 = STRLEN(s1);
4114
4115 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4116 if (tv1->vval.v_string == NULL)
4117 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004118 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004119 return FAIL;
4120 }
4121 mch_memmove(tv1->vval.v_string, s1, len1);
4122 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004123 vim_free(s1);
4124 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004125 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004126 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 }
4128 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004129 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004130 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004131 if (*op == '.')
4132 {
4133 if (may_generate_2STRING(-2, cctx) == FAIL
4134 || may_generate_2STRING(-1, cctx) == FAIL)
4135 return FAIL;
4136 generate_instr_drop(cctx, ISN_CONCAT, 1);
4137 }
4138 else
4139 generate_two_op(cctx, op);
4140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 }
4142
4143 return OK;
4144}
4145
4146/*
4147 * expr5a == expr5b
4148 * expr5a =~ expr5b
4149 * expr5a != expr5b
4150 * expr5a !~ expr5b
4151 * expr5a > expr5b
4152 * expr5a >= expr5b
4153 * expr5a < expr5b
4154 * expr5a <= expr5b
4155 * expr5a is expr5b
4156 * expr5a isnot expr5b
4157 *
4158 * Produces instructions:
4159 * EVAL expr5a Push result of "expr5a"
4160 * EVAL expr5b Push result of "expr5b"
4161 * COMPARE one of the compare instructions
4162 */
4163 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165{
4166 exptype_T type = EXPR_UNKNOWN;
4167 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004168 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004171 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172
4173 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 return FAIL;
4176
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004177 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004178 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179
4180 /*
4181 * If there is a comparative operator, use it.
4182 */
4183 if (type != EXPR_UNKNOWN)
4184 {
4185 int ic = FALSE; // Default: do not ignore case
4186
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004187 if (next != NULL)
4188 {
4189 *arg = next_line_from_context(cctx, TRUE);
4190 p = skipwhite(*arg);
4191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 if (type_is && (p[len] == '?' || p[len] == '#'))
4193 {
4194 semsg(_(e_invexpr2), *arg);
4195 return FAIL;
4196 }
4197 // extra question mark appended: ignore case
4198 if (p[len] == '?')
4199 {
4200 ic = TRUE;
4201 ++len;
4202 }
4203 // extra '#' appended: match case (ignored)
4204 else if (p[len] == '#')
4205 ++len;
4206 // nothing appended: match case
4207
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004208 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004210 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004211 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 }
4213
4214 // get the second variable
4215 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004216 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004217 return FAIL;
4218
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 return FAIL;
4221
Bram Moolenaara5565e42020-05-09 15:44:01 +02004222 if (ppconst->pp_used == ppconst_used + 2)
4223 {
4224 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4225 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4226 int ret;
4227
4228 // Both sides are a constant, compute the result now.
4229 // First check for a valid combination of types, this is more
4230 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004231 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232 ret = FAIL;
4233 else
4234 {
4235 ret = typval_compare(tv1, tv2, type, ic);
4236 tv1->v_type = VAR_BOOL;
4237 tv1->vval.v_number = tv1->vval.v_number
4238 ? VVAL_TRUE : VVAL_FALSE;
4239 clear_tv(tv2);
4240 --ppconst->pp_used;
4241 }
4242 return ret;
4243 }
4244
4245 generate_ppconst(cctx, ppconst);
4246 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 }
4248
4249 return OK;
4250}
4251
Bram Moolenaar7f141552020-05-09 17:35:53 +02004252static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254/*
4255 * Compile || or &&.
4256 */
4257 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258compile_and_or(
4259 char_u **arg,
4260 cctx_T *cctx,
4261 char *op,
4262 ppconst_T *ppconst,
4263 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004265 char_u *next;
4266 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 int opchar = *op;
4268
4269 if (p[0] == opchar && p[1] == opchar)
4270 {
4271 garray_T *instr = &cctx->ctx_instr;
4272 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004273 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004274 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275
4276 /*
4277 * Repeat until there is no following "||" or "&&"
4278 */
4279 ga_init2(&end_ga, sizeof(int), 10);
4280 while (p[0] == opchar && p[1] == opchar)
4281 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004282 if (next != NULL)
4283 {
4284 *arg = next_line_from_context(cctx, TRUE);
4285 p = skipwhite(*arg);
4286 }
4287
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004288 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4289 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004290 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004291 return FAIL;
4292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004294 // TODO: use ppconst if the value is a constant and check
4295 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004296 generate_ppconst(cctx, ppconst);
4297
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004298 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4299 all_bool_values = FALSE;
4300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 if (ga_grow(&end_ga, 1) == FAIL)
4302 {
4303 ga_clear(&end_ga);
4304 return FAIL;
4305 }
4306 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4307 ++end_ga.ga_len;
4308 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004309 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310
4311 // eval the next expression
4312 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004313 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004314 return FAIL;
4315
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4317 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 {
4319 ga_clear(&end_ga);
4320 return FAIL;
4321 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004322
4323 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326
4327 // Fill in the end label in all jumps.
4328 while (end_ga.ga_len > 0)
4329 {
4330 isn_T *isn;
4331
4332 --end_ga.ga_len;
4333 isn = ((isn_T *)instr->ga_data)
4334 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4335 isn->isn_arg.jump.jump_where = instr->ga_len;
4336 }
4337 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004338
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004339 // The resulting type is converted to bool if needed.
4340 if (!all_bool_values)
4341 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 }
4343
4344 return OK;
4345}
4346
4347/*
4348 * expr4a && expr4a && expr4a logical AND
4349 *
4350 * Produces instructions:
4351 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004352 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004354 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004356 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 * end:
4358 */
4359 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004360compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 int ppconst_used = ppconst->pp_used;
4363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004365 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 return FAIL;
4367
4368 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370}
4371
4372/*
4373 * expr3a || expr3b || expr3c logical OR
4374 *
4375 * Produces instructions:
4376 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004377 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004379 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004381 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 * end:
4383 */
4384 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387 int ppconst_used = ppconst->pp_used;
4388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004390 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 return FAIL;
4392
4393 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395}
4396
4397/*
4398 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004400 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 * JUMP_IF_FALSE alt jump if false
4402 * EVAL expr1a
4403 * JUMP_ALWAYS end
4404 * alt: EVAL expr1b
4405 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004406 *
4407 * Toplevel expression: expr2 ?? expr1
4408 * Produces instructions:
4409 * EVAL expr2 Push result of "expr2"
4410 * JUMP_AND_KEEP_IF_TRUE end jump if true
4411 * EVAL expr1
4412 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 */
4414 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004415compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416{
4417 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004419 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420
Bram Moolenaar3988f642020-08-27 22:43:03 +02004421 // Ignore all kinds of errors when not producing code.
4422 if (cctx->ctx_skip == SKIP_YES)
4423 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004424 evalarg_T evalarg;
4425
4426 CLEAR_FIELD(evalarg);
4427 evalarg.eval_cctx = cctx;
4428 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004429 return OK;
4430 }
4431
Bram Moolenaar61a89812020-05-07 16:58:17 +02004432 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004433 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 return FAIL;
4435
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004436 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 if (*p == '?')
4438 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004439 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 garray_T *instr = &cctx->ctx_instr;
4441 garray_T *stack = &cctx->ctx_type_stack;
4442 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004443 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004445 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 int has_const_expr = FALSE;
4447 int const_value = FALSE;
4448 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004450 if (next != NULL)
4451 {
4452 *arg = next_line_from_context(cctx, TRUE);
4453 p = skipwhite(*arg);
4454 }
4455
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004456 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004457 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004458 semsg(_(e_white_space_required_before_and_after_str),
4459 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004460 return FAIL;
4461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 if (ppconst->pp_used == ppconst_used + 1)
4464 {
4465 // the condition is a constant, we know whether the ? or the :
4466 // expression is to be evaluated.
4467 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004468 if (op_falsy)
4469 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4470 else
4471 {
4472 int error = FALSE;
4473
4474 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4475 &error);
4476 if (error)
4477 return FAIL;
4478 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004479 cctx->ctx_skip = save_skip == SKIP_YES ||
4480 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4481
4482 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4483 // "left ?? right" and "left" is truthy: produce "left"
4484 generate_ppconst(cctx, ppconst);
4485 else
4486 {
4487 clear_tv(&ppconst->pp_tv[ppconst_used]);
4488 --ppconst->pp_used;
4489 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004490 }
4491 else
4492 {
4493 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004494 if (op_falsy)
4495 end_idx = instr->ga_len;
4496 generate_JUMP(cctx, op_falsy
4497 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4498 if (op_falsy)
4499 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501
4502 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004503 *arg = skipwhite(p + 1 + op_falsy);
4504 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004505 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004506 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004507 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 if (!has_const_expr)
4510 {
4511 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004513 if (!op_falsy)
4514 {
4515 // remember the type and drop it
4516 --stack->ga_len;
4517 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004519 end_idx = instr->ga_len;
4520 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004522 // jump here from JUMP_IF_FALSE
4523 isn = ((isn_T *)instr->ga_data) + alt_idx;
4524 isn->isn_arg.jump.jump_where = instr->ga_len;
4525 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004528 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004530 // Check for the ":".
4531 p = may_peek_next_line(cctx, *arg, &next);
4532 if (*p != ':')
4533 {
4534 emsg(_(e_missing_colon));
4535 return FAIL;
4536 }
4537 if (next != NULL)
4538 {
4539 *arg = next_line_from_context(cctx, TRUE);
4540 p = skipwhite(*arg);
4541 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004542
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004543 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4544 {
4545 semsg(_(e_white_space_required_before_and_after_str), ":");
4546 return FAIL;
4547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004549 // evaluate the third expression
4550 if (has_const_expr)
4551 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004552 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004553 *arg = skipwhite(p + 1);
4554 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4555 return FAIL;
4556 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4557 return FAIL;
4558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559
Bram Moolenaara5565e42020-05-09 15:44:01 +02004560 if (!has_const_expr)
4561 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004562 type_T **typep;
4563
Bram Moolenaara5565e42020-05-09 15:44:01 +02004564 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565
Bram Moolenaara5565e42020-05-09 15:44:01 +02004566 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004567 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4568 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004570 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571 isn = ((isn_T *)instr->ga_data) + end_idx;
4572 isn->isn_arg.jump.jump_where = instr->ga_len;
4573 }
4574
4575 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 }
4577 return OK;
4578}
4579
4580/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004581 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004582 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4583 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004584 */
4585 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004586compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004587{
4588 ppconst_T ppconst;
4589
4590 CLEAR_FIELD(ppconst);
4591 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4592 {
4593 clear_ppconst(&ppconst);
4594 return FAIL;
4595 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004596 if (is_const != NULL)
4597 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598 if (generate_ppconst(cctx, &ppconst) == FAIL)
4599 return FAIL;
4600 return OK;
4601}
4602
4603/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004604 * Toplevel expression.
4605 */
4606 static int
4607compile_expr0(char_u **arg, cctx_T *cctx)
4608{
4609 return compile_expr0_ext(arg, cctx, NULL);
4610}
4611
4612/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 * compile "return [expr]"
4614 */
4615 static char_u *
4616compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4617{
4618 char_u *p = arg;
4619 garray_T *stack = &cctx->ctx_type_stack;
4620 type_T *stack_type;
4621
4622 if (*p != NUL && *p != '|' && *p != '\n')
4623 {
4624 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004625 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 return NULL;
4627
4628 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4629 if (set_return_type)
4630 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004631 else
4632 {
4633 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4634 && stack_type->tt_type != VAR_VOID
4635 && stack_type->tt_type != VAR_UNKNOWN)
4636 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004637 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004638 return NULL;
4639 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004640 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004641 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004642 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 }
4645 else
4646 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004647 // "set_return_type" cannot be TRUE, only used for a lambda which
4648 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004649 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4650 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004652 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 return NULL;
4654 }
4655
4656 // No argument, return zero.
4657 generate_PUSHNR(cctx, 0);
4658 }
4659
4660 if (generate_instr(cctx, ISN_RETURN) == NULL)
4661 return NULL;
4662
4663 // "return val | endif" is possible
4664 return skipwhite(p);
4665}
4666
4667/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004668 * Get a line from the compilation context, compatible with exarg_T getline().
4669 * Return a pointer to the line in allocated memory.
4670 * Return NULL for end-of-file or some error.
4671 */
4672 static char_u *
4673exarg_getline(
4674 int c UNUSED,
4675 void *cookie,
4676 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004677 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004678{
4679 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004680 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004681
Bram Moolenaar66250c92020-08-20 15:02:42 +02004682 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004683 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004684 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004685 return NULL;
4686 ++cctx->ctx_lnum;
4687 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4688 // Comment lines result in NULL pointers, skip them.
4689 if (p != NULL)
4690 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004691 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004692}
4693
4694/*
4695 * Compile a nested :def command.
4696 */
4697 static char_u *
4698compile_nested_function(exarg_T *eap, cctx_T *cctx)
4699{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004700 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004701 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004702 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004703 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004704 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004705 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004706
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004707 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004708 {
4709 emsg(_(e_cannot_use_bang_with_nested_def));
4710 return NULL;
4711 }
4712
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004713 // Only g:Func() can use a namespace.
4714 if (name_start[1] == ':' && !is_global)
4715 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004716 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004717 return NULL;
4718 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004719 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4720 return NULL;
4721
Bram Moolenaar04b12692020-05-04 23:24:44 +02004722 eap->arg = name_end;
4723 eap->getline = exarg_getline;
4724 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004725 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004726 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004727 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004728 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004729
Bram Moolenaar822ba242020-05-24 23:00:18 +02004730 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004731 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004732 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004733 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004734 {
4735 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004736 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004737 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004738
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004739 if (is_global)
4740 {
4741 char_u *func_name = vim_strnsave(name_start + 2,
4742 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004743
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004744 if (func_name == NULL)
4745 r = FAIL;
4746 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004747 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004748 }
4749 else
4750 {
4751 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004752 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004753 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004754 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004755
Bram Moolenaareef21022020-08-01 22:16:43 +02004756 if (lvar == NULL)
4757 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004758 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004759 return NULL;
4760 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004761
4762 // copy over the block scope IDs
4763 if (block_depth > 0)
4764 {
4765 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4766 if (ufunc->uf_block_ids != NULL)
4767 {
4768 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4769 sizeof(int) * block_depth);
4770 ufunc->uf_block_depth = block_depth;
4771 }
4772 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004773 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004774
Bram Moolenaar61a89812020-05-07 16:58:17 +02004775 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004776 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004777}
4778
4779/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 * Return the length of an assignment operator, or zero if there isn't one.
4781 */
4782 int
4783assignment_len(char_u *p, int *heredoc)
4784{
4785 if (*p == '=')
4786 {
4787 if (p[1] == '<' && p[2] == '<')
4788 {
4789 *heredoc = TRUE;
4790 return 3;
4791 }
4792 return 1;
4793 }
4794 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4795 return 2;
4796 if (STRNCMP(p, "..=", 3) == 0)
4797 return 3;
4798 return 0;
4799}
4800
4801// words that cannot be used as a variable
4802static char *reserved[] = {
4803 "true",
4804 "false",
4805 NULL
4806};
4807
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004808typedef enum {
4809 dest_local,
4810 dest_option,
4811 dest_env,
4812 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004813 dest_buffer,
4814 dest_window,
4815 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004816 dest_vimvar,
4817 dest_script,
4818 dest_reg,
4819} assign_dest_T;
4820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004822 * Generate the load instruction for "name".
4823 */
4824 static void
4825generate_loadvar(
4826 cctx_T *cctx,
4827 assign_dest_T dest,
4828 char_u *name,
4829 lvar_T *lvar,
4830 type_T *type)
4831{
4832 switch (dest)
4833 {
4834 case dest_option:
4835 // TODO: check the option exists
4836 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4837 break;
4838 case dest_global:
4839 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4840 break;
4841 case dest_buffer:
4842 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4843 break;
4844 case dest_window:
4845 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4846 break;
4847 case dest_tab:
4848 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4849 break;
4850 case dest_script:
4851 compile_load_scriptvar(cctx,
4852 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4853 break;
4854 case dest_env:
4855 // Include $ in the name here
4856 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4857 break;
4858 case dest_reg:
4859 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4860 break;
4861 case dest_vimvar:
4862 generate_LOADV(cctx, name + 2, TRUE);
4863 break;
4864 case dest_local:
4865 if (lvar->lv_from_outer)
4866 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4867 NULL, type);
4868 else
4869 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4870 break;
4871 }
4872}
4873
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004874 void
4875vim9_declare_error(char_u *name)
4876{
4877 char *scope = "";
4878
4879 switch (*name)
4880 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004881 case 'g': scope = _("global"); break;
4882 case 'b': scope = _("buffer"); break;
4883 case 'w': scope = _("window"); break;
4884 case 't': scope = _("tab"); break;
4885 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004886 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004887 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004888 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004889 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004890 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004891 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004892 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004893 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004894 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004895}
4896
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004897/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004899 * "let name"
4900 * "var name = expr"
4901 * "final name = expr"
4902 * "const name = expr"
4903 * "name = expr"
4904 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004905 * Return NULL for an error.
4906 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 */
4908 static char_u *
4909compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4910{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004911 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004913 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 char_u *ret = NULL;
4915 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004916 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004917 int scriptvar_sid = 0;
4918 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004921 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 int oplen = 0;
4924 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004925 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004926 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004927 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004929 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4930 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 // Skip over the "var" or "[var, var]" to get to any "=".
4933 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4934 if (p == NULL)
4935 return *arg == '[' ? arg : NULL;
4936
4937 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004939 // TODO: should we allow this, and figure out type inference from list
4940 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004941 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 return NULL;
4943 }
4944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 sp = p;
4946 p = skipwhite(p);
4947 op = p;
4948 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949
4950 if (var_count > 0 && oplen == 0)
4951 // can be something like "[1, 2]->func()"
4952 return arg;
4953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4955 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004956 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004958 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 if (heredoc)
4961 {
4962 list_T *l;
4963 listitem_T *li;
4964
4965 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004966 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004968 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004969 if (l == NULL)
4970 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
Bram Moolenaar078269b2020-09-21 20:35:55 +02004972 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004974 // Push each line and the create the list.
4975 FOR_ALL_LIST_ITEMS(l, li)
4976 {
4977 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4978 li->li_tv.vval.v_string = NULL;
4979 }
4980 generate_NEWLIST(cctx, l->lv_len);
4981 type = &t_list_string;
4982 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 list_free(l);
4985 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004986 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004990 // for "[var, var] = expr" evaluate the expression here, loop over the
4991 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004992
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993 p = skipwhite(op + oplen);
4994 if (compile_expr0(&p, cctx) == FAIL)
4995 return NULL;
4996 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004998 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005000 type_T *stacktype;
5001
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005002 stacktype = stack->ga_len == 0 ? &t_void
5003 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005006 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 goto theend;
5008 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005009 if (need_type(stacktype, &t_list_any, -1, cctx,
5010 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005012 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005013 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5014 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 }
5016 }
5017
5018 /*
5019 * Loop over variables in "[var, var] = expr".
5020 * For "var = expr" and "let var: type" this is done only once.
5021 */
5022 if (var_count > 0)
5023 var_start = skipwhite(arg + 1); // skip over the "["
5024 else
5025 var_start = arg;
5026 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5027 {
5028 char_u *var_end = skip_var_one(var_start, FALSE);
5029 size_t varlen;
5030 int new_local = FALSE;
5031 int opt_type;
5032 int opt_flags = 0;
5033 assign_dest_T dest = dest_local;
5034 int vimvaridx = -1;
5035 lvar_T *lvar = NULL;
5036 lvar_T arg_lvar;
5037 int has_type = FALSE;
5038 int has_index = FALSE;
5039 int instr_count = -1;
5040
Bram Moolenaar65821722020-08-02 18:58:54 +02005041 if (*var_start == '@')
5042 p = var_start + 2;
5043 else
5044 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005045 // skip over the leading "&", "&l:", "&g:" and "$"
5046 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005047 p = to_name_end(p, TRUE);
5048 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049
5050 // "a: type" is declaring variable "a" with a type, not "a:".
5051 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5052 --var_end;
5053 if (is_decl && p == var_start + 2 && p[-1] == ':')
5054 --p;
5055
5056 varlen = p - var_start;
5057 vim_free(name);
5058 name = vim_strnsave(var_start, varlen);
5059 if (name == NULL)
5060 return NULL;
5061 if (!heredoc)
5062 type = &t_any;
5063
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005064 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005065 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005066 int declare_error = FALSE;
5067
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005068 if (*var_start == '&')
5069 {
5070 int cc;
5071 long numval;
5072
5073 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005074 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 emsg(_(e_const_option));
5077 goto theend;
5078 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005079 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 p = var_start;
5081 p = find_option_end(&p, &opt_flags);
5082 if (p == NULL)
5083 {
5084 // cannot happen?
5085 emsg(_(e_letunexp));
5086 goto theend;
5087 }
5088 cc = *p;
5089 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005090 opt_type = get_option_value(skip_option_env_lead(var_start),
5091 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 *p = cc;
5093 if (opt_type == -3)
5094 {
5095 semsg(_(e_unknown_option), var_start);
5096 goto theend;
5097 }
5098 if (opt_type == -2 || opt_type == 0)
5099 type = &t_string;
5100 else
5101 type = &t_number; // both number and boolean option
5102 }
5103 else if (*var_start == '$')
5104 {
5105 dest = dest_env;
5106 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005107 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005108 }
5109 else if (*var_start == '@')
5110 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005111 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005112 {
5113 emsg_invreg(var_start[1]);
5114 goto theend;
5115 }
5116 dest = dest_reg;
5117 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005118 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005120 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 {
5122 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005123 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005124 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005125 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005126 {
5127 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005128 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005129 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005130 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 {
5132 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005133 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005135 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 {
5137 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005138 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005140 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 {
5142 typval_T *vtv;
5143 int di_flags;
5144
5145 vimvaridx = find_vim_var(name + 2, &di_flags);
5146 if (vimvaridx < 0)
5147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005148 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005149 goto theend;
5150 }
5151 // We use the current value of "sandbox" here, is that OK?
5152 if (var_check_ro(di_flags, name, FALSE))
5153 goto theend;
5154 dest = dest_vimvar;
5155 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005156 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005157 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005158 }
5159 else
5160 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005161 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005162
5163 for (idx = 0; reserved[idx] != NULL; ++idx)
5164 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005166 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005167 goto theend;
5168 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169
5170 lvar = lookup_local(var_start, varlen, cctx);
5171 if (lvar == NULL)
5172 {
5173 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005174 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5176 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005177 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005178 if (is_decl)
5179 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005180 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 goto theend;
5182 }
5183 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005184 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005185 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005186 if (lvar != NULL)
5187 {
5188 if (is_decl)
5189 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005190 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005191 goto theend;
5192 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005193 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005194 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005196 int script_namespace = varlen > 1
5197 && STRNCMP(var_start, "s:", 2) == 0;
5198 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005199 ? script_var_exists(var_start + 2, varlen - 2,
5200 FALSE, cctx)
5201 : script_var_exists(var_start, varlen,
5202 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005203 imported_T *import =
5204 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005205
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005206 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005208 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5209
5210 if (is_decl)
5211 {
5212 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005213 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005214 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005215 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005216 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005217 name);
5218 goto theend;
5219 }
5220 else if (cctx->ctx_ufunc->uf_script_ctx_version
5221 == SCRIPT_VERSION_VIM9
5222 && script_namespace
5223 && !script_var && import == NULL)
5224 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005225 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005226 goto theend;
5227 }
5228
5229 dest = dest_script;
5230
5231 // existing script-local variables should have a type
5232 scriptvar_sid = current_sctx.sc_sid;
5233 if (import != NULL)
5234 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005235 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005236 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005237 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005238 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005239 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005240 {
5241 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5242 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005243 ((svar_T *)si->sn_var_vals.ga_data)
5244 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005245 type = sv->sv_type;
5246 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005247 }
5248 }
5249 else if (name[1] == ':' && name[2] != NUL)
5250 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005251 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005252 goto theend;
5253 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005254 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005255 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005256 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005257 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005258 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005259 else if (check_defined(var_start, varlen, cctx) == FAIL)
5260 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005261 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005263
5264 if (declare_error)
5265 {
5266 vim9_declare_error(name);
5267 goto theend;
5268 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 }
5270
5271 // handle "a:name" as a name, not index "name" on "a"
5272 if (varlen > 1 || var_start[varlen] != ':')
5273 p = var_end;
5274
5275 if (dest != dest_option)
5276 {
5277 if (is_decl && *p == ':')
5278 {
5279 // parse optional type: "let var: type = expr"
5280 if (!VIM_ISWHITE(p[1]))
5281 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005282 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 goto theend;
5284 }
5285 p = skipwhite(p + 1);
5286 type = parse_type(&p, cctx->ctx_type_list);
5287 has_type = TRUE;
5288 }
5289 else if (lvar != NULL)
5290 type = lvar->lv_type;
5291 }
5292
5293 if (oplen == 3 && !heredoc && dest != dest_global
5294 && type->tt_type != VAR_STRING
5295 && type->tt_type != VAR_ANY)
5296 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005297 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 goto theend;
5299 }
5300
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005301 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302 {
5303 if (oplen > 1 && !heredoc)
5304 {
5305 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005306 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307 goto theend;
5308 }
5309
5310 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005311 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5312 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005313 goto theend;
5314 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005315 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 if (lvar == NULL)
5317 goto theend;
5318 new_local = TRUE;
5319 }
5320
5321 member_type = type;
5322 if (var_end > var_start + varlen)
5323 {
5324 // Something follows after the variable: "var[idx]".
5325 if (is_decl)
5326 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005327 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005328 goto theend;
5329 }
5330
5331 if (var_start[varlen] == '[')
5332 {
5333 has_index = TRUE;
5334 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005335 member_type = &t_any;
5336 else
5337 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 }
5339 else
5340 {
5341 semsg("Not supported yet: %s", var_start);
5342 goto theend;
5343 }
5344 }
5345 else if (lvar == &arg_lvar)
5346 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005347 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 goto theend;
5349 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005350 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5351 {
5352 semsg(_(e_cannot_assign_to_constant), name);
5353 goto theend;
5354 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005355
5356 if (!heredoc)
5357 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005358 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005359 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005360 if (oplen > 0 && var_count == 0)
5361 {
5362 // skip over the "=" and the expression
5363 p = skipwhite(op + oplen);
5364 compile_expr0(&p, cctx);
5365 }
5366 }
5367 else if (oplen > 0)
5368 {
5369 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005370 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005371
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005372 // For "var = expr" evaluate the expression.
5373 if (var_count == 0)
5374 {
5375 int r;
5376
5377 // for "+=", "*=", "..=" etc. first load the current value
5378 if (*op != '=')
5379 {
5380 generate_loadvar(cctx, dest, name, lvar, type);
5381
5382 if (has_index)
5383 {
5384 // TODO: get member from list or dict
5385 emsg("Index with operation not supported yet");
5386 goto theend;
5387 }
5388 }
5389
5390 // Compile the expression. Temporarily hide the new local
5391 // variable here, it is not available to this expression.
5392 if (new_local)
5393 --cctx->ctx_locals.ga_len;
5394 instr_count = instr->ga_len;
5395 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005396 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005397 if (new_local)
5398 ++cctx->ctx_locals.ga_len;
5399 if (r == FAIL)
5400 goto theend;
5401 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005402 else if (semicolon && var_idx == var_count - 1)
5403 {
5404 // For "[var; var] = expr" get the rest of the list
5405 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5406 goto theend;
5407 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005408 else
5409 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005410 // For "[var, var] = expr" get the "var_idx" item from the
5411 // list.
5412 if (generate_GETITEM(cctx, var_idx) == FAIL)
5413 return FAIL;
5414 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005415
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005416 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005417 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005418 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005420 if ((stacktype->tt_type == VAR_FUNC
5421 || stacktype->tt_type == VAR_PARTIAL)
5422 && var_wrong_func_name(name, TRUE))
5423 goto theend;
5424
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005425 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005427 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005429 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005430 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005431 }
5432 else
5433 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005434 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005435 // for a variable that implies &t_any.
5436 if (stacktype == &t_list_empty)
5437 lvar->lv_type = &t_list_any;
5438 else if (stacktype == &t_dict_empty)
5439 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005440 else if (stacktype == &t_unknown)
5441 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005442 else
5443 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005444 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005445 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005446 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005447 {
5448 type_T *use_type = lvar->lv_type;
5449
Bram Moolenaar71abe482020-09-14 22:28:30 +02005450 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005451 if (has_index)
5452 {
5453 use_type = use_type->tt_member;
5454 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005455 // could be indexing "any"
5456 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005457 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005458 if (need_type(stacktype, use_type, -1, cctx,
5459 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005460 goto theend;
5461 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005462 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005463 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005464 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005465 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005467 else if (cmdidx == CMD_final)
5468 {
5469 emsg(_(e_final_requires_a_value));
5470 goto theend;
5471 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005472 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005474 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005475 goto theend;
5476 }
5477 else if (!has_type || dest == dest_option)
5478 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005479 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005480 goto theend;
5481 }
5482 else
5483 {
5484 // variables are always initialized
5485 if (ga_grow(instr, 1) == FAIL)
5486 goto theend;
5487 switch (member_type->tt_type)
5488 {
5489 case VAR_BOOL:
5490 generate_PUSHBOOL(cctx, VVAL_FALSE);
5491 break;
5492 case VAR_FLOAT:
5493#ifdef FEAT_FLOAT
5494 generate_PUSHF(cctx, 0.0);
5495#endif
5496 break;
5497 case VAR_STRING:
5498 generate_PUSHS(cctx, NULL);
5499 break;
5500 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005501 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005502 break;
5503 case VAR_FUNC:
5504 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5505 break;
5506 case VAR_LIST:
5507 generate_NEWLIST(cctx, 0);
5508 break;
5509 case VAR_DICT:
5510 generate_NEWDICT(cctx, 0);
5511 break;
5512 case VAR_JOB:
5513 generate_PUSHJOB(cctx, NULL);
5514 break;
5515 case VAR_CHANNEL:
5516 generate_PUSHCHANNEL(cctx, NULL);
5517 break;
5518 case VAR_NUMBER:
5519 case VAR_UNKNOWN:
5520 case VAR_ANY:
5521 case VAR_PARTIAL:
5522 case VAR_VOID:
5523 case VAR_SPECIAL: // cannot happen
5524 generate_PUSHNR(cctx, 0);
5525 break;
5526 }
5527 }
5528 if (var_count == 0)
5529 end = p;
5530 }
5531
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005532 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005533 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005534 break;
5535
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005536 if (oplen > 0 && *op != '=')
5537 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005538 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005539 type_T *stacktype;
5540
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005541 if (*op == '.')
5542 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005543 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005544 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005546 if (
5547#ifdef FEAT_FLOAT
5548 // If variable is float operation with number is OK.
5549 !(expected == &t_float && stacktype == &t_number) &&
5550#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005551 need_type(stacktype, expected, -1, cctx,
5552 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005553 goto theend;
5554
5555 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005556 {
5557 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5558 goto theend;
5559 }
5560 else if (*op == '+')
5561 {
5562 if (generate_add_instr(cctx,
5563 operator_type(member_type, stacktype),
5564 member_type, stacktype) == FAIL)
5565 goto theend;
5566 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005567 else if (generate_two_op(cctx, op) == FAIL)
5568 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005572 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005573 int r;
5574
5575 // Compile the "idx" in "var[idx]".
5576 if (new_local)
5577 --cctx->ctx_locals.ga_len;
5578 p = skipwhite(var_start + varlen + 1);
5579 r = compile_expr0(&p, cctx);
5580 if (new_local)
5581 ++cctx->ctx_locals.ga_len;
5582 if (r == FAIL)
5583 goto theend;
5584 if (*skipwhite(p) != ']')
5585 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005586 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005587 emsg(_(e_missbrac));
5588 goto theend;
5589 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005590 if (type == &t_any)
5591 {
5592 type_T *idx_type = ((type_T **)stack->ga_data)[
5593 stack->ga_len - 1];
5594 // Index on variable of unknown type: guess the type from the
5595 // index type: number is dict, otherwise dict.
5596 // TODO: should do the assignment at runtime
5597 if (idx_type->tt_type == VAR_NUMBER)
5598 type = &t_list_any;
5599 else
5600 type = &t_dict_any;
5601 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005602 if (type->tt_type == VAR_DICT
5603 && may_generate_2STRING(-1, cctx) == FAIL)
5604 goto theend;
5605 if (type->tt_type == VAR_LIST
5606 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005607 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 {
5609 emsg(_(e_number_exp));
5610 goto theend;
5611 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005612
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613 // Load the dict or list. On the stack we then have:
5614 // - value
5615 // - index
5616 // - variable
5617 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005618
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005619 if (type->tt_type == VAR_LIST)
5620 {
5621 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5622 return FAIL;
5623 }
5624 else if (type->tt_type == VAR_DICT)
5625 {
5626 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5627 return FAIL;
5628 }
5629 else
5630 {
5631 emsg(_(e_listreq));
5632 goto theend;
5633 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005634 }
5635 else
5636 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005637 if (is_decl && cmdidx == CMD_const
5638 && (dest == dest_script || dest == dest_local))
5639 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005640 generate_LOCKCONST(cctx);
5641
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005642 switch (dest)
5643 {
5644 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005645 generate_STOREOPT(cctx, skip_option_env_lead(name),
5646 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005647 break;
5648 case dest_global:
5649 // include g: with the name, easier to execute that way
5650 generate_STORE(cctx, ISN_STOREG, 0, name);
5651 break;
5652 case dest_buffer:
5653 // include b: with the name, easier to execute that way
5654 generate_STORE(cctx, ISN_STOREB, 0, name);
5655 break;
5656 case dest_window:
5657 // include w: with the name, easier to execute that way
5658 generate_STORE(cctx, ISN_STOREW, 0, name);
5659 break;
5660 case dest_tab:
5661 // include t: with the name, easier to execute that way
5662 generate_STORE(cctx, ISN_STORET, 0, name);
5663 break;
5664 case dest_env:
5665 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5666 break;
5667 case dest_reg:
5668 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5669 break;
5670 case dest_vimvar:
5671 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5672 break;
5673 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005674 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005675 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005676 {
5677 char_u *name_s = name;
5678
5679 // Include s: in the name for store_var()
5680 if (name[1] != ':')
5681 {
5682 int len = (int)STRLEN(name) + 3;
5683
5684 name_s = alloc(len);
5685 if (name_s == NULL)
5686 name_s = name;
5687 else
5688 vim_snprintf((char *)name_s, len,
5689 "s:%s", name);
5690 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005691 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5692 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005693 if (name_s != name)
5694 vim_free(name_s);
5695 }
5696 else
5697 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005698 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005699 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005700 break;
5701 case dest_local:
5702 if (lvar != NULL)
5703 {
5704 isn_T *isn = ((isn_T *)instr->ga_data)
5705 + instr->ga_len - 1;
5706
5707 // optimization: turn "var = 123" from ISN_PUSHNR +
5708 // ISN_STORE into ISN_STORENR
5709 if (!lvar->lv_from_outer
5710 && instr->ga_len == instr_count + 1
5711 && isn->isn_type == ISN_PUSHNR)
5712 {
5713 varnumber_T val = isn->isn_arg.number;
5714
5715 isn->isn_type = ISN_STORENR;
5716 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5717 isn->isn_arg.storenr.stnr_val = val;
5718 if (stack->ga_len > 0)
5719 --stack->ga_len;
5720 }
5721 else if (lvar->lv_from_outer)
5722 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005723 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005724 else
5725 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5726 }
5727 break;
5728 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005729 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005730
5731 if (var_idx + 1 < var_count)
5732 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005734
5735 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005736 if (var_count > 0 && !semicolon)
5737 {
5738 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5739 goto theend;
5740 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005741
Bram Moolenaarb2097502020-07-19 17:17:02 +02005742 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743
5744theend:
5745 vim_free(name);
5746 return ret;
5747}
5748
5749/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005750 * Check if "name" can be "unlet".
5751 */
5752 int
5753check_vim9_unlet(char_u *name)
5754{
5755 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5756 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005757 // "unlet s:var" is allowed in legacy script.
5758 if (*name == 's' && !script_is_vim9())
5759 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005760 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005761 return FAIL;
5762 }
5763 return OK;
5764}
5765
5766/*
5767 * Callback passed to ex_unletlock().
5768 */
5769 static int
5770compile_unlet(
5771 lval_T *lvp,
5772 char_u *name_end,
5773 exarg_T *eap,
5774 int deep UNUSED,
5775 void *coookie)
5776{
5777 cctx_T *cctx = coookie;
5778
5779 if (lvp->ll_tv == NULL)
5780 {
5781 char_u *p = lvp->ll_name;
5782 int cc = *name_end;
5783 int ret = OK;
5784
5785 // Normal name. Only supports g:, w:, t: and b: namespaces.
5786 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005787 if (*p == '$')
5788 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5789 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005790 ret = FAIL;
5791 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005792 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005793
5794 *name_end = cc;
5795 return ret;
5796 }
5797
5798 // TODO: unlet {list}[idx]
5799 // TODO: unlet {dict}[key]
5800 emsg("Sorry, :unlet not fully implemented yet");
5801 return FAIL;
5802}
5803
5804/*
5805 * compile "unlet var", "lock var" and "unlock var"
5806 * "arg" points to "var".
5807 */
5808 static char_u *
5809compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5810{
5811 char_u *p = arg;
5812
5813 if (eap->cmdidx != CMD_unlet)
5814 {
5815 emsg("Sorry, :lock and unlock not implemented yet");
5816 return NULL;
5817 }
5818
5819 if (*p == '!')
5820 {
5821 p = skipwhite(p + 1);
5822 eap->forceit = TRUE;
5823 }
5824
5825 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5826 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5827}
5828
5829/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005830 * Compile an :import command.
5831 */
5832 static char_u *
5833compile_import(char_u *arg, cctx_T *cctx)
5834{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005835 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836}
5837
5838/*
5839 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5840 */
5841 static int
5842compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5843{
5844 garray_T *instr = &cctx->ctx_instr;
5845 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5846
5847 if (endlabel == NULL)
5848 return FAIL;
5849 endlabel->el_next = *el;
5850 *el = endlabel;
5851 endlabel->el_end_label = instr->ga_len;
5852
5853 generate_JUMP(cctx, when, 0);
5854 return OK;
5855}
5856
5857 static void
5858compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5859{
5860 garray_T *instr = &cctx->ctx_instr;
5861
5862 while (*el != NULL)
5863 {
5864 endlabel_T *cur = (*el);
5865 isn_T *isn;
5866
5867 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5868 isn->isn_arg.jump.jump_where = instr->ga_len;
5869 *el = cur->el_next;
5870 vim_free(cur);
5871 }
5872}
5873
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005874 static void
5875compile_free_jump_to_end(endlabel_T **el)
5876{
5877 while (*el != NULL)
5878 {
5879 endlabel_T *cur = (*el);
5880
5881 *el = cur->el_next;
5882 vim_free(cur);
5883 }
5884}
5885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886/*
5887 * Create a new scope and set up the generic items.
5888 */
5889 static scope_T *
5890new_scope(cctx_T *cctx, scopetype_T type)
5891{
5892 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5893
5894 if (scope == NULL)
5895 return NULL;
5896 scope->se_outer = cctx->ctx_scope;
5897 cctx->ctx_scope = scope;
5898 scope->se_type = type;
5899 scope->se_local_count = cctx->ctx_locals.ga_len;
5900 return scope;
5901}
5902
5903/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005904 * Free the current scope and go back to the outer scope.
5905 */
5906 static void
5907drop_scope(cctx_T *cctx)
5908{
5909 scope_T *scope = cctx->ctx_scope;
5910
5911 if (scope == NULL)
5912 {
5913 iemsg("calling drop_scope() without a scope");
5914 return;
5915 }
5916 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005917 switch (scope->se_type)
5918 {
5919 case IF_SCOPE:
5920 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5921 case FOR_SCOPE:
5922 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5923 case WHILE_SCOPE:
5924 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5925 case TRY_SCOPE:
5926 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5927 case NO_SCOPE:
5928 case BLOCK_SCOPE:
5929 break;
5930 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005931 vim_free(scope);
5932}
5933
5934/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005935 * Check that the top of the type stack has a type that can be used as a
5936 * condition. Give an error and return FAIL if not.
5937 */
5938 static int
5939bool_on_stack(cctx_T *cctx)
5940{
5941 garray_T *stack = &cctx->ctx_type_stack;
5942 type_T *type;
5943
5944 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5945 if (type != &t_bool && type != &t_number && type != &t_any
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005946 && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar13106602020-10-04 16:06:05 +02005947 return FAIL;
5948 return OK;
5949}
5950
5951/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952 * compile "if expr"
5953 *
5954 * "if expr" Produces instructions:
5955 * EVAL expr Push result of "expr"
5956 * JUMP_IF_FALSE end
5957 * ... body ...
5958 * end:
5959 *
5960 * "if expr | else" Produces instructions:
5961 * EVAL expr Push result of "expr"
5962 * JUMP_IF_FALSE else
5963 * ... body ...
5964 * JUMP_ALWAYS end
5965 * else:
5966 * ... body ...
5967 * end:
5968 *
5969 * "if expr1 | elseif expr2 | else" Produces instructions:
5970 * EVAL expr Push result of "expr"
5971 * JUMP_IF_FALSE elseif
5972 * ... body ...
5973 * JUMP_ALWAYS end
5974 * elseif:
5975 * EVAL expr Push result of "expr"
5976 * JUMP_IF_FALSE else
5977 * ... body ...
5978 * JUMP_ALWAYS end
5979 * else:
5980 * ... body ...
5981 * end:
5982 */
5983 static char_u *
5984compile_if(char_u *arg, cctx_T *cctx)
5985{
5986 char_u *p = arg;
5987 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005988 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005990 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005991 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992
Bram Moolenaara5565e42020-05-09 15:44:01 +02005993 CLEAR_FIELD(ppconst);
5994 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005995 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005996 clear_ppconst(&ppconst);
5997 return NULL;
5998 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005999 if (cctx->ctx_skip == SKIP_YES)
6000 clear_ppconst(&ppconst);
6001 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006002 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006003 int error = FALSE;
6004 int v;
6005
Bram Moolenaara5565e42020-05-09 15:44:01 +02006006 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006007 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006008 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006009 if (error)
6010 return NULL;
6011 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006012 }
6013 else
6014 {
6015 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006016 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006017 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006018 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006019 if (bool_on_stack(cctx) == FAIL)
6020 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006022
6023 scope = new_scope(cctx, IF_SCOPE);
6024 if (scope == NULL)
6025 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006026 scope->se_skip_save = skip_save;
6027 // "is_had_return" will be reset if any block does not end in :return
6028 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006030 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006031 {
6032 // "where" is set when ":elseif", "else" or ":endif" is found
6033 scope->se_u.se_if.is_if_label = instr->ga_len;
6034 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6035 }
6036 else
6037 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038
6039 return p;
6040}
6041
6042 static char_u *
6043compile_elseif(char_u *arg, cctx_T *cctx)
6044{
6045 char_u *p = arg;
6046 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006047 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 isn_T *isn;
6049 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006050 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006051 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052
6053 if (scope == NULL || scope->se_type != IF_SCOPE)
6054 {
6055 emsg(_(e_elseif_without_if));
6056 return NULL;
6057 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006058 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006059 if (!cctx->ctx_had_return)
6060 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006062 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006063 {
6064 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006066 return NULL;
6067 // previous "if" or "elseif" jumps here
6068 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6069 isn->isn_arg.jump.jump_where = instr->ga_len;
6070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006072 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006073 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006074 if (cctx->ctx_skip == SKIP_YES)
6075 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006076 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006077 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006078 clear_ppconst(&ppconst);
6079 return NULL;
6080 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006081 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006082 if (scope->se_skip_save == SKIP_YES)
6083 clear_ppconst(&ppconst);
6084 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006085 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006086 int error = FALSE;
6087 int v;
6088
Bram Moolenaar7f141552020-05-09 17:35:53 +02006089 // The expression results in a constant.
6090 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006091 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6092 if (error)
6093 return NULL;
6094 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006095 clear_ppconst(&ppconst);
6096 scope->se_u.se_if.is_if_label = -1;
6097 }
6098 else
6099 {
6100 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006101 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006102 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006103 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006104 if (bool_on_stack(cctx) == FAIL)
6105 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006107 // "where" is set when ":elseif", "else" or ":endif" is found
6108 scope->se_u.se_if.is_if_label = instr->ga_len;
6109 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111
6112 return p;
6113}
6114
6115 static char_u *
6116compile_else(char_u *arg, cctx_T *cctx)
6117{
6118 char_u *p = arg;
6119 garray_T *instr = &cctx->ctx_instr;
6120 isn_T *isn;
6121 scope_T *scope = cctx->ctx_scope;
6122
6123 if (scope == NULL || scope->se_type != IF_SCOPE)
6124 {
6125 emsg(_(e_else_without_if));
6126 return NULL;
6127 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006128 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006129 if (!cctx->ctx_had_return)
6130 scope->se_u.se_if.is_had_return = FALSE;
6131 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132
Bram Moolenaarefd88552020-06-18 20:50:10 +02006133 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006134 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006135 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006136 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006137 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006138 if (!cctx->ctx_had_return
6139 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6140 JUMP_ALWAYS, cctx) == FAIL)
6141 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006142 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006143
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006144 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006145 {
6146 if (scope->se_u.se_if.is_if_label >= 0)
6147 {
6148 // previous "if" or "elseif" jumps here
6149 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6150 isn->isn_arg.jump.jump_where = instr->ga_len;
6151 scope->se_u.se_if.is_if_label = -1;
6152 }
6153 }
6154
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006155 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006156 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158
6159 return p;
6160}
6161
6162 static char_u *
6163compile_endif(char_u *arg, cctx_T *cctx)
6164{
6165 scope_T *scope = cctx->ctx_scope;
6166 ifscope_T *ifscope;
6167 garray_T *instr = &cctx->ctx_instr;
6168 isn_T *isn;
6169
6170 if (scope == NULL || scope->se_type != IF_SCOPE)
6171 {
6172 emsg(_(e_endif_without_if));
6173 return NULL;
6174 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006175 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006176 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006177 if (!cctx->ctx_had_return)
6178 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006180 if (scope->se_u.se_if.is_if_label >= 0)
6181 {
6182 // previous "if" or "elseif" jumps here
6183 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6184 isn->isn_arg.jump.jump_where = instr->ga_len;
6185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 // Fill in the "end" label in jumps at the end of the blocks.
6187 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006188 cctx->ctx_skip = scope->se_skip_save;
6189
6190 // If all the blocks end in :return and there is an :else then the
6191 // had_return flag is set.
6192 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006194 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 return arg;
6196}
6197
6198/*
6199 * compile "for var in expr"
6200 *
6201 * Produces instructions:
6202 * PUSHNR -1
6203 * STORE loop-idx Set index to -1
6204 * EVAL expr Push result of "expr"
6205 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6206 * - if beyond end, jump to "end"
6207 * - otherwise get item from list and push it
6208 * STORE var Store item in "var"
6209 * ... body ...
6210 * JUMP top Jump back to repeat
6211 * end: DROP Drop the result of "expr"
6212 *
6213 */
6214 static char_u *
6215compile_for(char_u *arg, cctx_T *cctx)
6216{
6217 char_u *p;
6218 size_t varlen;
6219 garray_T *instr = &cctx->ctx_instr;
6220 garray_T *stack = &cctx->ctx_type_stack;
6221 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006222 lvar_T *loop_lvar; // loop iteration variable
6223 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 type_T *vartype;
6225
6226 // TODO: list of variables: "for [key, value] in dict"
6227 // parse "var"
6228 for (p = arg; eval_isnamec1(*p); ++p)
6229 ;
6230 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006231 var_lvar = lookup_local(arg, varlen, cctx);
6232 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006234 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 return NULL;
6236 }
6237
6238 // consume "in"
6239 p = skipwhite(p);
6240 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6241 {
6242 emsg(_(e_missing_in));
6243 return NULL;
6244 }
6245 p = skipwhite(p + 2);
6246
6247
6248 scope = new_scope(cctx, FOR_SCOPE);
6249 if (scope == NULL)
6250 return NULL;
6251
6252 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006253 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6254 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006255 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006256 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006257 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260
6261 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006262 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6263 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006264 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006265 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006266 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006270 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271
6272 // compile "expr", it remains on the stack until "endfor"
6273 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006274 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006275 {
6276 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006280 // Now that we know the type of "var", check that it is a list, now or at
6281 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006283 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006285 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 return NULL;
6287 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006288 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006289 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290
6291 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006292 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006294 generate_FOR(cctx, loop_lvar->lv_idx);
6295 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296
6297 return arg;
6298}
6299
6300/*
6301 * compile "endfor"
6302 */
6303 static char_u *
6304compile_endfor(char_u *arg, cctx_T *cctx)
6305{
6306 garray_T *instr = &cctx->ctx_instr;
6307 scope_T *scope = cctx->ctx_scope;
6308 forscope_T *forscope;
6309 isn_T *isn;
6310
6311 if (scope == NULL || scope->se_type != FOR_SCOPE)
6312 {
6313 emsg(_(e_for));
6314 return NULL;
6315 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006316 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006318 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319
6320 // At end of ":for" scope jump back to the FOR instruction.
6321 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6322
6323 // Fill in the "end" label in the FOR statement so it can jump here
6324 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6325 isn->isn_arg.forloop.for_end = instr->ga_len;
6326
6327 // Fill in the "end" label any BREAK statements
6328 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6329
6330 // Below the ":for" scope drop the "expr" list from the stack.
6331 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6332 return NULL;
6333
6334 vim_free(scope);
6335
6336 return arg;
6337}
6338
6339/*
6340 * compile "while expr"
6341 *
6342 * Produces instructions:
6343 * top: EVAL expr Push result of "expr"
6344 * JUMP_IF_FALSE end jump if false
6345 * ... body ...
6346 * JUMP top Jump back to repeat
6347 * end:
6348 *
6349 */
6350 static char_u *
6351compile_while(char_u *arg, cctx_T *cctx)
6352{
6353 char_u *p = arg;
6354 garray_T *instr = &cctx->ctx_instr;
6355 scope_T *scope;
6356
6357 scope = new_scope(cctx, WHILE_SCOPE);
6358 if (scope == NULL)
6359 return NULL;
6360
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006361 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006362
6363 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006364 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365 return NULL;
6366
Bram Moolenaar13106602020-10-04 16:06:05 +02006367 if (bool_on_stack(cctx) == FAIL)
6368 return FAIL;
6369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006371 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372 JUMP_IF_FALSE, cctx) == FAIL)
6373 return FAIL;
6374
6375 return p;
6376}
6377
6378/*
6379 * compile "endwhile"
6380 */
6381 static char_u *
6382compile_endwhile(char_u *arg, cctx_T *cctx)
6383{
6384 scope_T *scope = cctx->ctx_scope;
6385
6386 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6387 {
6388 emsg(_(e_while));
6389 return NULL;
6390 }
6391 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006392 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393
6394 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006395 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006396
6397 // Fill in the "end" label in the WHILE statement so it can jump here.
6398 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006399 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400
6401 vim_free(scope);
6402
6403 return arg;
6404}
6405
6406/*
6407 * compile "continue"
6408 */
6409 static char_u *
6410compile_continue(char_u *arg, cctx_T *cctx)
6411{
6412 scope_T *scope = cctx->ctx_scope;
6413
6414 for (;;)
6415 {
6416 if (scope == NULL)
6417 {
6418 emsg(_(e_continue));
6419 return NULL;
6420 }
6421 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6422 break;
6423 scope = scope->se_outer;
6424 }
6425
6426 // Jump back to the FOR or WHILE instruction.
6427 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006428 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6429 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 return arg;
6431}
6432
6433/*
6434 * compile "break"
6435 */
6436 static char_u *
6437compile_break(char_u *arg, cctx_T *cctx)
6438{
6439 scope_T *scope = cctx->ctx_scope;
6440 endlabel_T **el;
6441
6442 for (;;)
6443 {
6444 if (scope == NULL)
6445 {
6446 emsg(_(e_break));
6447 return NULL;
6448 }
6449 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6450 break;
6451 scope = scope->se_outer;
6452 }
6453
6454 // Jump to the end of the FOR or WHILE loop.
6455 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006456 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006458 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6460 return FAIL;
6461
6462 return arg;
6463}
6464
6465/*
6466 * compile "{" start of block
6467 */
6468 static char_u *
6469compile_block(char_u *arg, cctx_T *cctx)
6470{
6471 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6472 return NULL;
6473 return skipwhite(arg + 1);
6474}
6475
6476/*
6477 * compile end of block: drop one scope
6478 */
6479 static void
6480compile_endblock(cctx_T *cctx)
6481{
6482 scope_T *scope = cctx->ctx_scope;
6483
6484 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006485 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 vim_free(scope);
6487}
6488
6489/*
6490 * compile "try"
6491 * Creates a new scope for the try-endtry, pointing to the first catch and
6492 * finally.
6493 * Creates another scope for the "try" block itself.
6494 * TRY instruction sets up exception handling at runtime.
6495 *
6496 * "try"
6497 * TRY -> catch1, -> finally push trystack entry
6498 * ... try block
6499 * "throw {exception}"
6500 * EVAL {exception}
6501 * THROW create exception
6502 * ... try block
6503 * " catch {expr}"
6504 * JUMP -> finally
6505 * catch1: PUSH exeception
6506 * EVAL {expr}
6507 * MATCH
6508 * JUMP nomatch -> catch2
6509 * CATCH remove exception
6510 * ... catch block
6511 * " catch"
6512 * JUMP -> finally
6513 * catch2: CATCH remove exception
6514 * ... catch block
6515 * " finally"
6516 * finally:
6517 * ... finally block
6518 * " endtry"
6519 * ENDTRY pop trystack entry, may rethrow
6520 */
6521 static char_u *
6522compile_try(char_u *arg, cctx_T *cctx)
6523{
6524 garray_T *instr = &cctx->ctx_instr;
6525 scope_T *try_scope;
6526 scope_T *scope;
6527
6528 // scope that holds the jumps that go to catch/finally/endtry
6529 try_scope = new_scope(cctx, TRY_SCOPE);
6530 if (try_scope == NULL)
6531 return NULL;
6532
6533 // "catch" is set when the first ":catch" is found.
6534 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006535 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 if (generate_instr(cctx, ISN_TRY) == NULL)
6537 return NULL;
6538
6539 // scope for the try block itself
6540 scope = new_scope(cctx, BLOCK_SCOPE);
6541 if (scope == NULL)
6542 return NULL;
6543
6544 return arg;
6545}
6546
6547/*
6548 * compile "catch {expr}"
6549 */
6550 static char_u *
6551compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6552{
6553 scope_T *scope = cctx->ctx_scope;
6554 garray_T *instr = &cctx->ctx_instr;
6555 char_u *p;
6556 isn_T *isn;
6557
6558 // end block scope from :try or :catch
6559 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6560 compile_endblock(cctx);
6561 scope = cctx->ctx_scope;
6562
6563 // Error if not in a :try scope
6564 if (scope == NULL || scope->se_type != TRY_SCOPE)
6565 {
6566 emsg(_(e_catch));
6567 return NULL;
6568 }
6569
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006570 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006572 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 return NULL;
6574 }
6575
6576 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006577 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 JUMP_ALWAYS, cctx) == FAIL)
6579 return NULL;
6580
6581 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006582 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 if (isn->isn_arg.try.try_catch == 0)
6584 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006585 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 {
6587 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006588 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 isn->isn_arg.jump.jump_where = instr->ga_len;
6590 }
6591
6592 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006593 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006595 scope->se_u.se_try.ts_caught_all = TRUE;
6596 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 }
6598 else
6599 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006600 char_u *end;
6601 char_u *pat;
6602 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006603 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006604 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 // Push v:exception, push {expr} and MATCH
6607 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6608
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006609 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006610 if (*end != *p)
6611 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006612 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006613 vim_free(tofree);
6614 return FAIL;
6615 }
6616 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006617 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006618 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006619 len = (int)(end - tofree);
6620 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006621 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006622 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006623 if (pat == NULL)
6624 return FAIL;
6625 if (generate_PUSHS(cctx, pat) == FAIL)
6626 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6629 return NULL;
6630
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006631 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6633 return NULL;
6634 }
6635
6636 if (generate_instr(cctx, ISN_CATCH) == NULL)
6637 return NULL;
6638
6639 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6640 return NULL;
6641 return p;
6642}
6643
6644 static char_u *
6645compile_finally(char_u *arg, cctx_T *cctx)
6646{
6647 scope_T *scope = cctx->ctx_scope;
6648 garray_T *instr = &cctx->ctx_instr;
6649 isn_T *isn;
6650
6651 // end block scope from :try or :catch
6652 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6653 compile_endblock(cctx);
6654 scope = cctx->ctx_scope;
6655
6656 // Error if not in a :try scope
6657 if (scope == NULL || scope->se_type != TRY_SCOPE)
6658 {
6659 emsg(_(e_finally));
6660 return NULL;
6661 }
6662
6663 // End :catch or :finally scope: set value in ISN_TRY instruction
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_finally != 0)
6666 {
6667 emsg(_(e_finally_dup));
6668 return NULL;
6669 }
6670
6671 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006672 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673
Bram Moolenaar585fea72020-04-02 22:33:21 +02006674 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006675 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 {
6677 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006678 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006680 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 }
6682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 // TODO: set index in ts_finally_label jumps
6684
6685 return arg;
6686}
6687
6688 static char_u *
6689compile_endtry(char_u *arg, cctx_T *cctx)
6690{
6691 scope_T *scope = cctx->ctx_scope;
6692 garray_T *instr = &cctx->ctx_instr;
6693 isn_T *isn;
6694
6695 // end block scope from :catch or :finally
6696 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6697 compile_endblock(cctx);
6698 scope = cctx->ctx_scope;
6699
6700 // Error if not in a :try scope
6701 if (scope == NULL || scope->se_type != TRY_SCOPE)
6702 {
6703 if (scope == NULL)
6704 emsg(_(e_no_endtry));
6705 else if (scope->se_type == WHILE_SCOPE)
6706 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006707 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708 emsg(_(e_endfor));
6709 else
6710 emsg(_(e_endif));
6711 return NULL;
6712 }
6713
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006714 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6716 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006717 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 return NULL;
6719 }
6720
6721 // Fill in the "end" label in jumps at the end of the blocks, if not done
6722 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006723 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724
6725 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006726 if (isn->isn_arg.try.try_catch == 0)
6727 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728 if (isn->isn_arg.try.try_finally == 0)
6729 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006730
6731 if (scope->se_u.se_try.ts_catch_label != 0)
6732 {
6733 // Last catch without match jumps here
6734 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6735 isn->isn_arg.jump.jump_where = instr->ga_len;
6736 }
6737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 compile_endblock(cctx);
6739
6740 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6741 return NULL;
6742 return arg;
6743}
6744
6745/*
6746 * compile "throw {expr}"
6747 */
6748 static char_u *
6749compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6750{
6751 char_u *p = skipwhite(arg);
6752
Bram Moolenaara5565e42020-05-09 15:44:01 +02006753 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 return NULL;
6755 if (may_generate_2STRING(-1, cctx) == FAIL)
6756 return NULL;
6757 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6758 return NULL;
6759
6760 return p;
6761}
6762
6763/*
6764 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006765 * compile "echomsg expr"
6766 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006767 * compile "execute expr"
6768 */
6769 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006770compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006771{
6772 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006773 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006774 int count = 0;
6775
6776 for (;;)
6777 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006778 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006779 return NULL;
6780 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006781 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006782 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006783 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006784 break;
6785 }
6786
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006787 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6788 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6789 else if (cmdidx == CMD_execute)
6790 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6791 else if (cmdidx == CMD_echomsg)
6792 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6793 else
6794 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 return p;
6796}
6797
6798/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006799 * :put r
6800 * :put ={expr}
6801 */
6802 static char_u *
6803compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6804{
6805 char_u *line = arg;
6806 linenr_T lnum;
6807 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006808 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006809
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006810 eap->regname = *line;
6811
6812 if (eap->regname == '=')
6813 {
6814 char_u *p = line + 1;
6815
6816 if (compile_expr0(&p, cctx) == FAIL)
6817 return NULL;
6818 line = p;
6819 }
6820 else if (eap->regname != NUL)
6821 ++line;
6822
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006823 // "errormsg" will not be set because the range is ADDR_LINES.
6824 // TODO: if the range contains something like "$" or "." need to evaluate
6825 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006826 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6827 return NULL;
6828 if (eap->addr_count == 0)
6829 lnum = -1;
6830 else
6831 lnum = eap->line2;
6832 if (above)
6833 --lnum;
6834
6835 generate_PUT(cctx, eap->regname, lnum);
6836 return line;
6837}
6838
6839/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006840 * A command that is not compiled, execute with legacy code.
6841 */
6842 static char_u *
6843compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6844{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006845 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006846 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006847 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006848
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006849 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006850 goto theend;
6851
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006852 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006853 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006854 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006855 int usefilter = FALSE;
6856
6857 has_expr = argt & (EX_XFILE | EX_EXPAND);
6858
6859 // If the command can be followed by a bar, find the bar and truncate
6860 // it, so that the following command can be compiled.
6861 // The '|' is overwritten with a NUL, it is put back below.
6862 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6863 && *eap->arg == '!')
6864 // :w !filter or :r !filter or :r! filter
6865 usefilter = TRUE;
6866 if ((argt & EX_TRLBAR) && !usefilter)
6867 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006868 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006869 separate_nextcmd(eap);
6870 if (eap->nextcmd != NULL)
6871 nextcmd = eap->nextcmd;
6872 }
6873 }
6874
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006875 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6876 {
6877 // expand filename in "syntax include [@group] filename"
6878 has_expr = TRUE;
6879 eap->arg = skipwhite(eap->arg + 7);
6880 if (*eap->arg == '@')
6881 eap->arg = skiptowhite(eap->arg);
6882 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006883
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006884 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006885 {
6886 int count = 0;
6887 char_u *start = skipwhite(line);
6888
6889 // :cmd xxx`=expr1`yyy`=expr2`zzz
6890 // PUSHS ":cmd xxx"
6891 // eval expr1
6892 // PUSHS "yyy"
6893 // eval expr2
6894 // PUSHS "zzz"
6895 // EXECCONCAT 5
6896 for (;;)
6897 {
6898 if (p > start)
6899 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006900 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006901 ++count;
6902 }
6903 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006904 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006905 return NULL;
6906 may_generate_2STRING(-1, cctx);
6907 ++count;
6908 p = skipwhite(p);
6909 if (*p != '`')
6910 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006911 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006912 return NULL;
6913 }
6914 start = p + 1;
6915
6916 p = (char_u *)strstr((char *)start, "`=");
6917 if (p == NULL)
6918 {
6919 if (*skipwhite(start) != NUL)
6920 {
6921 generate_PUSHS(cctx, vim_strsave(start));
6922 ++count;
6923 }
6924 break;
6925 }
6926 }
6927 generate_EXECCONCAT(cctx, count);
6928 }
6929 else
6930 generate_EXEC(cctx, line);
6931
6932theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006933 if (*nextcmd != NUL)
6934 {
6935 // the parser expects a pointer to the bar, put it back
6936 --nextcmd;
6937 *nextcmd = '|';
6938 }
6939
6940 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006941}
6942
6943/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006944 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006945 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006946 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006947 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006948add_def_function(ufunc_T *ufunc)
6949{
6950 dfunc_T *dfunc;
6951
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006952 if (def_functions.ga_len == 0)
6953 {
6954 // The first position is not used, so that a zero uf_dfunc_idx means it
6955 // wasn't set.
6956 if (ga_grow(&def_functions, 1) == FAIL)
6957 return FAIL;
6958 ++def_functions.ga_len;
6959 }
6960
Bram Moolenaar09689a02020-05-09 22:50:08 +02006961 // Add the function to "def_functions".
6962 if (ga_grow(&def_functions, 1) == FAIL)
6963 return FAIL;
6964 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6965 CLEAR_POINTER(dfunc);
6966 dfunc->df_idx = def_functions.ga_len;
6967 ufunc->uf_dfunc_idx = dfunc->df_idx;
6968 dfunc->df_ufunc = ufunc;
6969 ++def_functions.ga_len;
6970 return OK;
6971}
6972
6973/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 * After ex_function() has collected all the function lines: parse and compile
6975 * the lines into instructions.
6976 * Adds the function to "def_functions".
6977 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6978 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006979 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006980 * This can be used recursively through compile_lambda(), which may reallocate
6981 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006982 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006984 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006985compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 char_u *line = NULL;
6988 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 cctx_T cctx;
6991 garray_T *instr;
6992 int called_emsg_before = called_emsg;
6993 int ret = FAIL;
6994 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006995 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006996 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006997 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006998 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007000 // When using a function that was compiled before: Free old instructions.
7001 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007002 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007004 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7005 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007006 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007008 else
7009 {
7010 if (add_def_function(ufunc) == FAIL)
7011 return FAIL;
7012 new_def_function = TRUE;
7013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014
Bram Moolenaar985116a2020-07-12 17:31:09 +02007015 ufunc->uf_def_status = UF_COMPILING;
7016
Bram Moolenaara80faa82020-04-12 19:37:17 +02007017 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 cctx.ctx_ufunc = ufunc;
7019 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007020 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7022 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7023 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7024 cctx.ctx_type_list = &ufunc->uf_type_list;
7025 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7026 instr = &cctx.ctx_instr;
7027
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007028 // Set the context to the function, it may be compiled when called from
7029 // another script. Set the script version to the most modern one.
7030 // The line number will be set in next_line_from_context().
7031 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7033
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007034 // Make sure error messages are OK.
7035 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7036 if (do_estack_push)
7037 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007038 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007039
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007040 if (ufunc->uf_def_args.ga_len > 0)
7041 {
7042 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007043 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007044 int i;
7045 char_u *arg;
7046 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007047 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007048
7049 // Produce instructions for the default values of optional arguments.
7050 // Store the instruction index in uf_def_arg_idx[] so that we know
7051 // where to start when the function is called, depending on the number
7052 // of arguments.
7053 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7054 if (ufunc->uf_def_arg_idx == NULL)
7055 goto erret;
7056 for (i = 0; i < count; ++i)
7057 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007058 garray_T *stack = &cctx.ctx_type_stack;
7059 type_T *val_type;
7060 int arg_idx = first_def_arg + i;
7061
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007062 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7063 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007064 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007065 goto erret;
7066
7067 // If no type specified use the type of the default value.
7068 // Otherwise check that the default value type matches the
7069 // specified type.
7070 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7071 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007072 {
7073 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007074 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007075 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007076 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7077 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007078 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007079
7080 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007081 goto erret;
7082 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007083 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007084
7085 if (did_set_arg_type)
7086 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007087 }
7088
7089 /*
7090 * Loop over all the lines of the function and generate instructions.
7091 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 for (;;)
7093 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007094 exarg_T ea;
7095 cmdmod_T save_cmdmod;
7096 int starts_with_colon = FALSE;
7097 char_u *cmd;
7098 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007099
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007100 // Bail out on the first error to avoid a flood of errors and report
7101 // the right line number when inside try/catch.
7102 if (emsg_before != called_emsg)
7103 goto erret;
7104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 if (line != NULL && *line == '|')
7106 // the line continues after a '|'
7107 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007108 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007109 && !(*line == '#' && (line == cctx.ctx_line_start
7110 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007112 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 goto erret;
7114 }
7115 else
7116 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007117 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007118 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007119 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007122 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123
Bram Moolenaara80faa82020-04-12 19:37:17 +02007124 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125 ea.cmdlinep = &line;
7126 ea.cmd = skipwhite(line);
7127
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007128 // Some things can be recognized by the first character.
7129 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007131 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007132 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007133 if (ea.cmd[1] != '{')
7134 {
7135 line = (char_u *)"";
7136 continue;
7137 }
7138 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007140 case '}':
7141 {
7142 // "}" ends a block scope
7143 scopetype_T stype = cctx.ctx_scope == NULL
7144 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007146 if (stype == BLOCK_SCOPE)
7147 {
7148 compile_endblock(&cctx);
7149 line = ea.cmd;
7150 }
7151 else
7152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007153 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007154 goto erret;
7155 }
7156 if (line != NULL)
7157 line = skipwhite(ea.cmd + 1);
7158 continue;
7159 }
7160
7161 case '{':
7162 // "{" starts a block scope
7163 // "{'a': 1}->func() is something else
7164 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7165 {
7166 line = compile_block(ea.cmd, &cctx);
7167 continue;
7168 }
7169 break;
7170
7171 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007172 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007173 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174 }
7175
7176 /*
7177 * COMMAND MODIFIERS
7178 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007179 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7181 {
7182 if (errormsg != NULL)
7183 goto erret;
7184 // empty line or comment
7185 line = (char_u *)"";
7186 continue;
7187 }
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007188 generate_cmdmods(&cctx);
7189
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007190 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007191 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192
7193 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007194 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007196 {
7197 if (*ea.cmd == '(')
7198 // not for "call()"
7199 ea.cmd = p;
7200 else
7201 ea.cmd = skipwhite(ea.cmd);
7202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007204 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007206 char_u *pskip;
7207
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007208 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007209 // find what follows.
7210 // Skip over "var.member", "var[idx]" and the like.
7211 // Also "&opt = val", "$ENV = val" and "@r = val".
7212 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007213 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007214 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007215 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007216 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007217 char_u *var_end;
7218 int oplen;
7219 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220
Bram Moolenaar65821722020-08-02 18:58:54 +02007221 if (ea.cmd[0] == '@')
7222 var_end = ea.cmd + 2;
7223 else
7224 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007225 FNE_CHECK_START | FNE_INCL_BR);
7226 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007227 if (oplen > 0)
7228 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007229 size_t len = p - ea.cmd;
7230
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007231 // Recognize an assignment if we recognize the variable
7232 // name:
7233 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007234 // "local = expr" where "local" is a local var.
7235 // "script = expr" where "script" is a script-local var.
7236 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007237 // "&opt = expr"
7238 // "$ENV = expr"
7239 // "@r = expr"
7240 if (*ea.cmd == '&'
7241 || *ea.cmd == '$'
7242 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007243 || ((len) > 2 && ea.cmd[1] == ':')
7244 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007245 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007246 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007247 || script_var_exists(ea.cmd, len,
7248 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007249 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007250 {
7251 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007252 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007253 goto erret;
7254 continue;
7255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 }
7257 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007258
7259 if (*ea.cmd == '[')
7260 {
7261 // [var, var] = expr
7262 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7263 if (line == NULL)
7264 goto erret;
7265 if (line != ea.cmd)
7266 continue;
7267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 }
7269
7270 /*
7271 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007272 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007274 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007275 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007276 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007277 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007278 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007279 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007280 if (!starts_with_colon)
7281 {
7282 emsg(_(e_colon_required_before_a_range));
7283 goto erret;
7284 }
7285 if (ends_excmd2(line, ea.cmd))
7286 {
7287 // A range without a command: jump to the line.
7288 // TODO: compile to a more efficient command, possibly
7289 // calling parse_cmd_address().
7290 ea.cmdidx = CMD_SIZE;
7291 line = compile_exec(line, &ea, &cctx);
7292 goto nextline;
7293 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007294 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007295 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007296 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007297 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007298 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299
7300 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7301 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007302 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007303 {
7304 line += STRLEN(line);
7305 continue;
7306 }
7307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007309 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007311 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007312 iemsg("Command from find_ex_command() not handled");
7313 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 }
7316
Bram Moolenaar3988f642020-08-27 22:43:03 +02007317 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007318 && ea.cmdidx != CMD_elseif
7319 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007320 && ea.cmdidx != CMD_endif
7321 && ea.cmdidx != CMD_endfor
7322 && ea.cmdidx != CMD_endwhile
7323 && ea.cmdidx != CMD_catch
7324 && ea.cmdidx != CMD_finally
7325 && ea.cmdidx != CMD_endtry)
7326 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007327 emsg(_(e_unreachable_code_after_return));
7328 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007329 }
7330
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007331 p = skipwhite(p);
7332 if (ea.cmdidx != CMD_SIZE
7333 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7334 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007335 if (ea.cmdidx >= 0)
7336 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007337 if ((ea.argt & EX_BANG) && *p == '!')
7338 {
7339 ea.forceit = TRUE;
7340 p = skipwhite(p + 1);
7341 }
7342 }
7343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 switch (ea.cmdidx)
7345 {
7346 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007347 ea.arg = p;
7348 line = compile_nested_function(&ea, &cctx);
7349 break;
7350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007352 // TODO: should we allow this, e.g. to declare a global
7353 // function?
7354 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 goto erret;
7356
7357 case CMD_return:
7358 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007359 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 break;
7361
7362 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007363 emsg(_(e_cannot_use_let_in_vim9_script));
7364 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007365 case CMD_var:
7366 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 case CMD_const:
7368 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007369 if (line == p)
7370 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371 break;
7372
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007373 case CMD_unlet:
7374 case CMD_unlockvar:
7375 case CMD_lockvar:
7376 line = compile_unletlock(p, &ea, &cctx);
7377 break;
7378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 case CMD_import:
7380 line = compile_import(p, &cctx);
7381 break;
7382
7383 case CMD_if:
7384 line = compile_if(p, &cctx);
7385 break;
7386 case CMD_elseif:
7387 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007388 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 break;
7390 case CMD_else:
7391 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007392 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 break;
7394 case CMD_endif:
7395 line = compile_endif(p, &cctx);
7396 break;
7397
7398 case CMD_while:
7399 line = compile_while(p, &cctx);
7400 break;
7401 case CMD_endwhile:
7402 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007403 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 break;
7405
7406 case CMD_for:
7407 line = compile_for(p, &cctx);
7408 break;
7409 case CMD_endfor:
7410 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007411 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 break;
7413 case CMD_continue:
7414 line = compile_continue(p, &cctx);
7415 break;
7416 case CMD_break:
7417 line = compile_break(p, &cctx);
7418 break;
7419
7420 case CMD_try:
7421 line = compile_try(p, &cctx);
7422 break;
7423 case CMD_catch:
7424 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007425 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 break;
7427 case CMD_finally:
7428 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007429 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 break;
7431 case CMD_endtry:
7432 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007433 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 break;
7435 case CMD_throw:
7436 line = compile_throw(p, &cctx);
7437 break;
7438
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007439 case CMD_eval:
7440 if (compile_expr0(&p, &cctx) == FAIL)
7441 goto erret;
7442
Bram Moolenaar3988f642020-08-27 22:43:03 +02007443 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007444 generate_instr_drop(&cctx, ISN_DROP, 1);
7445
7446 line = skipwhite(p);
7447 break;
7448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007451 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007452 case CMD_echomsg:
7453 case CMD_echoerr:
7454 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007455 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007457 case CMD_put:
7458 ea.cmd = cmd;
7459 line = compile_put(p, &ea, &cctx);
7460 break;
7461
Bram Moolenaar3988f642020-08-27 22:43:03 +02007462 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007463
Bram Moolenaarae616492020-07-28 20:07:27 +02007464 case CMD_append:
7465 case CMD_change:
7466 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007467 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007468 case CMD_xit:
7469 not_in_vim9(&ea);
7470 goto erret;
7471
Bram Moolenaar002262f2020-07-08 17:47:57 +02007472 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007473 if (cctx.ctx_skip != SKIP_YES)
7474 {
7475 semsg(_(e_invalid_command_str), ea.cmd);
7476 goto erret;
7477 }
7478 // We don't check for a next command here.
7479 line = (char_u *)"";
7480 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007483 if (cctx.ctx_skip == SKIP_YES)
7484 {
7485 // We don't check for a next command here.
7486 line = (char_u *)"";
7487 }
7488 else
7489 {
7490 // Not recognized, execute with do_cmdline_cmd().
7491 ea.arg = p;
7492 line = compile_exec(line, &ea, &cctx);
7493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 break;
7495 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007496nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 if (line == NULL)
7498 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007499 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007501 // Undo any command modifiers.
7502 generate_restore_cmdmods(&cctx);
7503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504 if (cctx.ctx_type_stack.ga_len < 0)
7505 {
7506 iemsg("Type stack underflow");
7507 goto erret;
7508 }
7509 }
7510
7511 if (cctx.ctx_scope != NULL)
7512 {
7513 if (cctx.ctx_scope->se_type == IF_SCOPE)
7514 emsg(_(e_endif));
7515 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7516 emsg(_(e_endwhile));
7517 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7518 emsg(_(e_endfor));
7519 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007520 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 goto erret;
7522 }
7523
Bram Moolenaarefd88552020-06-18 20:50:10 +02007524 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 {
7526 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7527 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007528 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 goto erret;
7530 }
7531
7532 // Return zero if there is no return at the end.
7533 generate_PUSHNR(&cctx, 0);
7534 generate_instr(&cctx, ISN_RETURN);
7535 }
7536
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007537 {
7538 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7539 + ufunc->uf_dfunc_idx;
7540 dfunc->df_deleted = FALSE;
7541 dfunc->df_instr = instr->ga_data;
7542 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007543 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007544 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007545 if (cctx.ctx_outer_used)
7546 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007547 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549
7550 ret = OK;
7551
7552erret:
7553 if (ret == FAIL)
7554 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007555 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007556 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7557 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007558
7559 for (idx = 0; idx < instr->ga_len; ++idx)
7560 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007561 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007562
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007563 // If using the last entry in the table and it was added above, we
7564 // might as well remove it.
7565 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007566 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007567 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007568 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007569 ufunc->uf_dfunc_idx = 0;
7570 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007571 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007572
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007573 while (cctx.ctx_scope != NULL)
7574 drop_scope(&cctx);
7575
Bram Moolenaar20431c92020-03-20 18:39:46 +01007576 // Don't execute this function body.
7577 ga_clear_strings(&ufunc->uf_lines);
7578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 if (errormsg != NULL)
7580 emsg(errormsg);
7581 else if (called_emsg == called_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007582 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007583 }
7584
7585 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007586 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007587 if (do_estack_push)
7588 estack_pop();
7589
Bram Moolenaar20431c92020-03-20 18:39:46 +01007590 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007591 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007593 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594}
7595
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007596 void
7597set_function_type(ufunc_T *ufunc)
7598{
7599 int varargs = ufunc->uf_va_name != NULL;
7600 int argcount = ufunc->uf_args.ga_len;
7601
7602 // Create a type for the function, with the return type and any
7603 // argument types.
7604 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7605 // The type is included in "tt_args".
7606 if (argcount > 0 || varargs)
7607 {
7608 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7609 argcount, &ufunc->uf_type_list);
7610 // Add argument types to the function type.
7611 if (func_type_add_arg_types(ufunc->uf_func_type,
7612 argcount + varargs,
7613 &ufunc->uf_type_list) == FAIL)
7614 return;
7615 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7616 ufunc->uf_func_type->tt_min_argcount =
7617 argcount - ufunc->uf_def_args.ga_len;
7618 if (ufunc->uf_arg_types == NULL)
7619 {
7620 int i;
7621
7622 // lambda does not have argument types.
7623 for (i = 0; i < argcount; ++i)
7624 ufunc->uf_func_type->tt_args[i] = &t_any;
7625 }
7626 else
7627 mch_memmove(ufunc->uf_func_type->tt_args,
7628 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7629 if (varargs)
7630 {
7631 ufunc->uf_func_type->tt_args[argcount] =
7632 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7633 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7634 }
7635 }
7636 else
7637 // No arguments, can use a predefined type.
7638 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7639 argcount, &ufunc->uf_type_list);
7640}
7641
7642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643/*
7644 * Delete an instruction, free what it contains.
7645 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007646 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647delete_instr(isn_T *isn)
7648{
7649 switch (isn->isn_type)
7650 {
7651 case ISN_EXEC:
7652 case ISN_LOADENV:
7653 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007654 case ISN_LOADB:
7655 case ISN_LOADW:
7656 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007658 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 case ISN_PUSHEXC:
7660 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007661 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007663 case ISN_STOREB:
7664 case ISN_STOREW:
7665 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007666 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 vim_free(isn->isn_arg.string);
7668 break;
7669
7670 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007671 case ISN_STORES:
7672 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 break;
7674
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007675 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007676 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007677 vim_free(isn->isn_arg.unlet.ul_name);
7678 break;
7679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 case ISN_STOREOPT:
7681 vim_free(isn->isn_arg.storeopt.so_name);
7682 break;
7683
7684 case ISN_PUSHBLOB: // push blob isn_arg.blob
7685 blob_unref(isn->isn_arg.blob);
7686 break;
7687
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007688 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007689#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007690 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007691#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007692 break;
7693
7694 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007695#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007696 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007697#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007698 break;
7699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 case ISN_UCALL:
7701 vim_free(isn->isn_arg.ufunc.cuf_name);
7702 break;
7703
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007704 case ISN_FUNCREF:
7705 {
7706 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7707 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007708
7709 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7710 func_ptr_unref(dfunc->df_ufunc);
7711 }
7712 break;
7713
7714 case ISN_DCALL:
7715 {
7716 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7717 + isn->isn_arg.dfunc.cdf_idx;
7718
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007719 if (dfunc->df_ufunc != NULL
7720 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007721 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007722 }
7723 break;
7724
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007725 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007726 {
7727 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7728 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7729
7730 if (ufunc != NULL)
7731 {
7732 // Clear uf_dfunc_idx so that the function is deleted.
7733 clear_def_function(ufunc);
7734 ufunc->uf_dfunc_idx = 0;
7735 func_ptr_unref(ufunc);
7736 }
7737
7738 vim_free(lambda);
7739 vim_free(isn->isn_arg.newfunc.nf_global);
7740 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007741 break;
7742
Bram Moolenaar5e654232020-09-16 15:22:00 +02007743 case ISN_CHECKTYPE:
7744 free_type(isn->isn_arg.type.ct_type);
7745 break;
7746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 case ISN_2BOOL:
7748 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007749 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007750 case ISN_ADDBLOB:
7751 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007752 case ISN_ANYINDEX:
7753 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007755 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007757 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 case ISN_COMPAREANY:
7760 case ISN_COMPAREBLOB:
7761 case ISN_COMPAREBOOL:
7762 case ISN_COMPAREDICT:
7763 case ISN_COMPAREFLOAT:
7764 case ISN_COMPAREFUNC:
7765 case ISN_COMPARELIST:
7766 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 case ISN_COMPARESPECIAL:
7768 case ISN_COMPARESTRING:
7769 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007770 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 case ISN_DROP:
7772 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007773 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007774 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007776 case ISN_EXECCONCAT:
7777 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007779 case ISN_GETITEM:
7780 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007781 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007782 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007783 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007785 case ISN_LOADBDICT:
7786 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007787 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007788 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007789 case ISN_LOADSCRIPT:
7790 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007791 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007792 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007793 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007794 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 case ISN_NEGATENR:
7796 case ISN_NEWDICT:
7797 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007799 case ISN_OPFLOAT:
7800 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007802 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007803 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007804 case ISN_PUSHF:
7805 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007807 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007808 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007809 case ISN_SHUFFLE:
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007810 case ISN_SILENT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007811 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007813 case ISN_STOREDICT:
7814 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007815 case ISN_STORENR:
7816 case ISN_STOREOUTER:
7817 case ISN_STOREREG:
7818 case ISN_STORESCRIPT:
7819 case ISN_STOREV:
7820 case ISN_STRINDEX:
7821 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 case ISN_THROW:
7823 case ISN_TRY:
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007824 case ISN_UNSILENT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 // nothing allocated
7826 break;
7827 }
7828}
7829
7830/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007831 * Free all instructions for "dfunc".
7832 */
7833 static void
7834delete_def_function_contents(dfunc_T *dfunc)
7835{
7836 int idx;
7837
7838 ga_clear(&dfunc->df_def_args_isn);
7839
7840 if (dfunc->df_instr != NULL)
7841 {
7842 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7843 delete_instr(dfunc->df_instr + idx);
7844 VIM_CLEAR(dfunc->df_instr);
7845 }
7846
7847 dfunc->df_deleted = TRUE;
7848}
7849
7850/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007851 * When a user function is deleted, clear the contents of any associated def
7852 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853 */
7854 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007855clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007856{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007857 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858 {
7859 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7860 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861
Bram Moolenaar20431c92020-03-20 18:39:46 +01007862 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007863 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864 }
7865}
7866
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007867/*
7868 * Used when a user function is about to be deleted: remove the pointer to it.
7869 * The entry in def_functions is then unused.
7870 */
7871 void
7872unlink_def_function(ufunc_T *ufunc)
7873{
7874 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7875
7876 dfunc->df_ufunc = NULL;
7877}
7878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007880/*
7881 * Free all functions defined with ":def".
7882 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883 void
7884free_def_functions(void)
7885{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007886 int idx;
7887
7888 for (idx = 0; idx < def_functions.ga_len; ++idx)
7889 {
7890 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7891
7892 delete_def_function_contents(dfunc);
7893 }
7894
7895 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896}
7897#endif
7898
7899
7900#endif // FEAT_EVAL