blob: b870023ea52a0c436717edb3aafc14681eae8757 [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
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD 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 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100823 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200824use_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 Moolenaarea2d4072020-11-12 12:08:51 +0100883 * Check that the top of the type stack has a type that can be used as a
884 * condition. Give an error and return FAIL if not.
885 */
886 static int
887bool_on_stack(cctx_T *cctx)
888{
889 garray_T *stack = &cctx->ctx_type_stack;
890 type_T *type;
891
892 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
893 if (type == &t_bool)
894 return OK;
895
896 if (type == &t_any || type == &t_number)
897 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
898 // This requires a runtime type check.
899 return generate_COND2BOOL(cctx);
900
901 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
902}
903
904/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 * Generate an ISN_PUSHNR instruction.
906 */
907 static int
908generate_PUSHNR(cctx_T *cctx, varnumber_T number)
909{
910 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200911 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
915 return FAIL;
916 isn->isn_arg.number = number;
917
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200918 if (number == 0 || number == 1)
919 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200920 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200921
922 // A 0 or 1 number can also be used as a bool.
923 if (type != NULL)
924 {
925 type->tt_type = VAR_NUMBER;
926 type->tt_flags = TTFLAG_BOOL_OK;
927 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
928 }
929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 return OK;
931}
932
933/*
934 * Generate an ISN_PUSHBOOL instruction.
935 */
936 static int
937generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
938{
939 isn_T *isn;
940
Bram Moolenaar080457c2020-03-03 21:53:32 +0100941 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
943 return FAIL;
944 isn->isn_arg.number = number;
945
946 return OK;
947}
948
949/*
950 * Generate an ISN_PUSHSPEC instruction.
951 */
952 static int
953generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
954{
955 isn_T *isn;
956
Bram Moolenaar080457c2020-03-03 21:53:32 +0100957 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
959 return FAIL;
960 isn->isn_arg.number = number;
961
962 return OK;
963}
964
965#ifdef FEAT_FLOAT
966/*
967 * Generate an ISN_PUSHF instruction.
968 */
969 static int
970generate_PUSHF(cctx_T *cctx, float_T fnumber)
971{
972 isn_T *isn;
973
Bram Moolenaar080457c2020-03-03 21:53:32 +0100974 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
976 return FAIL;
977 isn->isn_arg.fnumber = fnumber;
978
979 return OK;
980}
981#endif
982
983/*
984 * Generate an ISN_PUSHS instruction.
985 * Consumes "str".
986 */
987 static int
988generate_PUSHS(cctx_T *cctx, char_u *str)
989{
990 isn_T *isn;
991
Bram Moolenaar080457c2020-03-03 21:53:32 +0100992 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
994 return FAIL;
995 isn->isn_arg.string = str;
996
997 return OK;
998}
999
1000/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001001 * Generate an ISN_PUSHCHANNEL instruction.
1002 * Consumes "channel".
1003 */
1004 static int
1005generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1006{
1007 isn_T *isn;
1008
Bram Moolenaar080457c2020-03-03 21:53:32 +01001009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001010 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1011 return FAIL;
1012 isn->isn_arg.channel = channel;
1013
1014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHJOB instruction.
1019 * Consumes "job".
1020 */
1021 static int
1022generate_PUSHJOB(cctx_T *cctx, job_T *job)
1023{
1024 isn_T *isn;
1025
Bram Moolenaar080457c2020-03-03 21:53:32 +01001026 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001027 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001028 return FAIL;
1029 isn->isn_arg.job = job;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 * Generate an ISN_PUSHBLOB instruction.
1036 * Consumes "blob".
1037 */
1038 static int
1039generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1040{
1041 isn_T *isn;
1042
Bram Moolenaar080457c2020-03-03 21:53:32 +01001043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1045 return FAIL;
1046 isn->isn_arg.blob = blob;
1047
1048 return OK;
1049}
1050
1051/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001052 * Generate an ISN_PUSHFUNC instruction with name "name".
1053 * Consumes "name".
1054 */
1055 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001056generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001057{
1058 isn_T *isn;
1059
Bram Moolenaar080457c2020-03-03 21:53:32 +01001060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001061 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001062 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001063 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001064
1065 return OK;
1066}
1067
1068/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001069 * Generate an ISN_GETITEM instruction with "index".
1070 */
1071 static int
1072generate_GETITEM(cctx_T *cctx, int index)
1073{
1074 isn_T *isn;
1075 garray_T *stack = &cctx->ctx_type_stack;
1076 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1077 type_T *item_type = &t_any;
1078
1079 RETURN_OK_IF_SKIP(cctx);
1080
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001081 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001082 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001083 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 emsg(_(e_listreq));
1085 return FAIL;
1086 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001087 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001088 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.number = index;
1091
1092 // add the item type to the type stack
1093 if (ga_grow(stack, 1) == FAIL)
1094 return FAIL;
1095 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1096 ++stack->ga_len;
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001101 * Generate an ISN_SLICE instruction with "count".
1102 */
1103 static int
1104generate_SLICE(cctx_T *cctx, int count)
1105{
1106 isn_T *isn;
1107
1108 RETURN_OK_IF_SKIP(cctx);
1109 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.number = count;
1112 return OK;
1113}
1114
1115/*
1116 * Generate an ISN_CHECKLEN instruction with "min_len".
1117 */
1118 static int
1119generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1120{
1121 isn_T *isn;
1122
1123 RETURN_OK_IF_SKIP(cctx);
1124
1125 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1126 return FAIL;
1127 isn->isn_arg.checklen.cl_min_len = min_len;
1128 isn->isn_arg.checklen.cl_more_OK = more_OK;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 * Generate an ISN_STORE instruction.
1135 */
1136 static int
1137generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1138{
1139 isn_T *isn;
1140
Bram Moolenaar080457c2020-03-03 21:53:32 +01001141 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1143 return FAIL;
1144 if (name != NULL)
1145 isn->isn_arg.string = vim_strsave(name);
1146 else
1147 isn->isn_arg.number = idx;
1148
1149 return OK;
1150}
1151
1152/*
1153 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1154 */
1155 static int
1156generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1157{
1158 isn_T *isn;
1159
Bram Moolenaar080457c2020-03-03 21:53:32 +01001160 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1162 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001163 isn->isn_arg.storenr.stnr_idx = idx;
1164 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165
1166 return OK;
1167}
1168
1169/*
1170 * Generate an ISN_STOREOPT instruction
1171 */
1172 static int
1173generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1174{
1175 isn_T *isn;
1176
Bram Moolenaar080457c2020-03-03 21:53:32 +01001177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001178 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 return FAIL;
1180 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1181 isn->isn_arg.storeopt.so_flags = opt_flags;
1182
1183 return OK;
1184}
1185
1186/*
1187 * Generate an ISN_LOAD or similar instruction.
1188 */
1189 static int
1190generate_LOAD(
1191 cctx_T *cctx,
1192 isntype_T isn_type,
1193 int idx,
1194 char_u *name,
1195 type_T *type)
1196{
1197 isn_T *isn;
1198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1201 return FAIL;
1202 if (name != NULL)
1203 isn->isn_arg.string = vim_strsave(name);
1204 else
1205 isn->isn_arg.number = idx;
1206
1207 return OK;
1208}
1209
1210/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001211 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001212 */
1213 static int
1214generate_LOADV(
1215 cctx_T *cctx,
1216 char_u *name,
1217 int error)
1218{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001219 int di_flags;
1220 int vidx = find_vim_var(name, &di_flags);
1221 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001224 if (vidx < 0)
1225 {
1226 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001227 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228 return FAIL;
1229 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001230 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001231
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001232 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001233}
1234
1235/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001236 * Generate an ISN_UNLET instruction.
1237 */
1238 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001239generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001240{
1241 isn_T *isn;
1242
1243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001244 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001245 return FAIL;
1246 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1247 isn->isn_arg.unlet.ul_forceit = forceit;
1248
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001253 * Generate an ISN_LOCKCONST instruction.
1254 */
1255 static int
1256generate_LOCKCONST(cctx_T *cctx)
1257{
1258 isn_T *isn;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1262 return FAIL;
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 * Generate an ISN_LOADS instruction.
1268 */
1269 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274 int sid,
1275 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276{
1277 isn_T *isn;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 if (isn_type == ISN_LOADS)
1281 isn = generate_instr_type(cctx, isn_type, type);
1282 else
1283 isn = generate_instr_drop(cctx, isn_type, 1);
1284 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1287 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1294 */
1295 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 cctx_T *cctx,
1298 isntype_T isn_type,
1299 int sid,
1300 int idx,
1301 type_T *type)
1302{
1303 isn_T *isn;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (isn_type == ISN_LOADSCRIPT)
1307 isn = generate_instr_type(cctx, isn_type, type);
1308 else
1309 isn = generate_instr_drop(cctx, isn_type, 1);
1310 if (isn == NULL)
1311 return FAIL;
1312 isn->isn_arg.script.script_sid = sid;
1313 isn->isn_arg.script.script_idx = idx;
1314 return OK;
1315}
1316
1317/*
1318 * Generate an ISN_NEWLIST instruction.
1319 */
1320 static int
1321generate_NEWLIST(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 type_T *type;
1326 type_T *member;
1327
Bram Moolenaar080457c2020-03-03 21:53:32 +01001328 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1330 return FAIL;
1331 isn->isn_arg.number = count;
1332
Bram Moolenaar127542b2020-08-09 17:22:04 +02001333 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001334 if (count == 0)
1335 member = &t_void;
1336 else
1337 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001338 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1339 cctx->ctx_type_list);
1340 type = get_list_type(member, cctx->ctx_type_list);
1341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 // drop the value types
1343 stack->ga_len -= count;
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 // add the list type to the type stack
1346 if (ga_grow(stack, 1) == FAIL)
1347 return FAIL;
1348 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1349 ++stack->ga_len;
1350
1351 return OK;
1352}
1353
1354/*
1355 * Generate an ISN_NEWDICT instruction.
1356 */
1357 static int
1358generate_NEWDICT(cctx_T *cctx, int count)
1359{
1360 isn_T *isn;
1361 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 type_T *type;
1363 type_T *member;
1364
Bram Moolenaar080457c2020-03-03 21:53:32 +01001365 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1367 return FAIL;
1368 isn->isn_arg.number = count;
1369
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001370 if (count == 0)
1371 member = &t_void;
1372 else
1373 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001374 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1375 cctx->ctx_type_list);
1376 type = get_dict_type(member, cctx->ctx_type_list);
1377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 // drop the key and value types
1379 stack->ga_len -= 2 * count;
1380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 // add the dict type to the type stack
1382 if (ga_grow(stack, 1) == FAIL)
1383 return FAIL;
1384 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1385 ++stack->ga_len;
1386
1387 return OK;
1388}
1389
1390/*
1391 * Generate an ISN_FUNCREF instruction.
1392 */
1393 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001394generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395{
1396 isn_T *isn;
1397 garray_T *stack = &cctx->ctx_type_stack;
1398
Bram Moolenaar080457c2020-03-03 21:53:32 +01001399 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1401 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001402 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001403 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405 if (ga_grow(stack, 1) == FAIL)
1406 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001407 ((type_T **)stack->ga_data)[stack->ga_len] =
1408 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 ++stack->ga_len;
1410
1411 return OK;
1412}
1413
1414/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001415 * Generate an ISN_NEWFUNC instruction.
1416 */
1417 static int
1418generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1419{
1420 isn_T *isn;
1421 char_u *name;
1422
1423 RETURN_OK_IF_SKIP(cctx);
1424 name = vim_strsave(lambda_name);
1425 if (name == NULL)
1426 return FAIL;
1427 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1428 return FAIL;
1429 isn->isn_arg.newfunc.nf_lambda = name;
1430 isn->isn_arg.newfunc.nf_global = func_name;
1431
1432 return OK;
1433}
1434
1435/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001436 * Generate an ISN_DEF instruction: list functions
1437 */
1438 static int
1439generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1440{
1441 isn_T *isn;
1442
1443 RETURN_OK_IF_SKIP(cctx);
1444 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1445 return FAIL;
1446 if (len > 0)
1447 {
1448 isn->isn_arg.string = vim_strnsave(name, len);
1449 if (isn->isn_arg.string == NULL)
1450 return FAIL;
1451 }
1452 return OK;
1453}
1454
1455/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 * Generate an ISN_JUMP instruction.
1457 */
1458 static int
1459generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1460{
1461 isn_T *isn;
1462 garray_T *stack = &cctx->ctx_type_stack;
1463
Bram Moolenaar080457c2020-03-03 21:53:32 +01001464 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1466 return FAIL;
1467 isn->isn_arg.jump.jump_when = when;
1468 isn->isn_arg.jump.jump_where = where;
1469
1470 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1471 --stack->ga_len;
1472
1473 return OK;
1474}
1475
1476 static int
1477generate_FOR(cctx_T *cctx, int loop_idx)
1478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481
Bram Moolenaar080457c2020-03-03 21:53:32 +01001482 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1484 return FAIL;
1485 isn->isn_arg.forloop.for_idx = loop_idx;
1486
1487 if (ga_grow(stack, 1) == FAIL)
1488 return FAIL;
1489 // type doesn't matter, will be stored next
1490 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1491 ++stack->ga_len;
1492
1493 return OK;
1494}
1495
1496/*
1497 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001498 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 * Return FAIL if the number of arguments is wrong.
1500 */
1501 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001502generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001506 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001507 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001510 argoff = check_internal_func(func_idx, argcount);
1511 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 return FAIL;
1513
Bram Moolenaar389df252020-07-09 21:20:47 +02001514 if (method_call && argoff > 1)
1515 {
1516 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1517 return FAIL;
1518 isn->isn_arg.shuffle.shfl_item = argcount;
1519 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1520 }
1521
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001522 if (argcount > 0)
1523 {
1524 // Check the types of the arguments.
1525 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1526 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001527 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001528 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1531 return FAIL;
1532 isn->isn_arg.bfunc.cbf_idx = func_idx;
1533 isn->isn_arg.bfunc.cbf_argcount = argcount;
1534
Bram Moolenaar94738d82020-10-21 14:25:07 +02001535 // Drop the argument types and push the return type.
1536 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if (ga_grow(stack, 1) == FAIL)
1538 return FAIL;
1539 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001540 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001541 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
1543 return OK;
1544}
1545
1546/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001547 * Generate an ISN_LISTAPPEND instruction. Works like add().
1548 * Argument count is already checked.
1549 */
1550 static int
1551generate_LISTAPPEND(cctx_T *cctx)
1552{
1553 garray_T *stack = &cctx->ctx_type_stack;
1554 type_T *list_type;
1555 type_T *item_type;
1556 type_T *expected;
1557
1558 // Caller already checked that list_type is a list.
1559 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1560 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1561 expected = list_type->tt_member;
1562 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1563 return FAIL;
1564
1565 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1566 return FAIL;
1567
1568 --stack->ga_len; // drop the argument
1569 return OK;
1570}
1571
1572/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001573 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1574 * Argument count is already checked.
1575 */
1576 static int
1577generate_BLOBAPPEND(cctx_T *cctx)
1578{
1579 garray_T *stack = &cctx->ctx_type_stack;
1580 type_T *item_type;
1581
1582 // Caller already checked that blob_type is a blob.
1583 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1584 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1585 return FAIL;
1586
1587 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1588 return FAIL;
1589
1590 --stack->ga_len; // drop the argument
1591 return OK;
1592}
1593
1594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 * Generate an ISN_DCALL or ISN_UCALL instruction.
1596 * Return FAIL if the number of arguments is wrong.
1597 */
1598 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001599generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600{
1601 isn_T *isn;
1602 garray_T *stack = &cctx->ctx_type_stack;
1603 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001604 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605
Bram Moolenaar080457c2020-03-03 21:53:32 +01001606 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 if (argcount > regular_args && !has_varargs(ufunc))
1608 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001609 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 return FAIL;
1611 }
1612 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1613 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001614 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 return FAIL;
1616 }
1617
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001618 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001619 {
1620 int i;
1621
1622 for (i = 0; i < argcount; ++i)
1623 {
1624 type_T *expected;
1625 type_T *actual;
1626
1627 if (i < regular_args)
1628 {
1629 if (ufunc->uf_arg_types == NULL)
1630 continue;
1631 expected = ufunc->uf_arg_types[i];
1632 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001633 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1634 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001635 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001636 else
1637 expected = ufunc->uf_va_type->tt_member;
1638 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001639 if (need_type(actual, expected, -argcount + i, cctx,
1640 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001641 {
1642 arg_type_mismatch(expected, actual, i + 1);
1643 return FAIL;
1644 }
1645 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001646 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001647 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1648 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001649 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001650 }
1651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001653 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001654 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001656 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 {
1658 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1659 isn->isn_arg.dfunc.cdf_argcount = argcount;
1660 }
1661 else
1662 {
1663 // A user function may be deleted and redefined later, can't use the
1664 // ufunc pointer, need to look it up again at runtime.
1665 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1666 isn->isn_arg.ufunc.cuf_argcount = argcount;
1667 }
1668
1669 stack->ga_len -= argcount; // drop the arguments
1670 if (ga_grow(stack, 1) == FAIL)
1671 return FAIL;
1672 // add return value
1673 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1674 ++stack->ga_len;
1675
1676 return OK;
1677}
1678
1679/*
1680 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1681 */
1682 static int
1683generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1684{
1685 isn_T *isn;
1686 garray_T *stack = &cctx->ctx_type_stack;
1687
Bram Moolenaar080457c2020-03-03 21:53:32 +01001688 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1690 return FAIL;
1691 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1692 isn->isn_arg.ufunc.cuf_argcount = argcount;
1693
1694 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001695 if (ga_grow(stack, 1) == FAIL)
1696 return FAIL;
1697 // add return value
1698 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1699 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700
1701 return OK;
1702}
1703
1704/*
1705 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001706 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 */
1708 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001709generate_PCALL(
1710 cctx_T *cctx,
1711 int argcount,
1712 char_u *name,
1713 type_T *type,
1714 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715{
1716 isn_T *isn;
1717 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001718 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar080457c2020-03-03 21:53:32 +01001720 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001721
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001722 if (type->tt_type == VAR_ANY)
1723 ret_type = &t_any;
1724 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001725 {
1726 if (type->tt_argcount != -1)
1727 {
1728 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1729
1730 if (argcount < type->tt_min_argcount - varargs)
1731 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001732 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001733 return FAIL;
1734 }
1735 if (!varargs && argcount > type->tt_argcount)
1736 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001737 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001738 return FAIL;
1739 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001740 if (type->tt_args != NULL)
1741 {
1742 int i;
1743
1744 for (i = 0; i < argcount; ++i)
1745 {
1746 int offset = -argcount + i - 1;
1747 type_T *actual = ((type_T **)stack->ga_data)[
1748 stack->ga_len + offset];
1749 type_T *expected;
1750
1751 if (varargs && i >= type->tt_min_argcount - 1)
1752 expected = type->tt_args[
1753 type->tt_min_argcount - 1]->tt_member;
1754 else
1755 expected = type->tt_args[i];
1756 if (need_type(actual, expected, offset,
1757 cctx, TRUE, FALSE) == FAIL)
1758 {
1759 arg_type_mismatch(expected, actual, i + 1);
1760 return FAIL;
1761 }
1762 }
1763 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001764 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001765 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001766 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001767 else
1768 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001769 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001770 return FAIL;
1771 }
1772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1774 return FAIL;
1775 isn->isn_arg.pfunc.cpf_top = at_top;
1776 isn->isn_arg.pfunc.cpf_argcount = argcount;
1777
1778 stack->ga_len -= argcount; // drop the arguments
1779
1780 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001781 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001783 // If partial is above the arguments it must be cleared and replaced with
1784 // the return value.
1785 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1786 return FAIL;
1787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 return OK;
1789}
1790
1791/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001792 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 */
1794 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001795generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796{
1797 isn_T *isn;
1798 garray_T *stack = &cctx->ctx_type_stack;
1799 type_T *type;
1800
Bram Moolenaar080457c2020-03-03 21:53:32 +01001801 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001802 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001804 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001806 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001808 if (type->tt_type != VAR_DICT && type != &t_any)
1809 {
1810 emsg(_(e_dictreq));
1811 return FAIL;
1812 }
1813 // change dict type to dict member type
1814 if (type->tt_type == VAR_DICT)
1815 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816
1817 return OK;
1818}
1819
1820/*
1821 * Generate an ISN_ECHO instruction.
1822 */
1823 static int
1824generate_ECHO(cctx_T *cctx, int with_white, int count)
1825{
1826 isn_T *isn;
1827
Bram Moolenaar080457c2020-03-03 21:53:32 +01001828 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1830 return FAIL;
1831 isn->isn_arg.echo.echo_with_white = with_white;
1832 isn->isn_arg.echo.echo_count = count;
1833
1834 return OK;
1835}
1836
Bram Moolenaarad39c092020-02-26 18:23:43 +01001837/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001838 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001839 */
1840 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001841generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001842{
1843 isn_T *isn;
1844
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001845 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001846 return FAIL;
1847 isn->isn_arg.number = count;
1848
1849 return OK;
1850}
1851
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001852/*
1853 * Generate an ISN_PUT instruction.
1854 */
1855 static int
1856generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1857{
1858 isn_T *isn;
1859
1860 RETURN_OK_IF_SKIP(cctx);
1861 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1862 return FAIL;
1863 isn->isn_arg.put.put_regname = regname;
1864 isn->isn_arg.put.put_lnum = lnum;
1865 return OK;
1866}
1867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 static int
1869generate_EXEC(cctx_T *cctx, char_u *line)
1870{
1871 isn_T *isn;
1872
Bram Moolenaar080457c2020-03-03 21:53:32 +01001873 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1875 return FAIL;
1876 isn->isn_arg.string = vim_strsave(line);
1877 return OK;
1878}
1879
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001880 static int
1881generate_EXECCONCAT(cctx_T *cctx, int count)
1882{
1883 isn_T *isn;
1884
1885 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1886 return FAIL;
1887 isn->isn_arg.number = count;
1888 return OK;
1889}
1890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001892 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001893 */
1894 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001895generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001896{
1897 isn_T *isn;
1898
Bram Moolenaar02194d22020-10-24 23:08:38 +02001899 if (cmod->cmod_flags != 0
1900 || cmod->cmod_split != 0
1901 || cmod->cmod_verbose != 0
1902 || cmod->cmod_tab != 0
1903 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001904 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001905 cctx->ctx_has_cmdmod = TRUE;
1906
1907 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001908 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001909 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1910 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1911 return FAIL;
1912 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1913 // filter progam now belongs to the instruction
1914 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001915 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001916
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001917 return OK;
1918}
1919
1920 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001921generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001922{
1923 isn_T *isn;
1924
Bram Moolenaar02194d22020-10-24 23:08:38 +02001925 if (cctx->ctx_has_cmdmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001926 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001927 if ((isn = generate_instr(cctx, ISN_CMDMOD_REV)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001928 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001929 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001930
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001931 return OK;
1932}
1933
1934/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001936 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001938 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001939reserve_local(
1940 cctx_T *cctx,
1941 char_u *name,
1942 size_t len,
1943 int isConst,
1944 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 lvar_T *lvar;
1947
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001948 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001950 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001951 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 }
1953
1954 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001955 return NULL;
1956 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001957 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001959 // Every local variable uses the next entry on the stack. We could re-use
1960 // the last ones when leaving a scope, but then variables used in a closure
1961 // might get overwritten. To keep things simple do not re-use stack
1962 // entries. This is less efficient, but memory is cheap these days.
1963 lvar->lv_idx = cctx->ctx_locals_count++;
1964
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001965 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 lvar->lv_const = isConst;
1967 lvar->lv_type = type;
1968
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001969 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970}
1971
1972/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001973 * Remove local variables above "new_top".
1974 */
1975 static void
1976unwind_locals(cctx_T *cctx, int new_top)
1977{
1978 if (cctx->ctx_locals.ga_len > new_top)
1979 {
1980 int idx;
1981 lvar_T *lvar;
1982
1983 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1984 {
1985 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1986 vim_free(lvar->lv_name);
1987 }
1988 }
1989 cctx->ctx_locals.ga_len = new_top;
1990}
1991
1992/*
1993 * Free all local variables.
1994 */
1995 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001996free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001997{
1998 unwind_locals(cctx, 0);
1999 ga_clear(&cctx->ctx_locals);
2000}
2001
2002/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 * Find "name" in script-local items of script "sid".
2004 * Returns the index in "sn_var_vals" if found.
2005 * If found but not in "sn_var_vals" returns -1.
2006 * If not found returns -2.
2007 */
2008 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002009get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010{
2011 hashtab_T *ht;
2012 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002013 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002014 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 int idx;
2016
Bram Moolenaare3d46852020-08-29 13:39:17 +02002017 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002019 if (sid == current_sctx.sc_sid)
2020 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002021 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002022
2023 if (sav == NULL)
2024 return -2;
2025 idx = sav->sav_var_vals_idx;
2026 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2027 if (check_writable && sv->sv_const)
2028 semsg(_(e_readonlyvar), name);
2029 return idx;
2030 }
2031
2032 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 ht = &SCRIPT_VARS(sid);
2034 di = find_var_in_ht(ht, 0, name, TRUE);
2035 if (di == NULL)
2036 return -2;
2037
2038 // Now find the svar_T index in sn_var_vals.
2039 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2040 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002041 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 if (sv->sv_tv == &di->di_tv)
2043 {
2044 if (check_writable && sv->sv_const)
2045 semsg(_(e_readonlyvar), name);
2046 return idx;
2047 }
2048 }
2049 return -1;
2050}
2051
2052/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002053 * Find "name" in imported items of the current script or in "cctx" if not
2054 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 */
2056 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002057find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 int idx;
2060
Bram Moolenaare3d46852020-08-29 13:39:17 +02002061 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002062 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 if (cctx != NULL)
2064 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2065 {
2066 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2067 + idx;
2068
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002069 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2070 : STRLEN(import->imp_name) == len
2071 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 return import;
2073 }
2074
Bram Moolenaarefa94442020-08-08 22:16:00 +02002075 return find_imported_in_script(name, len, current_sctx.sc_sid);
2076}
2077
2078 imported_T *
2079find_imported_in_script(char_u *name, size_t len, int sid)
2080{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002081 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002082 int idx;
2083
Bram Moolenaare3d46852020-08-29 13:39:17 +02002084 if (!SCRIPT_ID_VALID(sid))
2085 return NULL;
2086 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2088 {
2089 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2090
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002091 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2092 : STRLEN(import->imp_name) == len
2093 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 return import;
2095 }
2096 return NULL;
2097}
2098
2099/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002100 * Free all imported variables.
2101 */
2102 static void
2103free_imported(cctx_T *cctx)
2104{
2105 int idx;
2106
2107 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2108 {
2109 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2110
2111 vim_free(import->imp_name);
2112 }
2113 ga_clear(&cctx->ctx_imports);
2114}
2115
2116/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002117 * Return TRUE if "p" points at a "#" but not at "#{".
2118 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002119 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002120vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002121{
2122 return p[0] == '#' && p[1] != '{';
2123}
2124
2125/*
2126 * Return a pointer to the next line that isn't empty or only contains a
2127 * comment. Skips over white space.
2128 * Returns NULL if there is none.
2129 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002130 char_u *
2131peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002132{
2133 int lnum = cctx->ctx_lnum;
2134
2135 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2136 {
2137 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002138 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002139
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002140 // ignore NULLs inserted for continuation lines
2141 if (line != NULL)
2142 {
2143 p = skipwhite(line);
2144 if (*p != NUL && !vim9_comment_start(p))
2145 return p;
2146 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002147 }
2148 return NULL;
2149}
2150
2151/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002152 * Called when checking for a following operator at "arg". When the rest of
2153 * the line is empty or only a comment, peek the next line. If there is a next
2154 * line return a pointer to it and set "nextp".
2155 * Otherwise skip over white space.
2156 */
2157 static char_u *
2158may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2159{
2160 char_u *p = skipwhite(arg);
2161
2162 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002163 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002164 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002165 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002166 if (*nextp != NULL)
2167 return *nextp;
2168 }
2169 return p;
2170}
2171
2172/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002173 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002174 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002175 * Returns NULL when at the end.
2176 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002177 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002178next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002179{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002180 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002181
2182 do
2183 {
2184 ++cctx->ctx_lnum;
2185 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002186 {
2187 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002188 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002189 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002190 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002191 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002192 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002193 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002194 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002195 return line;
2196}
2197
2198/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002199 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002200 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002201 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2202 */
2203 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002204may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002205{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002206 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002207 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002208 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002209
2210 if (next == NULL)
2211 return FAIL;
2212 *arg = skipwhite(next);
2213 }
2214 return OK;
2215}
2216
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002217/*
2218 * Idem, and give an error when failed.
2219 */
2220 static int
2221may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2222{
2223 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2224 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002225 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002226 return FAIL;
2227 }
2228 return OK;
2229}
2230
2231
Bram Moolenaara5565e42020-05-09 15:44:01 +02002232// Structure passed between the compile_expr* functions to keep track of
2233// constants that have been parsed but for which no code was produced yet. If
2234// possible expressions on these constants are applied at compile time. If
2235// that is not possible, the code to push the constants needs to be generated
2236// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002237// Using 50 should be more than enough of 5 levels of ().
2238#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002239typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002240 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002241 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002242 int pp_is_const; // all generated code was constants, used for a
2243 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002244} ppconst_T;
2245
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002246static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002247static int compile_expr0(char_u **arg, cctx_T *cctx);
2248static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2249
Bram Moolenaara5565e42020-05-09 15:44:01 +02002250/*
2251 * Generate a PUSH instruction for "tv".
2252 * "tv" will be consumed or cleared.
2253 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2254 */
2255 static int
2256generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2257{
2258 if (tv != NULL)
2259 {
2260 switch (tv->v_type)
2261 {
2262 case VAR_UNKNOWN:
2263 break;
2264 case VAR_BOOL:
2265 generate_PUSHBOOL(cctx, tv->vval.v_number);
2266 break;
2267 case VAR_SPECIAL:
2268 generate_PUSHSPEC(cctx, tv->vval.v_number);
2269 break;
2270 case VAR_NUMBER:
2271 generate_PUSHNR(cctx, tv->vval.v_number);
2272 break;
2273#ifdef FEAT_FLOAT
2274 case VAR_FLOAT:
2275 generate_PUSHF(cctx, tv->vval.v_float);
2276 break;
2277#endif
2278 case VAR_BLOB:
2279 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2280 tv->vval.v_blob = NULL;
2281 break;
2282 case VAR_STRING:
2283 generate_PUSHS(cctx, tv->vval.v_string);
2284 tv->vval.v_string = NULL;
2285 break;
2286 default:
2287 iemsg("constant type not supported");
2288 clear_tv(tv);
2289 return FAIL;
2290 }
2291 tv->v_type = VAR_UNKNOWN;
2292 }
2293 return OK;
2294}
2295
2296/*
2297 * Generate code for any ppconst entries.
2298 */
2299 static int
2300generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2301{
2302 int i;
2303 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002304 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002305
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002306 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002307 for (i = 0; i < ppconst->pp_used; ++i)
2308 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2309 ret = FAIL;
2310 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002311 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002312 return ret;
2313}
2314
2315/*
2316 * Clear ppconst constants. Used when failing.
2317 */
2318 static void
2319clear_ppconst(ppconst_T *ppconst)
2320{
2321 int i;
2322
2323 for (i = 0; i < ppconst->pp_used; ++i)
2324 clear_tv(&ppconst->pp_tv[i]);
2325 ppconst->pp_used = 0;
2326}
2327
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002328/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002329 * Generate an instruction to load script-local variable "name", without the
2330 * leading "s:".
2331 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 */
2333 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002334compile_load_scriptvar(
2335 cctx_T *cctx,
2336 char_u *name, // variable NUL terminated
2337 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002338 char_u **end, // end of variable
2339 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002341 scriptitem_T *si;
2342 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 imported_T *import;
2344
Bram Moolenaare3d46852020-08-29 13:39:17 +02002345 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2346 return FAIL;
2347 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002348 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002349 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002351 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002352 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2353 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 }
2355 if (idx >= 0)
2356 {
2357 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2358
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002359 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 current_sctx.sc_sid, idx, sv->sv_type);
2361 return OK;
2362 }
2363
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002364 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 if (import != NULL)
2366 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002367 if (import->imp_all)
2368 {
2369 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002370 char_u *exp_name;
2371 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002372 ufunc_T *ufunc;
2373 type_T *type;
2374
2375 // Used "import * as Name", need to lookup the member.
2376 if (*p != '.')
2377 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002378 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002379 return FAIL;
2380 }
2381 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002382 if (VIM_ISWHITE(*p))
2383 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002384 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002385 return FAIL;
2386 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002387
Bram Moolenaar1c991142020-07-04 13:15:31 +02002388 // isolate one name
2389 exp_name = p;
2390 while (eval_isnamec(*p))
2391 ++p;
2392 cc = *p;
2393 *p = NUL;
2394
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002395 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002396 *p = cc;
2397 p = skipwhite(p);
2398
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002399 // TODO: what if it is a function?
2400 if (idx < 0)
2401 return FAIL;
2402 *end = p;
2403
2404 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2405 import->imp_sid,
2406 idx,
2407 type);
2408 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002409 else if (import->imp_funcname != NULL)
2410 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002411 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002412 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2413 import->imp_sid,
2414 import->imp_var_vals_idx,
2415 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 return OK;
2417 }
2418
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002419 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002420 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 return FAIL;
2422}
2423
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002424 static int
2425generate_funcref(cctx_T *cctx, char_u *name)
2426{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002427 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002428
2429 if (ufunc == NULL)
2430 return FAIL;
2431
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002432 // Need to compile any default values to get the argument types.
2433 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2434 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2435 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002436 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002437}
2438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439/*
2440 * Compile a variable name into a load instruction.
2441 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002442 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 * When "error" is FALSE do not give an error when not found.
2444 */
2445 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002446compile_load(
2447 char_u **arg,
2448 char_u *end_arg,
2449 cctx_T *cctx,
2450 int is_expr,
2451 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452{
2453 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002454 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002455 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002457 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458
2459 if (*(*arg + 1) == ':')
2460 {
2461 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002462 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002463 {
2464 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002466 switch (**arg)
2467 {
2468 case 'g': isn_type = ISN_LOADGDICT; break;
2469 case 'w': isn_type = ISN_LOADWDICT; break;
2470 case 't': isn_type = ISN_LOADTDICT; break;
2471 case 'b': isn_type = ISN_LOADBDICT; break;
2472 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002473 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002474 goto theend;
2475 }
2476 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2477 goto theend;
2478 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 else
2481 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002482 isntype_T isn_type = ISN_DROP;
2483
2484 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2485 if (name == NULL)
2486 return FAIL;
2487
2488 switch (**arg)
2489 {
2490 case 'v': res = generate_LOADV(cctx, name, error);
2491 break;
2492 case 's': res = compile_load_scriptvar(cctx, name,
2493 NULL, NULL, error);
2494 break;
2495 case 'g': isn_type = ISN_LOADG; break;
2496 case 'w': isn_type = ISN_LOADW; break;
2497 case 't': isn_type = ISN_LOADT; break;
2498 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002499 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002500 goto theend;
2501 }
2502 if (isn_type != ISN_DROP)
2503 {
2504 // Global, Buffer-local, Window-local and Tabpage-local
2505 // variables can be defined later, thus we don't check if it
2506 // exists, give error at runtime.
2507 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 }
2510 }
2511 else
2512 {
2513 size_t len = end - *arg;
2514 int idx;
2515 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002516 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517
2518 name = vim_strnsave(*arg, end - *arg);
2519 if (name == NULL)
2520 return FAIL;
2521
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002522 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002524 if (!gen_load_outer)
2525 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 }
2527 else
2528 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002529 lvar_T *lvar = lookup_local(*arg, len, cctx);
2530
2531 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002533 type = lvar->lv_type;
2534 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002535 if (lvar->lv_from_outer)
2536 gen_load_outer = TRUE;
2537 else
2538 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 }
2540 else
2541 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002542 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002543 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002544 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002545 || find_imported(name, 0, cctx) != NULL)
2546 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002547
Bram Moolenaar0f769812020-09-12 18:32:34 +02002548 // When evaluating an expression and the name starts with an
2549 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002550 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002551 if (res == FAIL && is_expr
2552 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002553 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 }
2555 }
2556 if (gen_load)
2557 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002558 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002559 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002560 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002561 cctx->ctx_outer_used = TRUE;
2562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 }
2564
2565 *arg = end;
2566
2567theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002568 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002569 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 vim_free(name);
2571 return res;
2572}
2573
2574/*
2575 * Compile the argument expressions.
2576 * "arg" points to just after the "(" and is advanced to after the ")"
2577 */
2578 static int
2579compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2580{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581 char_u *p = *arg;
2582 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002583 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
Bram Moolenaare6085c52020-04-12 20:19:16 +02002585 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002587 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2588 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002589 if (*p == ')')
2590 {
2591 *arg = p + 1;
2592 return OK;
2593 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002594 if (must_end)
2595 {
2596 semsg(_(e_missing_comma_before_argument_str), p);
2597 return FAIL;
2598 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002599
Bram Moolenaara5565e42020-05-09 15:44:01 +02002600 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 return FAIL;
2602 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002603
2604 if (*p != ',' && *skipwhite(p) == ',')
2605 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002606 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002607 p = skipwhite(p);
2608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002610 {
2611 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002612 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002613 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002614 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002615 else
2616 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002617 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002618 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002620failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002621 emsg(_(e_missing_close));
2622 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623}
2624
2625/*
2626 * Compile a function call: name(arg1, arg2)
2627 * "arg" points to "name", "arg + varlen" to the "(".
2628 * "argcount_init" is 1 for "value->method()"
2629 * Instructions:
2630 * EVAL arg1
2631 * EVAL arg2
2632 * BCALL / DCALL / UCALL
2633 */
2634 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635compile_call(
2636 char_u **arg,
2637 size_t varlen,
2638 cctx_T *cctx,
2639 ppconst_T *ppconst,
2640 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641{
2642 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002643 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 int argcount = argcount_init;
2645 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002646 char_u fname_buf[FLEN_FIXED + 1];
2647 char_u *tofree = NULL;
2648 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002649 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002650 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002651 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652
Bram Moolenaara5565e42020-05-09 15:44:01 +02002653 // we can evaluate "has('name')" at compile time
2654 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2655 {
2656 char_u *s = skipwhite(*arg + varlen + 1);
2657 typval_T argvars[2];
2658
2659 argvars[0].v_type = VAR_UNKNOWN;
2660 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002661 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002662 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002663 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002664 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002665 if (*s == ')' && argvars[0].v_type == VAR_STRING
2666 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002667 {
2668 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2669
2670 *arg = s + 1;
2671 argvars[1].v_type = VAR_UNKNOWN;
2672 tv->v_type = VAR_NUMBER;
2673 tv->vval.v_number = 0;
2674 f_has(argvars, tv);
2675 clear_tv(&argvars[0]);
2676 ++ppconst->pp_used;
2677 return OK;
2678 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002679 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002680 }
2681
2682 if (generate_ppconst(cctx, ppconst) == FAIL)
2683 return FAIL;
2684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 if (varlen >= sizeof(namebuf))
2686 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002687 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 return FAIL;
2689 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002690 vim_strncpy(namebuf, *arg, varlen);
2691 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692
2693 *arg = skipwhite(*arg + varlen + 1);
2694 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002695 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696
Bram Moolenaara1773442020-08-12 15:21:22 +02002697 is_autoload = vim_strchr(name, '#') != NULL;
2698 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 {
2700 int idx;
2701
2702 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002703 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002705 {
2706 if (STRCMP(name, "add") == 0 && argcount == 2)
2707 {
2708 garray_T *stack = &cctx->ctx_type_stack;
2709 type_T *type = ((type_T **)stack->ga_data)[
2710 stack->ga_len - 2];
2711
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002712 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002713 if (type->tt_type == VAR_LIST)
2714 {
2715 // inline "add(list, item)" so that the type can be checked
2716 res = generate_LISTAPPEND(cctx);
2717 idx = -1;
2718 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002719 else if (type->tt_type == VAR_BLOB)
2720 {
2721 // inline "add(blob, nr)" so that the type can be checked
2722 res = generate_BLOBAPPEND(cctx);
2723 idx = -1;
2724 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002725 }
2726
2727 if (idx >= 0)
2728 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2729 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002730 else
2731 semsg(_(e_unknownfunc), namebuf);
2732 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 }
2734
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002735 // An argument or local variable can be a function reference, this
2736 // overrules a function name.
2737 if (lookup_local(namebuf, varlen, cctx) == NULL
2738 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002739 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002740 // If we can find the function by name generate the right call.
2741 // Skip global functions here, a local funcref takes precedence.
2742 ufunc = find_func(name, FALSE, cctx);
2743 if (ufunc != NULL && !func_is_global(ufunc))
2744 {
2745 res = generate_CALL(cctx, ufunc, argcount);
2746 goto theend;
2747 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749
2750 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002751 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002752 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002754 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002755 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002756 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002757 garray_T *stack = &cctx->ctx_type_stack;
2758 type_T *type;
2759
2760 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2761 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002762 goto theend;
2763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764
Bram Moolenaar0f769812020-09-12 18:32:34 +02002765 // If we can find a global function by name generate the right call.
2766 if (ufunc != NULL)
2767 {
2768 res = generate_CALL(cctx, ufunc, argcount);
2769 goto theend;
2770 }
2771
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002772 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002773 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002774 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002775 res = generate_UCALL(cctx, name, argcount);
2776 else
2777 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002778
2779theend:
2780 vim_free(tofree);
2781 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782}
2783
2784// like NAMESPACE_CHAR but with 'a' and 'l'.
2785#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2786
2787/*
2788 * Find the end of a variable or function name. Unlike find_name_end() this
2789 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002790 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 * Return a pointer to just after the name. Equal to "arg" if there is no
2792 * valid name.
2793 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002794 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002795to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796{
2797 char_u *p;
2798
2799 // Quick check for valid starting character.
2800 if (!eval_isnamec1(*arg))
2801 return arg;
2802
2803 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2804 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2805 // and can be used in slice "[n:]".
2806 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002807 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2809 break;
2810 return p;
2811}
2812
2813/*
2814 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002815 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 */
2817 char_u *
2818to_name_const_end(char_u *arg)
2819{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002820 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 typval_T rettv;
2822
2823 if (p == arg && *arg == '[')
2824 {
2825
2826 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002827 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 p = arg;
2829 }
2830 else if (p == arg && *arg == '#' && arg[1] == '{')
2831 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002832 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002834 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 p = arg;
2836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837
2838 return p;
2839}
2840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841/*
2842 * parse a list: [expr, expr]
2843 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002844 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 */
2846 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002847compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848{
2849 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002850 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002852 int is_const;
2853 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002855 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002857 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002858 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002859 semsg(_(e_list_end), *arg);
2860 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002861 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002862 if (*p == ',')
2863 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002864 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002865 return FAIL;
2866 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002867 if (*p == ']')
2868 {
2869 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002870 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002871 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002872 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002873 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002874 if (!is_const)
2875 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 ++count;
2877 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002878 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002880 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2881 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002882 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002883 return FAIL;
2884 }
2885 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002886 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 p = skipwhite(p);
2888 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002889 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002891 ppconst->pp_is_const = is_all_const;
2892 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893}
2894
2895/*
2896 * parse a lambda: {arg, arg -> expr}
2897 * "*arg" points to the '{'.
2898 */
2899 static int
2900compile_lambda(char_u **arg, cctx_T *cctx)
2901{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 typval_T rettv;
2903 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002904 evalarg_T evalarg;
2905
2906 CLEAR_FIELD(evalarg);
2907 evalarg.eval_flags = EVAL_EVALUATE;
2908 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909
2910 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002911 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002912 {
2913 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002915 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002918 ++ufunc->uf_refcount;
2919 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920
2921 // The function will have one line: "return {expr}".
2922 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002923 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002925 clear_evalarg(&evalarg, NULL);
2926
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002927 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002928 {
2929 // The return type will now be known.
2930 set_function_type(ufunc);
2931
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002932 // The function reference count will be 1. When the ISN_FUNCREF
2933 // instruction is deleted the reference count is decremented and the
2934 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002935 return generate_FUNCREF(cctx, ufunc);
2936 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002937
2938 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 return FAIL;
2940}
2941
2942/*
2943 * Compile a lamda call: expr->{lambda}(args)
2944 * "arg" points to the "{".
2945 */
2946 static int
2947compile_lambda_call(char_u **arg, cctx_T *cctx)
2948{
2949 ufunc_T *ufunc;
2950 typval_T rettv;
2951 int argcount = 1;
2952 int ret = FAIL;
2953
2954 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002955 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 return FAIL;
2957
2958 if (**arg != '(')
2959 {
2960 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002961 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 else
2963 semsg(_(e_missing_paren), "lambda");
2964 clear_tv(&rettv);
2965 return FAIL;
2966 }
2967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 ufunc = rettv.vval.v_partial->pt_func;
2969 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002970 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002971 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002972
Bram Moolenaara05e5242020-09-19 18:19:19 +02002973 // The function will have one line: "return {expr}". Compile it into
2974 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002975 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976
2977 // compile the arguments
2978 *arg = skipwhite(*arg + 1);
2979 if (compile_arguments(arg, cctx, &argcount) == OK)
2980 // call the compiled function
2981 ret = generate_CALL(cctx, ufunc, argcount);
2982
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002983 if (ret == FAIL)
2984 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 return ret;
2986}
2987
2988/*
2989 * parse a dict: {'key': val} or #{key: val}
2990 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002991 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 */
2993 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002994compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995{
2996 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002997 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 int count = 0;
2999 dict_T *d = dict_alloc();
3000 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003001 char_u *whitep = *arg;
3002 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003003 int is_const;
3004 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
3006 if (d == NULL)
3007 return FAIL;
3008 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003009 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003011 char_u *key = NULL;
3012 char_u *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
Bram Moolenaar23c55272020-06-21 16:58:13 +02003014 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003015 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003016 *arg = NULL;
3017 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003018 }
3019
3020 if (**arg == '}')
3021 break;
3022
Bram Moolenaar2bede172020-11-19 18:53:18 +01003023 // Eventually {name: value} will use "name" as a literal key and
3024 // {[expr]: value} for an evaluated key.
3025 // Temporarily: if "name" is indeed a valid key, or "[expr]" is
3026 // used, use the new method, like JavaScript. Otherwise fall back
3027 // to the old method.
3028 end = to_name_end(*arg, FALSE);
3029 if (literal || *end == ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003031 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003033 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 return FAIL;
3035 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003036 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 if (generate_PUSHS(cctx, key) == FAIL)
3038 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003039 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 }
3041 else
3042 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003043 isn_T *isn;
3044 int has_bracket = **arg == '[';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045
Bram Moolenaar2bede172020-11-19 18:53:18 +01003046 if (has_bracket)
3047 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003048 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3051 if (isn->isn_type == ISN_PUSHS)
3052 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003053 else
3054 {
3055 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003056 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003057 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003058 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003059 return FAIL;
3060 }
Bram Moolenaar2bede172020-11-19 18:53:18 +01003061 if (has_bracket)
3062 {
3063 *arg = skipwhite(*arg);
3064 if (**arg != ']')
3065 {
3066 emsg(_(e_missing_matching_bracket_after_dict_key));
3067 return FAIL;
3068 }
3069 ++*arg;
3070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 }
3072
3073 // Check for duplicate keys, if using string keys.
3074 if (key != NULL)
3075 {
3076 item = dict_find(d, key, -1);
3077 if (item != NULL)
3078 {
3079 semsg(_(e_duplicate_key), key);
3080 goto failret;
3081 }
3082 item = dictitem_alloc(key);
3083 if (item != NULL)
3084 {
3085 item->di_tv.v_type = VAR_UNKNOWN;
3086 item->di_tv.v_lock = 0;
3087 if (dict_add(d, item) == FAIL)
3088 dictitem_free(item);
3089 }
3090 }
3091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 if (**arg != ':')
3093 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003094 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003095 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003096 else
3097 semsg(_(e_missing_dict_colon), *arg);
3098 return FAIL;
3099 }
3100 whitep = *arg + 1;
3101 if (!IS_WHITE_OR_NUL(*whitep))
3102 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003103 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 return FAIL;
3105 }
3106
3107 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003108 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003109 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003110 *arg = NULL;
3111 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003112 }
3113
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003114 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003116 if (!is_const)
3117 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 ++count;
3119
Bram Moolenaar2c330432020-04-13 14:41:35 +02003120 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003121 *arg = skipwhite(*arg);
3122 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003123 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003124 *arg = NULL;
3125 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 if (**arg == '}')
3128 break;
3129 if (**arg != ',')
3130 {
3131 semsg(_(e_missing_dict_comma), *arg);
3132 goto failret;
3133 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003134 if (IS_WHITE_OR_NUL(*whitep))
3135 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003136 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003137 return FAIL;
3138 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003139 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003140 if (!IS_WHITE_OR_NUL(*whitep))
3141 {
3142 semsg(_(e_white_space_required_after_str), ",");
3143 return FAIL;
3144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 *arg = skipwhite(*arg + 1);
3146 }
3147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 *arg = *arg + 1;
3149
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003150 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003151 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003152 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003153 *arg += STRLEN(*arg);
3154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003156 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 return generate_NEWDICT(cctx, count);
3158
3159failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003160 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003161 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003162 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003163 *arg = (char_u *)"";
3164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 dict_unref(d);
3166 return FAIL;
3167}
3168
3169/*
3170 * Compile "&option".
3171 */
3172 static int
3173compile_get_option(char_u **arg, cctx_T *cctx)
3174{
3175 typval_T rettv;
3176 char_u *start = *arg;
3177 int ret;
3178
3179 // parse the option and get the current value to get the type.
3180 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003181 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 if (ret == OK)
3183 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003184 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 char_u *name = vim_strnsave(start, *arg - start);
3186 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3187
3188 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3189 vim_free(name);
3190 }
3191 clear_tv(&rettv);
3192
3193 return ret;
3194}
3195
3196/*
3197 * Compile "$VAR".
3198 */
3199 static int
3200compile_get_env(char_u **arg, cctx_T *cctx)
3201{
3202 char_u *start = *arg;
3203 int len;
3204 int ret;
3205 char_u *name;
3206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 ++*arg;
3208 len = get_env_len(arg);
3209 if (len == 0)
3210 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003211 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 return FAIL;
3213 }
3214
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003215 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 name = vim_strnsave(start, len + 1);
3217 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3218 vim_free(name);
3219 return ret;
3220}
3221
3222/*
3223 * Compile "@r".
3224 */
3225 static int
3226compile_get_register(char_u **arg, cctx_T *cctx)
3227{
3228 int ret;
3229
3230 ++*arg;
3231 if (**arg == NUL)
3232 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003233 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 return FAIL;
3235 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003236 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 {
3238 emsg_invreg(**arg);
3239 return FAIL;
3240 }
3241 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3242 ++*arg;
3243 return ret;
3244}
3245
3246/*
3247 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003248 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 */
3250 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003251apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003253 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254
3255 // this works from end to start
3256 while (p > start)
3257 {
3258 --p;
3259 if (*p == '-' || *p == '+')
3260 {
3261 // only '-' has an effect, for '+' we only check the type
3262#ifdef FEAT_FLOAT
3263 if (rettv->v_type == VAR_FLOAT)
3264 {
3265 if (*p == '-')
3266 rettv->vval.v_float = -rettv->vval.v_float;
3267 }
3268 else
3269#endif
3270 {
3271 varnumber_T val;
3272 int error = FALSE;
3273
3274 // tv_get_number_chk() accepts a string, but we don't want that
3275 // here
3276 if (check_not_string(rettv) == FAIL)
3277 return FAIL;
3278 val = tv_get_number_chk(rettv, &error);
3279 clear_tv(rettv);
3280 if (error)
3281 return FAIL;
3282 if (*p == '-')
3283 val = -val;
3284 rettv->v_type = VAR_NUMBER;
3285 rettv->vval.v_number = val;
3286 }
3287 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003288 else if (numeric_only)
3289 {
3290 ++p;
3291 break;
3292 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003293 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 {
3295 int v = tv2bool(rettv);
3296
3297 // '!' is permissive in the type.
3298 clear_tv(rettv);
3299 rettv->v_type = VAR_BOOL;
3300 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3301 }
3302 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003303 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 return OK;
3305}
3306
3307/*
3308 * Recognize v: variables that are constants and set "rettv".
3309 */
3310 static void
3311get_vim_constant(char_u **arg, typval_T *rettv)
3312{
3313 if (STRNCMP(*arg, "v:true", 6) == 0)
3314 {
3315 rettv->v_type = VAR_BOOL;
3316 rettv->vval.v_number = VVAL_TRUE;
3317 *arg += 6;
3318 }
3319 else if (STRNCMP(*arg, "v:false", 7) == 0)
3320 {
3321 rettv->v_type = VAR_BOOL;
3322 rettv->vval.v_number = VVAL_FALSE;
3323 *arg += 7;
3324 }
3325 else if (STRNCMP(*arg, "v:null", 6) == 0)
3326 {
3327 rettv->v_type = VAR_SPECIAL;
3328 rettv->vval.v_number = VVAL_NULL;
3329 *arg += 6;
3330 }
3331 else if (STRNCMP(*arg, "v:none", 6) == 0)
3332 {
3333 rettv->v_type = VAR_SPECIAL;
3334 rettv->vval.v_number = VVAL_NONE;
3335 *arg += 6;
3336 }
3337}
3338
Bram Moolenaar696ba232020-07-29 21:20:41 +02003339 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003340get_compare_type(char_u *p, int *len, int *type_is)
3341{
3342 exptype_T type = EXPR_UNKNOWN;
3343 int i;
3344
3345 switch (p[0])
3346 {
3347 case '=': if (p[1] == '=')
3348 type = EXPR_EQUAL;
3349 else if (p[1] == '~')
3350 type = EXPR_MATCH;
3351 break;
3352 case '!': if (p[1] == '=')
3353 type = EXPR_NEQUAL;
3354 else if (p[1] == '~')
3355 type = EXPR_NOMATCH;
3356 break;
3357 case '>': if (p[1] != '=')
3358 {
3359 type = EXPR_GREATER;
3360 *len = 1;
3361 }
3362 else
3363 type = EXPR_GEQUAL;
3364 break;
3365 case '<': if (p[1] != '=')
3366 {
3367 type = EXPR_SMALLER;
3368 *len = 1;
3369 }
3370 else
3371 type = EXPR_SEQUAL;
3372 break;
3373 case 'i': if (p[1] == 's')
3374 {
3375 // "is" and "isnot"; but not a prefix of a name
3376 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3377 *len = 5;
3378 i = p[*len];
3379 if (!isalnum(i) && i != '_')
3380 {
3381 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3382 *type_is = TRUE;
3383 }
3384 }
3385 break;
3386 }
3387 return type;
3388}
3389
3390/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003392 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 */
3394 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003395compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003397 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398
3399 // this works from end to start
3400 while (p > start)
3401 {
3402 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003403 while (VIM_ISWHITE(*p))
3404 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 if (*p == '-' || *p == '+')
3406 {
3407 int negate = *p == '-';
3408 isn_T *isn;
3409
3410 // TODO: check type
3411 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3412 {
3413 --p;
3414 if (*p == '-')
3415 negate = !negate;
3416 }
3417 // only '-' has an effect, for '+' we only check the type
3418 if (negate)
3419 isn = generate_instr(cctx, ISN_NEGATENR);
3420 else
3421 isn = generate_instr(cctx, ISN_CHECKNR);
3422 if (isn == NULL)
3423 return FAIL;
3424 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003425 else if (numeric_only)
3426 {
3427 ++p;
3428 break;
3429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 else
3431 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003432 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003434 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003436 if (p[-1] == '!')
3437 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 }
3440 if (generate_2BOOL(cctx, invert) == FAIL)
3441 return FAIL;
3442 }
3443 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003444 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 return OK;
3446}
3447
3448/*
3449 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003450 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 */
3452 static int
3453compile_subscript(
3454 char_u **arg,
3455 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003456 char_u *start_leader,
3457 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003458 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003460 char_u *name_start = *end_leader;
3461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 for (;;)
3463 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003464 char_u *p = skipwhite(*arg);
3465
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003466 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003467 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003468 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003469
3470 // If a following line starts with "->{" or "->X" advance to that
3471 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003472 // Also if a following line starts with ".x".
3473 if (next != NULL &&
3474 ((next[0] == '-' && next[1] == '>'
3475 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003476 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003477 {
3478 next = next_line_from_context(cctx, TRUE);
3479 if (next == NULL)
3480 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003481 *arg = next;
3482 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003483 }
3484 }
3485
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003486 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3487 // not a function call.
3488 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003490 garray_T *stack = &cctx->ctx_type_stack;
3491 type_T *type;
3492 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003494 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003495 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003496 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003499 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3500
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003501 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3503 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003504 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 return FAIL;
3506 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003507 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003509 char_u *pstart = p;
3510
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003511 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003512 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003513 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 // something->method()
3516 // Apply the '!', '-' and '+' first:
3517 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003518 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003521 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003522 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003523 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 if (**arg == '{')
3525 {
3526 // lambda call: list->{lambda}
3527 if (compile_lambda_call(arg, cctx) == FAIL)
3528 return FAIL;
3529 }
3530 else
3531 {
3532 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003533 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003534 if (!eval_isnamec1(*p))
3535 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003536 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003537 return FAIL;
3538 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003539 if (ASCII_ISALPHA(*p) && p[1] == ':')
3540 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003541 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 ;
3543 if (*p != '(')
3544 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003545 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 return FAIL;
3547 }
3548 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003549 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 return FAIL;
3551 }
3552 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003553 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003556 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003557 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003558 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003559 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003560
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003561 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003562 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003563 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003564 // TODO: blob index
3565 // TODO: more arguments
3566 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003567 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003568 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003569 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003570
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003571 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003572 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003573 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003574 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003575 if (**arg == ':')
3576 // missing first index is equal to zero
3577 generate_PUSHNR(cctx, 0);
3578 else
3579 {
3580 if (compile_expr0(arg, cctx) == FAIL)
3581 return FAIL;
3582 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3583 return FAIL;
3584 *arg = skipwhite(*arg);
3585 }
3586 if (**arg == ':')
3587 {
3588 *arg = skipwhite(*arg + 1);
3589 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3590 return FAIL;
3591 if (**arg == ']')
3592 // missing second index is equal to end of string
3593 generate_PUSHNR(cctx, -1);
3594 else
3595 {
3596 if (compile_expr0(arg, cctx) == FAIL)
3597 return FAIL;
3598 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3599 return FAIL;
3600 *arg = skipwhite(*arg);
3601 }
3602 is_slice = TRUE;
3603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604
3605 if (**arg != ']')
3606 {
3607 emsg(_(e_missbrac));
3608 return FAIL;
3609 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003610 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003612 // We can index a list and a dict. If we don't know the type
3613 // we can use the index value type.
3614 // TODO: If we don't know use an instruction to figure it out at
3615 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003616 typep = ((type_T **)stack->ga_data) + stack->ga_len
3617 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003618 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003619 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3620 // If the index is a string, the variable must be a Dict.
3621 if (*typep == &t_any && valtype == &t_string)
3622 vtype = VAR_DICT;
3623 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003624 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003625 if (need_type(valtype, &t_number, -1, cctx,
3626 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003627 return FAIL;
3628 if (is_slice)
3629 {
3630 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003631 if (need_type(valtype, &t_number, -2, cctx,
3632 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003633 return FAIL;
3634 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003635 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003636
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003637 if (vtype == VAR_DICT)
3638 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003639 if (is_slice)
3640 {
3641 emsg(_(e_cannot_slice_dictionary));
3642 return FAIL;
3643 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003644 if ((*typep)->tt_type == VAR_DICT)
3645 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003646 else
3647 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003648 if (need_type(*typep, &t_dict_any, -2, cctx,
3649 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003650 return FAIL;
3651 *typep = &t_any;
3652 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003653 if (may_generate_2STRING(-1, cctx) == FAIL)
3654 return FAIL;
3655 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3656 return FAIL;
3657 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003658 else if (vtype == VAR_STRING)
3659 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003660 *typep = &t_string;
3661 if ((is_slice
3662 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3663 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003664 return FAIL;
3665 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003666 else if (vtype == VAR_BLOB)
3667 {
3668 emsg("Sorry, blob index and slice not implemented yet");
3669 return FAIL;
3670 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003671 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003672 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003673 if (is_slice)
3674 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003675 if (generate_instr_drop(cctx,
3676 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3677 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003678 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003679 }
Bram Moolenaared591872020-08-15 22:14:53 +02003680 else
3681 {
3682 if ((*typep)->tt_type == VAR_LIST)
3683 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003684 if (generate_instr_drop(cctx,
3685 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3686 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003687 return FAIL;
3688 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003689 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003690 else
3691 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003692 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003693 return FAIL;
3694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003696 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003698 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003699 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003700 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003701 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003702
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003703 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003704 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003705 {
3706 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003707 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003708 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003709 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003710 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 while (eval_isnamec(*p))
3712 MB_PTR_ADV(p);
3713 if (p == *arg)
3714 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003715 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 return FAIL;
3717 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003718 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 return FAIL;
3720 *arg = p;
3721 }
3722 else
3723 break;
3724 }
3725
3726 // TODO - see handle_subscript():
3727 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3728 // Don't do this when "Func" is already a partial that was bound
3729 // explicitly (pt_auto is FALSE).
3730
3731 return OK;
3732}
3733
3734/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003735 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3736 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003738 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003739 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003740 *
3741 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 */
3743
3744/*
3745 * number number constant
3746 * 0zFFFFFFFF Blob constant
3747 * "string" string constant
3748 * 'string' literal string constant
3749 * &option-name option value
3750 * @r register contents
3751 * identifier variable value
3752 * function() function call
3753 * $VAR environment variable
3754 * (expression) nested expression
3755 * [expr, expr] List
3756 * {key: val, key: val} Dictionary
3757 * #{key: val, key: val} Dictionary with literal keys
3758 *
3759 * Also handle:
3760 * ! in front logical NOT
3761 * - in front unary minus
3762 * + in front unary plus (ignored)
3763 * trailing (arg) funcref/partial call
3764 * trailing [] subscript in String or List
3765 * trailing .name entry in Dictionary
3766 * trailing ->name() method call
3767 */
3768 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003769compile_expr7(
3770 char_u **arg,
3771 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003772 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 char_u *start_leader, *end_leader;
3775 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003776 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003777 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003779 ppconst->pp_is_const = FALSE;
3780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 /*
3782 * Skip '!', '-' and '+' characters. They are handled later.
3783 */
3784 start_leader = *arg;
3785 while (**arg == '!' || **arg == '-' || **arg == '+')
3786 *arg = skipwhite(*arg + 1);
3787 end_leader = *arg;
3788
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003789 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 switch (**arg)
3791 {
3792 /*
3793 * Number constant.
3794 */
3795 case '0': // also for blob starting with 0z
3796 case '1':
3797 case '2':
3798 case '3':
3799 case '4':
3800 case '5':
3801 case '6':
3802 case '7':
3803 case '8':
3804 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003805 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003807 // Apply "-" and "+" just before the number now, right to
3808 // left. Matters especially when "->" follows. Stops at
3809 // '!'.
3810 if (apply_leader(rettv, TRUE,
3811 start_leader, &end_leader) == FAIL)
3812 {
3813 clear_tv(rettv);
3814 return FAIL;
3815 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 break;
3817
3818 /*
3819 * String constant: "string".
3820 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003821 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 return FAIL;
3823 break;
3824
3825 /*
3826 * Literal string constant: 'str''ing'.
3827 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003828 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 return FAIL;
3830 break;
3831
3832 /*
3833 * Constant Vim variable.
3834 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003835 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 ret = NOTDONE;
3837 break;
3838
3839 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003840 * "true" constant
3841 */
3842 case 't': if (STRNCMP(*arg, "true", 4) == 0
3843 && !eval_isnamec((*arg)[4]))
3844 {
3845 *arg += 4;
3846 rettv->v_type = VAR_BOOL;
3847 rettv->vval.v_number = VVAL_TRUE;
3848 }
3849 else
3850 ret = NOTDONE;
3851 break;
3852
3853 /*
3854 * "false" constant
3855 */
3856 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3857 && !eval_isnamec((*arg)[5]))
3858 {
3859 *arg += 5;
3860 rettv->v_type = VAR_BOOL;
3861 rettv->vval.v_number = VVAL_FALSE;
3862 }
3863 else
3864 ret = NOTDONE;
3865 break;
3866
3867 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 * List: [expr, expr]
3869 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003870 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 break;
3872
3873 /*
3874 * Dictionary: #{key: val, key: val}
3875 */
3876 case '#': if ((*arg)[1] == '{')
3877 {
3878 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003879 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 }
3881 else
3882 ret = NOTDONE;
3883 break;
3884
3885 /*
3886 * Lambda: {arg, arg -> expr}
3887 * Dictionary: {'key': val, 'key': val}
3888 */
3889 case '{': {
3890 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003891 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892
3893 // Find out what comes after the arguments.
3894 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003895 &ga_arg, TRUE, NULL, NULL,
3896 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 if (ret != FAIL && *start == '>')
3898 ret = compile_lambda(arg, cctx);
3899 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003900 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 }
3902 break;
3903
3904 /*
3905 * Option value: &name
3906 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003907 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3908 return FAIL;
3909 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 break;
3911
3912 /*
3913 * Environment variable: $VAR.
3914 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003915 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3916 return FAIL;
3917 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 break;
3919
3920 /*
3921 * Register contents: @r.
3922 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003923 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3924 return FAIL;
3925 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 break;
3927 /*
3928 * nested expression: (expression).
3929 */
3930 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003931
3932 // recursive!
3933 if (ppconst->pp_used <= PPSIZE - 10)
3934 {
3935 ret = compile_expr1(arg, cctx, ppconst);
3936 }
3937 else
3938 {
3939 // Not enough space in ppconst, flush constants.
3940 if (generate_ppconst(cctx, ppconst) == FAIL)
3941 return FAIL;
3942 ret = compile_expr0(arg, cctx);
3943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 *arg = skipwhite(*arg);
3945 if (**arg == ')')
3946 ++*arg;
3947 else if (ret == OK)
3948 {
3949 emsg(_(e_missing_close));
3950 ret = FAIL;
3951 }
3952 break;
3953
3954 default: ret = NOTDONE;
3955 break;
3956 }
3957 if (ret == FAIL)
3958 return FAIL;
3959
Bram Moolenaar1c747212020-05-09 18:28:34 +02003960 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003962 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003963 clear_tv(rettv);
3964 else
3965 // A constant expression can possibly be handled compile time,
3966 // return the value instead of generating code.
3967 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 }
3969 else if (ret == NOTDONE)
3970 {
3971 char_u *p;
3972 int r;
3973
3974 if (!eval_isnamec1(**arg))
3975 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003976 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 return FAIL;
3978 }
3979
3980 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003981 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003983 {
3984 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987 {
3988 if (generate_ppconst(cctx, ppconst) == FAIL)
3989 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003990 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 if (r == FAIL)
3993 return FAIL;
3994 }
3995
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003996 // Handle following "[]", ".member", etc.
3997 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003998 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003999 ppconst) == FAIL)
4000 return FAIL;
4001 if (ppconst->pp_used > 0)
4002 {
4003 // apply the '!', '-' and '+' before the constant
4004 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004005 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006 return FAIL;
4007 return OK;
4008 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004009 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004011 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012}
4013
4014/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004015 * Give the "white on both sides" error, taking the operator from "p[len]".
4016 */
4017 void
4018error_white_both(char_u *op, int len)
4019{
4020 char_u buf[10];
4021
4022 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004023 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004024}
4025
4026/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004027 * <type>expr7: runtime type check / conversion
4028 */
4029 static int
4030compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4031{
4032 type_T *want_type = NULL;
4033
4034 // Recognize <type>
4035 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4036 {
4037 int called_emsg_before = called_emsg;
4038
4039 ++*arg;
4040 want_type = parse_type(arg, cctx->ctx_type_list);
4041 if (called_emsg != called_emsg_before)
4042 return FAIL;
4043
4044 if (**arg != '>')
4045 {
4046 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004047 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004048 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004049 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004050 return FAIL;
4051 }
4052 ++*arg;
4053 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4054 return FAIL;
4055 }
4056
4057 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4058 return FAIL;
4059
4060 if (want_type != NULL)
4061 {
4062 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004063 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004064
Bram Moolenaard1103582020-08-14 22:44:25 +02004065 generate_ppconst(cctx, ppconst);
4066 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004067 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004068 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004069 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004070 return FAIL;
4071 }
4072 }
4073
4074 return OK;
4075}
4076
4077/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 * * number multiplication
4079 * / number division
4080 * % number modulo
4081 */
4082 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004083compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084{
4085 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004086 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004087 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004089 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004090 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 return FAIL;
4092
4093 /*
4094 * Repeat computing, until no "*", "/" or "%" is following.
4095 */
4096 for (;;)
4097 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004098 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 if (*op != '*' && *op != '/' && *op != '%')
4100 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004101 if (next != NULL)
4102 {
4103 *arg = next_line_from_context(cctx, TRUE);
4104 op = skipwhite(*arg);
4105 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004106
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004107 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004109 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004110 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 }
4112 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004113 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004114 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004116 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004117 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004119
4120 if (ppconst->pp_used == ppconst_used + 2
4121 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4122 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004123 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004124 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4125 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004126 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004128 // both are numbers: compute the result
4129 switch (*op)
4130 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004131 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004132 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004133 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004135 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004136 break;
4137 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004138 tv1->vval.v_number = res;
4139 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004140 }
4141 else
4142 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004143 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004144 generate_two_op(cctx, op);
4145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 }
4147
4148 return OK;
4149}
4150
4151/*
4152 * + number addition
4153 * - number subtraction
4154 * .. string concatenation
4155 */
4156 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004157compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158{
4159 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004160 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163
4164 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004165 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 return FAIL;
4167
4168 /*
4169 * Repeat computing, until no "+", "-" or ".." is following.
4170 */
4171 for (;;)
4172 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004173 op = may_peek_next_line(cctx, *arg, &next);
4174 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 break;
4176 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004177 if (next != NULL)
4178 {
4179 *arg = next_line_from_context(cctx, TRUE);
4180 op = skipwhite(*arg);
4181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004183 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004185 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004186 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 }
4188
4189 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004190 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004191 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004194 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 return FAIL;
4196
Bram Moolenaara5565e42020-05-09 15:44:01 +02004197 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004199 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4200 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4201 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4202 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004204 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4205 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004206
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004207 // concat/subtract/add constant numbers
4208 if (*op == '+')
4209 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4210 else if (*op == '-')
4211 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4212 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004213 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004214 // concatenate constant strings
4215 char_u *s1 = tv1->vval.v_string;
4216 char_u *s2 = tv2->vval.v_string;
4217 size_t len1 = STRLEN(s1);
4218
4219 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4220 if (tv1->vval.v_string == NULL)
4221 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004222 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004223 return FAIL;
4224 }
4225 mch_memmove(tv1->vval.v_string, s1, len1);
4226 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004227 vim_free(s1);
4228 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004229 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004230 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 }
4232 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004233 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004234 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004235 if (*op == '.')
4236 {
4237 if (may_generate_2STRING(-2, cctx) == FAIL
4238 || may_generate_2STRING(-1, cctx) == FAIL)
4239 return FAIL;
4240 generate_instr_drop(cctx, ISN_CONCAT, 1);
4241 }
4242 else
4243 generate_two_op(cctx, op);
4244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 }
4246
4247 return OK;
4248}
4249
4250/*
4251 * expr5a == expr5b
4252 * expr5a =~ expr5b
4253 * expr5a != expr5b
4254 * expr5a !~ expr5b
4255 * expr5a > expr5b
4256 * expr5a >= expr5b
4257 * expr5a < expr5b
4258 * expr5a <= expr5b
4259 * expr5a is expr5b
4260 * expr5a isnot expr5b
4261 *
4262 * Produces instructions:
4263 * EVAL expr5a Push result of "expr5a"
4264 * EVAL expr5b Push result of "expr5b"
4265 * COMPARE one of the compare instructions
4266 */
4267 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269{
4270 exptype_T type = EXPR_UNKNOWN;
4271 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004272 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004275 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276
4277 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004278 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 return FAIL;
4280
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004281 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004282 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283
4284 /*
4285 * If there is a comparative operator, use it.
4286 */
4287 if (type != EXPR_UNKNOWN)
4288 {
4289 int ic = FALSE; // Default: do not ignore case
4290
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004291 if (next != NULL)
4292 {
4293 *arg = next_line_from_context(cctx, TRUE);
4294 p = skipwhite(*arg);
4295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 if (type_is && (p[len] == '?' || p[len] == '#'))
4297 {
4298 semsg(_(e_invexpr2), *arg);
4299 return FAIL;
4300 }
4301 // extra question mark appended: ignore case
4302 if (p[len] == '?')
4303 {
4304 ic = TRUE;
4305 ++len;
4306 }
4307 // extra '#' appended: match case (ignored)
4308 else if (p[len] == '#')
4309 ++len;
4310 // nothing appended: match case
4311
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004312 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004314 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004315 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 }
4317
4318 // get the second variable
4319 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004320 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004321 return FAIL;
4322
Bram Moolenaara5565e42020-05-09 15:44:01 +02004323 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 return FAIL;
4325
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326 if (ppconst->pp_used == ppconst_used + 2)
4327 {
4328 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4329 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4330 int ret;
4331
4332 // Both sides are a constant, compute the result now.
4333 // First check for a valid combination of types, this is more
4334 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004335 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004336 ret = FAIL;
4337 else
4338 {
4339 ret = typval_compare(tv1, tv2, type, ic);
4340 tv1->v_type = VAR_BOOL;
4341 tv1->vval.v_number = tv1->vval.v_number
4342 ? VVAL_TRUE : VVAL_FALSE;
4343 clear_tv(tv2);
4344 --ppconst->pp_used;
4345 }
4346 return ret;
4347 }
4348
4349 generate_ppconst(cctx, ppconst);
4350 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 }
4352
4353 return OK;
4354}
4355
Bram Moolenaar7f141552020-05-09 17:35:53 +02004356static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358/*
4359 * Compile || or &&.
4360 */
4361 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362compile_and_or(
4363 char_u **arg,
4364 cctx_T *cctx,
4365 char *op,
4366 ppconst_T *ppconst,
4367 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004369 char_u *next;
4370 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 int opchar = *op;
4372
4373 if (p[0] == opchar && p[1] == opchar)
4374 {
4375 garray_T *instr = &cctx->ctx_instr;
4376 garray_T end_ga;
4377
4378 /*
4379 * Repeat until there is no following "||" or "&&"
4380 */
4381 ga_init2(&end_ga, sizeof(int), 10);
4382 while (p[0] == opchar && p[1] == opchar)
4383 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004384 if (next != NULL)
4385 {
4386 *arg = next_line_from_context(cctx, TRUE);
4387 p = skipwhite(*arg);
4388 }
4389
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004390 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4391 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004392 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004393 return FAIL;
4394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004396 // TODO: use ppconst if the value is a constant and check
4397 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 generate_ppconst(cctx, ppconst);
4399
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004400 // Every part must evaluate to a bool.
4401 if (bool_on_stack(cctx) == FAIL)
4402 {
4403 ga_clear(&end_ga);
4404 return FAIL;
4405 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 if (ga_grow(&end_ga, 1) == FAIL)
4408 {
4409 ga_clear(&end_ga);
4410 return FAIL;
4411 }
4412 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4413 ++end_ga.ga_len;
4414 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004415 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416
4417 // eval the next expression
4418 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004419 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004420 return FAIL;
4421
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4423 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 {
4425 ga_clear(&end_ga);
4426 return FAIL;
4427 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004428
4429 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004433 // Every part must evaluate to a bool.
4434 if (bool_on_stack(cctx) == FAIL)
4435 {
4436 ga_clear(&end_ga);
4437 return FAIL;
4438 }
4439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 // Fill in the end label in all jumps.
4441 while (end_ga.ga_len > 0)
4442 {
4443 isn_T *isn;
4444
4445 --end_ga.ga_len;
4446 isn = ((isn_T *)instr->ga_data)
4447 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4448 isn->isn_arg.jump.jump_where = instr->ga_len;
4449 }
4450 ga_clear(&end_ga);
4451 }
4452
4453 return OK;
4454}
4455
4456/*
4457 * expr4a && expr4a && expr4a logical AND
4458 *
4459 * Produces instructions:
4460 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004461 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004462 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004464 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 * EVAL expr4c Push result of "expr4c"
4466 * end:
4467 */
4468 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004469compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 int ppconst_used = ppconst->pp_used;
4472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004474 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 return FAIL;
4476
4477 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004478 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479}
4480
4481/*
4482 * expr3a || expr3b || expr3c logical OR
4483 *
4484 * Produces instructions:
4485 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004486 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004487 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004489 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 * EVAL expr3c Push result of "expr3c"
4491 * end:
4492 */
4493 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004496 int ppconst_used = ppconst->pp_used;
4497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004499 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 return FAIL;
4501
4502 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004503 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504}
4505
4506/*
4507 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004509 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 * JUMP_IF_FALSE alt jump if false
4511 * EVAL expr1a
4512 * JUMP_ALWAYS end
4513 * alt: EVAL expr1b
4514 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004515 *
4516 * Toplevel expression: expr2 ?? expr1
4517 * Produces instructions:
4518 * EVAL expr2 Push result of "expr2"
4519 * JUMP_AND_KEEP_IF_TRUE end jump if true
4520 * EVAL expr1
4521 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 */
4523 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004524compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525{
4526 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004528 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529
Bram Moolenaar3988f642020-08-27 22:43:03 +02004530 // Ignore all kinds of errors when not producing code.
4531 if (cctx->ctx_skip == SKIP_YES)
4532 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004533 evalarg_T evalarg;
4534
4535 CLEAR_FIELD(evalarg);
4536 evalarg.eval_cctx = cctx;
4537 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004538 return OK;
4539 }
4540
Bram Moolenaar61a89812020-05-07 16:58:17 +02004541 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004542 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 return FAIL;
4544
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004545 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 if (*p == '?')
4547 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004548 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 garray_T *instr = &cctx->ctx_instr;
4550 garray_T *stack = &cctx->ctx_type_stack;
4551 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004552 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004554 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004555 int has_const_expr = FALSE;
4556 int const_value = FALSE;
4557 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004559 if (next != NULL)
4560 {
4561 *arg = next_line_from_context(cctx, TRUE);
4562 p = skipwhite(*arg);
4563 }
4564
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004565 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004566 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004567 semsg(_(e_white_space_required_before_and_after_str),
4568 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004569 return FAIL;
4570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571
Bram Moolenaara5565e42020-05-09 15:44:01 +02004572 if (ppconst->pp_used == ppconst_used + 1)
4573 {
4574 // the condition is a constant, we know whether the ? or the :
4575 // expression is to be evaluated.
4576 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004577 if (op_falsy)
4578 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4579 else
4580 {
4581 int error = FALSE;
4582
4583 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4584 &error);
4585 if (error)
4586 return FAIL;
4587 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004588 cctx->ctx_skip = save_skip == SKIP_YES ||
4589 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4590
4591 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4592 // "left ?? right" and "left" is truthy: produce "left"
4593 generate_ppconst(cctx, ppconst);
4594 else
4595 {
4596 clear_tv(&ppconst->pp_tv[ppconst_used]);
4597 --ppconst->pp_used;
4598 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004599 }
4600 else
4601 {
4602 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004603 if (op_falsy)
4604 end_idx = instr->ga_len;
4605 generate_JUMP(cctx, op_falsy
4606 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4607 if (op_falsy)
4608 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610
4611 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004612 *arg = skipwhite(p + 1 + op_falsy);
4613 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004614 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004615 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004616 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618 if (!has_const_expr)
4619 {
4620 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004622 if (!op_falsy)
4623 {
4624 // remember the type and drop it
4625 --stack->ga_len;
4626 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004628 end_idx = instr->ga_len;
4629 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004630
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004631 // jump here from JUMP_IF_FALSE
4632 isn = ((isn_T *)instr->ga_data) + alt_idx;
4633 isn->isn_arg.jump.jump_where = instr->ga_len;
4634 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004637 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004639 // Check for the ":".
4640 p = may_peek_next_line(cctx, *arg, &next);
4641 if (*p != ':')
4642 {
4643 emsg(_(e_missing_colon));
4644 return FAIL;
4645 }
4646 if (next != NULL)
4647 {
4648 *arg = next_line_from_context(cctx, TRUE);
4649 p = skipwhite(*arg);
4650 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004651
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004652 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4653 {
4654 semsg(_(e_white_space_required_before_and_after_str), ":");
4655 return FAIL;
4656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004658 // evaluate the third expression
4659 if (has_const_expr)
4660 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004661 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004662 *arg = skipwhite(p + 1);
4663 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4664 return FAIL;
4665 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4666 return FAIL;
4667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 if (!has_const_expr)
4670 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004671 type_T **typep;
4672
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004676 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4677 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004678
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004679 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004680 isn = ((isn_T *)instr->ga_data) + end_idx;
4681 isn->isn_arg.jump.jump_where = instr->ga_len;
4682 }
4683
4684 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 }
4686 return OK;
4687}
4688
4689/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004690 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004691 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4692 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 */
4694 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004695compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696{
4697 ppconst_T ppconst;
4698
4699 CLEAR_FIELD(ppconst);
4700 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4701 {
4702 clear_ppconst(&ppconst);
4703 return FAIL;
4704 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004705 if (is_const != NULL)
4706 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004707 if (generate_ppconst(cctx, &ppconst) == FAIL)
4708 return FAIL;
4709 return OK;
4710}
4711
4712/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004713 * Toplevel expression.
4714 */
4715 static int
4716compile_expr0(char_u **arg, cctx_T *cctx)
4717{
4718 return compile_expr0_ext(arg, cctx, NULL);
4719}
4720
4721/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 * compile "return [expr]"
4723 */
4724 static char_u *
4725compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4726{
4727 char_u *p = arg;
4728 garray_T *stack = &cctx->ctx_type_stack;
4729 type_T *stack_type;
4730
4731 if (*p != NUL && *p != '|' && *p != '\n')
4732 {
4733 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004734 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 return NULL;
4736
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004737 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004738 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004739 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4740 if (set_return_type)
4741 cctx->ctx_ufunc->uf_ret_type = stack_type;
4742 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004743 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004744 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4745 && stack_type->tt_type != VAR_VOID
4746 && stack_type->tt_type != VAR_UNKNOWN)
4747 {
4748 emsg(_(e_returning_value_in_function_without_return_type));
4749 return NULL;
4750 }
4751 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004752 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004753 return NULL;
4754 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 }
4757 else
4758 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004759 // "set_return_type" cannot be TRUE, only used for a lambda which
4760 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004761 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4762 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004764 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 return NULL;
4766 }
4767
4768 // No argument, return zero.
4769 generate_PUSHNR(cctx, 0);
4770 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004771 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 return NULL;
4773
4774 // "return val | endif" is possible
4775 return skipwhite(p);
4776}
4777
4778/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004779 * Get a line from the compilation context, compatible with exarg_T getline().
4780 * Return a pointer to the line in allocated memory.
4781 * Return NULL for end-of-file or some error.
4782 */
4783 static char_u *
4784exarg_getline(
4785 int c UNUSED,
4786 void *cookie,
4787 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004788 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004789{
4790 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004791 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004792
Bram Moolenaar66250c92020-08-20 15:02:42 +02004793 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004794 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004795 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004796 return NULL;
4797 ++cctx->ctx_lnum;
4798 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4799 // Comment lines result in NULL pointers, skip them.
4800 if (p != NULL)
4801 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004802 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004803}
4804
4805/*
4806 * Compile a nested :def command.
4807 */
4808 static char_u *
4809compile_nested_function(exarg_T *eap, cctx_T *cctx)
4810{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004811 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004812 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004813 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004814 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004815 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004816 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004817
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004818 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004819 {
4820 emsg(_(e_cannot_use_bang_with_nested_def));
4821 return NULL;
4822 }
4823
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004824 if (*name_start == '/')
4825 {
4826 name_end = skip_regexp(name_start + 1, '/', TRUE);
4827 if (*name_end == '/')
4828 ++name_end;
4829 eap->nextcmd = check_nextcmd(name_end);
4830 }
4831 if (name_end == name_start || *skipwhite(name_end) != '(')
4832 {
4833 if (!ends_excmd2(name_start, name_end))
4834 {
4835 semsg(_(e_invalid_command_str), eap->cmd);
4836 return NULL;
4837 }
4838
4839 // "def" or "def Name": list functions
4840 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4841 return NULL;
4842 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4843 }
4844
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004845 // Only g:Func() can use a namespace.
4846 if (name_start[1] == ':' && !is_global)
4847 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004848 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004849 return NULL;
4850 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004851 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4852 return NULL;
4853
Bram Moolenaar04b12692020-05-04 23:24:44 +02004854 eap->arg = name_end;
4855 eap->getline = exarg_getline;
4856 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004857 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004858 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004859 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004860 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004861
Bram Moolenaar822ba242020-05-24 23:00:18 +02004862 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004863 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004864 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004865 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004866 {
4867 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004868 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004869 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004870
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004871 if (is_global)
4872 {
4873 char_u *func_name = vim_strnsave(name_start + 2,
4874 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004875
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004876 if (func_name == NULL)
4877 r = FAIL;
4878 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004879 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004880 }
4881 else
4882 {
4883 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004884 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004885 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004886 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004887
Bram Moolenaareef21022020-08-01 22:16:43 +02004888 if (lvar == NULL)
4889 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004890 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004891 return NULL;
4892 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004893
4894 // copy over the block scope IDs
4895 if (block_depth > 0)
4896 {
4897 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4898 if (ufunc->uf_block_ids != NULL)
4899 {
4900 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4901 sizeof(int) * block_depth);
4902 ufunc->uf_block_depth = block_depth;
4903 }
4904 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004905 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004906
Bram Moolenaar61a89812020-05-07 16:58:17 +02004907 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004908 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004909}
4910
4911/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 * Return the length of an assignment operator, or zero if there isn't one.
4913 */
4914 int
4915assignment_len(char_u *p, int *heredoc)
4916{
4917 if (*p == '=')
4918 {
4919 if (p[1] == '<' && p[2] == '<')
4920 {
4921 *heredoc = TRUE;
4922 return 3;
4923 }
4924 return 1;
4925 }
4926 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4927 return 2;
4928 if (STRNCMP(p, "..=", 3) == 0)
4929 return 3;
4930 return 0;
4931}
4932
4933// words that cannot be used as a variable
4934static char *reserved[] = {
4935 "true",
4936 "false",
4937 NULL
4938};
4939
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004940typedef enum {
4941 dest_local,
4942 dest_option,
4943 dest_env,
4944 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004945 dest_buffer,
4946 dest_window,
4947 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004948 dest_vimvar,
4949 dest_script,
4950 dest_reg,
4951} assign_dest_T;
4952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004954 * Generate the load instruction for "name".
4955 */
4956 static void
4957generate_loadvar(
4958 cctx_T *cctx,
4959 assign_dest_T dest,
4960 char_u *name,
4961 lvar_T *lvar,
4962 type_T *type)
4963{
4964 switch (dest)
4965 {
4966 case dest_option:
4967 // TODO: check the option exists
4968 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4969 break;
4970 case dest_global:
4971 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4972 break;
4973 case dest_buffer:
4974 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4975 break;
4976 case dest_window:
4977 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4978 break;
4979 case dest_tab:
4980 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4981 break;
4982 case dest_script:
4983 compile_load_scriptvar(cctx,
4984 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4985 break;
4986 case dest_env:
4987 // Include $ in the name here
4988 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4989 break;
4990 case dest_reg:
4991 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4992 break;
4993 case dest_vimvar:
4994 generate_LOADV(cctx, name + 2, TRUE);
4995 break;
4996 case dest_local:
4997 if (lvar->lv_from_outer)
4998 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4999 NULL, type);
5000 else
5001 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5002 break;
5003 }
5004}
5005
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005006 void
5007vim9_declare_error(char_u *name)
5008{
5009 char *scope = "";
5010
5011 switch (*name)
5012 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005013 case 'g': scope = _("global"); break;
5014 case 'b': scope = _("buffer"); break;
5015 case 'w': scope = _("window"); break;
5016 case 't': scope = _("tab"); break;
5017 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005018 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005019 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005020 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005021 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005022 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005023 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005024 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005025 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005026 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005027}
5028
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005029/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005031 * "let name"
5032 * "var name = expr"
5033 * "final name = expr"
5034 * "const name = expr"
5035 * "name = expr"
5036 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 * Return NULL for an error.
5038 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 */
5040 static char_u *
5041compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5042{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005043 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005045 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 char_u *ret = NULL;
5047 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005049 int scriptvar_sid = 0;
5050 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005053 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 int oplen = 0;
5056 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005057 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005058 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005061 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5062 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005064 // Skip over the "var" or "[var, var]" to get to any "=".
5065 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5066 if (p == NULL)
5067 return *arg == '[' ? arg : NULL;
5068
5069 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005071 // TODO: should we allow this, and figure out type inference from list
5072 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005073 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 return NULL;
5075 }
5076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 sp = p;
5078 p = skipwhite(p);
5079 op = p;
5080 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081
5082 if (var_count > 0 && oplen == 0)
5083 // can be something like "[1, 2]->func()"
5084 return arg;
5085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5087 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005088 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005090 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 if (heredoc)
5093 {
5094 list_T *l;
5095 listitem_T *li;
5096
5097 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005098 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005100 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005101 if (l == NULL)
5102 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103
Bram Moolenaar078269b2020-09-21 20:35:55 +02005104 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005106 // Push each line and the create the list.
5107 FOR_ALL_LIST_ITEMS(l, li)
5108 {
5109 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5110 li->li_tv.vval.v_string = NULL;
5111 }
5112 generate_NEWLIST(cctx, l->lv_len);
5113 type = &t_list_string;
5114 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 list_free(l);
5117 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005118 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 // for "[var, var] = expr" evaluate the expression here, loop over the
5123 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005124
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005125 p = skipwhite(op + oplen);
5126 if (compile_expr0(&p, cctx) == FAIL)
5127 return NULL;
5128 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005130 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005132 type_T *stacktype;
5133
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005134 stacktype = stack->ga_len == 0 ? &t_void
5135 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005138 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 goto theend;
5140 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005141 if (need_type(stacktype, &t_list_any, -1, cctx,
5142 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005144 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005145 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5146 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005147 }
5148 }
5149
5150 /*
5151 * Loop over variables in "[var, var] = expr".
5152 * For "var = expr" and "let var: type" this is done only once.
5153 */
5154 if (var_count > 0)
5155 var_start = skipwhite(arg + 1); // skip over the "["
5156 else
5157 var_start = arg;
5158 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5159 {
5160 char_u *var_end = skip_var_one(var_start, FALSE);
5161 size_t varlen;
5162 int new_local = FALSE;
5163 int opt_type;
5164 int opt_flags = 0;
5165 assign_dest_T dest = dest_local;
5166 int vimvaridx = -1;
5167 lvar_T *lvar = NULL;
5168 lvar_T arg_lvar;
5169 int has_type = FALSE;
5170 int has_index = FALSE;
5171 int instr_count = -1;
5172
Bram Moolenaar65821722020-08-02 18:58:54 +02005173 if (*var_start == '@')
5174 p = var_start + 2;
5175 else
5176 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005177 // skip over the leading "&", "&l:", "&g:" and "$"
5178 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005179 p = to_name_end(p, TRUE);
5180 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181
5182 // "a: type" is declaring variable "a" with a type, not "a:".
5183 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5184 --var_end;
5185 if (is_decl && p == var_start + 2 && p[-1] == ':')
5186 --p;
5187
5188 varlen = p - var_start;
5189 vim_free(name);
5190 name = vim_strnsave(var_start, varlen);
5191 if (name == NULL)
5192 return NULL;
5193 if (!heredoc)
5194 type = &t_any;
5195
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005196 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005198 int declare_error = FALSE;
5199
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 if (*var_start == '&')
5201 {
5202 int cc;
5203 long numval;
5204
5205 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005206 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005208 emsg(_(e_const_option));
5209 goto theend;
5210 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005211 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 p = var_start;
5213 p = find_option_end(&p, &opt_flags);
5214 if (p == NULL)
5215 {
5216 // cannot happen?
5217 emsg(_(e_letunexp));
5218 goto theend;
5219 }
5220 cc = *p;
5221 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005222 opt_type = get_option_value(skip_option_env_lead(var_start),
5223 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005224 *p = cc;
5225 if (opt_type == -3)
5226 {
5227 semsg(_(e_unknown_option), var_start);
5228 goto theend;
5229 }
5230 if (opt_type == -2 || opt_type == 0)
5231 type = &t_string;
5232 else
5233 type = &t_number; // both number and boolean option
5234 }
5235 else if (*var_start == '$')
5236 {
5237 dest = dest_env;
5238 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005239 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240 }
5241 else if (*var_start == '@')
5242 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005243 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005244 {
5245 emsg_invreg(var_start[1]);
5246 goto theend;
5247 }
5248 dest = dest_reg;
5249 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005250 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005252 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253 {
5254 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005255 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005257 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005258 {
5259 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005260 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005261 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005262 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005263 {
5264 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005265 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005267 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005268 {
5269 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005270 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005272 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005273 {
5274 typval_T *vtv;
5275 int di_flags;
5276
5277 vimvaridx = find_vim_var(name + 2, &di_flags);
5278 if (vimvaridx < 0)
5279 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005280 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005281 goto theend;
5282 }
5283 // We use the current value of "sandbox" here, is that OK?
5284 if (var_check_ro(di_flags, name, FALSE))
5285 goto theend;
5286 dest = dest_vimvar;
5287 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005288 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005289 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005290 }
5291 else
5292 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005293 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294
5295 for (idx = 0; reserved[idx] != NULL; ++idx)
5296 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005297 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005298 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005299 goto theend;
5300 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005301
5302 lvar = lookup_local(var_start, varlen, cctx);
5303 if (lvar == NULL)
5304 {
5305 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005306 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5308 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005309 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005310 if (is_decl)
5311 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005312 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005313 goto theend;
5314 }
5315 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005316 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005317 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005318 if (lvar != NULL)
5319 {
5320 if (is_decl)
5321 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005322 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 goto theend;
5324 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005325 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005326 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005328 int script_namespace = varlen > 1
5329 && STRNCMP(var_start, "s:", 2) == 0;
5330 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005331 ? script_var_exists(var_start + 2, varlen - 2,
5332 FALSE, cctx)
5333 : script_var_exists(var_start, varlen,
5334 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005335 imported_T *import =
5336 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005337
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005338 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005339 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005340 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5341
5342 if (is_decl)
5343 {
5344 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005345 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005346 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005347 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005348 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005349 name);
5350 goto theend;
5351 }
5352 else if (cctx->ctx_ufunc->uf_script_ctx_version
5353 == SCRIPT_VERSION_VIM9
5354 && script_namespace
5355 && !script_var && import == NULL)
5356 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005357 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005358 goto theend;
5359 }
5360
5361 dest = dest_script;
5362
5363 // existing script-local variables should have a type
5364 scriptvar_sid = current_sctx.sc_sid;
5365 if (import != NULL)
5366 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005367 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005368 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005369 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005370 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005371 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005372 {
5373 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5374 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005375 ((svar_T *)si->sn_var_vals.ga_data)
5376 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005377 type = sv->sv_type;
5378 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005379 }
5380 }
5381 else if (name[1] == ':' && name[2] != NUL)
5382 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005383 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384 goto theend;
5385 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005386 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005387 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005388 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005389 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005390 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005391 else if (check_defined(var_start, varlen, cctx) == FAIL)
5392 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005393 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005394 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005395
5396 if (declare_error)
5397 {
5398 vim9_declare_error(name);
5399 goto theend;
5400 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005401 }
5402
5403 // handle "a:name" as a name, not index "name" on "a"
5404 if (varlen > 1 || var_start[varlen] != ':')
5405 p = var_end;
5406
5407 if (dest != dest_option)
5408 {
5409 if (is_decl && *p == ':')
5410 {
5411 // parse optional type: "let var: type = expr"
5412 if (!VIM_ISWHITE(p[1]))
5413 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005414 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 goto theend;
5416 }
5417 p = skipwhite(p + 1);
5418 type = parse_type(&p, cctx->ctx_type_list);
5419 has_type = TRUE;
5420 }
5421 else if (lvar != NULL)
5422 type = lvar->lv_type;
5423 }
5424
5425 if (oplen == 3 && !heredoc && dest != dest_global
5426 && type->tt_type != VAR_STRING
5427 && type->tt_type != VAR_ANY)
5428 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005429 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 goto theend;
5431 }
5432
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005433 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005434 {
5435 if (oplen > 1 && !heredoc)
5436 {
5437 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005438 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005439 goto theend;
5440 }
5441
5442 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005443 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5444 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005445 goto theend;
5446 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005447 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005448 if (lvar == NULL)
5449 goto theend;
5450 new_local = TRUE;
5451 }
5452
5453 member_type = type;
5454 if (var_end > var_start + varlen)
5455 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005456 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005457 if (is_decl)
5458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005459 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 goto theend;
5461 }
5462
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005463 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 {
5465 has_index = TRUE;
5466 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005467 member_type = &t_any;
5468 else
5469 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005470 }
5471 else
5472 {
5473 semsg("Not supported yet: %s", var_start);
5474 goto theend;
5475 }
5476 }
5477 else if (lvar == &arg_lvar)
5478 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005479 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005480 goto theend;
5481 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005482 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5483 {
5484 semsg(_(e_cannot_assign_to_constant), name);
5485 goto theend;
5486 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005487
5488 if (!heredoc)
5489 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005490 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005491 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005492 if (oplen > 0 && var_count == 0)
5493 {
5494 // skip over the "=" and the expression
5495 p = skipwhite(op + oplen);
5496 compile_expr0(&p, cctx);
5497 }
5498 }
5499 else if (oplen > 0)
5500 {
5501 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005502 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005503
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005504 // For "var = expr" evaluate the expression.
5505 if (var_count == 0)
5506 {
5507 int r;
5508
5509 // for "+=", "*=", "..=" etc. first load the current value
5510 if (*op != '=')
5511 {
5512 generate_loadvar(cctx, dest, name, lvar, type);
5513
5514 if (has_index)
5515 {
5516 // TODO: get member from list or dict
5517 emsg("Index with operation not supported yet");
5518 goto theend;
5519 }
5520 }
5521
5522 // Compile the expression. Temporarily hide the new local
5523 // variable here, it is not available to this expression.
5524 if (new_local)
5525 --cctx->ctx_locals.ga_len;
5526 instr_count = instr->ga_len;
5527 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005528 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005529 if (new_local)
5530 ++cctx->ctx_locals.ga_len;
5531 if (r == FAIL)
5532 goto theend;
5533 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005534 else if (semicolon && var_idx == var_count - 1)
5535 {
5536 // For "[var; var] = expr" get the rest of the list
5537 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5538 goto theend;
5539 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005540 else
5541 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005542 // For "[var, var] = expr" get the "var_idx" item from the
5543 // list.
5544 if (generate_GETITEM(cctx, var_idx) == FAIL)
5545 return FAIL;
5546 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005547
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005548 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005549 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005550 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005552 if ((stacktype->tt_type == VAR_FUNC
5553 || stacktype->tt_type == VAR_PARTIAL)
5554 && var_wrong_func_name(name, TRUE))
5555 goto theend;
5556
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005557 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005558 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005559 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005561 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005562 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563 }
5564 else
5565 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005566 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005567 // for a variable that implies &t_any.
5568 if (stacktype == &t_list_empty)
5569 lvar->lv_type = &t_list_any;
5570 else if (stacktype == &t_dict_empty)
5571 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005572 else if (stacktype == &t_unknown)
5573 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005574 else
5575 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005576 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005577 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005578 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005579 {
5580 type_T *use_type = lvar->lv_type;
5581
Bram Moolenaar71abe482020-09-14 22:28:30 +02005582 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005583 if (has_index)
5584 {
5585 use_type = use_type->tt_member;
5586 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005587 // could be indexing "any"
5588 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005589 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005590 if (need_type(stacktype, use_type, -1, cctx,
5591 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005592 goto theend;
5593 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005594 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005595 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005596 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005597 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005599 else if (cmdidx == CMD_final)
5600 {
5601 emsg(_(e_final_requires_a_value));
5602 goto theend;
5603 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005604 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005606 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005607 goto theend;
5608 }
5609 else if (!has_type || dest == dest_option)
5610 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005611 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 goto theend;
5613 }
5614 else
5615 {
5616 // variables are always initialized
5617 if (ga_grow(instr, 1) == FAIL)
5618 goto theend;
5619 switch (member_type->tt_type)
5620 {
5621 case VAR_BOOL:
5622 generate_PUSHBOOL(cctx, VVAL_FALSE);
5623 break;
5624 case VAR_FLOAT:
5625#ifdef FEAT_FLOAT
5626 generate_PUSHF(cctx, 0.0);
5627#endif
5628 break;
5629 case VAR_STRING:
5630 generate_PUSHS(cctx, NULL);
5631 break;
5632 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005633 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005634 break;
5635 case VAR_FUNC:
5636 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5637 break;
5638 case VAR_LIST:
5639 generate_NEWLIST(cctx, 0);
5640 break;
5641 case VAR_DICT:
5642 generate_NEWDICT(cctx, 0);
5643 break;
5644 case VAR_JOB:
5645 generate_PUSHJOB(cctx, NULL);
5646 break;
5647 case VAR_CHANNEL:
5648 generate_PUSHCHANNEL(cctx, NULL);
5649 break;
5650 case VAR_NUMBER:
5651 case VAR_UNKNOWN:
5652 case VAR_ANY:
5653 case VAR_PARTIAL:
5654 case VAR_VOID:
5655 case VAR_SPECIAL: // cannot happen
5656 generate_PUSHNR(cctx, 0);
5657 break;
5658 }
5659 }
5660 if (var_count == 0)
5661 end = p;
5662 }
5663
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005664 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005665 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005666 break;
5667
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005668 if (oplen > 0 && *op != '=')
5669 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005670 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005671 type_T *stacktype;
5672
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005673 if (*op == '.')
5674 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005675 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005676 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005677 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005678 if (
5679#ifdef FEAT_FLOAT
5680 // If variable is float operation with number is OK.
5681 !(expected == &t_float && stacktype == &t_number) &&
5682#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005683 need_type(stacktype, expected, -1, cctx,
5684 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685 goto theend;
5686
5687 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005688 {
5689 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5690 goto theend;
5691 }
5692 else if (*op == '+')
5693 {
5694 if (generate_add_instr(cctx,
5695 operator_type(member_type, stacktype),
5696 member_type, stacktype) == FAIL)
5697 goto theend;
5698 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005699 else if (generate_two_op(cctx, op) == FAIL)
5700 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005702
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005703 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005704 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005705 int r;
5706
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005707 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005708 if (new_local)
5709 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005710 p = var_start + varlen;
5711 if (*p == '[')
5712 {
5713 p = skipwhite(p + 1);
5714 r = compile_expr0(&p, cctx);
5715 if (r == OK && *skipwhite(p) != ']')
5716 {
5717 // this should not happen
5718 emsg(_(e_missbrac));
5719 r = FAIL;
5720 }
5721 }
5722 else // if (*p == '.')
5723 {
5724 char_u *key_end = to_name_end(p + 1, TRUE);
5725 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5726
5727 r = generate_PUSHS(cctx, key);
5728 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005729 if (new_local)
5730 ++cctx->ctx_locals.ga_len;
5731 if (r == FAIL)
5732 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005733
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005734 if (type == &t_any)
5735 {
5736 type_T *idx_type = ((type_T **)stack->ga_data)[
5737 stack->ga_len - 1];
5738 // Index on variable of unknown type: guess the type from the
5739 // index type: number is dict, otherwise dict.
5740 // TODO: should do the assignment at runtime
5741 if (idx_type->tt_type == VAR_NUMBER)
5742 type = &t_list_any;
5743 else
5744 type = &t_dict_any;
5745 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005746 if (type->tt_type == VAR_DICT
5747 && may_generate_2STRING(-1, cctx) == FAIL)
5748 goto theend;
5749 if (type->tt_type == VAR_LIST
5750 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005751 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005752 {
5753 emsg(_(e_number_exp));
5754 goto theend;
5755 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005756
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005757 // Load the dict or list. On the stack we then have:
5758 // - value
5759 // - index
5760 // - variable
5761 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005762
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005763 if (type->tt_type == VAR_LIST)
5764 {
5765 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5766 return FAIL;
5767 }
5768 else if (type->tt_type == VAR_DICT)
5769 {
5770 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5771 return FAIL;
5772 }
5773 else
5774 {
5775 emsg(_(e_listreq));
5776 goto theend;
5777 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005778 }
5779 else
5780 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005781 if (is_decl && cmdidx == CMD_const
5782 && (dest == dest_script || dest == dest_local))
5783 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005784 generate_LOCKCONST(cctx);
5785
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005786 switch (dest)
5787 {
5788 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005789 generate_STOREOPT(cctx, skip_option_env_lead(name),
5790 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005791 break;
5792 case dest_global:
5793 // include g: with the name, easier to execute that way
5794 generate_STORE(cctx, ISN_STOREG, 0, name);
5795 break;
5796 case dest_buffer:
5797 // include b: with the name, easier to execute that way
5798 generate_STORE(cctx, ISN_STOREB, 0, name);
5799 break;
5800 case dest_window:
5801 // include w: with the name, easier to execute that way
5802 generate_STORE(cctx, ISN_STOREW, 0, name);
5803 break;
5804 case dest_tab:
5805 // include t: with the name, easier to execute that way
5806 generate_STORE(cctx, ISN_STORET, 0, name);
5807 break;
5808 case dest_env:
5809 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5810 break;
5811 case dest_reg:
5812 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5813 break;
5814 case dest_vimvar:
5815 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5816 break;
5817 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005818 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005819 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005820 {
5821 char_u *name_s = name;
5822
5823 // Include s: in the name for store_var()
5824 if (name[1] != ':')
5825 {
5826 int len = (int)STRLEN(name) + 3;
5827
5828 name_s = alloc(len);
5829 if (name_s == NULL)
5830 name_s = name;
5831 else
5832 vim_snprintf((char *)name_s, len,
5833 "s:%s", name);
5834 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005835 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5836 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005837 if (name_s != name)
5838 vim_free(name_s);
5839 }
5840 else
5841 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005842 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005843 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005844 break;
5845 case dest_local:
5846 if (lvar != NULL)
5847 {
5848 isn_T *isn = ((isn_T *)instr->ga_data)
5849 + instr->ga_len - 1;
5850
5851 // optimization: turn "var = 123" from ISN_PUSHNR +
5852 // ISN_STORE into ISN_STORENR
5853 if (!lvar->lv_from_outer
5854 && instr->ga_len == instr_count + 1
5855 && isn->isn_type == ISN_PUSHNR)
5856 {
5857 varnumber_T val = isn->isn_arg.number;
5858
5859 isn->isn_type = ISN_STORENR;
5860 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5861 isn->isn_arg.storenr.stnr_val = val;
5862 if (stack->ga_len > 0)
5863 --stack->ga_len;
5864 }
5865 else if (lvar->lv_from_outer)
5866 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005867 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005868 else
5869 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5870 }
5871 break;
5872 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005873 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005874
5875 if (var_idx + 1 < var_count)
5876 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005878
5879 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005880 if (var_count > 0 && !semicolon)
5881 {
5882 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5883 goto theend;
5884 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005885
Bram Moolenaarb2097502020-07-19 17:17:02 +02005886 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887
5888theend:
5889 vim_free(name);
5890 return ret;
5891}
5892
5893/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005894 * Check if "name" can be "unlet".
5895 */
5896 int
5897check_vim9_unlet(char_u *name)
5898{
5899 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5900 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005901 // "unlet s:var" is allowed in legacy script.
5902 if (*name == 's' && !script_is_vim9())
5903 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005904 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005905 return FAIL;
5906 }
5907 return OK;
5908}
5909
5910/*
5911 * Callback passed to ex_unletlock().
5912 */
5913 static int
5914compile_unlet(
5915 lval_T *lvp,
5916 char_u *name_end,
5917 exarg_T *eap,
5918 int deep UNUSED,
5919 void *coookie)
5920{
5921 cctx_T *cctx = coookie;
5922
5923 if (lvp->ll_tv == NULL)
5924 {
5925 char_u *p = lvp->ll_name;
5926 int cc = *name_end;
5927 int ret = OK;
5928
5929 // Normal name. Only supports g:, w:, t: and b: namespaces.
5930 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005931 if (*p == '$')
5932 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5933 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005934 ret = FAIL;
5935 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005936 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005937
5938 *name_end = cc;
5939 return ret;
5940 }
5941
5942 // TODO: unlet {list}[idx]
5943 // TODO: unlet {dict}[key]
5944 emsg("Sorry, :unlet not fully implemented yet");
5945 return FAIL;
5946}
5947
5948/*
5949 * compile "unlet var", "lock var" and "unlock var"
5950 * "arg" points to "var".
5951 */
5952 static char_u *
5953compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5954{
5955 char_u *p = arg;
5956
5957 if (eap->cmdidx != CMD_unlet)
5958 {
5959 emsg("Sorry, :lock and unlock not implemented yet");
5960 return NULL;
5961 }
5962
5963 if (*p == '!')
5964 {
5965 p = skipwhite(p + 1);
5966 eap->forceit = TRUE;
5967 }
5968
5969 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5970 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5971}
5972
5973/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 * Compile an :import command.
5975 */
5976 static char_u *
5977compile_import(char_u *arg, cctx_T *cctx)
5978{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005979 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980}
5981
5982/*
5983 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5984 */
5985 static int
5986compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5987{
5988 garray_T *instr = &cctx->ctx_instr;
5989 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5990
5991 if (endlabel == NULL)
5992 return FAIL;
5993 endlabel->el_next = *el;
5994 *el = endlabel;
5995 endlabel->el_end_label = instr->ga_len;
5996
5997 generate_JUMP(cctx, when, 0);
5998 return OK;
5999}
6000
6001 static void
6002compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6003{
6004 garray_T *instr = &cctx->ctx_instr;
6005
6006 while (*el != NULL)
6007 {
6008 endlabel_T *cur = (*el);
6009 isn_T *isn;
6010
6011 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6012 isn->isn_arg.jump.jump_where = instr->ga_len;
6013 *el = cur->el_next;
6014 vim_free(cur);
6015 }
6016}
6017
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006018 static void
6019compile_free_jump_to_end(endlabel_T **el)
6020{
6021 while (*el != NULL)
6022 {
6023 endlabel_T *cur = (*el);
6024
6025 *el = cur->el_next;
6026 vim_free(cur);
6027 }
6028}
6029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030/*
6031 * Create a new scope and set up the generic items.
6032 */
6033 static scope_T *
6034new_scope(cctx_T *cctx, scopetype_T type)
6035{
6036 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6037
6038 if (scope == NULL)
6039 return NULL;
6040 scope->se_outer = cctx->ctx_scope;
6041 cctx->ctx_scope = scope;
6042 scope->se_type = type;
6043 scope->se_local_count = cctx->ctx_locals.ga_len;
6044 return scope;
6045}
6046
6047/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006048 * Free the current scope and go back to the outer scope.
6049 */
6050 static void
6051drop_scope(cctx_T *cctx)
6052{
6053 scope_T *scope = cctx->ctx_scope;
6054
6055 if (scope == NULL)
6056 {
6057 iemsg("calling drop_scope() without a scope");
6058 return;
6059 }
6060 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006061 switch (scope->se_type)
6062 {
6063 case IF_SCOPE:
6064 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6065 case FOR_SCOPE:
6066 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6067 case WHILE_SCOPE:
6068 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6069 case TRY_SCOPE:
6070 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6071 case NO_SCOPE:
6072 case BLOCK_SCOPE:
6073 break;
6074 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006075 vim_free(scope);
6076}
6077
6078/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006079 * compile "if expr"
6080 *
6081 * "if expr" Produces instructions:
6082 * EVAL expr Push result of "expr"
6083 * JUMP_IF_FALSE end
6084 * ... body ...
6085 * end:
6086 *
6087 * "if expr | else" Produces instructions:
6088 * EVAL expr Push result of "expr"
6089 * JUMP_IF_FALSE else
6090 * ... body ...
6091 * JUMP_ALWAYS end
6092 * else:
6093 * ... body ...
6094 * end:
6095 *
6096 * "if expr1 | elseif expr2 | else" Produces instructions:
6097 * EVAL expr Push result of "expr"
6098 * JUMP_IF_FALSE elseif
6099 * ... body ...
6100 * JUMP_ALWAYS end
6101 * elseif:
6102 * EVAL expr Push result of "expr"
6103 * JUMP_IF_FALSE else
6104 * ... body ...
6105 * JUMP_ALWAYS end
6106 * else:
6107 * ... body ...
6108 * end:
6109 */
6110 static char_u *
6111compile_if(char_u *arg, cctx_T *cctx)
6112{
6113 char_u *p = arg;
6114 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006115 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006117 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006118 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119
Bram Moolenaara5565e42020-05-09 15:44:01 +02006120 CLEAR_FIELD(ppconst);
6121 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006122 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006123 clear_ppconst(&ppconst);
6124 return NULL;
6125 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006126 if (cctx->ctx_skip == SKIP_YES)
6127 clear_ppconst(&ppconst);
6128 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006129 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006130 int error = FALSE;
6131 int v;
6132
Bram Moolenaara5565e42020-05-09 15:44:01 +02006133 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006134 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006135 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006136 if (error)
6137 return NULL;
6138 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006139 }
6140 else
6141 {
6142 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006143 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006144 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006145 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006146 if (bool_on_stack(cctx) == FAIL)
6147 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149
6150 scope = new_scope(cctx, IF_SCOPE);
6151 if (scope == NULL)
6152 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006153 scope->se_skip_save = skip_save;
6154 // "is_had_return" will be reset if any block does not end in :return
6155 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006157 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006158 {
6159 // "where" is set when ":elseif", "else" or ":endif" is found
6160 scope->se_u.se_if.is_if_label = instr->ga_len;
6161 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6162 }
6163 else
6164 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165
6166 return p;
6167}
6168
6169 static char_u *
6170compile_elseif(char_u *arg, cctx_T *cctx)
6171{
6172 char_u *p = arg;
6173 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006174 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175 isn_T *isn;
6176 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006177 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006178 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179
6180 if (scope == NULL || scope->se_type != IF_SCOPE)
6181 {
6182 emsg(_(e_elseif_without_if));
6183 return NULL;
6184 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006185 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006186 if (!cctx->ctx_had_return)
6187 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006189 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006190 {
6191 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006193 return NULL;
6194 // previous "if" or "elseif" jumps here
6195 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6196 isn->isn_arg.jump.jump_where = instr->ga_len;
6197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006199 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006200 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006201 if (cctx->ctx_skip == SKIP_YES)
6202 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006203 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006204 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006205 clear_ppconst(&ppconst);
6206 return NULL;
6207 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006208 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006209 if (scope->se_skip_save == SKIP_YES)
6210 clear_ppconst(&ppconst);
6211 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006212 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006213 int error = FALSE;
6214 int v;
6215
Bram Moolenaar7f141552020-05-09 17:35:53 +02006216 // The expression results in a constant.
6217 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006218 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6219 if (error)
6220 return NULL;
6221 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006222 clear_ppconst(&ppconst);
6223 scope->se_u.se_if.is_if_label = -1;
6224 }
6225 else
6226 {
6227 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006228 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006229 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006230 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006231 if (bool_on_stack(cctx) == FAIL)
6232 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006234 // "where" is set when ":elseif", "else" or ":endif" is found
6235 scope->se_u.se_if.is_if_label = instr->ga_len;
6236 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238
6239 return p;
6240}
6241
6242 static char_u *
6243compile_else(char_u *arg, cctx_T *cctx)
6244{
6245 char_u *p = arg;
6246 garray_T *instr = &cctx->ctx_instr;
6247 isn_T *isn;
6248 scope_T *scope = cctx->ctx_scope;
6249
6250 if (scope == NULL || scope->se_type != IF_SCOPE)
6251 {
6252 emsg(_(e_else_without_if));
6253 return NULL;
6254 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006255 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006256 if (!cctx->ctx_had_return)
6257 scope->se_u.se_if.is_had_return = FALSE;
6258 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259
Bram Moolenaarefd88552020-06-18 20:50:10 +02006260 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006261 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006262 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006263 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006264 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006265 if (!cctx->ctx_had_return
6266 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6267 JUMP_ALWAYS, cctx) == FAIL)
6268 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006269 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006270
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006271 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006272 {
6273 if (scope->se_u.se_if.is_if_label >= 0)
6274 {
6275 // previous "if" or "elseif" jumps here
6276 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6277 isn->isn_arg.jump.jump_where = instr->ga_len;
6278 scope->se_u.se_if.is_if_label = -1;
6279 }
6280 }
6281
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006282 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006283 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285
6286 return p;
6287}
6288
6289 static char_u *
6290compile_endif(char_u *arg, cctx_T *cctx)
6291{
6292 scope_T *scope = cctx->ctx_scope;
6293 ifscope_T *ifscope;
6294 garray_T *instr = &cctx->ctx_instr;
6295 isn_T *isn;
6296
6297 if (scope == NULL || scope->se_type != IF_SCOPE)
6298 {
6299 emsg(_(e_endif_without_if));
6300 return NULL;
6301 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006302 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006303 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006304 if (!cctx->ctx_had_return)
6305 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006307 if (scope->se_u.se_if.is_if_label >= 0)
6308 {
6309 // previous "if" or "elseif" jumps here
6310 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6311 isn->isn_arg.jump.jump_where = instr->ga_len;
6312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 // Fill in the "end" label in jumps at the end of the blocks.
6314 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006315 cctx->ctx_skip = scope->se_skip_save;
6316
6317 // If all the blocks end in :return and there is an :else then the
6318 // had_return flag is set.
6319 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006321 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 return arg;
6323}
6324
6325/*
6326 * compile "for var in expr"
6327 *
6328 * Produces instructions:
6329 * PUSHNR -1
6330 * STORE loop-idx Set index to -1
6331 * EVAL expr Push result of "expr"
6332 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6333 * - if beyond end, jump to "end"
6334 * - otherwise get item from list and push it
6335 * STORE var Store item in "var"
6336 * ... body ...
6337 * JUMP top Jump back to repeat
6338 * end: DROP Drop the result of "expr"
6339 *
6340 */
6341 static char_u *
6342compile_for(char_u *arg, cctx_T *cctx)
6343{
6344 char_u *p;
6345 size_t varlen;
6346 garray_T *instr = &cctx->ctx_instr;
6347 garray_T *stack = &cctx->ctx_type_stack;
6348 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006349 lvar_T *loop_lvar; // loop iteration variable
6350 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351 type_T *vartype;
6352
6353 // TODO: list of variables: "for [key, value] in dict"
6354 // parse "var"
6355 for (p = arg; eval_isnamec1(*p); ++p)
6356 ;
6357 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006358 var_lvar = lookup_local(arg, varlen, cctx);
6359 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006361 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006362 return NULL;
6363 }
6364
6365 // consume "in"
6366 p = skipwhite(p);
6367 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6368 {
6369 emsg(_(e_missing_in));
6370 return NULL;
6371 }
6372 p = skipwhite(p + 2);
6373
6374
6375 scope = new_scope(cctx, FOR_SCOPE);
6376 if (scope == NULL)
6377 return NULL;
6378
6379 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006380 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6381 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006382 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006383 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006384 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387
6388 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006389 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6390 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006391 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006392 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006393 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006396
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006397 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398
6399 // compile "expr", it remains on the stack until "endfor"
6400 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006401 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006402 {
6403 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006407 // Now that we know the type of "var", check that it is a list, now or at
6408 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006410 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006412 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 return NULL;
6414 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006415 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006416 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417
6418 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006419 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006421 generate_FOR(cctx, loop_lvar->lv_idx);
6422 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423
6424 return arg;
6425}
6426
6427/*
6428 * compile "endfor"
6429 */
6430 static char_u *
6431compile_endfor(char_u *arg, cctx_T *cctx)
6432{
6433 garray_T *instr = &cctx->ctx_instr;
6434 scope_T *scope = cctx->ctx_scope;
6435 forscope_T *forscope;
6436 isn_T *isn;
6437
6438 if (scope == NULL || scope->se_type != FOR_SCOPE)
6439 {
6440 emsg(_(e_for));
6441 return NULL;
6442 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006443 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006445 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446
6447 // At end of ":for" scope jump back to the FOR instruction.
6448 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6449
6450 // Fill in the "end" label in the FOR statement so it can jump here
6451 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6452 isn->isn_arg.forloop.for_end = instr->ga_len;
6453
6454 // Fill in the "end" label any BREAK statements
6455 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6456
6457 // Below the ":for" scope drop the "expr" list from the stack.
6458 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6459 return NULL;
6460
6461 vim_free(scope);
6462
6463 return arg;
6464}
6465
6466/*
6467 * compile "while expr"
6468 *
6469 * Produces instructions:
6470 * top: EVAL expr Push result of "expr"
6471 * JUMP_IF_FALSE end jump if false
6472 * ... body ...
6473 * JUMP top Jump back to repeat
6474 * end:
6475 *
6476 */
6477 static char_u *
6478compile_while(char_u *arg, cctx_T *cctx)
6479{
6480 char_u *p = arg;
6481 garray_T *instr = &cctx->ctx_instr;
6482 scope_T *scope;
6483
6484 scope = new_scope(cctx, WHILE_SCOPE);
6485 if (scope == NULL)
6486 return NULL;
6487
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006488 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006489
6490 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006491 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492 return NULL;
6493
Bram Moolenaar13106602020-10-04 16:06:05 +02006494 if (bool_on_stack(cctx) == FAIL)
6495 return FAIL;
6496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006498 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 JUMP_IF_FALSE, cctx) == FAIL)
6500 return FAIL;
6501
6502 return p;
6503}
6504
6505/*
6506 * compile "endwhile"
6507 */
6508 static char_u *
6509compile_endwhile(char_u *arg, cctx_T *cctx)
6510{
6511 scope_T *scope = cctx->ctx_scope;
6512
6513 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6514 {
6515 emsg(_(e_while));
6516 return NULL;
6517 }
6518 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006519 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520
6521 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006522 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523
6524 // Fill in the "end" label in the WHILE statement so it can jump here.
6525 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006526 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527
6528 vim_free(scope);
6529
6530 return arg;
6531}
6532
6533/*
6534 * compile "continue"
6535 */
6536 static char_u *
6537compile_continue(char_u *arg, cctx_T *cctx)
6538{
6539 scope_T *scope = cctx->ctx_scope;
6540
6541 for (;;)
6542 {
6543 if (scope == NULL)
6544 {
6545 emsg(_(e_continue));
6546 return NULL;
6547 }
6548 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6549 break;
6550 scope = scope->se_outer;
6551 }
6552
6553 // Jump back to the FOR or WHILE instruction.
6554 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006555 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6556 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557 return arg;
6558}
6559
6560/*
6561 * compile "break"
6562 */
6563 static char_u *
6564compile_break(char_u *arg, cctx_T *cctx)
6565{
6566 scope_T *scope = cctx->ctx_scope;
6567 endlabel_T **el;
6568
6569 for (;;)
6570 {
6571 if (scope == NULL)
6572 {
6573 emsg(_(e_break));
6574 return NULL;
6575 }
6576 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6577 break;
6578 scope = scope->se_outer;
6579 }
6580
6581 // Jump to the end of the FOR or WHILE loop.
6582 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006583 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006585 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6587 return FAIL;
6588
6589 return arg;
6590}
6591
6592/*
6593 * compile "{" start of block
6594 */
6595 static char_u *
6596compile_block(char_u *arg, cctx_T *cctx)
6597{
6598 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6599 return NULL;
6600 return skipwhite(arg + 1);
6601}
6602
6603/*
6604 * compile end of block: drop one scope
6605 */
6606 static void
6607compile_endblock(cctx_T *cctx)
6608{
6609 scope_T *scope = cctx->ctx_scope;
6610
6611 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006612 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 vim_free(scope);
6614}
6615
6616/*
6617 * compile "try"
6618 * Creates a new scope for the try-endtry, pointing to the first catch and
6619 * finally.
6620 * Creates another scope for the "try" block itself.
6621 * TRY instruction sets up exception handling at runtime.
6622 *
6623 * "try"
6624 * TRY -> catch1, -> finally push trystack entry
6625 * ... try block
6626 * "throw {exception}"
6627 * EVAL {exception}
6628 * THROW create exception
6629 * ... try block
6630 * " catch {expr}"
6631 * JUMP -> finally
6632 * catch1: PUSH exeception
6633 * EVAL {expr}
6634 * MATCH
6635 * JUMP nomatch -> catch2
6636 * CATCH remove exception
6637 * ... catch block
6638 * " catch"
6639 * JUMP -> finally
6640 * catch2: CATCH remove exception
6641 * ... catch block
6642 * " finally"
6643 * finally:
6644 * ... finally block
6645 * " endtry"
6646 * ENDTRY pop trystack entry, may rethrow
6647 */
6648 static char_u *
6649compile_try(char_u *arg, cctx_T *cctx)
6650{
6651 garray_T *instr = &cctx->ctx_instr;
6652 scope_T *try_scope;
6653 scope_T *scope;
6654
6655 // scope that holds the jumps that go to catch/finally/endtry
6656 try_scope = new_scope(cctx, TRY_SCOPE);
6657 if (try_scope == NULL)
6658 return NULL;
6659
6660 // "catch" is set when the first ":catch" is found.
6661 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006662 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 if (generate_instr(cctx, ISN_TRY) == NULL)
6664 return NULL;
6665
6666 // scope for the try block itself
6667 scope = new_scope(cctx, BLOCK_SCOPE);
6668 if (scope == NULL)
6669 return NULL;
6670
6671 return arg;
6672}
6673
6674/*
6675 * compile "catch {expr}"
6676 */
6677 static char_u *
6678compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6679{
6680 scope_T *scope = cctx->ctx_scope;
6681 garray_T *instr = &cctx->ctx_instr;
6682 char_u *p;
6683 isn_T *isn;
6684
6685 // end block scope from :try or :catch
6686 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6687 compile_endblock(cctx);
6688 scope = cctx->ctx_scope;
6689
6690 // Error if not in a :try scope
6691 if (scope == NULL || scope->se_type != TRY_SCOPE)
6692 {
6693 emsg(_(e_catch));
6694 return NULL;
6695 }
6696
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006697 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006699 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 return NULL;
6701 }
6702
6703 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006704 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 JUMP_ALWAYS, cctx) == FAIL)
6706 return NULL;
6707
6708 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006709 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 if (isn->isn_arg.try.try_catch == 0)
6711 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006712 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 {
6714 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006715 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 isn->isn_arg.jump.jump_where = instr->ga_len;
6717 }
6718
6719 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006720 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006722 scope->se_u.se_try.ts_caught_all = TRUE;
6723 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724 }
6725 else
6726 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006727 char_u *end;
6728 char_u *pat;
6729 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006730 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006731 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733 // Push v:exception, push {expr} and MATCH
6734 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6735
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006736 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006737 if (*end != *p)
6738 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006739 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006740 vim_free(tofree);
6741 return FAIL;
6742 }
6743 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006744 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006745 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006746 len = (int)(end - tofree);
6747 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006748 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006749 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006750 if (pat == NULL)
6751 return FAIL;
6752 if (generate_PUSHS(cctx, pat) == FAIL)
6753 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6756 return NULL;
6757
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006758 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6760 return NULL;
6761 }
6762
6763 if (generate_instr(cctx, ISN_CATCH) == NULL)
6764 return NULL;
6765
6766 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6767 return NULL;
6768 return p;
6769}
6770
6771 static char_u *
6772compile_finally(char_u *arg, cctx_T *cctx)
6773{
6774 scope_T *scope = cctx->ctx_scope;
6775 garray_T *instr = &cctx->ctx_instr;
6776 isn_T *isn;
6777
6778 // end block scope from :try or :catch
6779 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6780 compile_endblock(cctx);
6781 scope = cctx->ctx_scope;
6782
6783 // Error if not in a :try scope
6784 if (scope == NULL || scope->se_type != TRY_SCOPE)
6785 {
6786 emsg(_(e_finally));
6787 return NULL;
6788 }
6789
6790 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006791 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792 if (isn->isn_arg.try.try_finally != 0)
6793 {
6794 emsg(_(e_finally_dup));
6795 return NULL;
6796 }
6797
6798 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006799 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800
Bram Moolenaar585fea72020-04-02 22:33:21 +02006801 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006802 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 {
6804 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006805 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006807 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 }
6809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810 // TODO: set index in ts_finally_label jumps
6811
6812 return arg;
6813}
6814
6815 static char_u *
6816compile_endtry(char_u *arg, cctx_T *cctx)
6817{
6818 scope_T *scope = cctx->ctx_scope;
6819 garray_T *instr = &cctx->ctx_instr;
6820 isn_T *isn;
6821
6822 // end block scope from :catch or :finally
6823 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6824 compile_endblock(cctx);
6825 scope = cctx->ctx_scope;
6826
6827 // Error if not in a :try scope
6828 if (scope == NULL || scope->se_type != TRY_SCOPE)
6829 {
6830 if (scope == NULL)
6831 emsg(_(e_no_endtry));
6832 else if (scope->se_type == WHILE_SCOPE)
6833 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006834 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 emsg(_(e_endfor));
6836 else
6837 emsg(_(e_endif));
6838 return NULL;
6839 }
6840
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006841 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6843 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006844 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 return NULL;
6846 }
6847
6848 // Fill in the "end" label in jumps at the end of the blocks, if not done
6849 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006850 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006851
6852 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006853 if (isn->isn_arg.try.try_catch == 0)
6854 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 if (isn->isn_arg.try.try_finally == 0)
6856 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006857
6858 if (scope->se_u.se_try.ts_catch_label != 0)
6859 {
6860 // Last catch without match jumps here
6861 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6862 isn->isn_arg.jump.jump_where = instr->ga_len;
6863 }
6864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 compile_endblock(cctx);
6866
6867 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6868 return NULL;
6869 return arg;
6870}
6871
6872/*
6873 * compile "throw {expr}"
6874 */
6875 static char_u *
6876compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6877{
6878 char_u *p = skipwhite(arg);
6879
Bram Moolenaara5565e42020-05-09 15:44:01 +02006880 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 return NULL;
6882 if (may_generate_2STRING(-1, cctx) == FAIL)
6883 return NULL;
6884 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6885 return NULL;
6886
6887 return p;
6888}
6889
6890/*
6891 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006892 * compile "echomsg expr"
6893 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006894 * compile "execute expr"
6895 */
6896 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006897compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006898{
6899 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006900 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006901 int count = 0;
6902
6903 for (;;)
6904 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006905 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006906 return NULL;
6907 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006908 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006909 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006910 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006911 break;
6912 }
6913
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006914 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6915 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6916 else if (cmdidx == CMD_execute)
6917 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6918 else if (cmdidx == CMD_echomsg)
6919 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6920 else
6921 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 return p;
6923}
6924
6925/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006926 * :put r
6927 * :put ={expr}
6928 */
6929 static char_u *
6930compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6931{
6932 char_u *line = arg;
6933 linenr_T lnum;
6934 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006935 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006936
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006937 eap->regname = *line;
6938
6939 if (eap->regname == '=')
6940 {
6941 char_u *p = line + 1;
6942
6943 if (compile_expr0(&p, cctx) == FAIL)
6944 return NULL;
6945 line = p;
6946 }
6947 else if (eap->regname != NUL)
6948 ++line;
6949
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006950 // "errormsg" will not be set because the range is ADDR_LINES.
6951 // TODO: if the range contains something like "$" or "." need to evaluate
6952 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006953 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6954 return NULL;
6955 if (eap->addr_count == 0)
6956 lnum = -1;
6957 else
6958 lnum = eap->line2;
6959 if (above)
6960 --lnum;
6961
6962 generate_PUT(cctx, eap->regname, lnum);
6963 return line;
6964}
6965
6966/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006967 * A command that is not compiled, execute with legacy code.
6968 */
6969 static char_u *
6970compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6971{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006972 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006973 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006974 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006975
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006976 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006977 goto theend;
6978
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006979 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006980 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006981 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006982 int usefilter = FALSE;
6983
6984 has_expr = argt & (EX_XFILE | EX_EXPAND);
6985
6986 // If the command can be followed by a bar, find the bar and truncate
6987 // it, so that the following command can be compiled.
6988 // The '|' is overwritten with a NUL, it is put back below.
6989 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6990 && *eap->arg == '!')
6991 // :w !filter or :r !filter or :r! filter
6992 usefilter = TRUE;
6993 if ((argt & EX_TRLBAR) && !usefilter)
6994 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006995 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006996 separate_nextcmd(eap);
6997 if (eap->nextcmd != NULL)
6998 nextcmd = eap->nextcmd;
6999 }
7000 }
7001
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007002 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7003 {
7004 // expand filename in "syntax include [@group] filename"
7005 has_expr = TRUE;
7006 eap->arg = skipwhite(eap->arg + 7);
7007 if (*eap->arg == '@')
7008 eap->arg = skiptowhite(eap->arg);
7009 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007010
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007011 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007012 {
7013 int count = 0;
7014 char_u *start = skipwhite(line);
7015
7016 // :cmd xxx`=expr1`yyy`=expr2`zzz
7017 // PUSHS ":cmd xxx"
7018 // eval expr1
7019 // PUSHS "yyy"
7020 // eval expr2
7021 // PUSHS "zzz"
7022 // EXECCONCAT 5
7023 for (;;)
7024 {
7025 if (p > start)
7026 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007027 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007028 ++count;
7029 }
7030 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007031 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007032 return NULL;
7033 may_generate_2STRING(-1, cctx);
7034 ++count;
7035 p = skipwhite(p);
7036 if (*p != '`')
7037 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007038 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007039 return NULL;
7040 }
7041 start = p + 1;
7042
7043 p = (char_u *)strstr((char *)start, "`=");
7044 if (p == NULL)
7045 {
7046 if (*skipwhite(start) != NUL)
7047 {
7048 generate_PUSHS(cctx, vim_strsave(start));
7049 ++count;
7050 }
7051 break;
7052 }
7053 }
7054 generate_EXECCONCAT(cctx, count);
7055 }
7056 else
7057 generate_EXEC(cctx, line);
7058
7059theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007060 if (*nextcmd != NUL)
7061 {
7062 // the parser expects a pointer to the bar, put it back
7063 --nextcmd;
7064 *nextcmd = '|';
7065 }
7066
7067 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007068}
7069
7070/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007071 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007072 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007073 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007074 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007075add_def_function(ufunc_T *ufunc)
7076{
7077 dfunc_T *dfunc;
7078
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007079 if (def_functions.ga_len == 0)
7080 {
7081 // The first position is not used, so that a zero uf_dfunc_idx means it
7082 // wasn't set.
7083 if (ga_grow(&def_functions, 1) == FAIL)
7084 return FAIL;
7085 ++def_functions.ga_len;
7086 }
7087
Bram Moolenaar09689a02020-05-09 22:50:08 +02007088 // Add the function to "def_functions".
7089 if (ga_grow(&def_functions, 1) == FAIL)
7090 return FAIL;
7091 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7092 CLEAR_POINTER(dfunc);
7093 dfunc->df_idx = def_functions.ga_len;
7094 ufunc->uf_dfunc_idx = dfunc->df_idx;
7095 dfunc->df_ufunc = ufunc;
7096 ++def_functions.ga_len;
7097 return OK;
7098}
7099
7100/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 * After ex_function() has collected all the function lines: parse and compile
7102 * the lines into instructions.
7103 * Adds the function to "def_functions".
7104 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7105 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007106 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007107 * This can be used recursively through compile_lambda(), which may reallocate
7108 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007109 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007111 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007112compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 char_u *line = NULL;
7115 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 cctx_T cctx;
7118 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007119 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 int ret = FAIL;
7121 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007122 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007123 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007124 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007126 // When using a function that was compiled before: Free old instructions.
7127 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007128 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007130 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7131 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007132 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007134 else
7135 {
7136 if (add_def_function(ufunc) == FAIL)
7137 return FAIL;
7138 new_def_function = TRUE;
7139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140
Bram Moolenaar985116a2020-07-12 17:31:09 +02007141 ufunc->uf_def_status = UF_COMPILING;
7142
Bram Moolenaara80faa82020-04-12 19:37:17 +02007143 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 cctx.ctx_ufunc = ufunc;
7145 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007146 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7148 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7149 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7150 cctx.ctx_type_list = &ufunc->uf_type_list;
7151 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7152 instr = &cctx.ctx_instr;
7153
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007154 // Set the context to the function, it may be compiled when called from
7155 // another script. Set the script version to the most modern one.
7156 // The line number will be set in next_line_from_context().
7157 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7159
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007160 // Make sure error messages are OK.
7161 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7162 if (do_estack_push)
7163 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007164 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007165
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007166 if (ufunc->uf_def_args.ga_len > 0)
7167 {
7168 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007169 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007170 int i;
7171 char_u *arg;
7172 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007173 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007174
7175 // Produce instructions for the default values of optional arguments.
7176 // Store the instruction index in uf_def_arg_idx[] so that we know
7177 // where to start when the function is called, depending on the number
7178 // of arguments.
7179 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7180 if (ufunc->uf_def_arg_idx == NULL)
7181 goto erret;
7182 for (i = 0; i < count; ++i)
7183 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007184 garray_T *stack = &cctx.ctx_type_stack;
7185 type_T *val_type;
7186 int arg_idx = first_def_arg + i;
7187
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007188 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7189 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007190 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007191 goto erret;
7192
7193 // If no type specified use the type of the default value.
7194 // Otherwise check that the default value type matches the
7195 // specified type.
7196 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7197 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007198 {
7199 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007200 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007201 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007202 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7203 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007204 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007205
7206 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007207 goto erret;
7208 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007209 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007210
7211 if (did_set_arg_type)
7212 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007213 }
7214
7215 /*
7216 * Loop over all the lines of the function and generate instructions.
7217 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218 for (;;)
7219 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007220 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007221 int starts_with_colon = FALSE;
7222 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007223 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007224
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007225 // Bail out on the first error to avoid a flood of errors and report
7226 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007227 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007228 goto erret;
7229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 if (line != NULL && *line == '|')
7231 // the line continues after a '|'
7232 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007233 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007234 && !(*line == '#' && (line == cctx.ctx_line_start
7235 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007237 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238 goto erret;
7239 }
7240 else
7241 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007242 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007243 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007244 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 }
7247
Bram Moolenaara80faa82020-04-12 19:37:17 +02007248 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 ea.cmdlinep = &line;
7250 ea.cmd = skipwhite(line);
7251
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007252 // Some things can be recognized by the first character.
7253 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007255 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007256 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007257 if (ea.cmd[1] != '{')
7258 {
7259 line = (char_u *)"";
7260 continue;
7261 }
7262 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007264 case '}':
7265 {
7266 // "}" ends a block scope
7267 scopetype_T stype = cctx.ctx_scope == NULL
7268 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007270 if (stype == BLOCK_SCOPE)
7271 {
7272 compile_endblock(&cctx);
7273 line = ea.cmd;
7274 }
7275 else
7276 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007277 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007278 goto erret;
7279 }
7280 if (line != NULL)
7281 line = skipwhite(ea.cmd + 1);
7282 continue;
7283 }
7284
7285 case '{':
7286 // "{" starts a block scope
7287 // "{'a': 1}->func() is something else
7288 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7289 {
7290 line = compile_block(ea.cmd, &cctx);
7291 continue;
7292 }
7293 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 }
7295
7296 /*
7297 * COMMAND MODIFIERS
7298 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007299 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007300 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7301 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 {
7303 if (errormsg != NULL)
7304 goto erret;
7305 // empty line or comment
7306 line = (char_u *)"";
7307 continue;
7308 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007309 generate_cmdmods(&cctx, &local_cmdmod);
7310 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007312 // Check if there was a colon after the last command modifier or before
7313 // the current position.
7314 for (p = ea.cmd; p >= line; --p)
7315 {
7316 if (*p == ':')
7317 starts_with_colon = TRUE;
7318 if (p < ea.cmd && !VIM_ISWHITE(*p))
7319 break;
7320 }
7321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007323 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007325 {
7326 if (*ea.cmd == '(')
7327 // not for "call()"
7328 ea.cmd = p;
7329 else
7330 ea.cmd = skipwhite(ea.cmd);
7331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007333 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007335 char_u *pskip;
7336
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007337 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007338 // find what follows.
7339 // Skip over "var.member", "var[idx]" and the like.
7340 // Also "&opt = val", "$ENV = val" and "@r = val".
7341 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007342 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007343 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007344 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007346 char_u *var_end;
7347 int oplen;
7348 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349
Bram Moolenaar65821722020-08-02 18:58:54 +02007350 if (ea.cmd[0] == '@')
7351 var_end = ea.cmd + 2;
7352 else
7353 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007354 FNE_CHECK_START | FNE_INCL_BR);
7355 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007356 if (oplen > 0)
7357 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007358 size_t len = p - ea.cmd;
7359
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007360 // Recognize an assignment if we recognize the variable
7361 // name:
7362 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007363 // "local = expr" where "local" is a local var.
7364 // "script = expr" where "script" is a script-local var.
7365 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007366 // "&opt = expr"
7367 // "$ENV = expr"
7368 // "@r = expr"
7369 if (*ea.cmd == '&'
7370 || *ea.cmd == '$'
7371 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007372 || ((len) > 2 && ea.cmd[1] == ':')
7373 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007374 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007375 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007376 || script_var_exists(ea.cmd, len,
7377 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007378 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007379 {
7380 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007381 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007382 goto erret;
7383 continue;
7384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 }
7386 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007387
7388 if (*ea.cmd == '[')
7389 {
7390 // [var, var] = expr
7391 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7392 if (line == NULL)
7393 goto erret;
7394 if (line != ea.cmd)
7395 continue;
7396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 }
7398
7399 /*
7400 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007401 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007403 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007404 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007405 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007406 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007407 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007408 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007409 if (!starts_with_colon)
7410 {
7411 emsg(_(e_colon_required_before_a_range));
7412 goto erret;
7413 }
7414 if (ends_excmd2(line, ea.cmd))
7415 {
7416 // A range without a command: jump to the line.
7417 // TODO: compile to a more efficient command, possibly
7418 // calling parse_cmd_address().
7419 ea.cmdidx = CMD_SIZE;
7420 line = compile_exec(line, &ea, &cctx);
7421 goto nextline;
7422 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007423 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007424 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007425 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007426 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007427 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428
7429 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7430 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007431 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007432 {
7433 line += STRLEN(line);
7434 continue;
7435 }
7436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007438 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007440 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007441 iemsg("Command from find_ex_command() not handled");
7442 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 }
7445
Bram Moolenaar3988f642020-08-27 22:43:03 +02007446 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007447 && ea.cmdidx != CMD_elseif
7448 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007449 && ea.cmdidx != CMD_endif
7450 && ea.cmdidx != CMD_endfor
7451 && ea.cmdidx != CMD_endwhile
7452 && ea.cmdidx != CMD_catch
7453 && ea.cmdidx != CMD_finally
7454 && ea.cmdidx != CMD_endtry)
7455 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007456 emsg(_(e_unreachable_code_after_return));
7457 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007458 }
7459
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007460 p = skipwhite(p);
7461 if (ea.cmdidx != CMD_SIZE
7462 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7463 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007464 if (ea.cmdidx >= 0)
7465 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007466 if ((ea.argt & EX_BANG) && *p == '!')
7467 {
7468 ea.forceit = TRUE;
7469 p = skipwhite(p + 1);
7470 }
7471 }
7472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 switch (ea.cmdidx)
7474 {
7475 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007476 ea.arg = p;
7477 line = compile_nested_function(&ea, &cctx);
7478 break;
7479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007481 // TODO: should we allow this, e.g. to declare a global
7482 // function?
7483 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 goto erret;
7485
7486 case CMD_return:
7487 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007488 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 break;
7490
7491 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007492 emsg(_(e_cannot_use_let_in_vim9_script));
7493 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007494 case CMD_var:
7495 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 case CMD_const:
7497 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007498 if (line == p)
7499 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500 break;
7501
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007502 case CMD_unlet:
7503 case CMD_unlockvar:
7504 case CMD_lockvar:
7505 line = compile_unletlock(p, &ea, &cctx);
7506 break;
7507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508 case CMD_import:
7509 line = compile_import(p, &cctx);
7510 break;
7511
7512 case CMD_if:
7513 line = compile_if(p, &cctx);
7514 break;
7515 case CMD_elseif:
7516 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007517 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 break;
7519 case CMD_else:
7520 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007521 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 break;
7523 case CMD_endif:
7524 line = compile_endif(p, &cctx);
7525 break;
7526
7527 case CMD_while:
7528 line = compile_while(p, &cctx);
7529 break;
7530 case CMD_endwhile:
7531 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007532 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 break;
7534
7535 case CMD_for:
7536 line = compile_for(p, &cctx);
7537 break;
7538 case CMD_endfor:
7539 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007540 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 break;
7542 case CMD_continue:
7543 line = compile_continue(p, &cctx);
7544 break;
7545 case CMD_break:
7546 line = compile_break(p, &cctx);
7547 break;
7548
7549 case CMD_try:
7550 line = compile_try(p, &cctx);
7551 break;
7552 case CMD_catch:
7553 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007554 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 break;
7556 case CMD_finally:
7557 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007558 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559 break;
7560 case CMD_endtry:
7561 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007562 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 break;
7564 case CMD_throw:
7565 line = compile_throw(p, &cctx);
7566 break;
7567
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007568 case CMD_eval:
7569 if (compile_expr0(&p, &cctx) == FAIL)
7570 goto erret;
7571
Bram Moolenaar3988f642020-08-27 22:43:03 +02007572 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007573 generate_instr_drop(&cctx, ISN_DROP, 1);
7574
7575 line = skipwhite(p);
7576 break;
7577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007580 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007581 case CMD_echomsg:
7582 case CMD_echoerr:
7583 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007584 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007586 case CMD_put:
7587 ea.cmd = cmd;
7588 line = compile_put(p, &ea, &cctx);
7589 break;
7590
Bram Moolenaar3988f642020-08-27 22:43:03 +02007591 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007592
Bram Moolenaarae616492020-07-28 20:07:27 +02007593 case CMD_append:
7594 case CMD_change:
7595 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007596 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007597 case CMD_xit:
7598 not_in_vim9(&ea);
7599 goto erret;
7600
Bram Moolenaar002262f2020-07-08 17:47:57 +02007601 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007602 if (cctx.ctx_skip != SKIP_YES)
7603 {
7604 semsg(_(e_invalid_command_str), ea.cmd);
7605 goto erret;
7606 }
7607 // We don't check for a next command here.
7608 line = (char_u *)"";
7609 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007612 if (cctx.ctx_skip == SKIP_YES)
7613 {
7614 // We don't check for a next command here.
7615 line = (char_u *)"";
7616 }
7617 else
7618 {
7619 // Not recognized, execute with do_cmdline_cmd().
7620 ea.arg = p;
7621 line = compile_exec(line, &ea, &cctx);
7622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 break;
7624 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007625nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 if (line == NULL)
7627 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007628 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007630 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007631 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633 if (cctx.ctx_type_stack.ga_len < 0)
7634 {
7635 iemsg("Type stack underflow");
7636 goto erret;
7637 }
7638 }
7639
7640 if (cctx.ctx_scope != NULL)
7641 {
7642 if (cctx.ctx_scope->se_type == IF_SCOPE)
7643 emsg(_(e_endif));
7644 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7645 emsg(_(e_endwhile));
7646 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7647 emsg(_(e_endfor));
7648 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007649 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650 goto erret;
7651 }
7652
Bram Moolenaarefd88552020-06-18 20:50:10 +02007653 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654 {
7655 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7656 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007657 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 goto erret;
7659 }
7660
7661 // Return zero if there is no return at the end.
7662 generate_PUSHNR(&cctx, 0);
7663 generate_instr(&cctx, ISN_RETURN);
7664 }
7665
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007666 {
7667 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7668 + ufunc->uf_dfunc_idx;
7669 dfunc->df_deleted = FALSE;
7670 dfunc->df_instr = instr->ga_data;
7671 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007672 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007673 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007674 if (cctx.ctx_outer_used)
7675 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007676 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678
7679 ret = OK;
7680
7681erret:
7682 if (ret == FAIL)
7683 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007684 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007685 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7686 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007687
7688 for (idx = 0; idx < instr->ga_len; ++idx)
7689 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007691
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007692 // If using the last entry in the table and it was added above, we
7693 // might as well remove it.
7694 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007695 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007696 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007697 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007698 ufunc->uf_dfunc_idx = 0;
7699 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007700 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007701
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007702 while (cctx.ctx_scope != NULL)
7703 drop_scope(&cctx);
7704
Bram Moolenaar20431c92020-03-20 18:39:46 +01007705 // Don't execute this function body.
7706 ga_clear_strings(&ufunc->uf_lines);
7707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 if (errormsg != NULL)
7709 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007710 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007711 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712 }
7713
7714 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007715 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007716 if (do_estack_push)
7717 estack_pop();
7718
Bram Moolenaar20431c92020-03-20 18:39:46 +01007719 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007720 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007721 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007722 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007723}
7724
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007725 void
7726set_function_type(ufunc_T *ufunc)
7727{
7728 int varargs = ufunc->uf_va_name != NULL;
7729 int argcount = ufunc->uf_args.ga_len;
7730
7731 // Create a type for the function, with the return type and any
7732 // argument types.
7733 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7734 // The type is included in "tt_args".
7735 if (argcount > 0 || varargs)
7736 {
7737 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7738 argcount, &ufunc->uf_type_list);
7739 // Add argument types to the function type.
7740 if (func_type_add_arg_types(ufunc->uf_func_type,
7741 argcount + varargs,
7742 &ufunc->uf_type_list) == FAIL)
7743 return;
7744 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7745 ufunc->uf_func_type->tt_min_argcount =
7746 argcount - ufunc->uf_def_args.ga_len;
7747 if (ufunc->uf_arg_types == NULL)
7748 {
7749 int i;
7750
7751 // lambda does not have argument types.
7752 for (i = 0; i < argcount; ++i)
7753 ufunc->uf_func_type->tt_args[i] = &t_any;
7754 }
7755 else
7756 mch_memmove(ufunc->uf_func_type->tt_args,
7757 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7758 if (varargs)
7759 {
7760 ufunc->uf_func_type->tt_args[argcount] =
7761 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7762 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7763 }
7764 }
7765 else
7766 // No arguments, can use a predefined type.
7767 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7768 argcount, &ufunc->uf_type_list);
7769}
7770
7771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772/*
7773 * Delete an instruction, free what it contains.
7774 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007775 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776delete_instr(isn_T *isn)
7777{
7778 switch (isn->isn_type)
7779 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007780 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 case ISN_EXEC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007782 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 case ISN_LOADENV:
7784 case ISN_LOADG:
7785 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007786 case ISN_LOADT:
7787 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007788 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007789 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 case ISN_PUSHS:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007791 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007792 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007794 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01007795 case ISN_STOREW:
7796 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797 vim_free(isn->isn_arg.string);
7798 break;
7799
7800 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007801 case ISN_STORES:
7802 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 break;
7804
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007805 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007806 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007807 vim_free(isn->isn_arg.unlet.ul_name);
7808 break;
7809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 case ISN_STOREOPT:
7811 vim_free(isn->isn_arg.storeopt.so_name);
7812 break;
7813
7814 case ISN_PUSHBLOB: // push blob isn_arg.blob
7815 blob_unref(isn->isn_arg.blob);
7816 break;
7817
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007818 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007819#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007820 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007821#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007822 break;
7823
7824 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007825#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007826 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007827#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007828 break;
7829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 case ISN_UCALL:
7831 vim_free(isn->isn_arg.ufunc.cuf_name);
7832 break;
7833
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007834 case ISN_FUNCREF:
7835 {
7836 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7837 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007838
7839 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7840 func_ptr_unref(dfunc->df_ufunc);
7841 }
7842 break;
7843
7844 case ISN_DCALL:
7845 {
7846 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7847 + isn->isn_arg.dfunc.cdf_idx;
7848
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007849 if (dfunc->df_ufunc != NULL
7850 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007851 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007852 }
7853 break;
7854
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007855 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007856 {
7857 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7858 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7859
7860 if (ufunc != NULL)
7861 {
7862 // Clear uf_dfunc_idx so that the function is deleted.
7863 clear_def_function(ufunc);
7864 ufunc->uf_dfunc_idx = 0;
7865 func_ptr_unref(ufunc);
7866 }
7867
7868 vim_free(lambda);
7869 vim_free(isn->isn_arg.newfunc.nf_global);
7870 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007871 break;
7872
Bram Moolenaar5e654232020-09-16 15:22:00 +02007873 case ISN_CHECKTYPE:
7874 free_type(isn->isn_arg.type.ct_type);
7875 break;
7876
Bram Moolenaar02194d22020-10-24 23:08:38 +02007877 case ISN_CMDMOD:
7878 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7879 ->cmod_filter_regmatch.regprog);
7880 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7881 break;
7882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883 case ISN_2BOOL:
7884 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007885 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886 case ISN_ADDBLOB:
7887 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007888 case ISN_ANYINDEX:
7889 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007891 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007892 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007893 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007895 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 case ISN_COMPAREANY:
7897 case ISN_COMPAREBLOB:
7898 case ISN_COMPAREBOOL:
7899 case ISN_COMPAREDICT:
7900 case ISN_COMPAREFLOAT:
7901 case ISN_COMPAREFUNC:
7902 case ISN_COMPARELIST:
7903 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904 case ISN_COMPARESPECIAL:
7905 case ISN_COMPARESTRING:
7906 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007907 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908 case ISN_DROP:
7909 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007910 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007911 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007913 case ISN_EXECCONCAT:
7914 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007915 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007916 case ISN_GETITEM:
7917 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007918 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007919 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007920 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007921 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007922 case ISN_LOADBDICT:
7923 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007924 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007925 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007926 case ISN_LOADSCRIPT:
7927 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007929 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007930 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007931 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 case ISN_NEGATENR:
7933 case ISN_NEWDICT:
7934 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007936 case ISN_OPFLOAT:
7937 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007939 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007940 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007941 case ISN_PUSHF:
7942 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007943 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007944 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007946 case ISN_SHUFFLE:
7947 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007949 case ISN_STOREDICT:
7950 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007951 case ISN_STORENR:
7952 case ISN_STOREOUTER:
7953 case ISN_STOREREG:
7954 case ISN_STORESCRIPT:
7955 case ISN_STOREV:
7956 case ISN_STRINDEX:
7957 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007958 case ISN_THROW:
7959 case ISN_TRY:
7960 // nothing allocated
7961 break;
7962 }
7963}
7964
7965/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007966 * Free all instructions for "dfunc".
7967 */
7968 static void
7969delete_def_function_contents(dfunc_T *dfunc)
7970{
7971 int idx;
7972
7973 ga_clear(&dfunc->df_def_args_isn);
7974
7975 if (dfunc->df_instr != NULL)
7976 {
7977 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7978 delete_instr(dfunc->df_instr + idx);
7979 VIM_CLEAR(dfunc->df_instr);
7980 }
7981
7982 dfunc->df_deleted = TRUE;
7983}
7984
7985/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007986 * When a user function is deleted, clear the contents of any associated def
7987 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007988 */
7989 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007990clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007992 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 {
7994 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7995 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007996
Bram Moolenaar20431c92020-03-20 18:39:46 +01007997 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007998 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007999 }
8000}
8001
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008002/*
8003 * Used when a user function is about to be deleted: remove the pointer to it.
8004 * The entry in def_functions is then unused.
8005 */
8006 void
8007unlink_def_function(ufunc_T *ufunc)
8008{
8009 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
8010
8011 dfunc->df_ufunc = NULL;
8012}
8013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008015/*
8016 * Free all functions defined with ":def".
8017 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018 void
8019free_def_functions(void)
8020{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008021 int idx;
8022
8023 for (idx = 0; idx < def_functions.ga_len; ++idx)
8024 {
8025 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8026
8027 delete_def_function_contents(dfunc);
8028 }
8029
8030 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008031}
8032#endif
8033
8034
8035#endif // FEAT_EVAL