blob: c5d92aa1b847c86a1892eaf5085ec60c81f7f064 [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 Moolenaar8a7d6542020-01-26 15:56:19 +01001436 * Generate an ISN_JUMP instruction.
1437 */
1438 static int
1439generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1440{
1441 isn_T *isn;
1442 garray_T *stack = &cctx->ctx_type_stack;
1443
Bram Moolenaar080457c2020-03-03 21:53:32 +01001444 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1446 return FAIL;
1447 isn->isn_arg.jump.jump_when = when;
1448 isn->isn_arg.jump.jump_where = where;
1449
1450 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1451 --stack->ga_len;
1452
1453 return OK;
1454}
1455
1456 static int
1457generate_FOR(cctx_T *cctx, int loop_idx)
1458{
1459 isn_T *isn;
1460 garray_T *stack = &cctx->ctx_type_stack;
1461
Bram Moolenaar080457c2020-03-03 21:53:32 +01001462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1464 return FAIL;
1465 isn->isn_arg.forloop.for_idx = loop_idx;
1466
1467 if (ga_grow(stack, 1) == FAIL)
1468 return FAIL;
1469 // type doesn't matter, will be stored next
1470 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1471 ++stack->ga_len;
1472
1473 return OK;
1474}
1475
1476/*
1477 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001478 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 * Return FAIL if the number of arguments is wrong.
1480 */
1481 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001482generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001486 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001487 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488
Bram Moolenaar080457c2020-03-03 21:53:32 +01001489 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001490 argoff = check_internal_func(func_idx, argcount);
1491 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 return FAIL;
1493
Bram Moolenaar389df252020-07-09 21:20:47 +02001494 if (method_call && argoff > 1)
1495 {
1496 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1497 return FAIL;
1498 isn->isn_arg.shuffle.shfl_item = argcount;
1499 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1500 }
1501
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001502 if (argcount > 0)
1503 {
1504 // Check the types of the arguments.
1505 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1506 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001507 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001508 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.bfunc.cbf_idx = func_idx;
1513 isn->isn_arg.bfunc.cbf_argcount = argcount;
1514
Bram Moolenaar94738d82020-10-21 14:25:07 +02001515 // Drop the argument types and push the return type.
1516 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 if (ga_grow(stack, 1) == FAIL)
1518 return FAIL;
1519 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001520 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001521 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 return OK;
1524}
1525
1526/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001527 * Generate an ISN_LISTAPPEND instruction. Works like add().
1528 * Argument count is already checked.
1529 */
1530 static int
1531generate_LISTAPPEND(cctx_T *cctx)
1532{
1533 garray_T *stack = &cctx->ctx_type_stack;
1534 type_T *list_type;
1535 type_T *item_type;
1536 type_T *expected;
1537
1538 // Caller already checked that list_type is a list.
1539 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1540 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1541 expected = list_type->tt_member;
1542 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1543 return FAIL;
1544
1545 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1546 return FAIL;
1547
1548 --stack->ga_len; // drop the argument
1549 return OK;
1550}
1551
1552/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001553 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1554 * Argument count is already checked.
1555 */
1556 static int
1557generate_BLOBAPPEND(cctx_T *cctx)
1558{
1559 garray_T *stack = &cctx->ctx_type_stack;
1560 type_T *item_type;
1561
1562 // Caller already checked that blob_type is a blob.
1563 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1564 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1565 return FAIL;
1566
1567 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1568 return FAIL;
1569
1570 --stack->ga_len; // drop the argument
1571 return OK;
1572}
1573
1574/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 * Generate an ISN_DCALL or ISN_UCALL instruction.
1576 * Return FAIL if the number of arguments is wrong.
1577 */
1578 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001579generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580{
1581 isn_T *isn;
1582 garray_T *stack = &cctx->ctx_type_stack;
1583 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001584 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585
Bram Moolenaar080457c2020-03-03 21:53:32 +01001586 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 if (argcount > regular_args && !has_varargs(ufunc))
1588 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001589 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 return FAIL;
1591 }
1592 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1593 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001594 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 return FAIL;
1596 }
1597
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001598 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001599 {
1600 int i;
1601
1602 for (i = 0; i < argcount; ++i)
1603 {
1604 type_T *expected;
1605 type_T *actual;
1606
1607 if (i < regular_args)
1608 {
1609 if (ufunc->uf_arg_types == NULL)
1610 continue;
1611 expected = ufunc->uf_arg_types[i];
1612 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001613 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1614 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001615 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001616 else
1617 expected = ufunc->uf_va_type->tt_member;
1618 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001619 if (need_type(actual, expected, -argcount + i, cctx,
1620 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001621 {
1622 arg_type_mismatch(expected, actual, i + 1);
1623 return FAIL;
1624 }
1625 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001626 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001627 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1628 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001629 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001630 }
1631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001633 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001634 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001636 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 {
1638 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1639 isn->isn_arg.dfunc.cdf_argcount = argcount;
1640 }
1641 else
1642 {
1643 // A user function may be deleted and redefined later, can't use the
1644 // ufunc pointer, need to look it up again at runtime.
1645 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1646 isn->isn_arg.ufunc.cuf_argcount = argcount;
1647 }
1648
1649 stack->ga_len -= argcount; // drop the arguments
1650 if (ga_grow(stack, 1) == FAIL)
1651 return FAIL;
1652 // add return value
1653 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1654 ++stack->ga_len;
1655
1656 return OK;
1657}
1658
1659/*
1660 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1661 */
1662 static int
1663generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1664{
1665 isn_T *isn;
1666 garray_T *stack = &cctx->ctx_type_stack;
1667
Bram Moolenaar080457c2020-03-03 21:53:32 +01001668 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1670 return FAIL;
1671 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1672 isn->isn_arg.ufunc.cuf_argcount = argcount;
1673
1674 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001675 if (ga_grow(stack, 1) == FAIL)
1676 return FAIL;
1677 // add return value
1678 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1679 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680
1681 return OK;
1682}
1683
1684/*
1685 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001686 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 */
1688 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001689generate_PCALL(
1690 cctx_T *cctx,
1691 int argcount,
1692 char_u *name,
1693 type_T *type,
1694 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695{
1696 isn_T *isn;
1697 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001698 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
Bram Moolenaar080457c2020-03-03 21:53:32 +01001700 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001701
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001702 if (type->tt_type == VAR_ANY)
1703 ret_type = &t_any;
1704 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001705 {
1706 if (type->tt_argcount != -1)
1707 {
1708 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1709
1710 if (argcount < type->tt_min_argcount - varargs)
1711 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001712 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001713 return FAIL;
1714 }
1715 if (!varargs && argcount > type->tt_argcount)
1716 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001717 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001718 return FAIL;
1719 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001720 if (type->tt_args != NULL)
1721 {
1722 int i;
1723
1724 for (i = 0; i < argcount; ++i)
1725 {
1726 int offset = -argcount + i - 1;
1727 type_T *actual = ((type_T **)stack->ga_data)[
1728 stack->ga_len + offset];
1729 type_T *expected;
1730
1731 if (varargs && i >= type->tt_min_argcount - 1)
1732 expected = type->tt_args[
1733 type->tt_min_argcount - 1]->tt_member;
1734 else
1735 expected = type->tt_args[i];
1736 if (need_type(actual, expected, offset,
1737 cctx, TRUE, FALSE) == FAIL)
1738 {
1739 arg_type_mismatch(expected, actual, i + 1);
1740 return FAIL;
1741 }
1742 }
1743 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001744 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001745 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001746 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001747 else
1748 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001749 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001750 return FAIL;
1751 }
1752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.pfunc.cpf_top = at_top;
1756 isn->isn_arg.pfunc.cpf_argcount = argcount;
1757
1758 stack->ga_len -= argcount; // drop the arguments
1759
1760 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001761 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001763 // If partial is above the arguments it must be cleared and replaced with
1764 // the return value.
1765 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1766 return FAIL;
1767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 return OK;
1769}
1770
1771/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001772 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 */
1774 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001775generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776{
1777 isn_T *isn;
1778 garray_T *stack = &cctx->ctx_type_stack;
1779 type_T *type;
1780
Bram Moolenaar080457c2020-03-03 21:53:32 +01001781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001782 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001784 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001786 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001788 if (type->tt_type != VAR_DICT && type != &t_any)
1789 {
1790 emsg(_(e_dictreq));
1791 return FAIL;
1792 }
1793 // change dict type to dict member type
1794 if (type->tt_type == VAR_DICT)
1795 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796
1797 return OK;
1798}
1799
1800/*
1801 * Generate an ISN_ECHO instruction.
1802 */
1803 static int
1804generate_ECHO(cctx_T *cctx, int with_white, int count)
1805{
1806 isn_T *isn;
1807
Bram Moolenaar080457c2020-03-03 21:53:32 +01001808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1810 return FAIL;
1811 isn->isn_arg.echo.echo_with_white = with_white;
1812 isn->isn_arg.echo.echo_count = count;
1813
1814 return OK;
1815}
1816
Bram Moolenaarad39c092020-02-26 18:23:43 +01001817/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001818 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001819 */
1820 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001821generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001822{
1823 isn_T *isn;
1824
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001825 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001826 return FAIL;
1827 isn->isn_arg.number = count;
1828
1829 return OK;
1830}
1831
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001832/*
1833 * Generate an ISN_PUT instruction.
1834 */
1835 static int
1836generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1837{
1838 isn_T *isn;
1839
1840 RETURN_OK_IF_SKIP(cctx);
1841 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1842 return FAIL;
1843 isn->isn_arg.put.put_regname = regname;
1844 isn->isn_arg.put.put_lnum = lnum;
1845 return OK;
1846}
1847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 static int
1849generate_EXEC(cctx_T *cctx, char_u *line)
1850{
1851 isn_T *isn;
1852
Bram Moolenaar080457c2020-03-03 21:53:32 +01001853 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1855 return FAIL;
1856 isn->isn_arg.string = vim_strsave(line);
1857 return OK;
1858}
1859
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001860 static int
1861generate_EXECCONCAT(cctx_T *cctx, int count)
1862{
1863 isn_T *isn;
1864
1865 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1866 return FAIL;
1867 isn->isn_arg.number = count;
1868 return OK;
1869}
1870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001872 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001873 */
1874 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001875generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001876{
1877 isn_T *isn;
1878
Bram Moolenaar02194d22020-10-24 23:08:38 +02001879 if (cmod->cmod_flags != 0
1880 || cmod->cmod_split != 0
1881 || cmod->cmod_verbose != 0
1882 || cmod->cmod_tab != 0
1883 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001884 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001885 cctx->ctx_has_cmdmod = TRUE;
1886
1887 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001888 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001889 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1890 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1891 return FAIL;
1892 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1893 // filter progam now belongs to the instruction
1894 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001895 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001896
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001897 return OK;
1898}
1899
1900 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001901generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001902{
1903 isn_T *isn;
1904
Bram Moolenaar02194d22020-10-24 23:08:38 +02001905 if (cctx->ctx_has_cmdmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001906 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001907 if ((isn = generate_instr(cctx, ISN_CMDMOD_REV)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001908 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001909 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001910
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001911 return OK;
1912}
1913
1914/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001916 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001918 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001919reserve_local(
1920 cctx_T *cctx,
1921 char_u *name,
1922 size_t len,
1923 int isConst,
1924 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 lvar_T *lvar;
1927
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001928 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001930 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001931 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 }
1933
1934 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001935 return NULL;
1936 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001937 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001939 // Every local variable uses the next entry on the stack. We could re-use
1940 // the last ones when leaving a scope, but then variables used in a closure
1941 // might get overwritten. To keep things simple do not re-use stack
1942 // entries. This is less efficient, but memory is cheap these days.
1943 lvar->lv_idx = cctx->ctx_locals_count++;
1944
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001945 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 lvar->lv_const = isConst;
1947 lvar->lv_type = type;
1948
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001949 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950}
1951
1952/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001953 * Remove local variables above "new_top".
1954 */
1955 static void
1956unwind_locals(cctx_T *cctx, int new_top)
1957{
1958 if (cctx->ctx_locals.ga_len > new_top)
1959 {
1960 int idx;
1961 lvar_T *lvar;
1962
1963 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1964 {
1965 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1966 vim_free(lvar->lv_name);
1967 }
1968 }
1969 cctx->ctx_locals.ga_len = new_top;
1970}
1971
1972/*
1973 * Free all local variables.
1974 */
1975 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001976free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001977{
1978 unwind_locals(cctx, 0);
1979 ga_clear(&cctx->ctx_locals);
1980}
1981
1982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 * Find "name" in script-local items of script "sid".
1984 * Returns the index in "sn_var_vals" if found.
1985 * If found but not in "sn_var_vals" returns -1.
1986 * If not found returns -2.
1987 */
1988 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001989get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990{
1991 hashtab_T *ht;
1992 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001993 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001994 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 int idx;
1996
Bram Moolenaare3d46852020-08-29 13:39:17 +02001997 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001999 if (sid == current_sctx.sc_sid)
2000 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002001 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002002
2003 if (sav == NULL)
2004 return -2;
2005 idx = sav->sav_var_vals_idx;
2006 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2007 if (check_writable && sv->sv_const)
2008 semsg(_(e_readonlyvar), name);
2009 return idx;
2010 }
2011
2012 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 ht = &SCRIPT_VARS(sid);
2014 di = find_var_in_ht(ht, 0, name, TRUE);
2015 if (di == NULL)
2016 return -2;
2017
2018 // Now find the svar_T index in sn_var_vals.
2019 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2020 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002021 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 if (sv->sv_tv == &di->di_tv)
2023 {
2024 if (check_writable && sv->sv_const)
2025 semsg(_(e_readonlyvar), name);
2026 return idx;
2027 }
2028 }
2029 return -1;
2030}
2031
2032/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002033 * Find "name" in imported items of the current script or in "cctx" if not
2034 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 */
2036 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002037find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 int idx;
2040
Bram Moolenaare3d46852020-08-29 13:39:17 +02002041 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002042 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 if (cctx != NULL)
2044 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2045 {
2046 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2047 + idx;
2048
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002049 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2050 : STRLEN(import->imp_name) == len
2051 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 return import;
2053 }
2054
Bram Moolenaarefa94442020-08-08 22:16:00 +02002055 return find_imported_in_script(name, len, current_sctx.sc_sid);
2056}
2057
2058 imported_T *
2059find_imported_in_script(char_u *name, size_t len, int sid)
2060{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002061 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002062 int idx;
2063
Bram Moolenaare3d46852020-08-29 13:39:17 +02002064 if (!SCRIPT_ID_VALID(sid))
2065 return NULL;
2066 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2068 {
2069 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2070
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002071 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2072 : STRLEN(import->imp_name) == len
2073 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 return import;
2075 }
2076 return NULL;
2077}
2078
2079/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002080 * Free all imported variables.
2081 */
2082 static void
2083free_imported(cctx_T *cctx)
2084{
2085 int idx;
2086
2087 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2088 {
2089 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2090
2091 vim_free(import->imp_name);
2092 }
2093 ga_clear(&cctx->ctx_imports);
2094}
2095
2096/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002097 * Return TRUE if "p" points at a "#" but not at "#{".
2098 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002099 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002100vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002101{
2102 return p[0] == '#' && p[1] != '{';
2103}
2104
2105/*
2106 * Return a pointer to the next line that isn't empty or only contains a
2107 * comment. Skips over white space.
2108 * Returns NULL if there is none.
2109 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002110 char_u *
2111peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002112{
2113 int lnum = cctx->ctx_lnum;
2114
2115 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2116 {
2117 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002118 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002119
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002120 // ignore NULLs inserted for continuation lines
2121 if (line != NULL)
2122 {
2123 p = skipwhite(line);
2124 if (*p != NUL && !vim9_comment_start(p))
2125 return p;
2126 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002127 }
2128 return NULL;
2129}
2130
2131/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002132 * Called when checking for a following operator at "arg". When the rest of
2133 * the line is empty or only a comment, peek the next line. If there is a next
2134 * line return a pointer to it and set "nextp".
2135 * Otherwise skip over white space.
2136 */
2137 static char_u *
2138may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2139{
2140 char_u *p = skipwhite(arg);
2141
2142 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002143 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002144 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002145 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002146 if (*nextp != NULL)
2147 return *nextp;
2148 }
2149 return p;
2150}
2151
2152/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002153 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002154 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002155 * Returns NULL when at the end.
2156 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002157 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002158next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002159{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002160 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002161
2162 do
2163 {
2164 ++cctx->ctx_lnum;
2165 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002166 {
2167 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002168 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002169 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002170 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002171 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002172 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002173 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002174 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002175 return line;
2176}
2177
2178/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002179 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002180 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002181 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2182 */
2183 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002184may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002185{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002186 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002187 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002188 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002189
2190 if (next == NULL)
2191 return FAIL;
2192 *arg = skipwhite(next);
2193 }
2194 return OK;
2195}
2196
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002197/*
2198 * Idem, and give an error when failed.
2199 */
2200 static int
2201may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2202{
2203 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2204 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002205 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002206 return FAIL;
2207 }
2208 return OK;
2209}
2210
2211
Bram Moolenaara5565e42020-05-09 15:44:01 +02002212// Structure passed between the compile_expr* functions to keep track of
2213// constants that have been parsed but for which no code was produced yet. If
2214// possible expressions on these constants are applied at compile time. If
2215// that is not possible, the code to push the constants needs to be generated
2216// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002217// Using 50 should be more than enough of 5 levels of ().
2218#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002219typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002220 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002221 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002222 int pp_is_const; // all generated code was constants, used for a
2223 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002224} ppconst_T;
2225
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002226static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002227static int compile_expr0(char_u **arg, cctx_T *cctx);
2228static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2229
Bram Moolenaara5565e42020-05-09 15:44:01 +02002230/*
2231 * Generate a PUSH instruction for "tv".
2232 * "tv" will be consumed or cleared.
2233 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2234 */
2235 static int
2236generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2237{
2238 if (tv != NULL)
2239 {
2240 switch (tv->v_type)
2241 {
2242 case VAR_UNKNOWN:
2243 break;
2244 case VAR_BOOL:
2245 generate_PUSHBOOL(cctx, tv->vval.v_number);
2246 break;
2247 case VAR_SPECIAL:
2248 generate_PUSHSPEC(cctx, tv->vval.v_number);
2249 break;
2250 case VAR_NUMBER:
2251 generate_PUSHNR(cctx, tv->vval.v_number);
2252 break;
2253#ifdef FEAT_FLOAT
2254 case VAR_FLOAT:
2255 generate_PUSHF(cctx, tv->vval.v_float);
2256 break;
2257#endif
2258 case VAR_BLOB:
2259 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2260 tv->vval.v_blob = NULL;
2261 break;
2262 case VAR_STRING:
2263 generate_PUSHS(cctx, tv->vval.v_string);
2264 tv->vval.v_string = NULL;
2265 break;
2266 default:
2267 iemsg("constant type not supported");
2268 clear_tv(tv);
2269 return FAIL;
2270 }
2271 tv->v_type = VAR_UNKNOWN;
2272 }
2273 return OK;
2274}
2275
2276/*
2277 * Generate code for any ppconst entries.
2278 */
2279 static int
2280generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2281{
2282 int i;
2283 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002284 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002285
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002286 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002287 for (i = 0; i < ppconst->pp_used; ++i)
2288 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2289 ret = FAIL;
2290 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002291 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002292 return ret;
2293}
2294
2295/*
2296 * Clear ppconst constants. Used when failing.
2297 */
2298 static void
2299clear_ppconst(ppconst_T *ppconst)
2300{
2301 int i;
2302
2303 for (i = 0; i < ppconst->pp_used; ++i)
2304 clear_tv(&ppconst->pp_tv[i]);
2305 ppconst->pp_used = 0;
2306}
2307
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002308/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002309 * Generate an instruction to load script-local variable "name", without the
2310 * leading "s:".
2311 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 */
2313 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002314compile_load_scriptvar(
2315 cctx_T *cctx,
2316 char_u *name, // variable NUL terminated
2317 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002318 char_u **end, // end of variable
2319 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002321 scriptitem_T *si;
2322 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 imported_T *import;
2324
Bram Moolenaare3d46852020-08-29 13:39:17 +02002325 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2326 return FAIL;
2327 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002328 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002329 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002331 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002332 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2333 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 }
2335 if (idx >= 0)
2336 {
2337 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2338
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002339 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 current_sctx.sc_sid, idx, sv->sv_type);
2341 return OK;
2342 }
2343
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002344 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 if (import != NULL)
2346 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002347 if (import->imp_all)
2348 {
2349 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002350 char_u *exp_name;
2351 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002352 ufunc_T *ufunc;
2353 type_T *type;
2354
2355 // Used "import * as Name", need to lookup the member.
2356 if (*p != '.')
2357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002358 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002359 return FAIL;
2360 }
2361 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002362 if (VIM_ISWHITE(*p))
2363 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002364 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002365 return FAIL;
2366 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002367
Bram Moolenaar1c991142020-07-04 13:15:31 +02002368 // isolate one name
2369 exp_name = p;
2370 while (eval_isnamec(*p))
2371 ++p;
2372 cc = *p;
2373 *p = NUL;
2374
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002375 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002376 *p = cc;
2377 p = skipwhite(p);
2378
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002379 // TODO: what if it is a function?
2380 if (idx < 0)
2381 return FAIL;
2382 *end = p;
2383
2384 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2385 import->imp_sid,
2386 idx,
2387 type);
2388 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002389 else if (import->imp_funcname != NULL)
2390 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002391 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002392 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2393 import->imp_sid,
2394 import->imp_var_vals_idx,
2395 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 return OK;
2397 }
2398
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002399 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002400 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 return FAIL;
2402}
2403
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002404 static int
2405generate_funcref(cctx_T *cctx, char_u *name)
2406{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002407 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002408
2409 if (ufunc == NULL)
2410 return FAIL;
2411
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002412 // Need to compile any default values to get the argument types.
2413 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2414 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2415 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002416 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002417}
2418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419/*
2420 * Compile a variable name into a load instruction.
2421 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002422 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 * When "error" is FALSE do not give an error when not found.
2424 */
2425 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426compile_load(
2427 char_u **arg,
2428 char_u *end_arg,
2429 cctx_T *cctx,
2430 int is_expr,
2431 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432{
2433 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002434 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002435 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002437 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
2439 if (*(*arg + 1) == ':')
2440 {
2441 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002442 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002443 {
2444 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002446 switch (**arg)
2447 {
2448 case 'g': isn_type = ISN_LOADGDICT; break;
2449 case 'w': isn_type = ISN_LOADWDICT; break;
2450 case 't': isn_type = ISN_LOADTDICT; break;
2451 case 'b': isn_type = ISN_LOADBDICT; break;
2452 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002453 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002454 goto theend;
2455 }
2456 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2457 goto theend;
2458 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 else
2461 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002462 isntype_T isn_type = ISN_DROP;
2463
2464 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2465 if (name == NULL)
2466 return FAIL;
2467
2468 switch (**arg)
2469 {
2470 case 'v': res = generate_LOADV(cctx, name, error);
2471 break;
2472 case 's': res = compile_load_scriptvar(cctx, name,
2473 NULL, NULL, error);
2474 break;
2475 case 'g': isn_type = ISN_LOADG; break;
2476 case 'w': isn_type = ISN_LOADW; break;
2477 case 't': isn_type = ISN_LOADT; break;
2478 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002479 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002480 goto theend;
2481 }
2482 if (isn_type != ISN_DROP)
2483 {
2484 // Global, Buffer-local, Window-local and Tabpage-local
2485 // variables can be defined later, thus we don't check if it
2486 // exists, give error at runtime.
2487 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 }
2490 }
2491 else
2492 {
2493 size_t len = end - *arg;
2494 int idx;
2495 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002496 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497
2498 name = vim_strnsave(*arg, end - *arg);
2499 if (name == NULL)
2500 return FAIL;
2501
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002502 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002504 if (!gen_load_outer)
2505 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 }
2507 else
2508 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002509 lvar_T *lvar = lookup_local(*arg, len, cctx);
2510
2511 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002513 type = lvar->lv_type;
2514 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002515 if (lvar->lv_from_outer)
2516 gen_load_outer = TRUE;
2517 else
2518 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 }
2520 else
2521 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002522 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002523 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002524 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002525 || find_imported(name, 0, cctx) != NULL)
2526 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002527
Bram Moolenaar0f769812020-09-12 18:32:34 +02002528 // When evaluating an expression and the name starts with an
2529 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002530 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002531 if (res == FAIL && is_expr
2532 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002533 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 }
2535 }
2536 if (gen_load)
2537 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002538 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002539 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002540 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002541 cctx->ctx_outer_used = TRUE;
2542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 }
2544
2545 *arg = end;
2546
2547theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002548 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002549 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 vim_free(name);
2551 return res;
2552}
2553
2554/*
2555 * Compile the argument expressions.
2556 * "arg" points to just after the "(" and is advanced to after the ")"
2557 */
2558 static int
2559compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2560{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002561 char_u *p = *arg;
2562 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002563 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564
Bram Moolenaare6085c52020-04-12 20:19:16 +02002565 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002567 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2568 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002569 if (*p == ')')
2570 {
2571 *arg = p + 1;
2572 return OK;
2573 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002574 if (must_end)
2575 {
2576 semsg(_(e_missing_comma_before_argument_str), p);
2577 return FAIL;
2578 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002579
Bram Moolenaara5565e42020-05-09 15:44:01 +02002580 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 return FAIL;
2582 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002583
2584 if (*p != ',' && *skipwhite(p) == ',')
2585 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002586 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002587 p = skipwhite(p);
2588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002590 {
2591 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002592 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002593 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002594 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002595 else
2596 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002597 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002598 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002600failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002601 emsg(_(e_missing_close));
2602 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603}
2604
2605/*
2606 * Compile a function call: name(arg1, arg2)
2607 * "arg" points to "name", "arg + varlen" to the "(".
2608 * "argcount_init" is 1 for "value->method()"
2609 * Instructions:
2610 * EVAL arg1
2611 * EVAL arg2
2612 * BCALL / DCALL / UCALL
2613 */
2614 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002615compile_call(
2616 char_u **arg,
2617 size_t varlen,
2618 cctx_T *cctx,
2619 ppconst_T *ppconst,
2620 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621{
2622 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002623 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 int argcount = argcount_init;
2625 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002626 char_u fname_buf[FLEN_FIXED + 1];
2627 char_u *tofree = NULL;
2628 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002629 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002630 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002631 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632
Bram Moolenaara5565e42020-05-09 15:44:01 +02002633 // we can evaluate "has('name')" at compile time
2634 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2635 {
2636 char_u *s = skipwhite(*arg + varlen + 1);
2637 typval_T argvars[2];
2638
2639 argvars[0].v_type = VAR_UNKNOWN;
2640 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002641 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002642 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002643 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002644 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002645 if (*s == ')' && argvars[0].v_type == VAR_STRING
2646 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002647 {
2648 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2649
2650 *arg = s + 1;
2651 argvars[1].v_type = VAR_UNKNOWN;
2652 tv->v_type = VAR_NUMBER;
2653 tv->vval.v_number = 0;
2654 f_has(argvars, tv);
2655 clear_tv(&argvars[0]);
2656 ++ppconst->pp_used;
2657 return OK;
2658 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002659 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660 }
2661
2662 if (generate_ppconst(cctx, ppconst) == FAIL)
2663 return FAIL;
2664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 if (varlen >= sizeof(namebuf))
2666 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002667 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 return FAIL;
2669 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002670 vim_strncpy(namebuf, *arg, varlen);
2671 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
2673 *arg = skipwhite(*arg + varlen + 1);
2674 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002675 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676
Bram Moolenaara1773442020-08-12 15:21:22 +02002677 is_autoload = vim_strchr(name, '#') != NULL;
2678 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 {
2680 int idx;
2681
2682 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002683 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002685 {
2686 if (STRCMP(name, "add") == 0 && argcount == 2)
2687 {
2688 garray_T *stack = &cctx->ctx_type_stack;
2689 type_T *type = ((type_T **)stack->ga_data)[
2690 stack->ga_len - 2];
2691
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002692 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002693 if (type->tt_type == VAR_LIST)
2694 {
2695 // inline "add(list, item)" so that the type can be checked
2696 res = generate_LISTAPPEND(cctx);
2697 idx = -1;
2698 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002699 else if (type->tt_type == VAR_BLOB)
2700 {
2701 // inline "add(blob, nr)" so that the type can be checked
2702 res = generate_BLOBAPPEND(cctx);
2703 idx = -1;
2704 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002705 }
2706
2707 if (idx >= 0)
2708 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2709 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002710 else
2711 semsg(_(e_unknownfunc), namebuf);
2712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 }
2714
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002715 // An argument or local variable can be a function reference, this
2716 // overrules a function name.
2717 if (lookup_local(namebuf, varlen, cctx) == NULL
2718 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002719 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002720 // If we can find the function by name generate the right call.
2721 // Skip global functions here, a local funcref takes precedence.
2722 ufunc = find_func(name, FALSE, cctx);
2723 if (ufunc != NULL && !func_is_global(ufunc))
2724 {
2725 res = generate_CALL(cctx, ufunc, argcount);
2726 goto theend;
2727 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729
2730 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002731 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002732 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002734 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002735 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002736 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002737 garray_T *stack = &cctx->ctx_type_stack;
2738 type_T *type;
2739
2740 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2741 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002742 goto theend;
2743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744
Bram Moolenaar0f769812020-09-12 18:32:34 +02002745 // If we can find a global function by name generate the right call.
2746 if (ufunc != NULL)
2747 {
2748 res = generate_CALL(cctx, ufunc, argcount);
2749 goto theend;
2750 }
2751
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002752 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002753 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002754 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002755 res = generate_UCALL(cctx, name, argcount);
2756 else
2757 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002758
2759theend:
2760 vim_free(tofree);
2761 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762}
2763
2764// like NAMESPACE_CHAR but with 'a' and 'l'.
2765#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2766
2767/*
2768 * Find the end of a variable or function name. Unlike find_name_end() this
2769 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002770 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 * Return a pointer to just after the name. Equal to "arg" if there is no
2772 * valid name.
2773 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002774 static char_u *
2775to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776{
2777 char_u *p;
2778
2779 // Quick check for valid starting character.
2780 if (!eval_isnamec1(*arg))
2781 return arg;
2782
2783 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2784 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2785 // and can be used in slice "[n:]".
2786 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002787 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2789 break;
2790 return p;
2791}
2792
2793/*
2794 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002795 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 */
2797 char_u *
2798to_name_const_end(char_u *arg)
2799{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002800 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 typval_T rettv;
2802
2803 if (p == arg && *arg == '[')
2804 {
2805
2806 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002807 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 p = arg;
2809 }
2810 else if (p == arg && *arg == '#' && arg[1] == '{')
2811 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002812 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002814 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 p = arg;
2816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817
2818 return p;
2819}
2820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821/*
2822 * parse a list: [expr, expr]
2823 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002824 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 */
2826 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002827compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828{
2829 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002830 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002832 int is_const;
2833 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002835 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002837 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002838 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002839 semsg(_(e_list_end), *arg);
2840 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002841 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002842 if (*p == ',')
2843 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002844 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002845 return FAIL;
2846 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002847 if (*p == ']')
2848 {
2849 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002850 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002851 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002852 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002853 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002854 if (!is_const)
2855 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 ++count;
2857 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002858 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002860 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2861 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002862 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002863 return FAIL;
2864 }
2865 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002866 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 p = skipwhite(p);
2868 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002869 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871 ppconst->pp_is_const = is_all_const;
2872 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873}
2874
2875/*
2876 * parse a lambda: {arg, arg -> expr}
2877 * "*arg" points to the '{'.
2878 */
2879 static int
2880compile_lambda(char_u **arg, cctx_T *cctx)
2881{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 typval_T rettv;
2883 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002884 evalarg_T evalarg;
2885
2886 CLEAR_FIELD(evalarg);
2887 evalarg.eval_flags = EVAL_EVALUATE;
2888 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
2890 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002891 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002892 {
2893 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002895 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002898 ++ufunc->uf_refcount;
2899 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900
2901 // The function will have one line: "return {expr}".
2902 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002903 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002905 clear_evalarg(&evalarg, NULL);
2906
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002907 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002908 {
2909 // The return type will now be known.
2910 set_function_type(ufunc);
2911
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002912 // The function reference count will be 1. When the ISN_FUNCREF
2913 // instruction is deleted the reference count is decremented and the
2914 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002915 return generate_FUNCREF(cctx, ufunc);
2916 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002917
2918 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 return FAIL;
2920}
2921
2922/*
2923 * Compile a lamda call: expr->{lambda}(args)
2924 * "arg" points to the "{".
2925 */
2926 static int
2927compile_lambda_call(char_u **arg, cctx_T *cctx)
2928{
2929 ufunc_T *ufunc;
2930 typval_T rettv;
2931 int argcount = 1;
2932 int ret = FAIL;
2933
2934 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002935 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 return FAIL;
2937
2938 if (**arg != '(')
2939 {
2940 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002941 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 else
2943 semsg(_(e_missing_paren), "lambda");
2944 clear_tv(&rettv);
2945 return FAIL;
2946 }
2947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 ufunc = rettv.vval.v_partial->pt_func;
2949 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002950 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002951 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002952
Bram Moolenaara05e5242020-09-19 18:19:19 +02002953 // The function will have one line: "return {expr}". Compile it into
2954 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002955 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
2957 // compile the arguments
2958 *arg = skipwhite(*arg + 1);
2959 if (compile_arguments(arg, cctx, &argcount) == OK)
2960 // call the compiled function
2961 ret = generate_CALL(cctx, ufunc, argcount);
2962
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002963 if (ret == FAIL)
2964 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 return ret;
2966}
2967
2968/*
2969 * parse a dict: {'key': val} or #{key: val}
2970 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002971 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 */
2973 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002974compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975{
2976 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002977 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 int count = 0;
2979 dict_T *d = dict_alloc();
2980 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002981 char_u *whitep = *arg;
2982 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002983 int is_const;
2984 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985
2986 if (d == NULL)
2987 return FAIL;
2988 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002989 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 {
2991 char_u *key = NULL;
2992
Bram Moolenaar23c55272020-06-21 16:58:13 +02002993 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002994 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002995 *arg = NULL;
2996 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002997 }
2998
2999 if (**arg == '}')
3000 break;
3001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 if (literal)
3003 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003004 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
Bram Moolenaar2c330432020-04-13 14:41:35 +02003006 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003008 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return FAIL;
3010 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003011 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 if (generate_PUSHS(cctx, key) == FAIL)
3013 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003014 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
3016 else
3017 {
3018 isn_T *isn;
3019
Bram Moolenaara5565e42020-05-09 15:44:01 +02003020 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3023 if (isn->isn_type == ISN_PUSHS)
3024 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003025 else
3026 {
3027 type_T *keytype = ((type_T **)stack->ga_data)
3028 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003029 if (need_type(keytype, &t_string, -1, cctx,
3030 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003031 return FAIL;
3032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034
3035 // Check for duplicate keys, if using string keys.
3036 if (key != NULL)
3037 {
3038 item = dict_find(d, key, -1);
3039 if (item != NULL)
3040 {
3041 semsg(_(e_duplicate_key), key);
3042 goto failret;
3043 }
3044 item = dictitem_alloc(key);
3045 if (item != NULL)
3046 {
3047 item->di_tv.v_type = VAR_UNKNOWN;
3048 item->di_tv.v_lock = 0;
3049 if (dict_add(d, item) == FAIL)
3050 dictitem_free(item);
3051 }
3052 }
3053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 if (**arg != ':')
3055 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003056 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003057 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003058 else
3059 semsg(_(e_missing_dict_colon), *arg);
3060 return FAIL;
3061 }
3062 whitep = *arg + 1;
3063 if (!IS_WHITE_OR_NUL(*whitep))
3064 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003065 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 return FAIL;
3067 }
3068
3069 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003070 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003071 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003072 *arg = NULL;
3073 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003074 }
3075
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003076 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003078 if (!is_const)
3079 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 ++count;
3081
Bram Moolenaar2c330432020-04-13 14:41:35 +02003082 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003083 *arg = skipwhite(*arg);
3084 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003085 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003086 *arg = NULL;
3087 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 if (**arg == '}')
3090 break;
3091 if (**arg != ',')
3092 {
3093 semsg(_(e_missing_dict_comma), *arg);
3094 goto failret;
3095 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003096 if (IS_WHITE_OR_NUL(*whitep))
3097 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003098 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003099 return FAIL;
3100 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003101 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003102 if (!IS_WHITE_OR_NUL(*whitep))
3103 {
3104 semsg(_(e_white_space_required_after_str), ",");
3105 return FAIL;
3106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 *arg = skipwhite(*arg + 1);
3108 }
3109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 *arg = *arg + 1;
3111
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003112 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003113 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003114 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003115 *arg += STRLEN(*arg);
3116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003118 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 return generate_NEWDICT(cctx, count);
3120
3121failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003123 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003124 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003125 *arg = (char_u *)"";
3126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 dict_unref(d);
3128 return FAIL;
3129}
3130
3131/*
3132 * Compile "&option".
3133 */
3134 static int
3135compile_get_option(char_u **arg, cctx_T *cctx)
3136{
3137 typval_T rettv;
3138 char_u *start = *arg;
3139 int ret;
3140
3141 // parse the option and get the current value to get the type.
3142 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003143 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 if (ret == OK)
3145 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003146 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 char_u *name = vim_strnsave(start, *arg - start);
3148 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3149
3150 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3151 vim_free(name);
3152 }
3153 clear_tv(&rettv);
3154
3155 return ret;
3156}
3157
3158/*
3159 * Compile "$VAR".
3160 */
3161 static int
3162compile_get_env(char_u **arg, cctx_T *cctx)
3163{
3164 char_u *start = *arg;
3165 int len;
3166 int ret;
3167 char_u *name;
3168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 ++*arg;
3170 len = get_env_len(arg);
3171 if (len == 0)
3172 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003173 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 return FAIL;
3175 }
3176
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003177 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 name = vim_strnsave(start, len + 1);
3179 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3180 vim_free(name);
3181 return ret;
3182}
3183
3184/*
3185 * Compile "@r".
3186 */
3187 static int
3188compile_get_register(char_u **arg, cctx_T *cctx)
3189{
3190 int ret;
3191
3192 ++*arg;
3193 if (**arg == NUL)
3194 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003195 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 return FAIL;
3197 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003198 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 {
3200 emsg_invreg(**arg);
3201 return FAIL;
3202 }
3203 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3204 ++*arg;
3205 return ret;
3206}
3207
3208/*
3209 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003210 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 */
3212 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003213apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003215 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216
3217 // this works from end to start
3218 while (p > start)
3219 {
3220 --p;
3221 if (*p == '-' || *p == '+')
3222 {
3223 // only '-' has an effect, for '+' we only check the type
3224#ifdef FEAT_FLOAT
3225 if (rettv->v_type == VAR_FLOAT)
3226 {
3227 if (*p == '-')
3228 rettv->vval.v_float = -rettv->vval.v_float;
3229 }
3230 else
3231#endif
3232 {
3233 varnumber_T val;
3234 int error = FALSE;
3235
3236 // tv_get_number_chk() accepts a string, but we don't want that
3237 // here
3238 if (check_not_string(rettv) == FAIL)
3239 return FAIL;
3240 val = tv_get_number_chk(rettv, &error);
3241 clear_tv(rettv);
3242 if (error)
3243 return FAIL;
3244 if (*p == '-')
3245 val = -val;
3246 rettv->v_type = VAR_NUMBER;
3247 rettv->vval.v_number = val;
3248 }
3249 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003250 else if (numeric_only)
3251 {
3252 ++p;
3253 break;
3254 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003255 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 {
3257 int v = tv2bool(rettv);
3258
3259 // '!' is permissive in the type.
3260 clear_tv(rettv);
3261 rettv->v_type = VAR_BOOL;
3262 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3263 }
3264 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003265 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 return OK;
3267}
3268
3269/*
3270 * Recognize v: variables that are constants and set "rettv".
3271 */
3272 static void
3273get_vim_constant(char_u **arg, typval_T *rettv)
3274{
3275 if (STRNCMP(*arg, "v:true", 6) == 0)
3276 {
3277 rettv->v_type = VAR_BOOL;
3278 rettv->vval.v_number = VVAL_TRUE;
3279 *arg += 6;
3280 }
3281 else if (STRNCMP(*arg, "v:false", 7) == 0)
3282 {
3283 rettv->v_type = VAR_BOOL;
3284 rettv->vval.v_number = VVAL_FALSE;
3285 *arg += 7;
3286 }
3287 else if (STRNCMP(*arg, "v:null", 6) == 0)
3288 {
3289 rettv->v_type = VAR_SPECIAL;
3290 rettv->vval.v_number = VVAL_NULL;
3291 *arg += 6;
3292 }
3293 else if (STRNCMP(*arg, "v:none", 6) == 0)
3294 {
3295 rettv->v_type = VAR_SPECIAL;
3296 rettv->vval.v_number = VVAL_NONE;
3297 *arg += 6;
3298 }
3299}
3300
Bram Moolenaar696ba232020-07-29 21:20:41 +02003301 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003302get_compare_type(char_u *p, int *len, int *type_is)
3303{
3304 exptype_T type = EXPR_UNKNOWN;
3305 int i;
3306
3307 switch (p[0])
3308 {
3309 case '=': if (p[1] == '=')
3310 type = EXPR_EQUAL;
3311 else if (p[1] == '~')
3312 type = EXPR_MATCH;
3313 break;
3314 case '!': if (p[1] == '=')
3315 type = EXPR_NEQUAL;
3316 else if (p[1] == '~')
3317 type = EXPR_NOMATCH;
3318 break;
3319 case '>': if (p[1] != '=')
3320 {
3321 type = EXPR_GREATER;
3322 *len = 1;
3323 }
3324 else
3325 type = EXPR_GEQUAL;
3326 break;
3327 case '<': if (p[1] != '=')
3328 {
3329 type = EXPR_SMALLER;
3330 *len = 1;
3331 }
3332 else
3333 type = EXPR_SEQUAL;
3334 break;
3335 case 'i': if (p[1] == 's')
3336 {
3337 // "is" and "isnot"; but not a prefix of a name
3338 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3339 *len = 5;
3340 i = p[*len];
3341 if (!isalnum(i) && i != '_')
3342 {
3343 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3344 *type_is = TRUE;
3345 }
3346 }
3347 break;
3348 }
3349 return type;
3350}
3351
3352/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003354 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 */
3356 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003357compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003359 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
3361 // this works from end to start
3362 while (p > start)
3363 {
3364 --p;
3365 if (*p == '-' || *p == '+')
3366 {
3367 int negate = *p == '-';
3368 isn_T *isn;
3369
3370 // TODO: check type
3371 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3372 {
3373 --p;
3374 if (*p == '-')
3375 negate = !negate;
3376 }
3377 // only '-' has an effect, for '+' we only check the type
3378 if (negate)
3379 isn = generate_instr(cctx, ISN_NEGATENR);
3380 else
3381 isn = generate_instr(cctx, ISN_CHECKNR);
3382 if (isn == NULL)
3383 return FAIL;
3384 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003385 else if (numeric_only)
3386 {
3387 ++p;
3388 break;
3389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 else
3391 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003392 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003394 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003396 if (p[-1] == '!')
3397 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 }
3400 if (generate_2BOOL(cctx, invert) == FAIL)
3401 return FAIL;
3402 }
3403 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003404 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 return OK;
3406}
3407
3408/*
3409 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003410 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 */
3412 static int
3413compile_subscript(
3414 char_u **arg,
3415 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003416 char_u *start_leader,
3417 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003418 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003420 char_u *name_start = *end_leader;
3421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 for (;;)
3423 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003424 char_u *p = skipwhite(*arg);
3425
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003426 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003427 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003428 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003429
3430 // If a following line starts with "->{" or "->X" advance to that
3431 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003432 // Also if a following line starts with ".x".
3433 if (next != NULL &&
3434 ((next[0] == '-' && next[1] == '>'
3435 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003436 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003437 {
3438 next = next_line_from_context(cctx, TRUE);
3439 if (next == NULL)
3440 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003441 *arg = next;
3442 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003443 }
3444 }
3445
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003446 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3447 // not a function call.
3448 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003450 garray_T *stack = &cctx->ctx_type_stack;
3451 type_T *type;
3452 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003454 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003455 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003456 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003459 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3460
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003461 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3463 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003464 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 return FAIL;
3466 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003467 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003469 char_u *pstart = p;
3470
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003471 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003472 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003473 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 // something->method()
3476 // Apply the '!', '-' and '+' first:
3477 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003478 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003481 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003482 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003483 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 if (**arg == '{')
3485 {
3486 // lambda call: list->{lambda}
3487 if (compile_lambda_call(arg, cctx) == FAIL)
3488 return FAIL;
3489 }
3490 else
3491 {
3492 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003493 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003494 if (!eval_isnamec1(*p))
3495 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003496 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003497 return FAIL;
3498 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003499 if (ASCII_ISALPHA(*p) && p[1] == ':')
3500 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003501 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 ;
3503 if (*p != '(')
3504 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003505 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 return FAIL;
3507 }
3508 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003509 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 return FAIL;
3511 }
3512 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003513 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003516 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003517 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003518 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003519 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003520
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003521 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003522 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003523 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003524 // TODO: blob index
3525 // TODO: more arguments
3526 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003527 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003528 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003529 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003530
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003531 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003532 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003533 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003534 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003535 if (**arg == ':')
3536 // missing first index is equal to zero
3537 generate_PUSHNR(cctx, 0);
3538 else
3539 {
3540 if (compile_expr0(arg, cctx) == FAIL)
3541 return FAIL;
3542 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3543 return FAIL;
3544 *arg = skipwhite(*arg);
3545 }
3546 if (**arg == ':')
3547 {
3548 *arg = skipwhite(*arg + 1);
3549 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3550 return FAIL;
3551 if (**arg == ']')
3552 // missing second index is equal to end of string
3553 generate_PUSHNR(cctx, -1);
3554 else
3555 {
3556 if (compile_expr0(arg, cctx) == FAIL)
3557 return FAIL;
3558 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3559 return FAIL;
3560 *arg = skipwhite(*arg);
3561 }
3562 is_slice = TRUE;
3563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
3565 if (**arg != ']')
3566 {
3567 emsg(_(e_missbrac));
3568 return FAIL;
3569 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003570 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003572 // We can index a list and a dict. If we don't know the type
3573 // we can use the index value type.
3574 // TODO: If we don't know use an instruction to figure it out at
3575 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003576 typep = ((type_T **)stack->ga_data) + stack->ga_len
3577 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003578 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003579 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3580 // If the index is a string, the variable must be a Dict.
3581 if (*typep == &t_any && valtype == &t_string)
3582 vtype = VAR_DICT;
3583 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003584 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003585 if (need_type(valtype, &t_number, -1, cctx,
3586 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003587 return FAIL;
3588 if (is_slice)
3589 {
3590 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003591 if (need_type(valtype, &t_number, -2, cctx,
3592 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003593 return FAIL;
3594 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003595 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003596
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003597 if (vtype == VAR_DICT)
3598 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003599 if (is_slice)
3600 {
3601 emsg(_(e_cannot_slice_dictionary));
3602 return FAIL;
3603 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003604 if ((*typep)->tt_type == VAR_DICT)
3605 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003606 else
3607 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003608 if (need_type(*typep, &t_dict_any, -2, cctx,
3609 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003610 return FAIL;
3611 *typep = &t_any;
3612 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003613 if (may_generate_2STRING(-1, cctx) == FAIL)
3614 return FAIL;
3615 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3616 return FAIL;
3617 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003618 else if (vtype == VAR_STRING)
3619 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003620 *typep = &t_string;
3621 if ((is_slice
3622 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3623 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003624 return FAIL;
3625 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003626 else if (vtype == VAR_BLOB)
3627 {
3628 emsg("Sorry, blob index and slice not implemented yet");
3629 return FAIL;
3630 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003631 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003632 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003633 if (is_slice)
3634 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003635 if (generate_instr_drop(cctx,
3636 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3637 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003638 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003639 }
Bram Moolenaared591872020-08-15 22:14:53 +02003640 else
3641 {
3642 if ((*typep)->tt_type == VAR_LIST)
3643 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003644 if (generate_instr_drop(cctx,
3645 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3646 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003647 return FAIL;
3648 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003649 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003650 else
3651 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003652 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003653 return FAIL;
3654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003656 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003658 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003659 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003660 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003661 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003662
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003663 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003664 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003665 {
3666 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003667 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003668 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003669 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003670 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 while (eval_isnamec(*p))
3672 MB_PTR_ADV(p);
3673 if (p == *arg)
3674 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003675 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 return FAIL;
3677 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003678 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 return FAIL;
3680 *arg = p;
3681 }
3682 else
3683 break;
3684 }
3685
3686 // TODO - see handle_subscript():
3687 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3688 // Don't do this when "Func" is already a partial that was bound
3689 // explicitly (pt_auto is FALSE).
3690
3691 return OK;
3692}
3693
3694/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003695 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3696 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003698 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003699 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003700 *
3701 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 */
3703
3704/*
3705 * number number constant
3706 * 0zFFFFFFFF Blob constant
3707 * "string" string constant
3708 * 'string' literal string constant
3709 * &option-name option value
3710 * @r register contents
3711 * identifier variable value
3712 * function() function call
3713 * $VAR environment variable
3714 * (expression) nested expression
3715 * [expr, expr] List
3716 * {key: val, key: val} Dictionary
3717 * #{key: val, key: val} Dictionary with literal keys
3718 *
3719 * Also handle:
3720 * ! in front logical NOT
3721 * - in front unary minus
3722 * + in front unary plus (ignored)
3723 * trailing (arg) funcref/partial call
3724 * trailing [] subscript in String or List
3725 * trailing .name entry in Dictionary
3726 * trailing ->name() method call
3727 */
3728 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003729compile_expr7(
3730 char_u **arg,
3731 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003732 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 char_u *start_leader, *end_leader;
3735 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003736 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003737 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003739 ppconst->pp_is_const = FALSE;
3740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 /*
3742 * Skip '!', '-' and '+' characters. They are handled later.
3743 */
3744 start_leader = *arg;
3745 while (**arg == '!' || **arg == '-' || **arg == '+')
3746 *arg = skipwhite(*arg + 1);
3747 end_leader = *arg;
3748
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003749 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 switch (**arg)
3751 {
3752 /*
3753 * Number constant.
3754 */
3755 case '0': // also for blob starting with 0z
3756 case '1':
3757 case '2':
3758 case '3':
3759 case '4':
3760 case '5':
3761 case '6':
3762 case '7':
3763 case '8':
3764 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003765 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003767 // Apply "-" and "+" just before the number now, right to
3768 // left. Matters especially when "->" follows. Stops at
3769 // '!'.
3770 if (apply_leader(rettv, TRUE,
3771 start_leader, &end_leader) == FAIL)
3772 {
3773 clear_tv(rettv);
3774 return FAIL;
3775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 break;
3777
3778 /*
3779 * String constant: "string".
3780 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003781 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 return FAIL;
3783 break;
3784
3785 /*
3786 * Literal string constant: 'str''ing'.
3787 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003788 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 return FAIL;
3790 break;
3791
3792 /*
3793 * Constant Vim variable.
3794 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003795 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 ret = NOTDONE;
3797 break;
3798
3799 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003800 * "true" constant
3801 */
3802 case 't': if (STRNCMP(*arg, "true", 4) == 0
3803 && !eval_isnamec((*arg)[4]))
3804 {
3805 *arg += 4;
3806 rettv->v_type = VAR_BOOL;
3807 rettv->vval.v_number = VVAL_TRUE;
3808 }
3809 else
3810 ret = NOTDONE;
3811 break;
3812
3813 /*
3814 * "false" constant
3815 */
3816 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3817 && !eval_isnamec((*arg)[5]))
3818 {
3819 *arg += 5;
3820 rettv->v_type = VAR_BOOL;
3821 rettv->vval.v_number = VVAL_FALSE;
3822 }
3823 else
3824 ret = NOTDONE;
3825 break;
3826
3827 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 * List: [expr, expr]
3829 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003830 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 break;
3832
3833 /*
3834 * Dictionary: #{key: val, key: val}
3835 */
3836 case '#': if ((*arg)[1] == '{')
3837 {
3838 ++*arg;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003839 ret = compile_dict(arg, cctx, TRUE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 }
3841 else
3842 ret = NOTDONE;
3843 break;
3844
3845 /*
3846 * Lambda: {arg, arg -> expr}
3847 * Dictionary: {'key': val, 'key': val}
3848 */
3849 case '{': {
3850 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003851 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852
3853 // Find out what comes after the arguments.
3854 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003855 &ga_arg, TRUE, NULL, NULL,
3856 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 if (ret != FAIL && *start == '>')
3858 ret = compile_lambda(arg, cctx);
3859 else
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003860 ret = compile_dict(arg, cctx, FALSE, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 }
3862 break;
3863
3864 /*
3865 * Option value: &name
3866 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003867 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3868 return FAIL;
3869 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 break;
3871
3872 /*
3873 * Environment variable: $VAR.
3874 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003875 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3876 return FAIL;
3877 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 break;
3879
3880 /*
3881 * Register contents: @r.
3882 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003883 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3884 return FAIL;
3885 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 break;
3887 /*
3888 * nested expression: (expression).
3889 */
3890 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003891
3892 // recursive!
3893 if (ppconst->pp_used <= PPSIZE - 10)
3894 {
3895 ret = compile_expr1(arg, cctx, ppconst);
3896 }
3897 else
3898 {
3899 // Not enough space in ppconst, flush constants.
3900 if (generate_ppconst(cctx, ppconst) == FAIL)
3901 return FAIL;
3902 ret = compile_expr0(arg, cctx);
3903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 *arg = skipwhite(*arg);
3905 if (**arg == ')')
3906 ++*arg;
3907 else if (ret == OK)
3908 {
3909 emsg(_(e_missing_close));
3910 ret = FAIL;
3911 }
3912 break;
3913
3914 default: ret = NOTDONE;
3915 break;
3916 }
3917 if (ret == FAIL)
3918 return FAIL;
3919
Bram Moolenaar1c747212020-05-09 18:28:34 +02003920 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003922 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923 clear_tv(rettv);
3924 else
3925 // A constant expression can possibly be handled compile time,
3926 // return the value instead of generating code.
3927 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 }
3929 else if (ret == NOTDONE)
3930 {
3931 char_u *p;
3932 int r;
3933
3934 if (!eval_isnamec1(**arg))
3935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003936 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 return FAIL;
3938 }
3939
3940 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003941 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003943 {
3944 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003947 {
3948 if (generate_ppconst(cctx, ppconst) == FAIL)
3949 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003950 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 if (r == FAIL)
3953 return FAIL;
3954 }
3955
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003956 // Handle following "[]", ".member", etc.
3957 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003958 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003959 ppconst) == FAIL)
3960 return FAIL;
3961 if (ppconst->pp_used > 0)
3962 {
3963 // apply the '!', '-' and '+' before the constant
3964 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003965 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003966 return FAIL;
3967 return OK;
3968 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003969 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003971 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972}
3973
3974/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003975 * Give the "white on both sides" error, taking the operator from "p[len]".
3976 */
3977 void
3978error_white_both(char_u *op, int len)
3979{
3980 char_u buf[10];
3981
3982 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003983 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003984}
3985
3986/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003987 * <type>expr7: runtime type check / conversion
3988 */
3989 static int
3990compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3991{
3992 type_T *want_type = NULL;
3993
3994 // Recognize <type>
3995 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3996 {
3997 int called_emsg_before = called_emsg;
3998
3999 ++*arg;
4000 want_type = parse_type(arg, cctx->ctx_type_list);
4001 if (called_emsg != called_emsg_before)
4002 return FAIL;
4003
4004 if (**arg != '>')
4005 {
4006 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004007 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004008 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004009 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004010 return FAIL;
4011 }
4012 ++*arg;
4013 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4014 return FAIL;
4015 }
4016
4017 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4018 return FAIL;
4019
4020 if (want_type != NULL)
4021 {
4022 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004023 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004024
Bram Moolenaard1103582020-08-14 22:44:25 +02004025 generate_ppconst(cctx, ppconst);
4026 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004027 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004028 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004029 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004030 return FAIL;
4031 }
4032 }
4033
4034 return OK;
4035}
4036
4037/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 * * number multiplication
4039 * / number division
4040 * % number modulo
4041 */
4042 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004043compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044{
4045 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004046 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004047 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004049 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004050 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return FAIL;
4052
4053 /*
4054 * Repeat computing, until no "*", "/" or "%" is following.
4055 */
4056 for (;;)
4057 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004058 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 if (*op != '*' && *op != '/' && *op != '%')
4060 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004061 if (next != NULL)
4062 {
4063 *arg = next_line_from_context(cctx, TRUE);
4064 op = skipwhite(*arg);
4065 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004066
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004067 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004069 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004070 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
4072 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004073 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004076 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004077 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004079
4080 if (ppconst->pp_used == ppconst_used + 2
4081 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4082 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004083 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004084 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4085 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004088 // both are numbers: compute the result
4089 switch (*op)
4090 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004091 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004092 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004093 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004094 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004095 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004096 break;
4097 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004098 tv1->vval.v_number = res;
4099 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004100 }
4101 else
4102 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004104 generate_two_op(cctx, op);
4105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 }
4107
4108 return OK;
4109}
4110
4111/*
4112 * + number addition
4113 * - number subtraction
4114 * .. string concatenation
4115 */
4116 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118{
4119 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004120 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004122 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
4124 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 return FAIL;
4127
4128 /*
4129 * Repeat computing, until no "+", "-" or ".." is following.
4130 */
4131 for (;;)
4132 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004133 op = may_peek_next_line(cctx, *arg, &next);
4134 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 break;
4136 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004137 if (next != NULL)
4138 {
4139 *arg = next_line_from_context(cctx, TRUE);
4140 op = skipwhite(*arg);
4141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004143 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004145 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004146 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 }
4148
4149 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004150 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004151 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004153 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004154 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 return FAIL;
4156
Bram Moolenaara5565e42020-05-09 15:44:01 +02004157 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004158 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004159 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4160 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4161 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4162 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4165 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004166
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004167 // concat/subtract/add constant numbers
4168 if (*op == '+')
4169 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4170 else if (*op == '-')
4171 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4172 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004173 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004174 // concatenate constant strings
4175 char_u *s1 = tv1->vval.v_string;
4176 char_u *s2 = tv2->vval.v_string;
4177 size_t len1 = STRLEN(s1);
4178
4179 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4180 if (tv1->vval.v_string == NULL)
4181 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004182 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004183 return FAIL;
4184 }
4185 mch_memmove(tv1->vval.v_string, s1, len1);
4186 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004187 vim_free(s1);
4188 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004190 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 }
4192 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004194 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 if (*op == '.')
4196 {
4197 if (may_generate_2STRING(-2, cctx) == FAIL
4198 || may_generate_2STRING(-1, cctx) == FAIL)
4199 return FAIL;
4200 generate_instr_drop(cctx, ISN_CONCAT, 1);
4201 }
4202 else
4203 generate_two_op(cctx, op);
4204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 }
4206
4207 return OK;
4208}
4209
4210/*
4211 * expr5a == expr5b
4212 * expr5a =~ expr5b
4213 * expr5a != expr5b
4214 * expr5a !~ expr5b
4215 * expr5a > expr5b
4216 * expr5a >= expr5b
4217 * expr5a < expr5b
4218 * expr5a <= expr5b
4219 * expr5a is expr5b
4220 * expr5a isnot expr5b
4221 *
4222 * Produces instructions:
4223 * EVAL expr5a Push result of "expr5a"
4224 * EVAL expr5b Push result of "expr5b"
4225 * COMPARE one of the compare instructions
4226 */
4227 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004228compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229{
4230 exptype_T type = EXPR_UNKNOWN;
4231 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004232 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
4237 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004238 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 return FAIL;
4240
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004241 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004242 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
4244 /*
4245 * If there is a comparative operator, use it.
4246 */
4247 if (type != EXPR_UNKNOWN)
4248 {
4249 int ic = FALSE; // Default: do not ignore case
4250
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004251 if (next != NULL)
4252 {
4253 *arg = next_line_from_context(cctx, TRUE);
4254 p = skipwhite(*arg);
4255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 if (type_is && (p[len] == '?' || p[len] == '#'))
4257 {
4258 semsg(_(e_invexpr2), *arg);
4259 return FAIL;
4260 }
4261 // extra question mark appended: ignore case
4262 if (p[len] == '?')
4263 {
4264 ic = TRUE;
4265 ++len;
4266 }
4267 // extra '#' appended: match case (ignored)
4268 else if (p[len] == '#')
4269 ++len;
4270 // nothing appended: match case
4271
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004272 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004274 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004275 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 }
4277
4278 // get the second variable
4279 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004280 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004281 return FAIL;
4282
Bram Moolenaara5565e42020-05-09 15:44:01 +02004283 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 return FAIL;
4285
Bram Moolenaara5565e42020-05-09 15:44:01 +02004286 if (ppconst->pp_used == ppconst_used + 2)
4287 {
4288 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4289 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4290 int ret;
4291
4292 // Both sides are a constant, compute the result now.
4293 // First check for a valid combination of types, this is more
4294 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004295 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004296 ret = FAIL;
4297 else
4298 {
4299 ret = typval_compare(tv1, tv2, type, ic);
4300 tv1->v_type = VAR_BOOL;
4301 tv1->vval.v_number = tv1->vval.v_number
4302 ? VVAL_TRUE : VVAL_FALSE;
4303 clear_tv(tv2);
4304 --ppconst->pp_used;
4305 }
4306 return ret;
4307 }
4308
4309 generate_ppconst(cctx, ppconst);
4310 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 }
4312
4313 return OK;
4314}
4315
Bram Moolenaar7f141552020-05-09 17:35:53 +02004316static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318/*
4319 * Compile || or &&.
4320 */
4321 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322compile_and_or(
4323 char_u **arg,
4324 cctx_T *cctx,
4325 char *op,
4326 ppconst_T *ppconst,
4327 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004329 char_u *next;
4330 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 int opchar = *op;
4332
4333 if (p[0] == opchar && p[1] == opchar)
4334 {
4335 garray_T *instr = &cctx->ctx_instr;
4336 garray_T end_ga;
4337
4338 /*
4339 * Repeat until there is no following "||" or "&&"
4340 */
4341 ga_init2(&end_ga, sizeof(int), 10);
4342 while (p[0] == opchar && p[1] == opchar)
4343 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004344 if (next != NULL)
4345 {
4346 *arg = next_line_from_context(cctx, TRUE);
4347 p = skipwhite(*arg);
4348 }
4349
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004350 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4351 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004352 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004353 return FAIL;
4354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004356 // TODO: use ppconst if the value is a constant and check
4357 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004358 generate_ppconst(cctx, ppconst);
4359
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004360 // Every part must evaluate to a bool.
4361 if (bool_on_stack(cctx) == FAIL)
4362 {
4363 ga_clear(&end_ga);
4364 return FAIL;
4365 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 if (ga_grow(&end_ga, 1) == FAIL)
4368 {
4369 ga_clear(&end_ga);
4370 return FAIL;
4371 }
4372 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4373 ++end_ga.ga_len;
4374 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004375 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376
4377 // eval the next expression
4378 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004379 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004380 return FAIL;
4381
Bram Moolenaara5565e42020-05-09 15:44:01 +02004382 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4383 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 {
4385 ga_clear(&end_ga);
4386 return FAIL;
4387 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004388
4389 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004393 // Every part must evaluate to a bool.
4394 if (bool_on_stack(cctx) == FAIL)
4395 {
4396 ga_clear(&end_ga);
4397 return FAIL;
4398 }
4399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 // Fill in the end label in all jumps.
4401 while (end_ga.ga_len > 0)
4402 {
4403 isn_T *isn;
4404
4405 --end_ga.ga_len;
4406 isn = ((isn_T *)instr->ga_data)
4407 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4408 isn->isn_arg.jump.jump_where = instr->ga_len;
4409 }
4410 ga_clear(&end_ga);
4411 }
4412
4413 return OK;
4414}
4415
4416/*
4417 * expr4a && expr4a && expr4a logical AND
4418 *
4419 * Produces instructions:
4420 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004421 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004422 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004424 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 * EVAL expr4c Push result of "expr4c"
4426 * end:
4427 */
4428 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004429compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431 int ppconst_used = ppconst->pp_used;
4432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 return FAIL;
4436
4437 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004438 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439}
4440
4441/*
4442 * expr3a || expr3b || expr3c logical OR
4443 *
4444 * Produces instructions:
4445 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004446 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004447 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004449 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 * EVAL expr3c Push result of "expr3c"
4451 * end:
4452 */
4453 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 int ppconst_used = ppconst->pp_used;
4457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 return FAIL;
4461
4462 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464}
4465
4466/*
4467 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004469 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 * JUMP_IF_FALSE alt jump if false
4471 * EVAL expr1a
4472 * JUMP_ALWAYS end
4473 * alt: EVAL expr1b
4474 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004475 *
4476 * Toplevel expression: expr2 ?? expr1
4477 * Produces instructions:
4478 * EVAL expr2 Push result of "expr2"
4479 * JUMP_AND_KEEP_IF_TRUE end jump if true
4480 * EVAL expr1
4481 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 */
4483 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004484compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485{
4486 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004487 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004488 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489
Bram Moolenaar3988f642020-08-27 22:43:03 +02004490 // Ignore all kinds of errors when not producing code.
4491 if (cctx->ctx_skip == SKIP_YES)
4492 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004493 evalarg_T evalarg;
4494
4495 CLEAR_FIELD(evalarg);
4496 evalarg.eval_cctx = cctx;
4497 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004498 return OK;
4499 }
4500
Bram Moolenaar61a89812020-05-07 16:58:17 +02004501 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004502 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 return FAIL;
4504
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004505 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 if (*p == '?')
4507 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004508 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 garray_T *instr = &cctx->ctx_instr;
4510 garray_T *stack = &cctx->ctx_type_stack;
4511 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004512 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004514 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 int has_const_expr = FALSE;
4516 int const_value = FALSE;
4517 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004519 if (next != NULL)
4520 {
4521 *arg = next_line_from_context(cctx, TRUE);
4522 p = skipwhite(*arg);
4523 }
4524
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004525 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004526 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004527 semsg(_(e_white_space_required_before_and_after_str),
4528 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004529 return FAIL;
4530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 if (ppconst->pp_used == ppconst_used + 1)
4533 {
4534 // the condition is a constant, we know whether the ? or the :
4535 // expression is to be evaluated.
4536 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004537 if (op_falsy)
4538 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4539 else
4540 {
4541 int error = FALSE;
4542
4543 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4544 &error);
4545 if (error)
4546 return FAIL;
4547 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004548 cctx->ctx_skip = save_skip == SKIP_YES ||
4549 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4550
4551 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4552 // "left ?? right" and "left" is truthy: produce "left"
4553 generate_ppconst(cctx, ppconst);
4554 else
4555 {
4556 clear_tv(&ppconst->pp_tv[ppconst_used]);
4557 --ppconst->pp_used;
4558 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559 }
4560 else
4561 {
4562 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004563 if (op_falsy)
4564 end_idx = instr->ga_len;
4565 generate_JUMP(cctx, op_falsy
4566 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4567 if (op_falsy)
4568 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570
4571 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004572 *arg = skipwhite(p + 1 + op_falsy);
4573 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004574 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004576 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 if (!has_const_expr)
4579 {
4580 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004582 if (!op_falsy)
4583 {
4584 // remember the type and drop it
4585 --stack->ga_len;
4586 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004588 end_idx = instr->ga_len;
4589 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004591 // jump here from JUMP_IF_FALSE
4592 isn = ((isn_T *)instr->ga_data) + alt_idx;
4593 isn->isn_arg.jump.jump_where = instr->ga_len;
4594 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004597 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004599 // Check for the ":".
4600 p = may_peek_next_line(cctx, *arg, &next);
4601 if (*p != ':')
4602 {
4603 emsg(_(e_missing_colon));
4604 return FAIL;
4605 }
4606 if (next != NULL)
4607 {
4608 *arg = next_line_from_context(cctx, TRUE);
4609 p = skipwhite(*arg);
4610 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004611
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004612 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4613 {
4614 semsg(_(e_white_space_required_before_and_after_str), ":");
4615 return FAIL;
4616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004618 // evaluate the third expression
4619 if (has_const_expr)
4620 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004621 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004622 *arg = skipwhite(p + 1);
4623 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4624 return FAIL;
4625 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4626 return FAIL;
4627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 if (!has_const_expr)
4630 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004631 type_T **typep;
4632
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004636 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4637 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004639 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 isn = ((isn_T *)instr->ga_data) + end_idx;
4641 isn->isn_arg.jump.jump_where = instr->ga_len;
4642 }
4643
4644 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 }
4646 return OK;
4647}
4648
4649/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004651 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4652 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 */
4654 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004655compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656{
4657 ppconst_T ppconst;
4658
4659 CLEAR_FIELD(ppconst);
4660 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4661 {
4662 clear_ppconst(&ppconst);
4663 return FAIL;
4664 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004665 if (is_const != NULL)
4666 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 if (generate_ppconst(cctx, &ppconst) == FAIL)
4668 return FAIL;
4669 return OK;
4670}
4671
4672/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004673 * Toplevel expression.
4674 */
4675 static int
4676compile_expr0(char_u **arg, cctx_T *cctx)
4677{
4678 return compile_expr0_ext(arg, cctx, NULL);
4679}
4680
4681/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 * compile "return [expr]"
4683 */
4684 static char_u *
4685compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4686{
4687 char_u *p = arg;
4688 garray_T *stack = &cctx->ctx_type_stack;
4689 type_T *stack_type;
4690
4691 if (*p != NUL && *p != '|' && *p != '\n')
4692 {
4693 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004694 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 return NULL;
4696
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004697 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004698 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004699 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4700 if (set_return_type)
4701 cctx->ctx_ufunc->uf_ret_type = stack_type;
4702 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004703 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004704 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4705 && stack_type->tt_type != VAR_VOID
4706 && stack_type->tt_type != VAR_UNKNOWN)
4707 {
4708 emsg(_(e_returning_value_in_function_without_return_type));
4709 return NULL;
4710 }
4711 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004712 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004713 return NULL;
4714 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 }
4717 else
4718 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004719 // "set_return_type" cannot be TRUE, only used for a lambda which
4720 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004721 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4722 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004724 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 return NULL;
4726 }
4727
4728 // No argument, return zero.
4729 generate_PUSHNR(cctx, 0);
4730 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004731 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 return NULL;
4733
4734 // "return val | endif" is possible
4735 return skipwhite(p);
4736}
4737
4738/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004739 * Get a line from the compilation context, compatible with exarg_T getline().
4740 * Return a pointer to the line in allocated memory.
4741 * Return NULL for end-of-file or some error.
4742 */
4743 static char_u *
4744exarg_getline(
4745 int c UNUSED,
4746 void *cookie,
4747 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004748 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004749{
4750 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004751 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004752
Bram Moolenaar66250c92020-08-20 15:02:42 +02004753 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004754 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004755 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004756 return NULL;
4757 ++cctx->ctx_lnum;
4758 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4759 // Comment lines result in NULL pointers, skip them.
4760 if (p != NULL)
4761 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004762 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004763}
4764
4765/*
4766 * Compile a nested :def command.
4767 */
4768 static char_u *
4769compile_nested_function(exarg_T *eap, cctx_T *cctx)
4770{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004771 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004772 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004773 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004774 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004775 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004776 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004777
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004778 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004779 {
4780 emsg(_(e_cannot_use_bang_with_nested_def));
4781 return NULL;
4782 }
4783
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004784 // Only g:Func() can use a namespace.
4785 if (name_start[1] == ':' && !is_global)
4786 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004787 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004788 return NULL;
4789 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004790 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4791 return NULL;
4792
Bram Moolenaar04b12692020-05-04 23:24:44 +02004793 eap->arg = name_end;
4794 eap->getline = exarg_getline;
4795 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004796 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004797 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004798 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004799 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004800
Bram Moolenaar822ba242020-05-24 23:00:18 +02004801 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004802 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004803 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004804 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004805 {
4806 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004807 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004808 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004809
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004810 if (is_global)
4811 {
4812 char_u *func_name = vim_strnsave(name_start + 2,
4813 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004814
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004815 if (func_name == NULL)
4816 r = FAIL;
4817 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004818 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004819 }
4820 else
4821 {
4822 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004823 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004824 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004825 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004826
Bram Moolenaareef21022020-08-01 22:16:43 +02004827 if (lvar == NULL)
4828 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004829 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004830 return NULL;
4831 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004832
4833 // copy over the block scope IDs
4834 if (block_depth > 0)
4835 {
4836 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4837 if (ufunc->uf_block_ids != NULL)
4838 {
4839 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4840 sizeof(int) * block_depth);
4841 ufunc->uf_block_depth = block_depth;
4842 }
4843 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004844 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004845
Bram Moolenaar61a89812020-05-07 16:58:17 +02004846 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004847 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004848}
4849
4850/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 * Return the length of an assignment operator, or zero if there isn't one.
4852 */
4853 int
4854assignment_len(char_u *p, int *heredoc)
4855{
4856 if (*p == '=')
4857 {
4858 if (p[1] == '<' && p[2] == '<')
4859 {
4860 *heredoc = TRUE;
4861 return 3;
4862 }
4863 return 1;
4864 }
4865 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4866 return 2;
4867 if (STRNCMP(p, "..=", 3) == 0)
4868 return 3;
4869 return 0;
4870}
4871
4872// words that cannot be used as a variable
4873static char *reserved[] = {
4874 "true",
4875 "false",
4876 NULL
4877};
4878
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004879typedef enum {
4880 dest_local,
4881 dest_option,
4882 dest_env,
4883 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004884 dest_buffer,
4885 dest_window,
4886 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004887 dest_vimvar,
4888 dest_script,
4889 dest_reg,
4890} assign_dest_T;
4891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004893 * Generate the load instruction for "name".
4894 */
4895 static void
4896generate_loadvar(
4897 cctx_T *cctx,
4898 assign_dest_T dest,
4899 char_u *name,
4900 lvar_T *lvar,
4901 type_T *type)
4902{
4903 switch (dest)
4904 {
4905 case dest_option:
4906 // TODO: check the option exists
4907 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4908 break;
4909 case dest_global:
4910 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4911 break;
4912 case dest_buffer:
4913 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4914 break;
4915 case dest_window:
4916 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4917 break;
4918 case dest_tab:
4919 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4920 break;
4921 case dest_script:
4922 compile_load_scriptvar(cctx,
4923 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4924 break;
4925 case dest_env:
4926 // Include $ in the name here
4927 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4928 break;
4929 case dest_reg:
4930 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4931 break;
4932 case dest_vimvar:
4933 generate_LOADV(cctx, name + 2, TRUE);
4934 break;
4935 case dest_local:
4936 if (lvar->lv_from_outer)
4937 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4938 NULL, type);
4939 else
4940 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4941 break;
4942 }
4943}
4944
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004945 void
4946vim9_declare_error(char_u *name)
4947{
4948 char *scope = "";
4949
4950 switch (*name)
4951 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004952 case 'g': scope = _("global"); break;
4953 case 'b': scope = _("buffer"); break;
4954 case 'w': scope = _("window"); break;
4955 case 't': scope = _("tab"); break;
4956 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004957 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004958 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004959 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004960 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004961 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004962 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004963 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004964 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004965 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004966}
4967
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004968/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004969 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004970 * "let name"
4971 * "var name = expr"
4972 * "final name = expr"
4973 * "const name = expr"
4974 * "name = expr"
4975 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004976 * Return NULL for an error.
4977 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 */
4979 static char_u *
4980compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4981{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004984 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 char_u *ret = NULL;
4986 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004987 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004988 int scriptvar_sid = 0;
4989 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004992 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 int oplen = 0;
4995 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004996 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004997 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005000 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5001 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 // Skip over the "var" or "[var, var]" to get to any "=".
5004 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5005 if (p == NULL)
5006 return *arg == '[' ? arg : NULL;
5007
5008 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005010 // TODO: should we allow this, and figure out type inference from list
5011 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005012 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 return NULL;
5014 }
5015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 sp = p;
5017 p = skipwhite(p);
5018 op = p;
5019 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020
5021 if (var_count > 0 && oplen == 0)
5022 // can be something like "[1, 2]->func()"
5023 return arg;
5024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5026 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005027 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005029 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 if (heredoc)
5032 {
5033 list_T *l;
5034 listitem_T *li;
5035
5036 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005037 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005039 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005040 if (l == NULL)
5041 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042
Bram Moolenaar078269b2020-09-21 20:35:55 +02005043 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005045 // Push each line and the create the list.
5046 FOR_ALL_LIST_ITEMS(l, li)
5047 {
5048 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5049 li->li_tv.vval.v_string = NULL;
5050 }
5051 generate_NEWLIST(cctx, l->lv_len);
5052 type = &t_list_string;
5053 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 list_free(l);
5056 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005061 // for "[var, var] = expr" evaluate the expression here, loop over the
5062 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005063
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005064 p = skipwhite(op + oplen);
5065 if (compile_expr0(&p, cctx) == FAIL)
5066 return NULL;
5067 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005069 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005071 type_T *stacktype;
5072
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005073 stacktype = stack->ga_len == 0 ? &t_void
5074 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005077 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078 goto theend;
5079 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005080 if (need_type(stacktype, &t_list_any, -1, cctx,
5081 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005082 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005083 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005084 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5085 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 }
5087 }
5088
5089 /*
5090 * Loop over variables in "[var, var] = expr".
5091 * For "var = expr" and "let var: type" this is done only once.
5092 */
5093 if (var_count > 0)
5094 var_start = skipwhite(arg + 1); // skip over the "["
5095 else
5096 var_start = arg;
5097 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5098 {
5099 char_u *var_end = skip_var_one(var_start, FALSE);
5100 size_t varlen;
5101 int new_local = FALSE;
5102 int opt_type;
5103 int opt_flags = 0;
5104 assign_dest_T dest = dest_local;
5105 int vimvaridx = -1;
5106 lvar_T *lvar = NULL;
5107 lvar_T arg_lvar;
5108 int has_type = FALSE;
5109 int has_index = FALSE;
5110 int instr_count = -1;
5111
Bram Moolenaar65821722020-08-02 18:58:54 +02005112 if (*var_start == '@')
5113 p = var_start + 2;
5114 else
5115 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005116 // skip over the leading "&", "&l:", "&g:" and "$"
5117 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02005118 p = to_name_end(p, TRUE);
5119 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120
5121 // "a: type" is declaring variable "a" with a type, not "a:".
5122 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5123 --var_end;
5124 if (is_decl && p == var_start + 2 && p[-1] == ':')
5125 --p;
5126
5127 varlen = p - var_start;
5128 vim_free(name);
5129 name = vim_strnsave(var_start, varlen);
5130 if (name == NULL)
5131 return NULL;
5132 if (!heredoc)
5133 type = &t_any;
5134
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005135 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005137 int declare_error = FALSE;
5138
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 if (*var_start == '&')
5140 {
5141 int cc;
5142 long numval;
5143
5144 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005145 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005147 emsg(_(e_const_option));
5148 goto theend;
5149 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005150 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 p = var_start;
5152 p = find_option_end(&p, &opt_flags);
5153 if (p == NULL)
5154 {
5155 // cannot happen?
5156 emsg(_(e_letunexp));
5157 goto theend;
5158 }
5159 cc = *p;
5160 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02005161 opt_type = get_option_value(skip_option_env_lead(var_start),
5162 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005163 *p = cc;
5164 if (opt_type == -3)
5165 {
5166 semsg(_(e_unknown_option), var_start);
5167 goto theend;
5168 }
5169 if (opt_type == -2 || opt_type == 0)
5170 type = &t_string;
5171 else
5172 type = &t_number; // both number and boolean option
5173 }
5174 else if (*var_start == '$')
5175 {
5176 dest = dest_env;
5177 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005178 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179 }
5180 else if (*var_start == '@')
5181 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005182 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 {
5184 emsg_invreg(var_start[1]);
5185 goto theend;
5186 }
5187 dest = dest_reg;
5188 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005189 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005190 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005191 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 {
5193 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005194 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005196 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 {
5198 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005199 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005201 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005202 {
5203 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005204 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005205 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005206 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 {
5208 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005209 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005211 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 {
5213 typval_T *vtv;
5214 int di_flags;
5215
5216 vimvaridx = find_vim_var(name + 2, &di_flags);
5217 if (vimvaridx < 0)
5218 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005219 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 goto theend;
5221 }
5222 // We use the current value of "sandbox" here, is that OK?
5223 if (var_check_ro(di_flags, name, FALSE))
5224 goto theend;
5225 dest = dest_vimvar;
5226 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005227 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005228 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229 }
5230 else
5231 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005232 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233
5234 for (idx = 0; reserved[idx] != NULL; ++idx)
5235 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005236 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005237 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005238 goto theend;
5239 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240
5241 lvar = lookup_local(var_start, varlen, cctx);
5242 if (lvar == NULL)
5243 {
5244 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005245 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005246 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5247 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005248 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005249 if (is_decl)
5250 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005251 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005252 goto theend;
5253 }
5254 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005255 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005256 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005257 if (lvar != NULL)
5258 {
5259 if (is_decl)
5260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005261 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 goto theend;
5263 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005264 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005265 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005267 int script_namespace = varlen > 1
5268 && STRNCMP(var_start, "s:", 2) == 0;
5269 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005270 ? script_var_exists(var_start + 2, varlen - 2,
5271 FALSE, cctx)
5272 : script_var_exists(var_start, varlen,
5273 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005274 imported_T *import =
5275 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005276
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005277 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005279 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5280
5281 if (is_decl)
5282 {
5283 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005284 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005285 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005286 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005287 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005288 name);
5289 goto theend;
5290 }
5291 else if (cctx->ctx_ufunc->uf_script_ctx_version
5292 == SCRIPT_VERSION_VIM9
5293 && script_namespace
5294 && !script_var && import == NULL)
5295 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005296 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005297 goto theend;
5298 }
5299
5300 dest = dest_script;
5301
5302 // existing script-local variables should have a type
5303 scriptvar_sid = current_sctx.sc_sid;
5304 if (import != NULL)
5305 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005306 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005307 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005308 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005309 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005310 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005311 {
5312 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5313 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005314 ((svar_T *)si->sn_var_vals.ga_data)
5315 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005316 type = sv->sv_type;
5317 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005318 }
5319 }
5320 else if (name[1] == ':' && name[2] != NUL)
5321 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005322 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 goto theend;
5324 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005325 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005326 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005327 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005328 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005329 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005330 else if (check_defined(var_start, varlen, cctx) == FAIL)
5331 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005332 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005334
5335 if (declare_error)
5336 {
5337 vim9_declare_error(name);
5338 goto theend;
5339 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005340 }
5341
5342 // handle "a:name" as a name, not index "name" on "a"
5343 if (varlen > 1 || var_start[varlen] != ':')
5344 p = var_end;
5345
5346 if (dest != dest_option)
5347 {
5348 if (is_decl && *p == ':')
5349 {
5350 // parse optional type: "let var: type = expr"
5351 if (!VIM_ISWHITE(p[1]))
5352 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005353 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005354 goto theend;
5355 }
5356 p = skipwhite(p + 1);
5357 type = parse_type(&p, cctx->ctx_type_list);
5358 has_type = TRUE;
5359 }
5360 else if (lvar != NULL)
5361 type = lvar->lv_type;
5362 }
5363
5364 if (oplen == 3 && !heredoc && dest != dest_global
5365 && type->tt_type != VAR_STRING
5366 && type->tt_type != VAR_ANY)
5367 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005368 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005369 goto theend;
5370 }
5371
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005372 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373 {
5374 if (oplen > 1 && !heredoc)
5375 {
5376 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005377 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378 goto theend;
5379 }
5380
5381 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005382 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5383 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384 goto theend;
5385 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005386 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005387 if (lvar == NULL)
5388 goto theend;
5389 new_local = TRUE;
5390 }
5391
5392 member_type = type;
5393 if (var_end > var_start + varlen)
5394 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005395 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005396 if (is_decl)
5397 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005398 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005399 goto theend;
5400 }
5401
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005402 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005403 {
5404 has_index = TRUE;
5405 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005406 member_type = &t_any;
5407 else
5408 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409 }
5410 else
5411 {
5412 semsg("Not supported yet: %s", var_start);
5413 goto theend;
5414 }
5415 }
5416 else if (lvar == &arg_lvar)
5417 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005418 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419 goto theend;
5420 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005421 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5422 {
5423 semsg(_(e_cannot_assign_to_constant), name);
5424 goto theend;
5425 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005426
5427 if (!heredoc)
5428 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005429 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005431 if (oplen > 0 && var_count == 0)
5432 {
5433 // skip over the "=" and the expression
5434 p = skipwhite(op + oplen);
5435 compile_expr0(&p, cctx);
5436 }
5437 }
5438 else if (oplen > 0)
5439 {
5440 type_T *stacktype;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005441 int is_const = FALSE;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005442
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005443 // For "var = expr" evaluate the expression.
5444 if (var_count == 0)
5445 {
5446 int r;
5447
5448 // for "+=", "*=", "..=" etc. first load the current value
5449 if (*op != '=')
5450 {
5451 generate_loadvar(cctx, dest, name, lvar, type);
5452
5453 if (has_index)
5454 {
5455 // TODO: get member from list or dict
5456 emsg("Index with operation not supported yet");
5457 goto theend;
5458 }
5459 }
5460
5461 // Compile the expression. Temporarily hide the new local
5462 // variable here, it is not available to this expression.
5463 if (new_local)
5464 --cctx->ctx_locals.ga_len;
5465 instr_count = instr->ga_len;
5466 p = skipwhite(op + oplen);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005467 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005468 if (new_local)
5469 ++cctx->ctx_locals.ga_len;
5470 if (r == FAIL)
5471 goto theend;
5472 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005473 else if (semicolon && var_idx == var_count - 1)
5474 {
5475 // For "[var; var] = expr" get the rest of the list
5476 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5477 goto theend;
5478 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005479 else
5480 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005481 // For "[var, var] = expr" get the "var_idx" item from the
5482 // list.
5483 if (generate_GETITEM(cctx, var_idx) == FAIL)
5484 return FAIL;
5485 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005486
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005487 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005488 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005489 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005490 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005491 if ((stacktype->tt_type == VAR_FUNC
5492 || stacktype->tt_type == VAR_PARTIAL)
5493 && var_wrong_func_name(name, TRUE))
5494 goto theend;
5495
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005496 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005497 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005498 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005500 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005501 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005502 }
5503 else
5504 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005505 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005506 // for a variable that implies &t_any.
5507 if (stacktype == &t_list_empty)
5508 lvar->lv_type = &t_list_any;
5509 else if (stacktype == &t_dict_empty)
5510 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005511 else if (stacktype == &t_unknown)
5512 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005513 else
5514 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005516 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005517 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005518 {
5519 type_T *use_type = lvar->lv_type;
5520
Bram Moolenaar71abe482020-09-14 22:28:30 +02005521 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005522 if (has_index)
5523 {
5524 use_type = use_type->tt_member;
5525 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005526 // could be indexing "any"
5527 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005528 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005529 if (need_type(stacktype, use_type, -1, cctx,
5530 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005531 goto theend;
5532 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005533 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005534 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005535 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005536 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005537 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005538 else if (cmdidx == CMD_final)
5539 {
5540 emsg(_(e_final_requires_a_value));
5541 goto theend;
5542 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005545 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005546 goto theend;
5547 }
5548 else if (!has_type || dest == dest_option)
5549 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005550 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 goto theend;
5552 }
5553 else
5554 {
5555 // variables are always initialized
5556 if (ga_grow(instr, 1) == FAIL)
5557 goto theend;
5558 switch (member_type->tt_type)
5559 {
5560 case VAR_BOOL:
5561 generate_PUSHBOOL(cctx, VVAL_FALSE);
5562 break;
5563 case VAR_FLOAT:
5564#ifdef FEAT_FLOAT
5565 generate_PUSHF(cctx, 0.0);
5566#endif
5567 break;
5568 case VAR_STRING:
5569 generate_PUSHS(cctx, NULL);
5570 break;
5571 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005572 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005573 break;
5574 case VAR_FUNC:
5575 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5576 break;
5577 case VAR_LIST:
5578 generate_NEWLIST(cctx, 0);
5579 break;
5580 case VAR_DICT:
5581 generate_NEWDICT(cctx, 0);
5582 break;
5583 case VAR_JOB:
5584 generate_PUSHJOB(cctx, NULL);
5585 break;
5586 case VAR_CHANNEL:
5587 generate_PUSHCHANNEL(cctx, NULL);
5588 break;
5589 case VAR_NUMBER:
5590 case VAR_UNKNOWN:
5591 case VAR_ANY:
5592 case VAR_PARTIAL:
5593 case VAR_VOID:
5594 case VAR_SPECIAL: // cannot happen
5595 generate_PUSHNR(cctx, 0);
5596 break;
5597 }
5598 }
5599 if (var_count == 0)
5600 end = p;
5601 }
5602
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005603 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005604 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005605 break;
5606
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005607 if (oplen > 0 && *op != '=')
5608 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005609 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005610 type_T *stacktype;
5611
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 if (*op == '.')
5613 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005614 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005615 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005616 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005617 if (
5618#ifdef FEAT_FLOAT
5619 // If variable is float operation with number is OK.
5620 !(expected == &t_float && stacktype == &t_number) &&
5621#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005622 need_type(stacktype, expected, -1, cctx,
5623 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005624 goto theend;
5625
5626 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005627 {
5628 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5629 goto theend;
5630 }
5631 else if (*op == '+')
5632 {
5633 if (generate_add_instr(cctx,
5634 operator_type(member_type, stacktype),
5635 member_type, stacktype) == FAIL)
5636 goto theend;
5637 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005638 else if (generate_two_op(cctx, op) == FAIL)
5639 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005642 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005643 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005644 int r;
5645
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005646 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005647 if (new_local)
5648 --cctx->ctx_locals.ga_len;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005649 p = var_start + varlen;
5650 if (*p == '[')
5651 {
5652 p = skipwhite(p + 1);
5653 r = compile_expr0(&p, cctx);
5654 if (r == OK && *skipwhite(p) != ']')
5655 {
5656 // this should not happen
5657 emsg(_(e_missbrac));
5658 r = FAIL;
5659 }
5660 }
5661 else // if (*p == '.')
5662 {
5663 char_u *key_end = to_name_end(p + 1, TRUE);
5664 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5665
5666 r = generate_PUSHS(cctx, key);
5667 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005668 if (new_local)
5669 ++cctx->ctx_locals.ga_len;
5670 if (r == FAIL)
5671 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005672
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005673 if (type == &t_any)
5674 {
5675 type_T *idx_type = ((type_T **)stack->ga_data)[
5676 stack->ga_len - 1];
5677 // Index on variable of unknown type: guess the type from the
5678 // index type: number is dict, otherwise dict.
5679 // TODO: should do the assignment at runtime
5680 if (idx_type->tt_type == VAR_NUMBER)
5681 type = &t_list_any;
5682 else
5683 type = &t_dict_any;
5684 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685 if (type->tt_type == VAR_DICT
5686 && may_generate_2STRING(-1, cctx) == FAIL)
5687 goto theend;
5688 if (type->tt_type == VAR_LIST
5689 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005690 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005691 {
5692 emsg(_(e_number_exp));
5693 goto theend;
5694 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005695
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005696 // Load the dict or list. On the stack we then have:
5697 // - value
5698 // - index
5699 // - variable
5700 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005701
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 if (type->tt_type == VAR_LIST)
5703 {
5704 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5705 return FAIL;
5706 }
5707 else if (type->tt_type == VAR_DICT)
5708 {
5709 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5710 return FAIL;
5711 }
5712 else
5713 {
5714 emsg(_(e_listreq));
5715 goto theend;
5716 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005717 }
5718 else
5719 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005720 if (is_decl && cmdidx == CMD_const
5721 && (dest == dest_script || dest == dest_local))
5722 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005723 generate_LOCKCONST(cctx);
5724
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005725 switch (dest)
5726 {
5727 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005728 generate_STOREOPT(cctx, skip_option_env_lead(name),
5729 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005730 break;
5731 case dest_global:
5732 // include g: with the name, easier to execute that way
5733 generate_STORE(cctx, ISN_STOREG, 0, name);
5734 break;
5735 case dest_buffer:
5736 // include b: with the name, easier to execute that way
5737 generate_STORE(cctx, ISN_STOREB, 0, name);
5738 break;
5739 case dest_window:
5740 // include w: with the name, easier to execute that way
5741 generate_STORE(cctx, ISN_STOREW, 0, name);
5742 break;
5743 case dest_tab:
5744 // include t: with the name, easier to execute that way
5745 generate_STORE(cctx, ISN_STORET, 0, name);
5746 break;
5747 case dest_env:
5748 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5749 break;
5750 case dest_reg:
5751 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5752 break;
5753 case dest_vimvar:
5754 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5755 break;
5756 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005757 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005758 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005759 {
5760 char_u *name_s = name;
5761
5762 // Include s: in the name for store_var()
5763 if (name[1] != ':')
5764 {
5765 int len = (int)STRLEN(name) + 3;
5766
5767 name_s = alloc(len);
5768 if (name_s == NULL)
5769 name_s = name;
5770 else
5771 vim_snprintf((char *)name_s, len,
5772 "s:%s", name);
5773 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005774 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5775 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005776 if (name_s != name)
5777 vim_free(name_s);
5778 }
5779 else
5780 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005781 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005782 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005783 break;
5784 case dest_local:
5785 if (lvar != NULL)
5786 {
5787 isn_T *isn = ((isn_T *)instr->ga_data)
5788 + instr->ga_len - 1;
5789
5790 // optimization: turn "var = 123" from ISN_PUSHNR +
5791 // ISN_STORE into ISN_STORENR
5792 if (!lvar->lv_from_outer
5793 && instr->ga_len == instr_count + 1
5794 && isn->isn_type == ISN_PUSHNR)
5795 {
5796 varnumber_T val = isn->isn_arg.number;
5797
5798 isn->isn_type = ISN_STORENR;
5799 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5800 isn->isn_arg.storenr.stnr_val = val;
5801 if (stack->ga_len > 0)
5802 --stack->ga_len;
5803 }
5804 else if (lvar->lv_from_outer)
5805 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005806 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005807 else
5808 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5809 }
5810 break;
5811 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005812 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005813
5814 if (var_idx + 1 < var_count)
5815 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005817
5818 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005819 if (var_count > 0 && !semicolon)
5820 {
5821 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5822 goto theend;
5823 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005824
Bram Moolenaarb2097502020-07-19 17:17:02 +02005825 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005826
5827theend:
5828 vim_free(name);
5829 return ret;
5830}
5831
5832/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005833 * Check if "name" can be "unlet".
5834 */
5835 int
5836check_vim9_unlet(char_u *name)
5837{
5838 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5839 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005840 // "unlet s:var" is allowed in legacy script.
5841 if (*name == 's' && !script_is_vim9())
5842 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005843 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005844 return FAIL;
5845 }
5846 return OK;
5847}
5848
5849/*
5850 * Callback passed to ex_unletlock().
5851 */
5852 static int
5853compile_unlet(
5854 lval_T *lvp,
5855 char_u *name_end,
5856 exarg_T *eap,
5857 int deep UNUSED,
5858 void *coookie)
5859{
5860 cctx_T *cctx = coookie;
5861
5862 if (lvp->ll_tv == NULL)
5863 {
5864 char_u *p = lvp->ll_name;
5865 int cc = *name_end;
5866 int ret = OK;
5867
5868 // Normal name. Only supports g:, w:, t: and b: namespaces.
5869 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005870 if (*p == '$')
5871 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5872 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005873 ret = FAIL;
5874 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005875 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005876
5877 *name_end = cc;
5878 return ret;
5879 }
5880
5881 // TODO: unlet {list}[idx]
5882 // TODO: unlet {dict}[key]
5883 emsg("Sorry, :unlet not fully implemented yet");
5884 return FAIL;
5885}
5886
5887/*
5888 * compile "unlet var", "lock var" and "unlock var"
5889 * "arg" points to "var".
5890 */
5891 static char_u *
5892compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5893{
5894 char_u *p = arg;
5895
5896 if (eap->cmdidx != CMD_unlet)
5897 {
5898 emsg("Sorry, :lock and unlock not implemented yet");
5899 return NULL;
5900 }
5901
5902 if (*p == '!')
5903 {
5904 p = skipwhite(p + 1);
5905 eap->forceit = TRUE;
5906 }
5907
5908 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5909 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5910}
5911
5912/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 * Compile an :import command.
5914 */
5915 static char_u *
5916compile_import(char_u *arg, cctx_T *cctx)
5917{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005918 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919}
5920
5921/*
5922 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5923 */
5924 static int
5925compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5926{
5927 garray_T *instr = &cctx->ctx_instr;
5928 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5929
5930 if (endlabel == NULL)
5931 return FAIL;
5932 endlabel->el_next = *el;
5933 *el = endlabel;
5934 endlabel->el_end_label = instr->ga_len;
5935
5936 generate_JUMP(cctx, when, 0);
5937 return OK;
5938}
5939
5940 static void
5941compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5942{
5943 garray_T *instr = &cctx->ctx_instr;
5944
5945 while (*el != NULL)
5946 {
5947 endlabel_T *cur = (*el);
5948 isn_T *isn;
5949
5950 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5951 isn->isn_arg.jump.jump_where = instr->ga_len;
5952 *el = cur->el_next;
5953 vim_free(cur);
5954 }
5955}
5956
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005957 static void
5958compile_free_jump_to_end(endlabel_T **el)
5959{
5960 while (*el != NULL)
5961 {
5962 endlabel_T *cur = (*el);
5963
5964 *el = cur->el_next;
5965 vim_free(cur);
5966 }
5967}
5968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969/*
5970 * Create a new scope and set up the generic items.
5971 */
5972 static scope_T *
5973new_scope(cctx_T *cctx, scopetype_T type)
5974{
5975 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5976
5977 if (scope == NULL)
5978 return NULL;
5979 scope->se_outer = cctx->ctx_scope;
5980 cctx->ctx_scope = scope;
5981 scope->se_type = type;
5982 scope->se_local_count = cctx->ctx_locals.ga_len;
5983 return scope;
5984}
5985
5986/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005987 * Free the current scope and go back to the outer scope.
5988 */
5989 static void
5990drop_scope(cctx_T *cctx)
5991{
5992 scope_T *scope = cctx->ctx_scope;
5993
5994 if (scope == NULL)
5995 {
5996 iemsg("calling drop_scope() without a scope");
5997 return;
5998 }
5999 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006000 switch (scope->se_type)
6001 {
6002 case IF_SCOPE:
6003 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6004 case FOR_SCOPE:
6005 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6006 case WHILE_SCOPE:
6007 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6008 case TRY_SCOPE:
6009 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6010 case NO_SCOPE:
6011 case BLOCK_SCOPE:
6012 break;
6013 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006014 vim_free(scope);
6015}
6016
6017/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 * compile "if expr"
6019 *
6020 * "if expr" Produces instructions:
6021 * EVAL expr Push result of "expr"
6022 * JUMP_IF_FALSE end
6023 * ... body ...
6024 * end:
6025 *
6026 * "if expr | else" Produces instructions:
6027 * EVAL expr Push result of "expr"
6028 * JUMP_IF_FALSE else
6029 * ... body ...
6030 * JUMP_ALWAYS end
6031 * else:
6032 * ... body ...
6033 * end:
6034 *
6035 * "if expr1 | elseif expr2 | else" Produces instructions:
6036 * EVAL expr Push result of "expr"
6037 * JUMP_IF_FALSE elseif
6038 * ... body ...
6039 * JUMP_ALWAYS end
6040 * elseif:
6041 * EVAL expr Push result of "expr"
6042 * JUMP_IF_FALSE else
6043 * ... body ...
6044 * JUMP_ALWAYS end
6045 * else:
6046 * ... body ...
6047 * end:
6048 */
6049 static char_u *
6050compile_if(char_u *arg, cctx_T *cctx)
6051{
6052 char_u *p = arg;
6053 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006054 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006056 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006057 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058
Bram Moolenaara5565e42020-05-09 15:44:01 +02006059 CLEAR_FIELD(ppconst);
6060 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006061 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006062 clear_ppconst(&ppconst);
6063 return NULL;
6064 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006065 if (cctx->ctx_skip == SKIP_YES)
6066 clear_ppconst(&ppconst);
6067 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006068 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006069 int error = FALSE;
6070 int v;
6071
Bram Moolenaara5565e42020-05-09 15:44:01 +02006072 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006073 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006074 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006075 if (error)
6076 return NULL;
6077 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006078 }
6079 else
6080 {
6081 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006082 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006083 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006084 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006085 if (bool_on_stack(cctx) == FAIL)
6086 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088
6089 scope = new_scope(cctx, IF_SCOPE);
6090 if (scope == NULL)
6091 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006092 scope->se_skip_save = skip_save;
6093 // "is_had_return" will be reset if any block does not end in :return
6094 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006096 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006097 {
6098 // "where" is set when ":elseif", "else" or ":endif" is found
6099 scope->se_u.se_if.is_if_label = instr->ga_len;
6100 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6101 }
6102 else
6103 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104
6105 return p;
6106}
6107
6108 static char_u *
6109compile_elseif(char_u *arg, cctx_T *cctx)
6110{
6111 char_u *p = arg;
6112 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006113 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114 isn_T *isn;
6115 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006116 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006117 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
6119 if (scope == NULL || scope->se_type != IF_SCOPE)
6120 {
6121 emsg(_(e_elseif_without_if));
6122 return NULL;
6123 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006124 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006125 if (!cctx->ctx_had_return)
6126 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006128 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006129 {
6130 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006132 return NULL;
6133 // previous "if" or "elseif" jumps here
6134 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6135 isn->isn_arg.jump.jump_where = instr->ga_len;
6136 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006138 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006139 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006140 if (cctx->ctx_skip == SKIP_YES)
6141 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006142 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006143 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006144 clear_ppconst(&ppconst);
6145 return NULL;
6146 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006147 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006148 if (scope->se_skip_save == SKIP_YES)
6149 clear_ppconst(&ppconst);
6150 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006151 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006152 int error = FALSE;
6153 int v;
6154
Bram Moolenaar7f141552020-05-09 17:35:53 +02006155 // The expression results in a constant.
6156 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006157 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6158 if (error)
6159 return NULL;
6160 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006161 clear_ppconst(&ppconst);
6162 scope->se_u.se_if.is_if_label = -1;
6163 }
6164 else
6165 {
6166 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006167 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006168 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006169 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006170 if (bool_on_stack(cctx) == FAIL)
6171 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006173 // "where" is set when ":elseif", "else" or ":endif" is found
6174 scope->se_u.se_if.is_if_label = instr->ga_len;
6175 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177
6178 return p;
6179}
6180
6181 static char_u *
6182compile_else(char_u *arg, cctx_T *cctx)
6183{
6184 char_u *p = arg;
6185 garray_T *instr = &cctx->ctx_instr;
6186 isn_T *isn;
6187 scope_T *scope = cctx->ctx_scope;
6188
6189 if (scope == NULL || scope->se_type != IF_SCOPE)
6190 {
6191 emsg(_(e_else_without_if));
6192 return NULL;
6193 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006194 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006195 if (!cctx->ctx_had_return)
6196 scope->se_u.se_if.is_had_return = FALSE;
6197 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198
Bram Moolenaarefd88552020-06-18 20:50:10 +02006199 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006200 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006201 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006202 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006203 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006204 if (!cctx->ctx_had_return
6205 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6206 JUMP_ALWAYS, cctx) == FAIL)
6207 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006208 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006209
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006210 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006211 {
6212 if (scope->se_u.se_if.is_if_label >= 0)
6213 {
6214 // previous "if" or "elseif" jumps here
6215 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6216 isn->isn_arg.jump.jump_where = instr->ga_len;
6217 scope->se_u.se_if.is_if_label = -1;
6218 }
6219 }
6220
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006221 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006222 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224
6225 return p;
6226}
6227
6228 static char_u *
6229compile_endif(char_u *arg, cctx_T *cctx)
6230{
6231 scope_T *scope = cctx->ctx_scope;
6232 ifscope_T *ifscope;
6233 garray_T *instr = &cctx->ctx_instr;
6234 isn_T *isn;
6235
6236 if (scope == NULL || scope->se_type != IF_SCOPE)
6237 {
6238 emsg(_(e_endif_without_if));
6239 return NULL;
6240 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006241 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006242 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006243 if (!cctx->ctx_had_return)
6244 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006246 if (scope->se_u.se_if.is_if_label >= 0)
6247 {
6248 // previous "if" or "elseif" jumps here
6249 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6250 isn->isn_arg.jump.jump_where = instr->ga_len;
6251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 // Fill in the "end" label in jumps at the end of the blocks.
6253 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006254 cctx->ctx_skip = scope->se_skip_save;
6255
6256 // If all the blocks end in :return and there is an :else then the
6257 // had_return flag is set.
6258 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006260 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 return arg;
6262}
6263
6264/*
6265 * compile "for var in expr"
6266 *
6267 * Produces instructions:
6268 * PUSHNR -1
6269 * STORE loop-idx Set index to -1
6270 * EVAL expr Push result of "expr"
6271 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6272 * - if beyond end, jump to "end"
6273 * - otherwise get item from list and push it
6274 * STORE var Store item in "var"
6275 * ... body ...
6276 * JUMP top Jump back to repeat
6277 * end: DROP Drop the result of "expr"
6278 *
6279 */
6280 static char_u *
6281compile_for(char_u *arg, cctx_T *cctx)
6282{
6283 char_u *p;
6284 size_t varlen;
6285 garray_T *instr = &cctx->ctx_instr;
6286 garray_T *stack = &cctx->ctx_type_stack;
6287 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006288 lvar_T *loop_lvar; // loop iteration variable
6289 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 type_T *vartype;
6291
6292 // TODO: list of variables: "for [key, value] in dict"
6293 // parse "var"
6294 for (p = arg; eval_isnamec1(*p); ++p)
6295 ;
6296 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006297 var_lvar = lookup_local(arg, varlen, cctx);
6298 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006300 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 return NULL;
6302 }
6303
6304 // consume "in"
6305 p = skipwhite(p);
6306 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6307 {
6308 emsg(_(e_missing_in));
6309 return NULL;
6310 }
6311 p = skipwhite(p + 2);
6312
6313
6314 scope = new_scope(cctx, FOR_SCOPE);
6315 if (scope == NULL)
6316 return NULL;
6317
6318 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006319 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6320 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006321 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006322 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006323 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326
6327 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006328 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6329 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006330 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006331 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006332 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006336 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
6338 // compile "expr", it remains on the stack until "endfor"
6339 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006340 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006341 {
6342 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006346 // Now that we know the type of "var", check that it is a list, now or at
6347 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006349 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006351 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 return NULL;
6353 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006354 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006355 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356
6357 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006358 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006360 generate_FOR(cctx, loop_lvar->lv_idx);
6361 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006362
6363 return arg;
6364}
6365
6366/*
6367 * compile "endfor"
6368 */
6369 static char_u *
6370compile_endfor(char_u *arg, cctx_T *cctx)
6371{
6372 garray_T *instr = &cctx->ctx_instr;
6373 scope_T *scope = cctx->ctx_scope;
6374 forscope_T *forscope;
6375 isn_T *isn;
6376
6377 if (scope == NULL || scope->se_type != FOR_SCOPE)
6378 {
6379 emsg(_(e_for));
6380 return NULL;
6381 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006382 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006384 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385
6386 // At end of ":for" scope jump back to the FOR instruction.
6387 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6388
6389 // Fill in the "end" label in the FOR statement so it can jump here
6390 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6391 isn->isn_arg.forloop.for_end = instr->ga_len;
6392
6393 // Fill in the "end" label any BREAK statements
6394 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6395
6396 // Below the ":for" scope drop the "expr" list from the stack.
6397 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6398 return NULL;
6399
6400 vim_free(scope);
6401
6402 return arg;
6403}
6404
6405/*
6406 * compile "while expr"
6407 *
6408 * Produces instructions:
6409 * top: EVAL expr Push result of "expr"
6410 * JUMP_IF_FALSE end jump if false
6411 * ... body ...
6412 * JUMP top Jump back to repeat
6413 * end:
6414 *
6415 */
6416 static char_u *
6417compile_while(char_u *arg, cctx_T *cctx)
6418{
6419 char_u *p = arg;
6420 garray_T *instr = &cctx->ctx_instr;
6421 scope_T *scope;
6422
6423 scope = new_scope(cctx, WHILE_SCOPE);
6424 if (scope == NULL)
6425 return NULL;
6426
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006427 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428
6429 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006430 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 return NULL;
6432
Bram Moolenaar13106602020-10-04 16:06:05 +02006433 if (bool_on_stack(cctx) == FAIL)
6434 return FAIL;
6435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006437 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 JUMP_IF_FALSE, cctx) == FAIL)
6439 return FAIL;
6440
6441 return p;
6442}
6443
6444/*
6445 * compile "endwhile"
6446 */
6447 static char_u *
6448compile_endwhile(char_u *arg, cctx_T *cctx)
6449{
6450 scope_T *scope = cctx->ctx_scope;
6451
6452 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6453 {
6454 emsg(_(e_while));
6455 return NULL;
6456 }
6457 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006458 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459
6460 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006461 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462
6463 // Fill in the "end" label in the WHILE statement so it can jump here.
6464 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006465 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466
6467 vim_free(scope);
6468
6469 return arg;
6470}
6471
6472/*
6473 * compile "continue"
6474 */
6475 static char_u *
6476compile_continue(char_u *arg, cctx_T *cctx)
6477{
6478 scope_T *scope = cctx->ctx_scope;
6479
6480 for (;;)
6481 {
6482 if (scope == NULL)
6483 {
6484 emsg(_(e_continue));
6485 return NULL;
6486 }
6487 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6488 break;
6489 scope = scope->se_outer;
6490 }
6491
6492 // Jump back to the FOR or WHILE instruction.
6493 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6495 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 return arg;
6497}
6498
6499/*
6500 * compile "break"
6501 */
6502 static char_u *
6503compile_break(char_u *arg, cctx_T *cctx)
6504{
6505 scope_T *scope = cctx->ctx_scope;
6506 endlabel_T **el;
6507
6508 for (;;)
6509 {
6510 if (scope == NULL)
6511 {
6512 emsg(_(e_break));
6513 return NULL;
6514 }
6515 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6516 break;
6517 scope = scope->se_outer;
6518 }
6519
6520 // Jump to the end of the FOR or WHILE loop.
6521 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006522 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006524 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6526 return FAIL;
6527
6528 return arg;
6529}
6530
6531/*
6532 * compile "{" start of block
6533 */
6534 static char_u *
6535compile_block(char_u *arg, cctx_T *cctx)
6536{
6537 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6538 return NULL;
6539 return skipwhite(arg + 1);
6540}
6541
6542/*
6543 * compile end of block: drop one scope
6544 */
6545 static void
6546compile_endblock(cctx_T *cctx)
6547{
6548 scope_T *scope = cctx->ctx_scope;
6549
6550 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006551 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 vim_free(scope);
6553}
6554
6555/*
6556 * compile "try"
6557 * Creates a new scope for the try-endtry, pointing to the first catch and
6558 * finally.
6559 * Creates another scope for the "try" block itself.
6560 * TRY instruction sets up exception handling at runtime.
6561 *
6562 * "try"
6563 * TRY -> catch1, -> finally push trystack entry
6564 * ... try block
6565 * "throw {exception}"
6566 * EVAL {exception}
6567 * THROW create exception
6568 * ... try block
6569 * " catch {expr}"
6570 * JUMP -> finally
6571 * catch1: PUSH exeception
6572 * EVAL {expr}
6573 * MATCH
6574 * JUMP nomatch -> catch2
6575 * CATCH remove exception
6576 * ... catch block
6577 * " catch"
6578 * JUMP -> finally
6579 * catch2: CATCH remove exception
6580 * ... catch block
6581 * " finally"
6582 * finally:
6583 * ... finally block
6584 * " endtry"
6585 * ENDTRY pop trystack entry, may rethrow
6586 */
6587 static char_u *
6588compile_try(char_u *arg, cctx_T *cctx)
6589{
6590 garray_T *instr = &cctx->ctx_instr;
6591 scope_T *try_scope;
6592 scope_T *scope;
6593
6594 // scope that holds the jumps that go to catch/finally/endtry
6595 try_scope = new_scope(cctx, TRY_SCOPE);
6596 if (try_scope == NULL)
6597 return NULL;
6598
6599 // "catch" is set when the first ":catch" is found.
6600 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006601 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 if (generate_instr(cctx, ISN_TRY) == NULL)
6603 return NULL;
6604
6605 // scope for the try block itself
6606 scope = new_scope(cctx, BLOCK_SCOPE);
6607 if (scope == NULL)
6608 return NULL;
6609
6610 return arg;
6611}
6612
6613/*
6614 * compile "catch {expr}"
6615 */
6616 static char_u *
6617compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6618{
6619 scope_T *scope = cctx->ctx_scope;
6620 garray_T *instr = &cctx->ctx_instr;
6621 char_u *p;
6622 isn_T *isn;
6623
6624 // end block scope from :try or :catch
6625 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6626 compile_endblock(cctx);
6627 scope = cctx->ctx_scope;
6628
6629 // Error if not in a :try scope
6630 if (scope == NULL || scope->se_type != TRY_SCOPE)
6631 {
6632 emsg(_(e_catch));
6633 return NULL;
6634 }
6635
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006636 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006638 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 return NULL;
6640 }
6641
6642 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006643 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644 JUMP_ALWAYS, cctx) == FAIL)
6645 return NULL;
6646
6647 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006648 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006649 if (isn->isn_arg.try.try_catch == 0)
6650 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006651 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 {
6653 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006654 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006655 isn->isn_arg.jump.jump_where = instr->ga_len;
6656 }
6657
6658 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006659 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006661 scope->se_u.se_try.ts_caught_all = TRUE;
6662 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 }
6664 else
6665 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006666 char_u *end;
6667 char_u *pat;
6668 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006669 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006670 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 // Push v:exception, push {expr} and MATCH
6673 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6674
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006675 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006676 if (*end != *p)
6677 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006678 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006679 vim_free(tofree);
6680 return FAIL;
6681 }
6682 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006683 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006684 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006685 len = (int)(end - tofree);
6686 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006687 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006688 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006689 if (pat == NULL)
6690 return FAIL;
6691 if (generate_PUSHS(cctx, pat) == FAIL)
6692 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6695 return NULL;
6696
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006697 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6699 return NULL;
6700 }
6701
6702 if (generate_instr(cctx, ISN_CATCH) == NULL)
6703 return NULL;
6704
6705 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6706 return NULL;
6707 return p;
6708}
6709
6710 static char_u *
6711compile_finally(char_u *arg, cctx_T *cctx)
6712{
6713 scope_T *scope = cctx->ctx_scope;
6714 garray_T *instr = &cctx->ctx_instr;
6715 isn_T *isn;
6716
6717 // end block scope from :try or :catch
6718 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6719 compile_endblock(cctx);
6720 scope = cctx->ctx_scope;
6721
6722 // Error if not in a :try scope
6723 if (scope == NULL || scope->se_type != TRY_SCOPE)
6724 {
6725 emsg(_(e_finally));
6726 return NULL;
6727 }
6728
6729 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006730 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 if (isn->isn_arg.try.try_finally != 0)
6732 {
6733 emsg(_(e_finally_dup));
6734 return NULL;
6735 }
6736
6737 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006738 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739
Bram Moolenaar585fea72020-04-02 22:33:21 +02006740 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006741 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 {
6743 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006744 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006746 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 }
6748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 // TODO: set index in ts_finally_label jumps
6750
6751 return arg;
6752}
6753
6754 static char_u *
6755compile_endtry(char_u *arg, cctx_T *cctx)
6756{
6757 scope_T *scope = cctx->ctx_scope;
6758 garray_T *instr = &cctx->ctx_instr;
6759 isn_T *isn;
6760
6761 // end block scope from :catch or :finally
6762 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6763 compile_endblock(cctx);
6764 scope = cctx->ctx_scope;
6765
6766 // Error if not in a :try scope
6767 if (scope == NULL || scope->se_type != TRY_SCOPE)
6768 {
6769 if (scope == NULL)
6770 emsg(_(e_no_endtry));
6771 else if (scope->se_type == WHILE_SCOPE)
6772 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006773 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 emsg(_(e_endfor));
6775 else
6776 emsg(_(e_endif));
6777 return NULL;
6778 }
6779
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006780 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006783 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 return NULL;
6785 }
6786
6787 // Fill in the "end" label in jumps at the end of the blocks, if not done
6788 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006789 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790
6791 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006792 if (isn->isn_arg.try.try_catch == 0)
6793 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794 if (isn->isn_arg.try.try_finally == 0)
6795 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006796
6797 if (scope->se_u.se_try.ts_catch_label != 0)
6798 {
6799 // Last catch without match jumps here
6800 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6801 isn->isn_arg.jump.jump_where = instr->ga_len;
6802 }
6803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 compile_endblock(cctx);
6805
6806 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6807 return NULL;
6808 return arg;
6809}
6810
6811/*
6812 * compile "throw {expr}"
6813 */
6814 static char_u *
6815compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6816{
6817 char_u *p = skipwhite(arg);
6818
Bram Moolenaara5565e42020-05-09 15:44:01 +02006819 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 return NULL;
6821 if (may_generate_2STRING(-1, cctx) == FAIL)
6822 return NULL;
6823 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6824 return NULL;
6825
6826 return p;
6827}
6828
6829/*
6830 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006831 * compile "echomsg expr"
6832 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006833 * compile "execute expr"
6834 */
6835 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006836compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006837{
6838 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006839 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006840 int count = 0;
6841
6842 for (;;)
6843 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006844 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006845 return NULL;
6846 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006847 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006848 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006849 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006850 break;
6851 }
6852
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006853 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6854 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6855 else if (cmdidx == CMD_execute)
6856 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6857 else if (cmdidx == CMD_echomsg)
6858 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6859 else
6860 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861 return p;
6862}
6863
6864/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006865 * :put r
6866 * :put ={expr}
6867 */
6868 static char_u *
6869compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6870{
6871 char_u *line = arg;
6872 linenr_T lnum;
6873 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006874 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006875
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006876 eap->regname = *line;
6877
6878 if (eap->regname == '=')
6879 {
6880 char_u *p = line + 1;
6881
6882 if (compile_expr0(&p, cctx) == FAIL)
6883 return NULL;
6884 line = p;
6885 }
6886 else if (eap->regname != NUL)
6887 ++line;
6888
Bram Moolenaare13bdec2020-10-16 23:16:47 +02006889 // "errormsg" will not be set because the range is ADDR_LINES.
6890 // TODO: if the range contains something like "$" or "." need to evaluate
6891 // at runtime
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006892 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6893 return NULL;
6894 if (eap->addr_count == 0)
6895 lnum = -1;
6896 else
6897 lnum = eap->line2;
6898 if (above)
6899 --lnum;
6900
6901 generate_PUT(cctx, eap->regname, lnum);
6902 return line;
6903}
6904
6905/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006906 * A command that is not compiled, execute with legacy code.
6907 */
6908 static char_u *
6909compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6910{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006911 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006912 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006913 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006914
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006915 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006916 goto theend;
6917
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006918 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006919 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006920 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006921 int usefilter = FALSE;
6922
6923 has_expr = argt & (EX_XFILE | EX_EXPAND);
6924
6925 // If the command can be followed by a bar, find the bar and truncate
6926 // it, so that the following command can be compiled.
6927 // The '|' is overwritten with a NUL, it is put back below.
6928 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6929 && *eap->arg == '!')
6930 // :w !filter or :r !filter or :r! filter
6931 usefilter = TRUE;
6932 if ((argt & EX_TRLBAR) && !usefilter)
6933 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006934 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006935 separate_nextcmd(eap);
6936 if (eap->nextcmd != NULL)
6937 nextcmd = eap->nextcmd;
6938 }
6939 }
6940
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006941 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6942 {
6943 // expand filename in "syntax include [@group] filename"
6944 has_expr = TRUE;
6945 eap->arg = skipwhite(eap->arg + 7);
6946 if (*eap->arg == '@')
6947 eap->arg = skiptowhite(eap->arg);
6948 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006949
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006950 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006951 {
6952 int count = 0;
6953 char_u *start = skipwhite(line);
6954
6955 // :cmd xxx`=expr1`yyy`=expr2`zzz
6956 // PUSHS ":cmd xxx"
6957 // eval expr1
6958 // PUSHS "yyy"
6959 // eval expr2
6960 // PUSHS "zzz"
6961 // EXECCONCAT 5
6962 for (;;)
6963 {
6964 if (p > start)
6965 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006966 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006967 ++count;
6968 }
6969 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006970 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006971 return NULL;
6972 may_generate_2STRING(-1, cctx);
6973 ++count;
6974 p = skipwhite(p);
6975 if (*p != '`')
6976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006977 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006978 return NULL;
6979 }
6980 start = p + 1;
6981
6982 p = (char_u *)strstr((char *)start, "`=");
6983 if (p == NULL)
6984 {
6985 if (*skipwhite(start) != NUL)
6986 {
6987 generate_PUSHS(cctx, vim_strsave(start));
6988 ++count;
6989 }
6990 break;
6991 }
6992 }
6993 generate_EXECCONCAT(cctx, count);
6994 }
6995 else
6996 generate_EXEC(cctx, line);
6997
6998theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006999 if (*nextcmd != NUL)
7000 {
7001 // the parser expects a pointer to the bar, put it back
7002 --nextcmd;
7003 *nextcmd = '|';
7004 }
7005
7006 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007007}
7008
7009/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007010 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007011 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007012 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007013 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007014add_def_function(ufunc_T *ufunc)
7015{
7016 dfunc_T *dfunc;
7017
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007018 if (def_functions.ga_len == 0)
7019 {
7020 // The first position is not used, so that a zero uf_dfunc_idx means it
7021 // wasn't set.
7022 if (ga_grow(&def_functions, 1) == FAIL)
7023 return FAIL;
7024 ++def_functions.ga_len;
7025 }
7026
Bram Moolenaar09689a02020-05-09 22:50:08 +02007027 // Add the function to "def_functions".
7028 if (ga_grow(&def_functions, 1) == FAIL)
7029 return FAIL;
7030 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7031 CLEAR_POINTER(dfunc);
7032 dfunc->df_idx = def_functions.ga_len;
7033 ufunc->uf_dfunc_idx = dfunc->df_idx;
7034 dfunc->df_ufunc = ufunc;
7035 ++def_functions.ga_len;
7036 return OK;
7037}
7038
7039/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 * After ex_function() has collected all the function lines: parse and compile
7041 * the lines into instructions.
7042 * Adds the function to "def_functions".
7043 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7044 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007045 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007046 * This can be used recursively through compile_lambda(), which may reallocate
7047 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007048 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007050 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007051compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 char_u *line = NULL;
7054 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 cctx_T cctx;
7057 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007058 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 int ret = FAIL;
7060 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007061 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007062 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007063 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007065 // When using a function that was compiled before: Free old instructions.
7066 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007067 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007069 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7070 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007071 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007073 else
7074 {
7075 if (add_def_function(ufunc) == FAIL)
7076 return FAIL;
7077 new_def_function = TRUE;
7078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079
Bram Moolenaar985116a2020-07-12 17:31:09 +02007080 ufunc->uf_def_status = UF_COMPILING;
7081
Bram Moolenaara80faa82020-04-12 19:37:17 +02007082 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 cctx.ctx_ufunc = ufunc;
7084 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007085 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7087 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7088 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7089 cctx.ctx_type_list = &ufunc->uf_type_list;
7090 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7091 instr = &cctx.ctx_instr;
7092
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007093 // Set the context to the function, it may be compiled when called from
7094 // another script. Set the script version to the most modern one.
7095 // The line number will be set in next_line_from_context().
7096 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7098
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007099 // Make sure error messages are OK.
7100 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7101 if (do_estack_push)
7102 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007103 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007104
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007105 if (ufunc->uf_def_args.ga_len > 0)
7106 {
7107 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007108 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007109 int i;
7110 char_u *arg;
7111 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007112 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007113
7114 // Produce instructions for the default values of optional arguments.
7115 // Store the instruction index in uf_def_arg_idx[] so that we know
7116 // where to start when the function is called, depending on the number
7117 // of arguments.
7118 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7119 if (ufunc->uf_def_arg_idx == NULL)
7120 goto erret;
7121 for (i = 0; i < count; ++i)
7122 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007123 garray_T *stack = &cctx.ctx_type_stack;
7124 type_T *val_type;
7125 int arg_idx = first_def_arg + i;
7126
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007127 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7128 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007129 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007130 goto erret;
7131
7132 // If no type specified use the type of the default value.
7133 // Otherwise check that the default value type matches the
7134 // specified type.
7135 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7136 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007137 {
7138 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007139 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007140 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007141 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7142 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007143 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007144
7145 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007146 goto erret;
7147 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007148 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007149
7150 if (did_set_arg_type)
7151 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007152 }
7153
7154 /*
7155 * Loop over all the lines of the function and generate instructions.
7156 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 for (;;)
7158 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007159 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007160 int starts_with_colon = FALSE;
7161 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007162 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007163
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007164 // Bail out on the first error to avoid a flood of errors and report
7165 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007166 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007167 goto erret;
7168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 if (line != NULL && *line == '|')
7170 // the line continues after a '|'
7171 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007172 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007173 && !(*line == '#' && (line == cctx.ctx_line_start
7174 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007176 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 goto erret;
7178 }
7179 else
7180 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007181 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007182 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007183 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 }
7186
Bram Moolenaara80faa82020-04-12 19:37:17 +02007187 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 ea.cmdlinep = &line;
7189 ea.cmd = skipwhite(line);
7190
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007191 // Some things can be recognized by the first character.
7192 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007194 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007195 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007196 if (ea.cmd[1] != '{')
7197 {
7198 line = (char_u *)"";
7199 continue;
7200 }
7201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007203 case '}':
7204 {
7205 // "}" ends a block scope
7206 scopetype_T stype = cctx.ctx_scope == NULL
7207 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007209 if (stype == BLOCK_SCOPE)
7210 {
7211 compile_endblock(&cctx);
7212 line = ea.cmd;
7213 }
7214 else
7215 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007216 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007217 goto erret;
7218 }
7219 if (line != NULL)
7220 line = skipwhite(ea.cmd + 1);
7221 continue;
7222 }
7223
7224 case '{':
7225 // "{" starts a block scope
7226 // "{'a': 1}->func() is something else
7227 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7228 {
7229 line = compile_block(ea.cmd, &cctx);
7230 continue;
7231 }
7232 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233 }
7234
7235 /*
7236 * COMMAND MODIFIERS
7237 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007238 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007239 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7240 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 {
7242 if (errormsg != NULL)
7243 goto erret;
7244 // empty line or comment
7245 line = (char_u *)"";
7246 continue;
7247 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007248 generate_cmdmods(&cctx, &local_cmdmod);
7249 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007251 // Check if there was a colon after the last command modifier or before
7252 // the current position.
7253 for (p = ea.cmd; p >= line; --p)
7254 {
7255 if (*p == ':')
7256 starts_with_colon = TRUE;
7257 if (p < ea.cmd && !VIM_ISWHITE(*p))
7258 break;
7259 }
7260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007262 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007264 {
7265 if (*ea.cmd == '(')
7266 // not for "call()"
7267 ea.cmd = p;
7268 else
7269 ea.cmd = skipwhite(ea.cmd);
7270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007272 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007274 char_u *pskip;
7275
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007276 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007277 // find what follows.
7278 // Skip over "var.member", "var[idx]" and the like.
7279 // Also "&opt = val", "$ENV = val" and "@r = val".
7280 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007281 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007282 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007283 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007285 char_u *var_end;
7286 int oplen;
7287 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288
Bram Moolenaar65821722020-08-02 18:58:54 +02007289 if (ea.cmd[0] == '@')
7290 var_end = ea.cmd + 2;
7291 else
7292 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007293 FNE_CHECK_START | FNE_INCL_BR);
7294 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007295 if (oplen > 0)
7296 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007297 size_t len = p - ea.cmd;
7298
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007299 // Recognize an assignment if we recognize the variable
7300 // name:
7301 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007302 // "local = expr" where "local" is a local var.
7303 // "script = expr" where "script" is a script-local var.
7304 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007305 // "&opt = expr"
7306 // "$ENV = expr"
7307 // "@r = expr"
7308 if (*ea.cmd == '&'
7309 || *ea.cmd == '$'
7310 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007311 || ((len) > 2 && ea.cmd[1] == ':')
7312 || lookup_local(ea.cmd, len, &cctx) != NULL
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007313 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007314 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007315 || script_var_exists(ea.cmd, len,
7316 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007317 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007318 {
7319 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007320 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007321 goto erret;
7322 continue;
7323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 }
7325 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007326
7327 if (*ea.cmd == '[')
7328 {
7329 // [var, var] = expr
7330 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7331 if (line == NULL)
7332 goto erret;
7333 if (line != ea.cmd)
7334 continue;
7335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 }
7337
7338 /*
7339 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007340 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007342 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007343 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007344 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007345 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007346 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007347 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007348 if (!starts_with_colon)
7349 {
7350 emsg(_(e_colon_required_before_a_range));
7351 goto erret;
7352 }
7353 if (ends_excmd2(line, ea.cmd))
7354 {
7355 // A range without a command: jump to the line.
7356 // TODO: compile to a more efficient command, possibly
7357 // calling parse_cmd_address().
7358 ea.cmdidx = CMD_SIZE;
7359 line = compile_exec(line, &ea, &cctx);
7360 goto nextline;
7361 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007362 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007363 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007364 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007365 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007366 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367
7368 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7369 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007370 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007371 {
7372 line += STRLEN(line);
7373 continue;
7374 }
7375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007377 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007379 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007380 iemsg("Command from find_ex_command() not handled");
7381 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 }
7384
Bram Moolenaar3988f642020-08-27 22:43:03 +02007385 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007386 && ea.cmdidx != CMD_elseif
7387 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007388 && ea.cmdidx != CMD_endif
7389 && ea.cmdidx != CMD_endfor
7390 && ea.cmdidx != CMD_endwhile
7391 && ea.cmdidx != CMD_catch
7392 && ea.cmdidx != CMD_finally
7393 && ea.cmdidx != CMD_endtry)
7394 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007395 emsg(_(e_unreachable_code_after_return));
7396 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007397 }
7398
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007399 p = skipwhite(p);
7400 if (ea.cmdidx != CMD_SIZE
7401 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7402 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007403 if (ea.cmdidx >= 0)
7404 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007405 if ((ea.argt & EX_BANG) && *p == '!')
7406 {
7407 ea.forceit = TRUE;
7408 p = skipwhite(p + 1);
7409 }
7410 }
7411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 switch (ea.cmdidx)
7413 {
7414 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007415 ea.arg = p;
7416 line = compile_nested_function(&ea, &cctx);
7417 break;
7418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007420 // TODO: should we allow this, e.g. to declare a global
7421 // function?
7422 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 goto erret;
7424
7425 case CMD_return:
7426 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007427 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 break;
7429
7430 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007431 emsg(_(e_cannot_use_let_in_vim9_script));
7432 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007433 case CMD_var:
7434 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 case CMD_const:
7436 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007437 if (line == p)
7438 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 break;
7440
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007441 case CMD_unlet:
7442 case CMD_unlockvar:
7443 case CMD_lockvar:
7444 line = compile_unletlock(p, &ea, &cctx);
7445 break;
7446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 case CMD_import:
7448 line = compile_import(p, &cctx);
7449 break;
7450
7451 case CMD_if:
7452 line = compile_if(p, &cctx);
7453 break;
7454 case CMD_elseif:
7455 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007456 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 break;
7458 case CMD_else:
7459 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007460 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 break;
7462 case CMD_endif:
7463 line = compile_endif(p, &cctx);
7464 break;
7465
7466 case CMD_while:
7467 line = compile_while(p, &cctx);
7468 break;
7469 case CMD_endwhile:
7470 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007471 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 break;
7473
7474 case CMD_for:
7475 line = compile_for(p, &cctx);
7476 break;
7477 case CMD_endfor:
7478 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007479 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 break;
7481 case CMD_continue:
7482 line = compile_continue(p, &cctx);
7483 break;
7484 case CMD_break:
7485 line = compile_break(p, &cctx);
7486 break;
7487
7488 case CMD_try:
7489 line = compile_try(p, &cctx);
7490 break;
7491 case CMD_catch:
7492 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007493 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 break;
7495 case CMD_finally:
7496 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007497 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498 break;
7499 case CMD_endtry:
7500 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007501 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 break;
7503 case CMD_throw:
7504 line = compile_throw(p, &cctx);
7505 break;
7506
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007507 case CMD_eval:
7508 if (compile_expr0(&p, &cctx) == FAIL)
7509 goto erret;
7510
Bram Moolenaar3988f642020-08-27 22:43:03 +02007511 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007512 generate_instr_drop(&cctx, ISN_DROP, 1);
7513
7514 line = skipwhite(p);
7515 break;
7516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007519 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007520 case CMD_echomsg:
7521 case CMD_echoerr:
7522 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007523 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007525 case CMD_put:
7526 ea.cmd = cmd;
7527 line = compile_put(p, &ea, &cctx);
7528 break;
7529
Bram Moolenaar3988f642020-08-27 22:43:03 +02007530 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007531
Bram Moolenaarae616492020-07-28 20:07:27 +02007532 case CMD_append:
7533 case CMD_change:
7534 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007535 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007536 case CMD_xit:
7537 not_in_vim9(&ea);
7538 goto erret;
7539
Bram Moolenaar002262f2020-07-08 17:47:57 +02007540 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007541 if (cctx.ctx_skip != SKIP_YES)
7542 {
7543 semsg(_(e_invalid_command_str), ea.cmd);
7544 goto erret;
7545 }
7546 // We don't check for a next command here.
7547 line = (char_u *)"";
7548 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007551 if (cctx.ctx_skip == SKIP_YES)
7552 {
7553 // We don't check for a next command here.
7554 line = (char_u *)"";
7555 }
7556 else
7557 {
7558 // Not recognized, execute with do_cmdline_cmd().
7559 ea.arg = p;
7560 line = compile_exec(line, &ea, &cctx);
7561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 break;
7563 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007564nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 if (line == NULL)
7566 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007567 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007569 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007570 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 if (cctx.ctx_type_stack.ga_len < 0)
7573 {
7574 iemsg("Type stack underflow");
7575 goto erret;
7576 }
7577 }
7578
7579 if (cctx.ctx_scope != NULL)
7580 {
7581 if (cctx.ctx_scope->se_type == IF_SCOPE)
7582 emsg(_(e_endif));
7583 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7584 emsg(_(e_endwhile));
7585 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7586 emsg(_(e_endfor));
7587 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007588 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 goto erret;
7590 }
7591
Bram Moolenaarefd88552020-06-18 20:50:10 +02007592 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 {
7594 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7595 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007596 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597 goto erret;
7598 }
7599
7600 // Return zero if there is no return at the end.
7601 generate_PUSHNR(&cctx, 0);
7602 generate_instr(&cctx, ISN_RETURN);
7603 }
7604
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007605 {
7606 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7607 + ufunc->uf_dfunc_idx;
7608 dfunc->df_deleted = FALSE;
7609 dfunc->df_instr = instr->ga_data;
7610 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007611 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007612 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007613 if (cctx.ctx_outer_used)
7614 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007615 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617
7618 ret = OK;
7619
7620erret:
7621 if (ret == FAIL)
7622 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007623 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007624 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7625 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007626
7627 for (idx = 0; idx < instr->ga_len; ++idx)
7628 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007630
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007631 // If using the last entry in the table and it was added above, we
7632 // might as well remove it.
7633 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007634 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007635 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007636 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007637 ufunc->uf_dfunc_idx = 0;
7638 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007639 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007640
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007641 while (cctx.ctx_scope != NULL)
7642 drop_scope(&cctx);
7643
Bram Moolenaar20431c92020-03-20 18:39:46 +01007644 // Don't execute this function body.
7645 ga_clear_strings(&ufunc->uf_lines);
7646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647 if (errormsg != NULL)
7648 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007649 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007650 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 }
7652
7653 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007654 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007655 if (do_estack_push)
7656 estack_pop();
7657
Bram Moolenaar20431c92020-03-20 18:39:46 +01007658 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007659 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007661 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662}
7663
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007664 void
7665set_function_type(ufunc_T *ufunc)
7666{
7667 int varargs = ufunc->uf_va_name != NULL;
7668 int argcount = ufunc->uf_args.ga_len;
7669
7670 // Create a type for the function, with the return type and any
7671 // argument types.
7672 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7673 // The type is included in "tt_args".
7674 if (argcount > 0 || varargs)
7675 {
7676 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7677 argcount, &ufunc->uf_type_list);
7678 // Add argument types to the function type.
7679 if (func_type_add_arg_types(ufunc->uf_func_type,
7680 argcount + varargs,
7681 &ufunc->uf_type_list) == FAIL)
7682 return;
7683 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7684 ufunc->uf_func_type->tt_min_argcount =
7685 argcount - ufunc->uf_def_args.ga_len;
7686 if (ufunc->uf_arg_types == NULL)
7687 {
7688 int i;
7689
7690 // lambda does not have argument types.
7691 for (i = 0; i < argcount; ++i)
7692 ufunc->uf_func_type->tt_args[i] = &t_any;
7693 }
7694 else
7695 mch_memmove(ufunc->uf_func_type->tt_args,
7696 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7697 if (varargs)
7698 {
7699 ufunc->uf_func_type->tt_args[argcount] =
7700 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7701 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7702 }
7703 }
7704 else
7705 // No arguments, can use a predefined type.
7706 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7707 argcount, &ufunc->uf_type_list);
7708}
7709
7710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007711/*
7712 * Delete an instruction, free what it contains.
7713 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007714 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715delete_instr(isn_T *isn)
7716{
7717 switch (isn->isn_type)
7718 {
7719 case ISN_EXEC:
7720 case ISN_LOADENV:
7721 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007722 case ISN_LOADB:
7723 case ISN_LOADW:
7724 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007726 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 case ISN_PUSHEXC:
7728 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007729 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007731 case ISN_STOREB:
7732 case ISN_STOREW:
7733 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007734 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 vim_free(isn->isn_arg.string);
7736 break;
7737
7738 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007739 case ISN_STORES:
7740 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 break;
7742
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007743 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007744 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007745 vim_free(isn->isn_arg.unlet.ul_name);
7746 break;
7747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 case ISN_STOREOPT:
7749 vim_free(isn->isn_arg.storeopt.so_name);
7750 break;
7751
7752 case ISN_PUSHBLOB: // push blob isn_arg.blob
7753 blob_unref(isn->isn_arg.blob);
7754 break;
7755
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007756 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007757#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007758 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007759#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007760 break;
7761
7762 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007763#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007764 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007765#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007766 break;
7767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 case ISN_UCALL:
7769 vim_free(isn->isn_arg.ufunc.cuf_name);
7770 break;
7771
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007772 case ISN_FUNCREF:
7773 {
7774 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7775 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007776
7777 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7778 func_ptr_unref(dfunc->df_ufunc);
7779 }
7780 break;
7781
7782 case ISN_DCALL:
7783 {
7784 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7785 + isn->isn_arg.dfunc.cdf_idx;
7786
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007787 if (dfunc->df_ufunc != NULL
7788 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007789 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007790 }
7791 break;
7792
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007793 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007794 {
7795 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7796 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7797
7798 if (ufunc != NULL)
7799 {
7800 // Clear uf_dfunc_idx so that the function is deleted.
7801 clear_def_function(ufunc);
7802 ufunc->uf_dfunc_idx = 0;
7803 func_ptr_unref(ufunc);
7804 }
7805
7806 vim_free(lambda);
7807 vim_free(isn->isn_arg.newfunc.nf_global);
7808 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007809 break;
7810
Bram Moolenaar5e654232020-09-16 15:22:00 +02007811 case ISN_CHECKTYPE:
7812 free_type(isn->isn_arg.type.ct_type);
7813 break;
7814
Bram Moolenaar02194d22020-10-24 23:08:38 +02007815 case ISN_CMDMOD:
7816 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
7817 ->cmod_filter_regmatch.regprog);
7818 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
7819 break;
7820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821 case ISN_2BOOL:
7822 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007823 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 case ISN_ADDBLOB:
7825 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007826 case ISN_ANYINDEX:
7827 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02007829 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007831 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007832 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02007833 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834 case ISN_COMPAREANY:
7835 case ISN_COMPAREBLOB:
7836 case ISN_COMPAREBOOL:
7837 case ISN_COMPAREDICT:
7838 case ISN_COMPAREFLOAT:
7839 case ISN_COMPAREFUNC:
7840 case ISN_COMPARELIST:
7841 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842 case ISN_COMPARESPECIAL:
7843 case ISN_COMPARESTRING:
7844 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007845 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846 case ISN_DROP:
7847 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007848 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007849 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007851 case ISN_EXECCONCAT:
7852 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007854 case ISN_GETITEM:
7855 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02007856 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007857 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007858 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007859 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007860 case ISN_LOADBDICT:
7861 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007862 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007864 case ISN_LOADSCRIPT:
7865 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007867 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007868 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007869 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 case ISN_NEGATENR:
7871 case ISN_NEWDICT:
7872 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007874 case ISN_OPFLOAT:
7875 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007877 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007878 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879 case ISN_PUSHF:
7880 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007882 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007884 case ISN_SHUFFLE:
7885 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007887 case ISN_STOREDICT:
7888 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007889 case ISN_STORENR:
7890 case ISN_STOREOUTER:
7891 case ISN_STOREREG:
7892 case ISN_STORESCRIPT:
7893 case ISN_STOREV:
7894 case ISN_STRINDEX:
7895 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 case ISN_THROW:
7897 case ISN_TRY:
7898 // nothing allocated
7899 break;
7900 }
7901}
7902
7903/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007904 * Free all instructions for "dfunc".
7905 */
7906 static void
7907delete_def_function_contents(dfunc_T *dfunc)
7908{
7909 int idx;
7910
7911 ga_clear(&dfunc->df_def_args_isn);
7912
7913 if (dfunc->df_instr != NULL)
7914 {
7915 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7916 delete_instr(dfunc->df_instr + idx);
7917 VIM_CLEAR(dfunc->df_instr);
7918 }
7919
7920 dfunc->df_deleted = TRUE;
7921}
7922
7923/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007924 * When a user function is deleted, clear the contents of any associated def
7925 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926 */
7927 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007928clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007930 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931 {
7932 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7933 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007934
Bram Moolenaar20431c92020-03-20 18:39:46 +01007935 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007936 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 }
7938}
7939
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007940/*
7941 * Used when a user function is about to be deleted: remove the pointer to it.
7942 * The entry in def_functions is then unused.
7943 */
7944 void
7945unlink_def_function(ufunc_T *ufunc)
7946{
7947 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7948
7949 dfunc->df_ufunc = NULL;
7950}
7951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007953/*
7954 * Free all functions defined with ":def".
7955 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956 void
7957free_def_functions(void)
7958{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007959 int idx;
7960
7961 for (idx = 0; idx < def_functions.ga_len; ++idx)
7962 {
7963 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7964
7965 delete_def_function_contents(dfunc);
7966 }
7967
7968 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007969}
7970#endif
7971
7972
7973#endif // FEAT_EVAL