blob: bb33d38e9ef0d0be6f1e948ce85dcf7de7bfc92e [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 Moolenaarcdc40c42020-12-26 17:43:08 +0100148static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
152 * "lvar->lv_from_outer" is set accordingly.
153 * If "lvar" is NULL only check if the variable can be found.
154 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 static int
157lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158{
159 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100160 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100162 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164
165 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100168 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
169 if (STRNCMP(name, lvp->lv_name, len) == 0
170 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100172 if (lvar != NULL)
173 {
174 *lvar = *lvp;
175 lvar->lv_from_outer = FALSE;
176 }
177 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100184 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lvar != NULL)
187 {
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 }
191 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 }
193 }
194
Bram Moolenaar709664c2020-12-12 14:33:41 +0100195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200206arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200258 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 == OK)
260 {
261 *gen_load_outer = TRUE;
262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270 * Lookup a script-local variable in the current script, possibly defined in a
271 * block that contains the function "cctx->ctx_ufunc".
272 * "cctx" is NULL at the script level.
273 * if "len" is <= 0 "name" must be NUL terminated.
274 * Return NULL when not found.
275 */
276 static sallvar_T *
277find_script_var(char_u *name, size_t len, cctx_T *cctx)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 int cc;
282 sallvar_T *sav;
283 ufunc_T *ufunc;
284
285 // Find the list of all script variables with the right name.
286 if (len > 0)
287 {
288 cc = name[len];
289 name[len] = NUL;
290 }
291 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
292 if (len > 0)
293 name[len] = cc;
294 if (HASHITEM_EMPTY(hi))
295 return NULL;
296
297 sav = HI2SAV(hi);
298 if (sav->sav_block_id == 0 || cctx == NULL)
299 // variable defined in the script scope or not in a function.
300 return sav;
301
302 // Go over the variables with this name and find one that was visible
303 // from the function.
304 ufunc = cctx->ctx_ufunc;
305 while (sav != NULL)
306 {
307 int idx;
308
309 // Go over the blocks that this function was defined in. If the
310 // variable block ID matches it was visible to the function.
311 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
312 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
313 return sav;
314 sav = sav->sav_next;
315 }
316
317 return NULL;
318}
319
320/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100321 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200322 */
323 static int
324script_is_vim9()
325{
326 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200331 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
332 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Returns OK or FAIL.
335 */
336 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 is_vim9_script = script_is_vim9();
344 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 if (is_vim9_script)
347 {
348 // Check script variables that were visible where the function was
349 // defined.
350 if (find_script_var(name, len, cctx) != NULL)
351 return OK;
352 }
353 else
354 {
355 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
356 dictitem_T *di;
357 int cc;
358
359 // Check script variables that are currently visible
360 cc = name[len];
361 name[len] = NUL;
362 di = find_var_in_ht(ht, 0, name, TRUE);
363 name[len] = cc;
364 if (di != NULL)
365 return OK;
366 }
367
368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369}
370
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371/*
372 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200373 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375 * Return FAIL and give an error if it defined.
376 */
377 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200378check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200380 int c = p[len];
381 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382
383 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200384 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100385 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100386 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200388 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200389 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100390 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 // A local or script-local function can shadow a global function.
392 if (ufunc == NULL || !func_is_global(ufunc)
393 || (p[0] == 'g' && p[1] == ':'))
394 {
395 p[len] = c;
396 semsg(_(e_name_already_defined_str), p);
397 return FAIL;
398 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100399 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200400 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 return OK;
402}
403
Bram Moolenaar65b95452020-07-19 14:03:09 +0200404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/////////////////////////////////////////////////////////////////////
406// Following generate_ functions expect the caller to call ga_grow().
407
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200408#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
409#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411/*
412 * Generate an instruction without arguments.
413 * Returns a pointer to the new instruction, NULL if failed.
414 */
415 static isn_T *
416generate_instr(cctx_T *cctx, isntype_T isn_type)
417{
418 garray_T *instr = &cctx->ctx_instr;
419 isn_T *isn;
420
Bram Moolenaar080457c2020-03-03 21:53:32 +0100421 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ga_grow(instr, 1) == FAIL)
423 return NULL;
424 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
425 isn->isn_type = isn_type;
426 isn->isn_lnum = cctx->ctx_lnum + 1;
427 ++instr->ga_len;
428
429 return isn;
430}
431
432/*
433 * Generate an instruction without arguments.
434 * "drop" will be removed from the stack.
435 * Returns a pointer to the new instruction, NULL if failed.
436 */
437 static isn_T *
438generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
439{
440 garray_T *stack = &cctx->ctx_type_stack;
441
Bram Moolenaar080457c2020-03-03 21:53:32 +0100442 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 stack->ga_len -= drop;
444 return generate_instr(cctx, isn_type);
445}
446
447/*
448 * Generate instruction "isn_type" and put "type" on the type stack.
449 */
450 static isn_T *
451generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
452{
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
455
456 if ((isn = generate_instr(cctx, isn_type)) == NULL)
457 return NULL;
458
459 if (ga_grow(stack, 1) == FAIL)
460 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200461 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 ++stack->ga_len;
463
464 return isn;
465}
466
467/*
468 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 */
471 static int
472may_generate_2STRING(int offset, cctx_T *cctx)
473{
474 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200475 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 garray_T *stack = &cctx->ctx_type_stack;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 switch ((*type)->tt_type)
480 {
481 // nothing to be done
482 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 // conversion possible
485 case VAR_SPECIAL:
486 case VAR_BOOL:
487 case VAR_NUMBER:
488 case VAR_FLOAT:
489 break;
490
491 // conversion possible (with runtime check)
492 case VAR_ANY:
493 case VAR_UNKNOWN:
494 isntype = ISN_2STRING_ANY;
495 break;
496
497 // conversion not possible
498 case VAR_VOID:
499 case VAR_BLOB:
500 case VAR_FUNC:
501 case VAR_PARTIAL:
502 case VAR_LIST:
503 case VAR_DICT:
504 case VAR_JOB:
505 case VAR_CHANNEL:
506 to_string_error((*type)->tt_type);
507 return FAIL;
508 }
509
510 *type = &t_string;
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 return FAIL;
513 isn->isn_arg.number = offset;
514
515 return OK;
516}
517
518 static int
519check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
520{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200521 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200526 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 }
531 return OK;
532}
533
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200534 static int
535generate_add_instr(
536 cctx_T *cctx,
537 vartype_T vartype,
538 type_T *type1,
539 type_T *type2)
540{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100541 garray_T *stack = &cctx->ctx_type_stack;
542 isn_T *isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR
544 : vartype == VAR_LIST ? ISN_ADDLIST
545 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550
551 if (vartype != VAR_LIST && vartype != VAR_BLOB
552 && type1->tt_type != VAR_ANY
553 && type2->tt_type != VAR_ANY
554 && check_number_or_float(
555 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
556 return FAIL;
557
558 if (isn != NULL)
559 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560
561 // When concatenating two lists with different member types the member type
562 // becomes "any".
563 if (vartype == VAR_LIST
564 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
565 && type1->tt_member != type2->tt_member)
566 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
567
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200568 return isn == NULL ? FAIL : OK;
569}
570
571/*
572 * Get the type to use for an instruction for an operation on "type1" and
573 * "type2". If they are matching use a type-specific instruction. Otherwise
574 * fall back to runtime type checking.
575 */
576 static vartype_T
577operator_type(type_T *type1, type_T *type2)
578{
579 if (type1->tt_type == type2->tt_type
580 && (type1->tt_type == VAR_NUMBER
581 || type1->tt_type == VAR_LIST
582#ifdef FEAT_FLOAT
583 || type1->tt_type == VAR_FLOAT
584#endif
585 || type1->tt_type == VAR_BLOB))
586 return type1->tt_type;
587 return VAR_ANY;
588}
589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590/*
591 * Generate an instruction with two arguments. The instruction depends on the
592 * type of the arguments.
593 */
594 static int
595generate_two_op(cctx_T *cctx, char_u *op)
596{
597 garray_T *stack = &cctx->ctx_type_stack;
598 type_T *type1;
599 type_T *type2;
600 vartype_T vartype;
601 isn_T *isn;
602
Bram Moolenaar080457c2020-03-03 21:53:32 +0100603 RETURN_OK_IF_SKIP(cctx);
604
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
607 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 switch (*op)
611 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 case '+':
613 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 break;
616
617 case '-':
618 case '*':
619 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
620 op) == FAIL)
621 return FAIL;
622 if (vartype == VAR_NUMBER)
623 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
624#ifdef FEAT_FLOAT
625 else if (vartype == VAR_FLOAT)
626 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
627#endif
628 else
629 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = *op == '*'
632 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
633 break;
634
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type2->tt_type != VAR_NUMBER))
639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 isn = generate_instr_drop(cctx,
644 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
645 if (isn != NULL)
646 isn->isn_arg.op.op_type = EXPR_REM;
647 break;
648 }
649
650 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 type_T *type = &t_any;
654
655#ifdef FEAT_FLOAT
656 // float+number and number+float results in float
657 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
658 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
659 type = &t_float;
660#endif
661 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
662 }
663
664 return OK;
665}
666
667/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200668 * Get the instruction to use for comparing "type1" with "type2"
669 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200671 static isntype_T
672get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 if (type1 == VAR_UNKNOWN)
677 type1 = VAR_ANY;
678 if (type2 == VAR_UNKNOWN)
679 type2 = VAR_ANY;
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (type1 == type2)
682 {
683 switch (type1)
684 {
685 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
686 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
687 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
688 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
689 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
690 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
691 case VAR_LIST: isntype = ISN_COMPARELIST; break;
692 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
693 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 default: isntype = ISN_COMPAREANY; break;
695 }
696 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
699 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
700 isntype = ISN_COMPAREANY;
701
702 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
703 && (isntype == ISN_COMPAREBOOL
704 || isntype == ISN_COMPARESPECIAL
705 || isntype == ISN_COMPARENR
706 || isntype == ISN_COMPAREFLOAT))
707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200708 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 }
712 if (isntype == ISN_DROP
713 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
714 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
715 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
716 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
717 && exptype != EXPR_IS && exptype != EXPR_ISNOT
718 && (type1 == VAR_BLOB || type2 == VAR_BLOB
719 || type1 == VAR_LIST || type2 == VAR_LIST))))
720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200721 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return isntype;
726}
727
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200728 int
729check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
730{
731 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
732 return FAIL;
733 return OK;
734}
735
Bram Moolenaara5565e42020-05-09 15:44:01 +0200736/*
737 * Generate an ISN_COMPARE* instruction with a boolean result.
738 */
739 static int
740generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
741{
742 isntype_T isntype;
743 isn_T *isn;
744 garray_T *stack = &cctx->ctx_type_stack;
745 vartype_T type1;
746 vartype_T type2;
747
748 RETURN_OK_IF_SKIP(cctx);
749
750 // Get the known type of the two items on the stack. If they are matching
751 // use a type-specific instruction. Otherwise fall back to runtime type
752 // checking.
753 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
754 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
755 isntype = get_compare_isn(exptype, type1, type2);
756 if (isntype == ISN_DROP)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if ((isn = generate_instr(cctx, isntype)) == NULL)
760 return FAIL;
761 isn->isn_arg.op.op_type = exptype;
762 isn->isn_arg.op.op_ic = ic;
763
764 // takes two arguments, puts one bool back
765 if (stack->ga_len >= 2)
766 {
767 --stack->ga_len;
768 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
769 }
770
771 return OK;
772}
773
774/*
775 * Generate an ISN_2BOOL instruction.
776 */
777 static int
778generate_2BOOL(cctx_T *cctx, int invert)
779{
780 isn_T *isn;
781 garray_T *stack = &cctx->ctx_type_stack;
782
Bram Moolenaar080457c2020-03-03 21:53:32 +0100783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = invert;
787
788 // type becomes bool
789 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
790
791 return OK;
792}
793
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200794/*
795 * Generate an ISN_COND2BOOL instruction.
796 */
797 static int
798generate_COND2BOOL(cctx_T *cctx)
799{
800 isn_T *isn;
801 garray_T *stack = &cctx->ctx_type_stack;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
805 return FAIL;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200814generate_TYPECHECK(
815 cctx_T *cctx,
816 type_T *expected,
817 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
824 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200825 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 isn->isn_arg.type.ct_off = offset;
827
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 // type becomes expected
829 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return OK;
832}
833
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100834 static int
835generate_SETTYPE(
836 cctx_T *cctx,
837 type_T *expected)
838{
839 isn_T *isn;
840
841 RETURN_OK_IF_SKIP(cctx);
842 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
843 return FAIL;
844 isn->isn_arg.type.ct_type = alloc_type(expected);
845 return OK;
846}
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200849 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
850 * used. Return FALSE if the types will never match.
851 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100852 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200853use_typecheck(type_T *actual, type_T *expected)
854{
855 if (actual->tt_type == VAR_ANY
856 || actual->tt_type == VAR_UNKNOWN
857 || (actual->tt_type == VAR_FUNC
858 && (expected->tt_type == VAR_FUNC
859 || expected->tt_type == VAR_PARTIAL)
860 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
861 return TRUE;
862 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
863 && actual->tt_type == expected->tt_type)
864 // This takes care of a nested list or dict.
865 return use_typecheck(actual->tt_member, expected->tt_member);
866 return FALSE;
867}
868
869/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200871 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 * - "actual" is a type that can be "expected" type: add a runtime check; or
873 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200874 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
875 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200876 */
877 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200878need_type(
879 type_T *actual,
880 type_T *expected,
881 int offset,
882 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200883 int silent,
884 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200885{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200886 if (expected == &t_bool && actual != &t_bool
887 && (actual->tt_flags & TTFLAG_BOOL_OK))
888 {
889 // Using "0", "1" or the result of an expression with "&&" or "||" as a
890 // boolean is OK but requires a conversion.
891 generate_2BOOL(cctx, FALSE);
892 return OK;
893 }
894
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200895 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200896 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200897
898 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200899 // If it's a constant a runtime check makes no sense.
900 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200901 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200902 generate_TYPECHECK(cctx, expected, offset);
903 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200904 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200905
906 if (!silent)
907 type_mismatch(expected, actual);
908 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200909}
910
911/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100912 * Check that the top of the type stack has a type that can be used as a
913 * condition. Give an error and return FAIL if not.
914 */
915 static int
916bool_on_stack(cctx_T *cctx)
917{
918 garray_T *stack = &cctx->ctx_type_stack;
919 type_T *type;
920
921 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
922 if (type == &t_bool)
923 return OK;
924
925 if (type == &t_any || type == &t_number)
926 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
927 // This requires a runtime type check.
928 return generate_COND2BOOL(cctx);
929
930 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
931}
932
933/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 * Generate an ISN_PUSHNR instruction.
935 */
936 static int
937generate_PUSHNR(cctx_T *cctx, varnumber_T number)
938{
939 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200940 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
Bram Moolenaar080457c2020-03-03 21:53:32 +0100942 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
944 return FAIL;
945 isn->isn_arg.number = number;
946
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200947 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200948 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100949 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return OK;
951}
952
953/*
954 * Generate an ISN_PUSHBOOL instruction.
955 */
956 static int
957generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
958{
959 isn_T *isn;
960
Bram Moolenaar080457c2020-03-03 21:53:32 +0100961 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
963 return FAIL;
964 isn->isn_arg.number = number;
965
966 return OK;
967}
968
969/*
970 * Generate an ISN_PUSHSPEC instruction.
971 */
972 static int
973generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
974{
975 isn_T *isn;
976
Bram Moolenaar080457c2020-03-03 21:53:32 +0100977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
979 return FAIL;
980 isn->isn_arg.number = number;
981
982 return OK;
983}
984
985#ifdef FEAT_FLOAT
986/*
987 * Generate an ISN_PUSHF instruction.
988 */
989 static int
990generate_PUSHF(cctx_T *cctx, float_T fnumber)
991{
992 isn_T *isn;
993
Bram Moolenaar080457c2020-03-03 21:53:32 +0100994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
996 return FAIL;
997 isn->isn_arg.fnumber = fnumber;
998
999 return OK;
1000}
1001#endif
1002
1003/*
1004 * Generate an ISN_PUSHS instruction.
1005 * Consumes "str".
1006 */
1007 static int
1008generate_PUSHS(cctx_T *cctx, char_u *str)
1009{
1010 isn_T *isn;
1011
Bram Moolenaar080457c2020-03-03 21:53:32 +01001012 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1014 return FAIL;
1015 isn->isn_arg.string = str;
1016
1017 return OK;
1018}
1019
1020/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001021 * Generate an ISN_PUSHCHANNEL instruction.
1022 * Consumes "channel".
1023 */
1024 static int
1025generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001030 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1031 return FAIL;
1032 isn->isn_arg.channel = channel;
1033
1034 return OK;
1035}
1036
1037/*
1038 * Generate an ISN_PUSHJOB instruction.
1039 * Consumes "job".
1040 */
1041 static int
1042generate_PUSHJOB(cctx_T *cctx, job_T *job)
1043{
1044 isn_T *isn;
1045
Bram Moolenaar080457c2020-03-03 21:53:32 +01001046 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001047 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001048 return FAIL;
1049 isn->isn_arg.job = job;
1050
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 * Generate an ISN_PUSHBLOB instruction.
1056 * Consumes "blob".
1057 */
1058 static int
1059generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1060{
1061 isn_T *isn;
1062
Bram Moolenaar080457c2020-03-03 21:53:32 +01001063 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.blob = blob;
1067
1068 return OK;
1069}
1070
1071/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001072 * Generate an ISN_PUSHFUNC instruction with name "name".
1073 * Consumes "name".
1074 */
1075 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001076generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001077{
1078 isn_T *isn;
1079
Bram Moolenaar080457c2020-03-03 21:53:32 +01001080 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001081 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001082 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001083 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001089 * Generate an ISN_GETITEM instruction with "index".
1090 */
1091 static int
1092generate_GETITEM(cctx_T *cctx, int index)
1093{
1094 isn_T *isn;
1095 garray_T *stack = &cctx->ctx_type_stack;
1096 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1097 type_T *item_type = &t_any;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001101 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001102 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001103 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001104 emsg(_(e_listreq));
1105 return FAIL;
1106 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001107 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001108 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1109 return FAIL;
1110 isn->isn_arg.number = index;
1111
1112 // add the item type to the type stack
1113 if (ga_grow(stack, 1) == FAIL)
1114 return FAIL;
1115 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1116 ++stack->ga_len;
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001121 * Generate an ISN_SLICE instruction with "count".
1122 */
1123 static int
1124generate_SLICE(cctx_T *cctx, int count)
1125{
1126 isn_T *isn;
1127
1128 RETURN_OK_IF_SKIP(cctx);
1129 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1130 return FAIL;
1131 isn->isn_arg.number = count;
1132 return OK;
1133}
1134
1135/*
1136 * Generate an ISN_CHECKLEN instruction with "min_len".
1137 */
1138 static int
1139generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1140{
1141 isn_T *isn;
1142
1143 RETURN_OK_IF_SKIP(cctx);
1144
1145 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1146 return FAIL;
1147 isn->isn_arg.checklen.cl_min_len = min_len;
1148 isn->isn_arg.checklen.cl_more_OK = more_OK;
1149
1150 return OK;
1151}
1152
1153/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 * Generate an ISN_STORE instruction.
1155 */
1156 static int
1157generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1163 return FAIL;
1164 if (name != NULL)
1165 isn->isn_arg.string = vim_strsave(name);
1166 else
1167 isn->isn_arg.number = idx;
1168
1169 return OK;
1170}
1171
1172/*
1173 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1174 */
1175 static int
1176generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1177{
1178 isn_T *isn;
1179
Bram Moolenaar080457c2020-03-03 21:53:32 +01001180 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1182 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001183 isn->isn_arg.storenr.stnr_idx = idx;
1184 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185
1186 return OK;
1187}
1188
1189/*
1190 * Generate an ISN_STOREOPT instruction
1191 */
1192 static int
1193generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1194{
1195 isn_T *isn;
1196
Bram Moolenaar080457c2020-03-03 21:53:32 +01001197 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001198 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 return FAIL;
1200 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1201 isn->isn_arg.storeopt.so_flags = opt_flags;
1202
1203 return OK;
1204}
1205
1206/*
1207 * Generate an ISN_LOAD or similar instruction.
1208 */
1209 static int
1210generate_LOAD(
1211 cctx_T *cctx,
1212 isntype_T isn_type,
1213 int idx,
1214 char_u *name,
1215 type_T *type)
1216{
1217 isn_T *isn;
1218
Bram Moolenaar080457c2020-03-03 21:53:32 +01001219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1221 return FAIL;
1222 if (name != NULL)
1223 isn->isn_arg.string = vim_strsave(name);
1224 else
1225 isn->isn_arg.number = idx;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001231 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001232 */
1233 static int
1234generate_LOADV(
1235 cctx_T *cctx,
1236 char_u *name,
1237 int error)
1238{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001239 int di_flags;
1240 int vidx = find_vim_var(name, &di_flags);
1241 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001242
Bram Moolenaar080457c2020-03-03 21:53:32 +01001243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001244 if (vidx < 0)
1245 {
1246 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001247 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 return FAIL;
1249 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001250 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001251
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001252 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001253}
1254
1255/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001256 * Generate an ISN_UNLET instruction.
1257 */
1258 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001259generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001260{
1261 isn_T *isn;
1262
1263 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001264 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001265 return FAIL;
1266 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1267 isn->isn_arg.unlet.ul_forceit = forceit;
1268
1269 return OK;
1270}
1271
1272/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001273 * Generate an ISN_LOCKCONST instruction.
1274 */
1275 static int
1276generate_LOCKCONST(cctx_T *cctx)
1277{
1278 isn_T *isn;
1279
1280 RETURN_OK_IF_SKIP(cctx);
1281 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1282 return FAIL;
1283 return OK;
1284}
1285
1286/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 * Generate an ISN_LOADS instruction.
1288 */
1289 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001290generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001294 int sid,
1295 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296{
1297 isn_T *isn;
1298
Bram Moolenaar080457c2020-03-03 21:53:32 +01001299 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001300 if (isn_type == ISN_LOADS)
1301 isn = generate_instr_type(cctx, isn_type, type);
1302 else
1303 isn = generate_instr_drop(cctx, isn_type, 1);
1304 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001306 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1307 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308
1309 return OK;
1310}
1311
1312/*
1313 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1314 */
1315 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001316generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 cctx_T *cctx,
1318 isntype_T isn_type,
1319 int sid,
1320 int idx,
1321 type_T *type)
1322{
1323 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001324 scriptref_T *sref;
1325 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
Bram Moolenaar080457c2020-03-03 21:53:32 +01001327 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 if (isn_type == ISN_LOADSCRIPT)
1329 isn = generate_instr_type(cctx, isn_type, type);
1330 else
1331 isn = generate_instr_drop(cctx, isn_type, 1);
1332 if (isn == NULL)
1333 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001334
1335 // This requires three arguments, which doesn't fit in an instruction, thus
1336 // we need to allocate a struct for this.
1337 sref = ALLOC_ONE(scriptref_T);
1338 if (sref == NULL)
1339 return FAIL;
1340 isn->isn_arg.script.scriptref = sref;
1341 sref->sref_sid = sid;
1342 sref->sref_idx = idx;
1343 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001344 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 return OK;
1346}
1347
1348/*
1349 * Generate an ISN_NEWLIST instruction.
1350 */
1351 static int
1352generate_NEWLIST(cctx_T *cctx, int count)
1353{
1354 isn_T *isn;
1355 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 type_T *type;
1357 type_T *member;
1358
Bram Moolenaar080457c2020-03-03 21:53:32 +01001359 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1361 return FAIL;
1362 isn->isn_arg.number = count;
1363
Bram Moolenaar127542b2020-08-09 17:22:04 +02001364 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001365 if (count == 0)
1366 member = &t_void;
1367 else
1368 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001369 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1370 cctx->ctx_type_list);
1371 type = get_list_type(member, cctx->ctx_type_list);
1372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 // drop the value types
1374 stack->ga_len -= count;
1375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 // add the list type to the type stack
1377 if (ga_grow(stack, 1) == FAIL)
1378 return FAIL;
1379 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1380 ++stack->ga_len;
1381
1382 return OK;
1383}
1384
1385/*
1386 * Generate an ISN_NEWDICT instruction.
1387 */
1388 static int
1389generate_NEWDICT(cctx_T *cctx, int count)
1390{
1391 isn_T *isn;
1392 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 type_T *type;
1394 type_T *member;
1395
Bram Moolenaar080457c2020-03-03 21:53:32 +01001396 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1398 return FAIL;
1399 isn->isn_arg.number = count;
1400
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001401 if (count == 0)
1402 member = &t_void;
1403 else
1404 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001405 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1406 cctx->ctx_type_list);
1407 type = get_dict_type(member, cctx->ctx_type_list);
1408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 // drop the key and value types
1410 stack->ga_len -= 2 * count;
1411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 // add the dict type to the type stack
1413 if (ga_grow(stack, 1) == FAIL)
1414 return FAIL;
1415 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1416 ++stack->ga_len;
1417
1418 return OK;
1419}
1420
1421/*
1422 * Generate an ISN_FUNCREF instruction.
1423 */
1424 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001425generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426{
1427 isn_T *isn;
1428 garray_T *stack = &cctx->ctx_type_stack;
1429
Bram Moolenaar080457c2020-03-03 21:53:32 +01001430 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1432 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001433 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001434 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 if (ga_grow(stack, 1) == FAIL)
1437 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001438 ((type_T **)stack->ga_data)[stack->ga_len] =
1439 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 ++stack->ga_len;
1441
1442 return OK;
1443}
1444
1445/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001446 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001447 * "lambda_name" and "func_name" must be in allocated memory and will be
1448 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001449 */
1450 static int
1451generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1452{
1453 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001454
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001455 if (cctx->ctx_skip == SKIP_YES)
1456 {
1457 vim_free(lambda_name);
1458 vim_free(func_name);
1459 return OK;
1460 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001461 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001462 {
1463 vim_free(lambda_name);
1464 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001465 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001466 }
1467 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001468 isn->isn_arg.newfunc.nf_global = func_name;
1469
1470 return OK;
1471}
1472
1473/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001474 * Generate an ISN_DEF instruction: list functions
1475 */
1476 static int
1477generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1478{
1479 isn_T *isn;
1480
1481 RETURN_OK_IF_SKIP(cctx);
1482 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1483 return FAIL;
1484 if (len > 0)
1485 {
1486 isn->isn_arg.string = vim_strnsave(name, len);
1487 if (isn->isn_arg.string == NULL)
1488 return FAIL;
1489 }
1490 return OK;
1491}
1492
1493/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 * Generate an ISN_JUMP instruction.
1495 */
1496 static int
1497generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1498{
1499 isn_T *isn;
1500 garray_T *stack = &cctx->ctx_type_stack;
1501
Bram Moolenaar080457c2020-03-03 21:53:32 +01001502 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1504 return FAIL;
1505 isn->isn_arg.jump.jump_when = when;
1506 isn->isn_arg.jump.jump_where = where;
1507
1508 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1509 --stack->ga_len;
1510
1511 return OK;
1512}
1513
1514 static int
1515generate_FOR(cctx_T *cctx, int loop_idx)
1516{
1517 isn_T *isn;
1518 garray_T *stack = &cctx->ctx_type_stack;
1519
Bram Moolenaar080457c2020-03-03 21:53:32 +01001520 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1522 return FAIL;
1523 isn->isn_arg.forloop.for_idx = loop_idx;
1524
1525 if (ga_grow(stack, 1) == FAIL)
1526 return FAIL;
1527 // type doesn't matter, will be stored next
1528 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1529 ++stack->ga_len;
1530
1531 return OK;
1532}
1533
1534/*
1535 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001536 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 * Return FAIL if the number of arguments is wrong.
1538 */
1539 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001540generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541{
1542 isn_T *isn;
1543 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001544 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001545 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546
Bram Moolenaar080457c2020-03-03 21:53:32 +01001547 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001548 argoff = check_internal_func(func_idx, argcount);
1549 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 return FAIL;
1551
Bram Moolenaar389df252020-07-09 21:20:47 +02001552 if (method_call && argoff > 1)
1553 {
1554 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1555 return FAIL;
1556 isn->isn_arg.shuffle.shfl_item = argcount;
1557 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1558 }
1559
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001560 if (argcount > 0)
1561 {
1562 // Check the types of the arguments.
1563 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1564 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001565 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001566 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1569 return FAIL;
1570 isn->isn_arg.bfunc.cbf_idx = func_idx;
1571 isn->isn_arg.bfunc.cbf_argcount = argcount;
1572
Bram Moolenaar94738d82020-10-21 14:25:07 +02001573 // Drop the argument types and push the return type.
1574 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 if (ga_grow(stack, 1) == FAIL)
1576 return FAIL;
1577 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001578 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001579 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580
1581 return OK;
1582}
1583
1584/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001585 * Generate an ISN_LISTAPPEND instruction. Works like add().
1586 * Argument count is already checked.
1587 */
1588 static int
1589generate_LISTAPPEND(cctx_T *cctx)
1590{
1591 garray_T *stack = &cctx->ctx_type_stack;
1592 type_T *list_type;
1593 type_T *item_type;
1594 type_T *expected;
1595
1596 // Caller already checked that list_type is a list.
1597 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1598 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1599 expected = list_type->tt_member;
1600 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1601 return FAIL;
1602
1603 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1604 return FAIL;
1605
1606 --stack->ga_len; // drop the argument
1607 return OK;
1608}
1609
1610/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001611 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1612 * Argument count is already checked.
1613 */
1614 static int
1615generate_BLOBAPPEND(cctx_T *cctx)
1616{
1617 garray_T *stack = &cctx->ctx_type_stack;
1618 type_T *item_type;
1619
1620 // Caller already checked that blob_type is a blob.
1621 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1622 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1623 return FAIL;
1624
1625 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1626 return FAIL;
1627
1628 --stack->ga_len; // drop the argument
1629 return OK;
1630}
1631
1632/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 * Generate an ISN_DCALL or ISN_UCALL instruction.
1634 * Return FAIL if the number of arguments is wrong.
1635 */
1636 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001637generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638{
1639 isn_T *isn;
1640 garray_T *stack = &cctx->ctx_type_stack;
1641 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001642 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643
Bram Moolenaar080457c2020-03-03 21:53:32 +01001644 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 if (argcount > regular_args && !has_varargs(ufunc))
1646 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001647 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 return FAIL;
1649 }
1650 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1651 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001652 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 return FAIL;
1654 }
1655
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001656 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001657 {
1658 int i;
1659
1660 for (i = 0; i < argcount; ++i)
1661 {
1662 type_T *expected;
1663 type_T *actual;
1664
1665 if (i < regular_args)
1666 {
1667 if (ufunc->uf_arg_types == NULL)
1668 continue;
1669 expected = ufunc->uf_arg_types[i];
1670 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001671 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1672 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001673 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001674 else
1675 expected = ufunc->uf_va_type->tt_member;
1676 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001677 if (need_type(actual, expected, -argcount + i, cctx,
1678 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001679 {
1680 arg_type_mismatch(expected, actual, i + 1);
1681 return FAIL;
1682 }
1683 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001684 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001685 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1686 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001687 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001688 }
1689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001691 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001692 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001694 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 {
1696 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1697 isn->isn_arg.dfunc.cdf_argcount = argcount;
1698 }
1699 else
1700 {
1701 // A user function may be deleted and redefined later, can't use the
1702 // ufunc pointer, need to look it up again at runtime.
1703 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1704 isn->isn_arg.ufunc.cuf_argcount = argcount;
1705 }
1706
1707 stack->ga_len -= argcount; // drop the arguments
1708 if (ga_grow(stack, 1) == FAIL)
1709 return FAIL;
1710 // add return value
1711 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1712 ++stack->ga_len;
1713
1714 return OK;
1715}
1716
1717/*
1718 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1719 */
1720 static int
1721generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1722{
1723 isn_T *isn;
1724 garray_T *stack = &cctx->ctx_type_stack;
1725
Bram Moolenaar080457c2020-03-03 21:53:32 +01001726 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1730 isn->isn_arg.ufunc.cuf_argcount = argcount;
1731
1732 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001733 if (ga_grow(stack, 1) == FAIL)
1734 return FAIL;
1735 // add return value
1736 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1737 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738
1739 return OK;
1740}
1741
1742/*
1743 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001744 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 */
1746 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001747generate_PCALL(
1748 cctx_T *cctx,
1749 int argcount,
1750 char_u *name,
1751 type_T *type,
1752 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753{
1754 isn_T *isn;
1755 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001756 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757
Bram Moolenaar080457c2020-03-03 21:53:32 +01001758 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001759
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001760 if (type->tt_type == VAR_ANY)
1761 ret_type = &t_any;
1762 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001763 {
1764 if (type->tt_argcount != -1)
1765 {
1766 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1767
1768 if (argcount < type->tt_min_argcount - varargs)
1769 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001770 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001771 return FAIL;
1772 }
1773 if (!varargs && argcount > type->tt_argcount)
1774 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001775 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001776 return FAIL;
1777 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001778 if (type->tt_args != NULL)
1779 {
1780 int i;
1781
1782 for (i = 0; i < argcount; ++i)
1783 {
1784 int offset = -argcount + i - 1;
1785 type_T *actual = ((type_T **)stack->ga_data)[
1786 stack->ga_len + offset];
1787 type_T *expected;
1788
1789 if (varargs && i >= type->tt_min_argcount - 1)
1790 expected = type->tt_args[
1791 type->tt_min_argcount - 1]->tt_member;
1792 else
1793 expected = type->tt_args[i];
1794 if (need_type(actual, expected, offset,
1795 cctx, TRUE, FALSE) == FAIL)
1796 {
1797 arg_type_mismatch(expected, actual, i + 1);
1798 return FAIL;
1799 }
1800 }
1801 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001802 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001803 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001804 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001805 else
1806 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001807 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001808 return FAIL;
1809 }
1810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1812 return FAIL;
1813 isn->isn_arg.pfunc.cpf_top = at_top;
1814 isn->isn_arg.pfunc.cpf_argcount = argcount;
1815
1816 stack->ga_len -= argcount; // drop the arguments
1817
1818 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001819 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001821 // If partial is above the arguments it must be cleared and replaced with
1822 // the return value.
1823 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1824 return FAIL;
1825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 return OK;
1827}
1828
1829/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001830 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 */
1832 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001833generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834{
1835 isn_T *isn;
1836 garray_T *stack = &cctx->ctx_type_stack;
1837 type_T *type;
1838
Bram Moolenaar080457c2020-03-03 21:53:32 +01001839 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001840 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001842 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001844 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001846 if (type->tt_type != VAR_DICT && type != &t_any)
1847 {
1848 emsg(_(e_dictreq));
1849 return FAIL;
1850 }
1851 // change dict type to dict member type
1852 if (type->tt_type == VAR_DICT)
1853 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854
1855 return OK;
1856}
1857
1858/*
1859 * Generate an ISN_ECHO instruction.
1860 */
1861 static int
1862generate_ECHO(cctx_T *cctx, int with_white, int count)
1863{
1864 isn_T *isn;
1865
Bram Moolenaar080457c2020-03-03 21:53:32 +01001866 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1868 return FAIL;
1869 isn->isn_arg.echo.echo_with_white = with_white;
1870 isn->isn_arg.echo.echo_count = count;
1871
1872 return OK;
1873}
1874
Bram Moolenaarad39c092020-02-26 18:23:43 +01001875/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001876 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001877 */
1878 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001879generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001880{
1881 isn_T *isn;
1882
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001883 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001884 return FAIL;
1885 isn->isn_arg.number = count;
1886
1887 return OK;
1888}
1889
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001890/*
1891 * Generate an ISN_PUT instruction.
1892 */
1893 static int
1894generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1895{
1896 isn_T *isn;
1897
1898 RETURN_OK_IF_SKIP(cctx);
1899 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1900 return FAIL;
1901 isn->isn_arg.put.put_regname = regname;
1902 isn->isn_arg.put.put_lnum = lnum;
1903 return OK;
1904}
1905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 static int
1907generate_EXEC(cctx_T *cctx, char_u *line)
1908{
1909 isn_T *isn;
1910
Bram Moolenaar080457c2020-03-03 21:53:32 +01001911 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1913 return FAIL;
1914 isn->isn_arg.string = vim_strsave(line);
1915 return OK;
1916}
1917
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001918 static int
1919generate_EXECCONCAT(cctx_T *cctx, int count)
1920{
1921 isn_T *isn;
1922
1923 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1924 return FAIL;
1925 isn->isn_arg.number = count;
1926 return OK;
1927}
1928
Bram Moolenaar08597872020-12-10 19:43:40 +01001929/*
1930 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1931 */
1932 static int
1933generate_RANGE(cctx_T *cctx, char_u *range)
1934{
1935 isn_T *isn;
1936 garray_T *stack = &cctx->ctx_type_stack;
1937
1938 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1939 return FAIL;
1940 isn->isn_arg.string = range;
1941
1942 if (ga_grow(stack, 1) == FAIL)
1943 return FAIL;
1944 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1945 ++stack->ga_len;
1946 return OK;
1947}
1948
Bram Moolenaar792f7862020-11-23 08:31:18 +01001949 static int
1950generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1951{
1952 isn_T *isn;
1953
1954 RETURN_OK_IF_SKIP(cctx);
1955 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1956 return FAIL;
1957 isn->isn_arg.unpack.unp_count = var_count;
1958 isn->isn_arg.unpack.unp_semicolon = semicolon;
1959 return OK;
1960}
1961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001963 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001964 */
1965 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001966generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001967{
1968 isn_T *isn;
1969
Bram Moolenaar02194d22020-10-24 23:08:38 +02001970 if (cmod->cmod_flags != 0
1971 || cmod->cmod_split != 0
1972 || cmod->cmod_verbose != 0
1973 || cmod->cmod_tab != 0
1974 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001975 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001976 cctx->ctx_has_cmdmod = TRUE;
1977
1978 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001979 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001980 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1981 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1982 return FAIL;
1983 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001984 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02001985 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001986 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001987
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001988 return OK;
1989}
1990
1991 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001992generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001993{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001994 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1995 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001996 return OK;
1997}
1998
1999/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002001 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002003 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002004reserve_local(
2005 cctx_T *cctx,
2006 char_u *name,
2007 size_t len,
2008 int isConst,
2009 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 lvar_T *lvar;
2012
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002013 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002015 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002016 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 }
2018
2019 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002020 return NULL;
2021 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002022 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002024 // Every local variable uses the next entry on the stack. We could re-use
2025 // the last ones when leaving a scope, but then variables used in a closure
2026 // might get overwritten. To keep things simple do not re-use stack
2027 // entries. This is less efficient, but memory is cheap these days.
2028 lvar->lv_idx = cctx->ctx_locals_count++;
2029
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002030 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 lvar->lv_const = isConst;
2032 lvar->lv_type = type;
2033
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002034 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035}
2036
2037/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002038 * Remove local variables above "new_top".
2039 */
2040 static void
2041unwind_locals(cctx_T *cctx, int new_top)
2042{
2043 if (cctx->ctx_locals.ga_len > new_top)
2044 {
2045 int idx;
2046 lvar_T *lvar;
2047
2048 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2049 {
2050 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2051 vim_free(lvar->lv_name);
2052 }
2053 }
2054 cctx->ctx_locals.ga_len = new_top;
2055}
2056
2057/*
2058 * Free all local variables.
2059 */
2060 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002061free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002062{
2063 unwind_locals(cctx, 0);
2064 ga_clear(&cctx->ctx_locals);
2065}
2066
2067/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 * Find "name" in script-local items of script "sid".
2069 * Returns the index in "sn_var_vals" if found.
2070 * If found but not in "sn_var_vals" returns -1.
2071 * If not found returns -2.
2072 */
2073 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002074get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075{
2076 hashtab_T *ht;
2077 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002078 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002079 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 int idx;
2081
Bram Moolenaare3d46852020-08-29 13:39:17 +02002082 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002084 if (sid == current_sctx.sc_sid)
2085 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002086 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002087
2088 if (sav == NULL)
2089 return -2;
2090 idx = sav->sav_var_vals_idx;
2091 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2092 if (check_writable && sv->sv_const)
2093 semsg(_(e_readonlyvar), name);
2094 return idx;
2095 }
2096
2097 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 ht = &SCRIPT_VARS(sid);
2099 di = find_var_in_ht(ht, 0, name, TRUE);
2100 if (di == NULL)
2101 return -2;
2102
2103 // Now find the svar_T index in sn_var_vals.
2104 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2105 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002106 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 if (sv->sv_tv == &di->di_tv)
2108 {
2109 if (check_writable && sv->sv_const)
2110 semsg(_(e_readonlyvar), name);
2111 return idx;
2112 }
2113 }
2114 return -1;
2115}
2116
2117/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002118 * Find "name" in imported items of the current script or in "cctx" if not
2119 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 */
2121 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002122find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 int idx;
2125
Bram Moolenaare3d46852020-08-29 13:39:17 +02002126 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002127 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 if (cctx != NULL)
2129 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2130 {
2131 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2132 + idx;
2133
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002134 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2135 : STRLEN(import->imp_name) == len
2136 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 return import;
2138 }
2139
Bram Moolenaarefa94442020-08-08 22:16:00 +02002140 return find_imported_in_script(name, len, current_sctx.sc_sid);
2141}
2142
2143 imported_T *
2144find_imported_in_script(char_u *name, size_t len, int sid)
2145{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002146 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002147 int idx;
2148
Bram Moolenaare3d46852020-08-29 13:39:17 +02002149 if (!SCRIPT_ID_VALID(sid))
2150 return NULL;
2151 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2153 {
2154 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2155
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002156 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2157 : STRLEN(import->imp_name) == len
2158 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 return import;
2160 }
2161 return NULL;
2162}
2163
2164/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002165 * Free all imported variables.
2166 */
2167 static void
2168free_imported(cctx_T *cctx)
2169{
2170 int idx;
2171
2172 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2173 {
2174 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2175
2176 vim_free(import->imp_name);
2177 }
2178 ga_clear(&cctx->ctx_imports);
2179}
2180
2181/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002182 * Return a pointer to the next line that isn't empty or only contains a
2183 * comment. Skips over white space.
2184 * Returns NULL if there is none.
2185 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002186 char_u *
2187peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002188{
2189 int lnum = cctx->ctx_lnum;
2190
2191 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2192 {
2193 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002194 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002195
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002196 // ignore NULLs inserted for continuation lines
2197 if (line != NULL)
2198 {
2199 p = skipwhite(line);
2200 if (*p != NUL && !vim9_comment_start(p))
2201 return p;
2202 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002203 }
2204 return NULL;
2205}
2206
2207/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002208 * Called when checking for a following operator at "arg". When the rest of
2209 * the line is empty or only a comment, peek the next line. If there is a next
2210 * line return a pointer to it and set "nextp".
2211 * Otherwise skip over white space.
2212 */
2213 static char_u *
2214may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2215{
2216 char_u *p = skipwhite(arg);
2217
2218 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002219 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002220 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002221 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002222 if (*nextp != NULL)
2223 return *nextp;
2224 }
2225 return p;
2226}
2227
2228/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002229 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002230 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002231 * Returns NULL when at the end.
2232 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002233 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002234next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002235{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002236 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002237
2238 do
2239 {
2240 ++cctx->ctx_lnum;
2241 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002242 {
2243 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002244 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002245 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002246 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002247 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002248 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002249 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002250 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002251 return line;
2252}
2253
2254/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002255 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002256 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002257 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2258 */
2259 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002260may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002261{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002262 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002263 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002264 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002265
2266 if (next == NULL)
2267 return FAIL;
2268 *arg = skipwhite(next);
2269 }
2270 return OK;
2271}
2272
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002273/*
2274 * Idem, and give an error when failed.
2275 */
2276 static int
2277may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2278{
2279 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2280 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002281 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002282 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002283 return FAIL;
2284 }
2285 return OK;
2286}
2287
2288
Bram Moolenaara5565e42020-05-09 15:44:01 +02002289// Structure passed between the compile_expr* functions to keep track of
2290// constants that have been parsed but for which no code was produced yet. If
2291// possible expressions on these constants are applied at compile time. If
2292// that is not possible, the code to push the constants needs to be generated
2293// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002294// Using 50 should be more than enough of 5 levels of ().
2295#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002296typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002297 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002298 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002299 int pp_is_const; // all generated code was constants, used for a
2300 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002301} ppconst_T;
2302
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002303static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002304static int compile_expr0(char_u **arg, cctx_T *cctx);
2305static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2306
Bram Moolenaara5565e42020-05-09 15:44:01 +02002307/*
2308 * Generate a PUSH instruction for "tv".
2309 * "tv" will be consumed or cleared.
2310 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2311 */
2312 static int
2313generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2314{
2315 if (tv != NULL)
2316 {
2317 switch (tv->v_type)
2318 {
2319 case VAR_UNKNOWN:
2320 break;
2321 case VAR_BOOL:
2322 generate_PUSHBOOL(cctx, tv->vval.v_number);
2323 break;
2324 case VAR_SPECIAL:
2325 generate_PUSHSPEC(cctx, tv->vval.v_number);
2326 break;
2327 case VAR_NUMBER:
2328 generate_PUSHNR(cctx, tv->vval.v_number);
2329 break;
2330#ifdef FEAT_FLOAT
2331 case VAR_FLOAT:
2332 generate_PUSHF(cctx, tv->vval.v_float);
2333 break;
2334#endif
2335 case VAR_BLOB:
2336 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2337 tv->vval.v_blob = NULL;
2338 break;
2339 case VAR_STRING:
2340 generate_PUSHS(cctx, tv->vval.v_string);
2341 tv->vval.v_string = NULL;
2342 break;
2343 default:
2344 iemsg("constant type not supported");
2345 clear_tv(tv);
2346 return FAIL;
2347 }
2348 tv->v_type = VAR_UNKNOWN;
2349 }
2350 return OK;
2351}
2352
2353/*
2354 * Generate code for any ppconst entries.
2355 */
2356 static int
2357generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2358{
2359 int i;
2360 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002361 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002362
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002363 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002364 for (i = 0; i < ppconst->pp_used; ++i)
2365 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2366 ret = FAIL;
2367 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002368 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002369 return ret;
2370}
2371
2372/*
2373 * Clear ppconst constants. Used when failing.
2374 */
2375 static void
2376clear_ppconst(ppconst_T *ppconst)
2377{
2378 int i;
2379
2380 for (i = 0; i < ppconst->pp_used; ++i)
2381 clear_tv(&ppconst->pp_tv[i]);
2382 ppconst->pp_used = 0;
2383}
2384
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002385/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002386 * Generate an instruction to load script-local variable "name", without the
2387 * leading "s:".
2388 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 */
2390 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002391compile_load_scriptvar(
2392 cctx_T *cctx,
2393 char_u *name, // variable NUL terminated
2394 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002395 char_u **end, // end of variable
2396 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002398 scriptitem_T *si;
2399 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 imported_T *import;
2401
Bram Moolenaare3d46852020-08-29 13:39:17 +02002402 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2403 return FAIL;
2404 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002405 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002406 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002408 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002409 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2410 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 }
2412 if (idx >= 0)
2413 {
2414 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2415
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002416 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 current_sctx.sc_sid, idx, sv->sv_type);
2418 return OK;
2419 }
2420
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002421 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 if (import != NULL)
2423 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002424 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002425 {
2426 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002427 char_u *exp_name;
2428 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002429 ufunc_T *ufunc;
2430 type_T *type;
2431
2432 // Used "import * as Name", need to lookup the member.
2433 if (*p != '.')
2434 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002435 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002436 return FAIL;
2437 }
2438 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002439 if (VIM_ISWHITE(*p))
2440 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002441 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002442 return FAIL;
2443 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002444
Bram Moolenaar1c991142020-07-04 13:15:31 +02002445 // isolate one name
2446 exp_name = p;
2447 while (eval_isnamec(*p))
2448 ++p;
2449 cc = *p;
2450 *p = NUL;
2451
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002452 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002453 *p = cc;
2454 p = skipwhite(p);
2455
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002456 // TODO: what if it is a function?
2457 if (idx < 0)
2458 return FAIL;
2459 *end = p;
2460
2461 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2462 import->imp_sid,
2463 idx,
2464 type);
2465 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002466 else if (import->imp_funcname != NULL)
2467 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002468 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002469 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2470 import->imp_sid,
2471 import->imp_var_vals_idx,
2472 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 return OK;
2474 }
2475
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002476 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002477 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 return FAIL;
2479}
2480
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002481 static int
2482generate_funcref(cctx_T *cctx, char_u *name)
2483{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002484 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002485
2486 if (ufunc == NULL)
2487 return FAIL;
2488
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002489 // Need to compile any default values to get the argument types.
2490 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2491 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2492 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002493 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002494}
2495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496/*
2497 * Compile a variable name into a load instruction.
2498 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002499 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 * When "error" is FALSE do not give an error when not found.
2501 */
2502 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002503compile_load(
2504 char_u **arg,
2505 char_u *end_arg,
2506 cctx_T *cctx,
2507 int is_expr,
2508 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509{
2510 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002511 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002512 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002514 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515
2516 if (*(*arg + 1) == ':')
2517 {
2518 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002519 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002520 {
2521 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002523 switch (**arg)
2524 {
2525 case 'g': isn_type = ISN_LOADGDICT; break;
2526 case 'w': isn_type = ISN_LOADWDICT; break;
2527 case 't': isn_type = ISN_LOADTDICT; break;
2528 case 'b': isn_type = ISN_LOADBDICT; break;
2529 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002530 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002531 goto theend;
2532 }
2533 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2534 goto theend;
2535 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 else
2538 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002539 isntype_T isn_type = ISN_DROP;
2540
2541 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2542 if (name == NULL)
2543 return FAIL;
2544
2545 switch (**arg)
2546 {
2547 case 'v': res = generate_LOADV(cctx, name, error);
2548 break;
2549 case 's': res = compile_load_scriptvar(cctx, name,
2550 NULL, NULL, error);
2551 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002552 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2553 isn_type = ISN_LOADG;
2554 else
2555 {
2556 isn_type = ISN_LOADAUTO;
2557 vim_free(name);
2558 name = vim_strnsave(*arg, end - *arg);
2559 if (name == NULL)
2560 return FAIL;
2561 }
2562 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002563 case 'w': isn_type = ISN_LOADW; break;
2564 case 't': isn_type = ISN_LOADT; break;
2565 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002566 default: // cannot happen, just in case
2567 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002568 goto theend;
2569 }
2570 if (isn_type != ISN_DROP)
2571 {
2572 // Global, Buffer-local, Window-local and Tabpage-local
2573 // variables can be defined later, thus we don't check if it
2574 // exists, give error at runtime.
2575 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 }
2578 }
2579 else
2580 {
2581 size_t len = end - *arg;
2582 int idx;
2583 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002584 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585
2586 name = vim_strnsave(*arg, end - *arg);
2587 if (name == NULL)
2588 return FAIL;
2589
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002590 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002592 if (!gen_load_outer)
2593 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 }
2595 else
2596 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002597 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002598
Bram Moolenaar709664c2020-12-12 14:33:41 +01002599 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002601 type = lvar.lv_type;
2602 idx = lvar.lv_idx;
2603 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002604 gen_load_outer = TRUE;
2605 else
2606 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 }
2608 else
2609 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002610 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002611 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002612 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002613 || find_imported(name, 0, cctx) != NULL)
2614 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002615
Bram Moolenaar0f769812020-09-12 18:32:34 +02002616 // When evaluating an expression and the name starts with an
2617 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002618 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002619 if (res == FAIL && is_expr
2620 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002621 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 }
2623 }
2624 if (gen_load)
2625 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002626 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002627 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002628 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002629 cctx->ctx_outer_used = TRUE;
2630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 }
2632
2633 *arg = end;
2634
2635theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002636 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002637 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 vim_free(name);
2639 return res;
2640}
2641
2642/*
2643 * Compile the argument expressions.
2644 * "arg" points to just after the "(" and is advanced to after the ")"
2645 */
2646 static int
2647compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2648{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002649 char_u *p = *arg;
2650 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002651 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652
Bram Moolenaare6085c52020-04-12 20:19:16 +02002653 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002655 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2656 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002657 if (*p == ')')
2658 {
2659 *arg = p + 1;
2660 return OK;
2661 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002662 if (must_end)
2663 {
2664 semsg(_(e_missing_comma_before_argument_str), p);
2665 return FAIL;
2666 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002667
Bram Moolenaara5565e42020-05-09 15:44:01 +02002668 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 return FAIL;
2670 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002671
2672 if (*p != ',' && *skipwhite(p) == ',')
2673 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002674 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002675 p = skipwhite(p);
2676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002678 {
2679 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002680 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002681 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002682 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002683 else
2684 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002685 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002686 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002688failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002689 emsg(_(e_missing_close));
2690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691}
2692
2693/*
2694 * Compile a function call: name(arg1, arg2)
2695 * "arg" points to "name", "arg + varlen" to the "(".
2696 * "argcount_init" is 1 for "value->method()"
2697 * Instructions:
2698 * EVAL arg1
2699 * EVAL arg2
2700 * BCALL / DCALL / UCALL
2701 */
2702 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002703compile_call(
2704 char_u **arg,
2705 size_t varlen,
2706 cctx_T *cctx,
2707 ppconst_T *ppconst,
2708 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709{
2710 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002711 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 int argcount = argcount_init;
2713 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002714 char_u fname_buf[FLEN_FIXED + 1];
2715 char_u *tofree = NULL;
2716 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002717 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002718 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002719 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720
Bram Moolenaara5565e42020-05-09 15:44:01 +02002721 // we can evaluate "has('name')" at compile time
2722 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2723 {
2724 char_u *s = skipwhite(*arg + varlen + 1);
2725 typval_T argvars[2];
2726
2727 argvars[0].v_type = VAR_UNKNOWN;
2728 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002729 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002730 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002731 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002732 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002733 if (*s == ')' && argvars[0].v_type == VAR_STRING
2734 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002735 {
2736 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2737
2738 *arg = s + 1;
2739 argvars[1].v_type = VAR_UNKNOWN;
2740 tv->v_type = VAR_NUMBER;
2741 tv->vval.v_number = 0;
2742 f_has(argvars, tv);
2743 clear_tv(&argvars[0]);
2744 ++ppconst->pp_used;
2745 return OK;
2746 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002747 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002748 }
2749
2750 if (generate_ppconst(cctx, ppconst) == FAIL)
2751 return FAIL;
2752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 if (varlen >= sizeof(namebuf))
2754 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002755 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 return FAIL;
2757 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002758 vim_strncpy(namebuf, *arg, varlen);
2759 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760
2761 *arg = skipwhite(*arg + varlen + 1);
2762 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002763 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764
Bram Moolenaar03290b82020-12-19 16:30:44 +01002765 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002766 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 {
2768 int idx;
2769
2770 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002771 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002773 {
2774 if (STRCMP(name, "add") == 0 && argcount == 2)
2775 {
2776 garray_T *stack = &cctx->ctx_type_stack;
2777 type_T *type = ((type_T **)stack->ga_data)[
2778 stack->ga_len - 2];
2779
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002780 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002781 if (type->tt_type == VAR_LIST)
2782 {
2783 // inline "add(list, item)" so that the type can be checked
2784 res = generate_LISTAPPEND(cctx);
2785 idx = -1;
2786 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002787 else if (type->tt_type == VAR_BLOB)
2788 {
2789 // inline "add(blob, nr)" so that the type can be checked
2790 res = generate_BLOBAPPEND(cctx);
2791 idx = -1;
2792 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002793 }
2794
2795 if (idx >= 0)
2796 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2797 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002798 else
2799 semsg(_(e_unknownfunc), namebuf);
2800 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 }
2802
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002803 // An argument or local variable can be a function reference, this
2804 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002805 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002806 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002807 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002808 // If we can find the function by name generate the right call.
2809 // Skip global functions here, a local funcref takes precedence.
2810 ufunc = find_func(name, FALSE, cctx);
2811 if (ufunc != NULL && !func_is_global(ufunc))
2812 {
2813 res = generate_CALL(cctx, ufunc, argcount);
2814 goto theend;
2815 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817
2818 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002819 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002820 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002822 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002823 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002824 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002825 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002826 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002827
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002828 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002829 goto theend;
2830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831
Bram Moolenaar0f769812020-09-12 18:32:34 +02002832 // If we can find a global function by name generate the right call.
2833 if (ufunc != NULL)
2834 {
2835 res = generate_CALL(cctx, ufunc, argcount);
2836 goto theend;
2837 }
2838
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002839 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002840 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002841 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002842 res = generate_UCALL(cctx, name, argcount);
2843 else
2844 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002845
2846theend:
2847 vim_free(tofree);
2848 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849}
2850
2851// like NAMESPACE_CHAR but with 'a' and 'l'.
2852#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2853
2854/*
2855 * Find the end of a variable or function name. Unlike find_name_end() this
2856 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002857 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 * Return a pointer to just after the name. Equal to "arg" if there is no
2859 * valid name.
2860 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002861 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002862to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863{
2864 char_u *p;
2865
2866 // Quick check for valid starting character.
2867 if (!eval_isnamec1(*arg))
2868 return arg;
2869
2870 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2871 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2872 // and can be used in slice "[n:]".
2873 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002874 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2876 break;
2877 return p;
2878}
2879
2880/*
2881 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002882 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 */
2884 char_u *
2885to_name_const_end(char_u *arg)
2886{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002887 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 typval_T rettv;
2889
2890 if (p == arg && *arg == '[')
2891 {
2892
2893 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002894 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 p = arg;
2896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 return p;
2898}
2899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900/*
2901 * parse a list: [expr, expr]
2902 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002903 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 */
2905 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002906compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907{
2908 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002909 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002911 int is_const;
2912 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002914 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002916 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002917 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002918 semsg(_(e_list_end), *arg);
2919 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002920 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002921 if (*p == ',')
2922 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002923 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002924 return FAIL;
2925 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002926 if (*p == ']')
2927 {
2928 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002929 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002930 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002931 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002932 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002933 if (!is_const)
2934 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 ++count;
2936 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002937 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002939 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2940 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002941 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002942 return FAIL;
2943 }
2944 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002945 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 p = skipwhite(p);
2947 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002948 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002950 ppconst->pp_is_const = is_all_const;
2951 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952}
2953
2954/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002955 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01002957 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 */
2959 static int
2960compile_lambda(char_u **arg, cctx_T *cctx)
2961{
Bram Moolenaare462f522020-12-27 14:43:30 +01002962 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 typval_T rettv;
2964 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002965 evalarg_T evalarg;
2966
2967 CLEAR_FIELD(evalarg);
2968 evalarg.eval_flags = EVAL_EVALUATE;
2969 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970
2971 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01002972 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
2973 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002974 {
2975 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01002976 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002977 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002978
Bram Moolenaar65c44152020-12-24 15:14:01 +01002979 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002981 ++ufunc->uf_refcount;
2982 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983
Bram Moolenaar65c44152020-12-24 15:14:01 +01002984 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002985 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002987 clear_evalarg(&evalarg, NULL);
2988
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002989 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002990 {
2991 // The return type will now be known.
2992 set_function_type(ufunc);
2993
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002994 // The function reference count will be 1. When the ISN_FUNCREF
2995 // instruction is deleted the reference count is decremented and the
2996 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002997 return generate_FUNCREF(cctx, ufunc);
2998 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002999
3000 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 return FAIL;
3002}
3003
3004/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003005 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003007 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 */
3009 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003010compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011{
3012 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003013 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 int count = 0;
3015 dict_T *d = dict_alloc();
3016 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003017 char_u *whitep = *arg;
3018 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003019 int is_const;
3020 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021
3022 if (d == NULL)
3023 return FAIL;
3024 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003025 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003027 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028
Bram Moolenaar23c55272020-06-21 16:58:13 +02003029 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003030 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003031 *arg = NULL;
3032 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003033 }
3034
3035 if (**arg == '}')
3036 break;
3037
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003038 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003040 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003042 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003043 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003044 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3047 if (isn->isn_type == ISN_PUSHS)
3048 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003049 else
3050 {
3051 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003052 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003053 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003054 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003055 return FAIL;
3056 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003057 *arg = skipwhite(*arg);
3058 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003059 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003060 emsg(_(e_missing_matching_bracket_after_dict_key));
3061 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003062 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003063 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003065 else
3066 {
3067 // {"name": value},
3068 // {'name': value},
3069 // {name: value} use "name" as a literal key
3070 key = get_literal_key(arg);
3071 if (key == NULL)
3072 return FAIL;
3073 if (generate_PUSHS(cctx, key) == FAIL)
3074 return FAIL;
3075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076
3077 // Check for duplicate keys, if using string keys.
3078 if (key != NULL)
3079 {
3080 item = dict_find(d, key, -1);
3081 if (item != NULL)
3082 {
3083 semsg(_(e_duplicate_key), key);
3084 goto failret;
3085 }
3086 item = dictitem_alloc(key);
3087 if (item != NULL)
3088 {
3089 item->di_tv.v_type = VAR_UNKNOWN;
3090 item->di_tv.v_lock = 0;
3091 if (dict_add(d, item) == FAIL)
3092 dictitem_free(item);
3093 }
3094 }
3095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 if (**arg != ':')
3097 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003098 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003099 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003100 else
3101 semsg(_(e_missing_dict_colon), *arg);
3102 return FAIL;
3103 }
3104 whitep = *arg + 1;
3105 if (!IS_WHITE_OR_NUL(*whitep))
3106 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003107 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 return FAIL;
3109 }
3110
3111 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003112 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003113 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003114 *arg = NULL;
3115 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003116 }
3117
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003118 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003120 if (!is_const)
3121 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 ++count;
3123
Bram Moolenaar2c330432020-04-13 14:41:35 +02003124 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003125 *arg = skipwhite(*arg);
3126 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003127 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003128 *arg = NULL;
3129 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 if (**arg == '}')
3132 break;
3133 if (**arg != ',')
3134 {
3135 semsg(_(e_missing_dict_comma), *arg);
3136 goto failret;
3137 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003138 if (IS_WHITE_OR_NUL(*whitep))
3139 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003140 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003141 return FAIL;
3142 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003143 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003144 if (!IS_WHITE_OR_NUL(*whitep))
3145 {
3146 semsg(_(e_white_space_required_after_str), ",");
3147 return FAIL;
3148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 *arg = skipwhite(*arg + 1);
3150 }
3151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 *arg = *arg + 1;
3153
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003154 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003155 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003156 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003157 *arg += STRLEN(*arg);
3158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003160 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 return generate_NEWDICT(cctx, count);
3162
3163failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003164 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003165 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003166 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003167 *arg = (char_u *)"";
3168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 dict_unref(d);
3170 return FAIL;
3171}
3172
3173/*
3174 * Compile "&option".
3175 */
3176 static int
3177compile_get_option(char_u **arg, cctx_T *cctx)
3178{
3179 typval_T rettv;
3180 char_u *start = *arg;
3181 int ret;
3182
3183 // parse the option and get the current value to get the type.
3184 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003185 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 if (ret == OK)
3187 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003188 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003189 char_u *name = vim_strnsave(start, *arg - start);
3190 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3191 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192
3193 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3194 vim_free(name);
3195 }
3196 clear_tv(&rettv);
3197
3198 return ret;
3199}
3200
3201/*
3202 * Compile "$VAR".
3203 */
3204 static int
3205compile_get_env(char_u **arg, cctx_T *cctx)
3206{
3207 char_u *start = *arg;
3208 int len;
3209 int ret;
3210 char_u *name;
3211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 ++*arg;
3213 len = get_env_len(arg);
3214 if (len == 0)
3215 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003216 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 return FAIL;
3218 }
3219
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003220 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 name = vim_strnsave(start, len + 1);
3222 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3223 vim_free(name);
3224 return ret;
3225}
3226
3227/*
3228 * Compile "@r".
3229 */
3230 static int
3231compile_get_register(char_u **arg, cctx_T *cctx)
3232{
3233 int ret;
3234
3235 ++*arg;
3236 if (**arg == NUL)
3237 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003238 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 return FAIL;
3240 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003241 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 {
3243 emsg_invreg(**arg);
3244 return FAIL;
3245 }
3246 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3247 ++*arg;
3248 return ret;
3249}
3250
3251/*
3252 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003253 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 */
3255 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003256apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003258 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259
3260 // this works from end to start
3261 while (p > start)
3262 {
3263 --p;
3264 if (*p == '-' || *p == '+')
3265 {
3266 // only '-' has an effect, for '+' we only check the type
3267#ifdef FEAT_FLOAT
3268 if (rettv->v_type == VAR_FLOAT)
3269 {
3270 if (*p == '-')
3271 rettv->vval.v_float = -rettv->vval.v_float;
3272 }
3273 else
3274#endif
3275 {
3276 varnumber_T val;
3277 int error = FALSE;
3278
3279 // tv_get_number_chk() accepts a string, but we don't want that
3280 // here
3281 if (check_not_string(rettv) == FAIL)
3282 return FAIL;
3283 val = tv_get_number_chk(rettv, &error);
3284 clear_tv(rettv);
3285 if (error)
3286 return FAIL;
3287 if (*p == '-')
3288 val = -val;
3289 rettv->v_type = VAR_NUMBER;
3290 rettv->vval.v_number = val;
3291 }
3292 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003293 else if (numeric_only)
3294 {
3295 ++p;
3296 break;
3297 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003298 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 {
3300 int v = tv2bool(rettv);
3301
3302 // '!' is permissive in the type.
3303 clear_tv(rettv);
3304 rettv->v_type = VAR_BOOL;
3305 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3306 }
3307 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003308 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 return OK;
3310}
3311
3312/*
3313 * Recognize v: variables that are constants and set "rettv".
3314 */
3315 static void
3316get_vim_constant(char_u **arg, typval_T *rettv)
3317{
3318 if (STRNCMP(*arg, "v:true", 6) == 0)
3319 {
3320 rettv->v_type = VAR_BOOL;
3321 rettv->vval.v_number = VVAL_TRUE;
3322 *arg += 6;
3323 }
3324 else if (STRNCMP(*arg, "v:false", 7) == 0)
3325 {
3326 rettv->v_type = VAR_BOOL;
3327 rettv->vval.v_number = VVAL_FALSE;
3328 *arg += 7;
3329 }
3330 else if (STRNCMP(*arg, "v:null", 6) == 0)
3331 {
3332 rettv->v_type = VAR_SPECIAL;
3333 rettv->vval.v_number = VVAL_NULL;
3334 *arg += 6;
3335 }
3336 else if (STRNCMP(*arg, "v:none", 6) == 0)
3337 {
3338 rettv->v_type = VAR_SPECIAL;
3339 rettv->vval.v_number = VVAL_NONE;
3340 *arg += 6;
3341 }
3342}
3343
Bram Moolenaar696ba232020-07-29 21:20:41 +02003344 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003345get_compare_type(char_u *p, int *len, int *type_is)
3346{
3347 exptype_T type = EXPR_UNKNOWN;
3348 int i;
3349
3350 switch (p[0])
3351 {
3352 case '=': if (p[1] == '=')
3353 type = EXPR_EQUAL;
3354 else if (p[1] == '~')
3355 type = EXPR_MATCH;
3356 break;
3357 case '!': if (p[1] == '=')
3358 type = EXPR_NEQUAL;
3359 else if (p[1] == '~')
3360 type = EXPR_NOMATCH;
3361 break;
3362 case '>': if (p[1] != '=')
3363 {
3364 type = EXPR_GREATER;
3365 *len = 1;
3366 }
3367 else
3368 type = EXPR_GEQUAL;
3369 break;
3370 case '<': if (p[1] != '=')
3371 {
3372 type = EXPR_SMALLER;
3373 *len = 1;
3374 }
3375 else
3376 type = EXPR_SEQUAL;
3377 break;
3378 case 'i': if (p[1] == 's')
3379 {
3380 // "is" and "isnot"; but not a prefix of a name
3381 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3382 *len = 5;
3383 i = p[*len];
3384 if (!isalnum(i) && i != '_')
3385 {
3386 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3387 *type_is = TRUE;
3388 }
3389 }
3390 break;
3391 }
3392 return type;
3393}
3394
3395/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003396 * Skip over an expression, ignoring most errors.
3397 */
3398 static void
3399skip_expr_cctx(char_u **arg, cctx_T *cctx)
3400{
3401 evalarg_T evalarg;
3402
3403 CLEAR_FIELD(evalarg);
3404 evalarg.eval_cctx = cctx;
3405 skip_expr(arg, &evalarg);
3406}
3407
3408/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003410 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 */
3412 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003413compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003415 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416
3417 // this works from end to start
3418 while (p > start)
3419 {
3420 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003421 while (VIM_ISWHITE(*p))
3422 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 if (*p == '-' || *p == '+')
3424 {
3425 int negate = *p == '-';
3426 isn_T *isn;
3427
3428 // TODO: check type
3429 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3430 {
3431 --p;
3432 if (*p == '-')
3433 negate = !negate;
3434 }
3435 // only '-' has an effect, for '+' we only check the type
3436 if (negate)
3437 isn = generate_instr(cctx, ISN_NEGATENR);
3438 else
3439 isn = generate_instr(cctx, ISN_CHECKNR);
3440 if (isn == NULL)
3441 return FAIL;
3442 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003443 else if (numeric_only)
3444 {
3445 ++p;
3446 break;
3447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 else
3449 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003450 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003452 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003454 if (p[-1] == '!')
3455 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 }
3458 if (generate_2BOOL(cctx, invert) == FAIL)
3459 return FAIL;
3460 }
3461 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003462 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 return OK;
3464}
3465
3466/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003467 * Compile "(expression)": recursive!
3468 * Return FAIL/OK.
3469 */
3470 static int
3471compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3472{
3473 int ret;
3474
3475 *arg = skipwhite(*arg + 1);
3476 if (ppconst->pp_used <= PPSIZE - 10)
3477 {
3478 ret = compile_expr1(arg, cctx, ppconst);
3479 }
3480 else
3481 {
3482 // Not enough space in ppconst, flush constants.
3483 if (generate_ppconst(cctx, ppconst) == FAIL)
3484 return FAIL;
3485 ret = compile_expr0(arg, cctx);
3486 }
3487 *arg = skipwhite(*arg);
3488 if (**arg == ')')
3489 ++*arg;
3490 else if (ret == OK)
3491 {
3492 emsg(_(e_missing_close));
3493 ret = FAIL;
3494 }
3495 return ret;
3496}
3497
3498/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003500 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 */
3502 static int
3503compile_subscript(
3504 char_u **arg,
3505 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003506 char_u *start_leader,
3507 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003508 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003510 char_u *name_start = *end_leader;
3511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 for (;;)
3513 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003514 char_u *p = skipwhite(*arg);
3515
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003516 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003517 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003518 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003519
3520 // If a following line starts with "->{" or "->X" advance to that
3521 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003522 // Also if a following line starts with ".x".
3523 if (next != NULL &&
3524 ((next[0] == '-' && next[1] == '>'
3525 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003526 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003527 {
3528 next = next_line_from_context(cctx, TRUE);
3529 if (next == NULL)
3530 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003531 *arg = next;
3532 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003533 }
3534 }
3535
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003536 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003537 // not a function call.
3538 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003540 garray_T *stack = &cctx->ctx_type_stack;
3541 type_T *type;
3542 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003544 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003545 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003546 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003549 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3550
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003551 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3553 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003554 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 return FAIL;
3556 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003557 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003559 char_u *pstart = p;
3560
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003561 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003562 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003563 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 // something->method()
3566 // Apply the '!', '-' and '+' first:
3567 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003568 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003571 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003572 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003573 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003574 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003575 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003576 int argcount = 1;
3577 char_u *expr;
3578 garray_T *stack;
3579 type_T *type;
3580
3581 // Funcref call: list->(Refs[2])(arg)
3582 // or lambda: list->((arg) => expr)(arg)
3583 // Fist compile the arguments.
3584 expr = *arg;
3585 *arg = skipwhite(*arg + 1);
3586 skip_expr_cctx(arg, cctx);
3587 *arg = skipwhite(*arg);
3588 if (**arg != ')')
3589 {
3590 semsg(_(e_missing_paren), *arg);
3591 return FAIL;
3592 }
3593 ++*arg;
3594 if (**arg != '(')
3595 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003596 if (*skipwhite(*arg) == '(')
3597 emsg(_(e_nowhitespace));
3598 else
3599 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003600 return FAIL;
3601 }
3602
3603 *arg = skipwhite(*arg + 1);
3604 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3605 return FAIL;
3606
3607 // Compile the function expression.
3608 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3609 return FAIL;
3610
3611 stack = &cctx->ctx_type_stack;
3612 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3613 if (generate_PCALL(cctx, argcount,
3614 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 return FAIL;
3616 }
3617 else
3618 {
3619 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003620 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003621 if (!eval_isnamec1(*p))
3622 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003623 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003624 return FAIL;
3625 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003626 if (ASCII_ISALPHA(*p) && p[1] == ':')
3627 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003628 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 ;
3630 if (*p != '(')
3631 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003632 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 return FAIL;
3634 }
3635 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003636 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 return FAIL;
3638 }
3639 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003640 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003642 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003643 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003644 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003645 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003646 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003647
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003648 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003649 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003650 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003651 // TODO: blob index
3652 // TODO: more arguments
3653 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003654 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003655 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003656 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003657
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003658 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003659 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003660 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003661 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003662 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003663 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003664 // missing first index is equal to zero
3665 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003666 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003667 else
3668 {
3669 if (compile_expr0(arg, cctx) == FAIL)
3670 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003671 if (**arg == ':')
3672 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003673 semsg(_(e_white_space_required_before_and_after_str_at_str),
3674 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003675 return FAIL;
3676 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003677 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3678 return FAIL;
3679 *arg = skipwhite(*arg);
3680 }
3681 if (**arg == ':')
3682 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003683 is_slice = TRUE;
3684 ++*arg;
3685 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3686 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003687 semsg(_(e_white_space_required_before_and_after_str_at_str),
3688 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003689 return FAIL;
3690 }
3691 *arg = skipwhite(*arg);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003692 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3693 return FAIL;
3694 if (**arg == ']')
3695 // missing second index is equal to end of string
3696 generate_PUSHNR(cctx, -1);
3697 else
3698 {
3699 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003700 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003701 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3702 return FAIL;
3703 *arg = skipwhite(*arg);
3704 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
3707 if (**arg != ']')
3708 {
3709 emsg(_(e_missbrac));
3710 return FAIL;
3711 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003712 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003714 // We can index a list and a dict. If we don't know the type
3715 // we can use the index value type.
3716 // TODO: If we don't know use an instruction to figure it out at
3717 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003718 typep = ((type_T **)stack->ga_data) + stack->ga_len
3719 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003720 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003721 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3722 // If the index is a string, the variable must be a Dict.
3723 if (*typep == &t_any && valtype == &t_string)
3724 vtype = VAR_DICT;
3725 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003726 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003727 if (need_type(valtype, &t_number, -1, cctx,
3728 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003729 return FAIL;
3730 if (is_slice)
3731 {
3732 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003733 if (need_type(valtype, &t_number, -2, cctx,
3734 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003735 return FAIL;
3736 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003737 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003738
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003739 if (vtype == VAR_DICT)
3740 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003741 if (is_slice)
3742 {
3743 emsg(_(e_cannot_slice_dictionary));
3744 return FAIL;
3745 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003746 if ((*typep)->tt_type == VAR_DICT)
3747 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003748 else
3749 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003750 if (need_type(*typep, &t_dict_any, -2, cctx,
3751 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003752 return FAIL;
3753 *typep = &t_any;
3754 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003755 if (may_generate_2STRING(-1, cctx) == FAIL)
3756 return FAIL;
3757 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3758 return FAIL;
3759 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003760 else if (vtype == VAR_STRING)
3761 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003762 *typep = &t_string;
3763 if ((is_slice
3764 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3765 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003766 return FAIL;
3767 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003768 else if (vtype == VAR_BLOB)
3769 {
3770 emsg("Sorry, blob index and slice not implemented yet");
3771 return FAIL;
3772 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003773 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003774 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003775 if (is_slice)
3776 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003777 if (generate_instr_drop(cctx,
3778 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3779 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003780 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003781 }
Bram Moolenaared591872020-08-15 22:14:53 +02003782 else
3783 {
3784 if ((*typep)->tt_type == VAR_LIST)
3785 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003786 if (generate_instr_drop(cctx,
3787 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3788 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003789 return FAIL;
3790 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003791 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003792 else
3793 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003794 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003795 return FAIL;
3796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003798 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003800 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003801 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003802 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003803 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003804
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003805 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003806 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003807 {
3808 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003809 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003810 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003811 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003812 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 while (eval_isnamec(*p))
3814 MB_PTR_ADV(p);
3815 if (p == *arg)
3816 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003817 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 return FAIL;
3819 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003820 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 return FAIL;
3822 *arg = p;
3823 }
3824 else
3825 break;
3826 }
3827
3828 // TODO - see handle_subscript():
3829 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3830 // Don't do this when "Func" is already a partial that was bound
3831 // explicitly (pt_auto is FALSE).
3832
3833 return OK;
3834}
3835
3836/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003837 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3838 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003840 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003841 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003842 *
3843 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 */
3845
3846/*
3847 * number number constant
3848 * 0zFFFFFFFF Blob constant
3849 * "string" string constant
3850 * 'string' literal string constant
3851 * &option-name option value
3852 * @r register contents
3853 * identifier variable value
3854 * function() function call
3855 * $VAR environment variable
3856 * (expression) nested expression
3857 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003858 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 *
3860 * Also handle:
3861 * ! in front logical NOT
3862 * - in front unary minus
3863 * + in front unary plus (ignored)
3864 * trailing (arg) funcref/partial call
3865 * trailing [] subscript in String or List
3866 * trailing .name entry in Dictionary
3867 * trailing ->name() method call
3868 */
3869 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003870compile_expr7(
3871 char_u **arg,
3872 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003873 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 char_u *start_leader, *end_leader;
3876 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003877 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003878 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003880 ppconst->pp_is_const = FALSE;
3881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 /*
3883 * Skip '!', '-' and '+' characters. They are handled later.
3884 */
3885 start_leader = *arg;
3886 while (**arg == '!' || **arg == '-' || **arg == '+')
3887 *arg = skipwhite(*arg + 1);
3888 end_leader = *arg;
3889
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003890 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 switch (**arg)
3892 {
3893 /*
3894 * Number constant.
3895 */
3896 case '0': // also for blob starting with 0z
3897 case '1':
3898 case '2':
3899 case '3':
3900 case '4':
3901 case '5':
3902 case '6':
3903 case '7':
3904 case '8':
3905 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003906 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003908 // Apply "-" and "+" just before the number now, right to
3909 // left. Matters especially when "->" follows. Stops at
3910 // '!'.
3911 if (apply_leader(rettv, TRUE,
3912 start_leader, &end_leader) == FAIL)
3913 {
3914 clear_tv(rettv);
3915 return FAIL;
3916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 break;
3918
3919 /*
3920 * String constant: "string".
3921 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003922 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 return FAIL;
3924 break;
3925
3926 /*
3927 * Literal string constant: 'str''ing'.
3928 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003929 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 return FAIL;
3931 break;
3932
3933 /*
3934 * Constant Vim variable.
3935 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003936 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 ret = NOTDONE;
3938 break;
3939
3940 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003941 * "true" constant
3942 */
3943 case 't': if (STRNCMP(*arg, "true", 4) == 0
3944 && !eval_isnamec((*arg)[4]))
3945 {
3946 *arg += 4;
3947 rettv->v_type = VAR_BOOL;
3948 rettv->vval.v_number = VVAL_TRUE;
3949 }
3950 else
3951 ret = NOTDONE;
3952 break;
3953
3954 /*
3955 * "false" constant
3956 */
3957 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3958 && !eval_isnamec((*arg)[5]))
3959 {
3960 *arg += 5;
3961 rettv->v_type = VAR_BOOL;
3962 rettv->vval.v_number = VVAL_FALSE;
3963 }
3964 else
3965 ret = NOTDONE;
3966 break;
3967
3968 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 * List: [expr, expr]
3970 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003971 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 break;
3973
3974 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 * Dictionary: {'key': val, 'key': val}
3976 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003977 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 break;
3979
3980 /*
3981 * Option value: &name
3982 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003983 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3984 return FAIL;
3985 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 break;
3987
3988 /*
3989 * Environment variable: $VAR.
3990 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003991 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3992 return FAIL;
3993 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 break;
3995
3996 /*
3997 * Register contents: @r.
3998 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003999 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4000 return FAIL;
4001 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 break;
4003 /*
4004 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004005 * lambda: (arg, arg) => expr
4006 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004008 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4009 ret = compile_lambda(arg, cctx);
4010 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004011 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 break;
4013
4014 default: ret = NOTDONE;
4015 break;
4016 }
4017 if (ret == FAIL)
4018 return FAIL;
4019
Bram Moolenaar1c747212020-05-09 18:28:34 +02004020 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004022 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 clear_tv(rettv);
4024 else
4025 // A constant expression can possibly be handled compile time,
4026 // return the value instead of generating code.
4027 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 }
4029 else if (ret == NOTDONE)
4030 {
4031 char_u *p;
4032 int r;
4033
4034 if (!eval_isnamec1(**arg))
4035 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004036 if (ends_excmd(*skipwhite(*arg)))
4037 semsg(_(e_empty_expression_str), *arg);
4038 else
4039 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 return FAIL;
4041 }
4042
4043 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004044 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004046 {
4047 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004050 {
4051 if (generate_ppconst(cctx, ppconst) == FAIL)
4052 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004053 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 if (r == FAIL)
4056 return FAIL;
4057 }
4058
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004059 // Handle following "[]", ".member", etc.
4060 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004061 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004062 ppconst) == FAIL)
4063 return FAIL;
4064 if (ppconst->pp_used > 0)
4065 {
4066 // apply the '!', '-' and '+' before the constant
4067 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004068 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004069 return FAIL;
4070 return OK;
4071 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004072 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004074 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075}
4076
4077/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004078 * Give the "white on both sides" error, taking the operator from "p[len]".
4079 */
4080 void
4081error_white_both(char_u *op, int len)
4082{
4083 char_u buf[10];
4084
4085 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004086 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004087}
4088
4089/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004090 * <type>expr7: runtime type check / conversion
4091 */
4092 static int
4093compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4094{
4095 type_T *want_type = NULL;
4096
4097 // Recognize <type>
4098 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4099 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004100 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004101 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4102 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004103 return FAIL;
4104
4105 if (**arg != '>')
4106 {
4107 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004108 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004109 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004110 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004111 return FAIL;
4112 }
4113 ++*arg;
4114 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4115 return FAIL;
4116 }
4117
4118 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4119 return FAIL;
4120
4121 if (want_type != NULL)
4122 {
4123 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004124 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004125
Bram Moolenaard1103582020-08-14 22:44:25 +02004126 generate_ppconst(cctx, ppconst);
4127 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004128 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004129 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004130 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004131 return FAIL;
4132 }
4133 }
4134
4135 return OK;
4136}
4137
4138/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 * * number multiplication
4140 * / number division
4141 * % number modulo
4142 */
4143 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004144compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145{
4146 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004147 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004148 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004150 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004151 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 return FAIL;
4153
4154 /*
4155 * Repeat computing, until no "*", "/" or "%" is following.
4156 */
4157 for (;;)
4158 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004159 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 if (*op != '*' && *op != '/' && *op != '%')
4161 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004162 if (next != NULL)
4163 {
4164 *arg = next_line_from_context(cctx, TRUE);
4165 op = skipwhite(*arg);
4166 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004168 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004170 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004171 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 }
4173 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004174 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004175 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004177 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004178 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004180
4181 if (ppconst->pp_used == ppconst_used + 2
4182 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4183 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004185 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4186 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004187 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 // both are numbers: compute the result
4190 switch (*op)
4191 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004192 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004194 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004196 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004197 break;
4198 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004199 tv1->vval.v_number = res;
4200 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004201 }
4202 else
4203 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004204 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004205 generate_two_op(cctx, op);
4206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 }
4208
4209 return OK;
4210}
4211
4212/*
4213 * + number addition
4214 * - number subtraction
4215 * .. string concatenation
4216 */
4217 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004218compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219{
4220 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004221 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
4225 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004226 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 return FAIL;
4228
4229 /*
4230 * Repeat computing, until no "+", "-" or ".." is following.
4231 */
4232 for (;;)
4233 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004234 op = may_peek_next_line(cctx, *arg, &next);
4235 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 break;
4237 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004238 if (next != NULL)
4239 {
4240 *arg = next_line_from_context(cctx, TRUE);
4241 op = skipwhite(*arg);
4242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004244 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004246 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004247 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 }
4249
4250 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004251 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004252 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004254 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 return FAIL;
4257
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004259 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004260 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4261 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4262 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4263 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004265 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4266 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004267
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004268 // concat/subtract/add constant numbers
4269 if (*op == '+')
4270 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4271 else if (*op == '-')
4272 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4273 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004274 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004275 // concatenate constant strings
4276 char_u *s1 = tv1->vval.v_string;
4277 char_u *s2 = tv2->vval.v_string;
4278 size_t len1 = STRLEN(s1);
4279
4280 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4281 if (tv1->vval.v_string == NULL)
4282 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004283 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004284 return FAIL;
4285 }
4286 mch_memmove(tv1->vval.v_string, s1, len1);
4287 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 vim_free(s1);
4289 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004290 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004291 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004294 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004295 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004296 if (*op == '.')
4297 {
4298 if (may_generate_2STRING(-2, cctx) == FAIL
4299 || may_generate_2STRING(-1, cctx) == FAIL)
4300 return FAIL;
4301 generate_instr_drop(cctx, ISN_CONCAT, 1);
4302 }
4303 else
4304 generate_two_op(cctx, op);
4305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 }
4307
4308 return OK;
4309}
4310
4311/*
4312 * expr5a == expr5b
4313 * expr5a =~ expr5b
4314 * expr5a != expr5b
4315 * expr5a !~ expr5b
4316 * expr5a > expr5b
4317 * expr5a >= expr5b
4318 * expr5a < expr5b
4319 * expr5a <= expr5b
4320 * expr5a is expr5b
4321 * expr5a isnot expr5b
4322 *
4323 * Produces instructions:
4324 * EVAL expr5a Push result of "expr5a"
4325 * EVAL expr5b Push result of "expr5b"
4326 * COMPARE one of the compare instructions
4327 */
4328 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330{
4331 exptype_T type = EXPR_UNKNOWN;
4332 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004333 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004336 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337
4338 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 return FAIL;
4341
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004342 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004343 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344
4345 /*
4346 * If there is a comparative operator, use it.
4347 */
4348 if (type != EXPR_UNKNOWN)
4349 {
4350 int ic = FALSE; // Default: do not ignore case
4351
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004352 if (next != NULL)
4353 {
4354 *arg = next_line_from_context(cctx, TRUE);
4355 p = skipwhite(*arg);
4356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 if (type_is && (p[len] == '?' || p[len] == '#'))
4358 {
4359 semsg(_(e_invexpr2), *arg);
4360 return FAIL;
4361 }
4362 // extra question mark appended: ignore case
4363 if (p[len] == '?')
4364 {
4365 ic = TRUE;
4366 ++len;
4367 }
4368 // extra '#' appended: match case (ignored)
4369 else if (p[len] == '#')
4370 ++len;
4371 // nothing appended: match case
4372
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004373 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004375 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004376 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 }
4378
4379 // get the second variable
4380 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004381 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004382 return FAIL;
4383
Bram Moolenaara5565e42020-05-09 15:44:01 +02004384 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 return FAIL;
4386
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387 if (ppconst->pp_used == ppconst_used + 2)
4388 {
4389 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4390 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4391 int ret;
4392
4393 // Both sides are a constant, compute the result now.
4394 // First check for a valid combination of types, this is more
4395 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004396 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 ret = FAIL;
4398 else
4399 {
4400 ret = typval_compare(tv1, tv2, type, ic);
4401 tv1->v_type = VAR_BOOL;
4402 tv1->vval.v_number = tv1->vval.v_number
4403 ? VVAL_TRUE : VVAL_FALSE;
4404 clear_tv(tv2);
4405 --ppconst->pp_used;
4406 }
4407 return ret;
4408 }
4409
4410 generate_ppconst(cctx, ppconst);
4411 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 }
4413
4414 return OK;
4415}
4416
Bram Moolenaar7f141552020-05-09 17:35:53 +02004417static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419/*
4420 * Compile || or &&.
4421 */
4422 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423compile_and_or(
4424 char_u **arg,
4425 cctx_T *cctx,
4426 char *op,
4427 ppconst_T *ppconst,
4428 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004430 char_u *next;
4431 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 int opchar = *op;
4433
4434 if (p[0] == opchar && p[1] == opchar)
4435 {
4436 garray_T *instr = &cctx->ctx_instr;
4437 garray_T end_ga;
4438
4439 /*
4440 * Repeat until there is no following "||" or "&&"
4441 */
4442 ga_init2(&end_ga, sizeof(int), 10);
4443 while (p[0] == opchar && p[1] == opchar)
4444 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004445 if (next != NULL)
4446 {
4447 *arg = next_line_from_context(cctx, TRUE);
4448 p = skipwhite(*arg);
4449 }
4450
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004451 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4452 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004453 semsg(_(e_white_space_required_before_and_after_str_at_str),
4454 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004455 return FAIL;
4456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004458 // TODO: use ppconst if the value is a constant and check
4459 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004460 generate_ppconst(cctx, ppconst);
4461
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004462 // Every part must evaluate to a bool.
4463 if (bool_on_stack(cctx) == FAIL)
4464 {
4465 ga_clear(&end_ga);
4466 return FAIL;
4467 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 if (ga_grow(&end_ga, 1) == FAIL)
4470 {
4471 ga_clear(&end_ga);
4472 return FAIL;
4473 }
4474 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4475 ++end_ga.ga_len;
4476 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004477 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478
4479 // eval the next expression
4480 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004481 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004482 {
4483 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004484 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004485 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004486
Bram Moolenaara5565e42020-05-09 15:44:01 +02004487 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4488 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 {
4490 ga_clear(&end_ga);
4491 return FAIL;
4492 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004493
4494 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004496 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004498 // Every part must evaluate to a bool.
4499 if (bool_on_stack(cctx) == FAIL)
4500 {
4501 ga_clear(&end_ga);
4502 return FAIL;
4503 }
4504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 // Fill in the end label in all jumps.
4506 while (end_ga.ga_len > 0)
4507 {
4508 isn_T *isn;
4509
4510 --end_ga.ga_len;
4511 isn = ((isn_T *)instr->ga_data)
4512 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4513 isn->isn_arg.jump.jump_where = instr->ga_len;
4514 }
4515 ga_clear(&end_ga);
4516 }
4517
4518 return OK;
4519}
4520
4521/*
4522 * expr4a && expr4a && expr4a logical AND
4523 *
4524 * Produces instructions:
4525 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004526 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004527 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004529 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 * EVAL expr4c Push result of "expr4c"
4531 * end:
4532 */
4533 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 int ppconst_used = ppconst->pp_used;
4537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004539 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 return FAIL;
4541
4542 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004543 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544}
4545
4546/*
4547 * expr3a || expr3b || expr3c logical OR
4548 *
4549 * Produces instructions:
4550 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004551 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004552 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004554 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 * EVAL expr3c Push result of "expr3c"
4556 * end:
4557 */
4558 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004561 int ppconst_used = ppconst->pp_used;
4562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004564 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 return FAIL;
4566
4567 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004568 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569}
4570
4571/*
4572 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004574 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 * JUMP_IF_FALSE alt jump if false
4576 * EVAL expr1a
4577 * JUMP_ALWAYS end
4578 * alt: EVAL expr1b
4579 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004580 *
4581 * Toplevel expression: expr2 ?? expr1
4582 * Produces instructions:
4583 * EVAL expr2 Push result of "expr2"
4584 * JUMP_AND_KEEP_IF_TRUE end jump if true
4585 * EVAL expr1
4586 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 */
4588 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004589compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590{
4591 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004593 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
Bram Moolenaar3988f642020-08-27 22:43:03 +02004595 // Ignore all kinds of errors when not producing code.
4596 if (cctx->ctx_skip == SKIP_YES)
4597 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004598 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004599 return OK;
4600 }
4601
Bram Moolenaar61a89812020-05-07 16:58:17 +02004602 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 return FAIL;
4605
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004606 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 if (*p == '?')
4608 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004609 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 garray_T *instr = &cctx->ctx_instr;
4611 garray_T *stack = &cctx->ctx_type_stack;
4612 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004613 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004615 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004616 int has_const_expr = FALSE;
4617 int const_value = FALSE;
4618 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004620 if (next != NULL)
4621 {
4622 *arg = next_line_from_context(cctx, TRUE);
4623 p = skipwhite(*arg);
4624 }
4625
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004626 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004627 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004628 semsg(_(e_white_space_required_before_and_after_str_at_str),
4629 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004630 return FAIL;
4631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 if (ppconst->pp_used == ppconst_used + 1)
4634 {
4635 // the condition is a constant, we know whether the ? or the :
4636 // expression is to be evaluated.
4637 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004638 if (op_falsy)
4639 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4640 else
4641 {
4642 int error = FALSE;
4643
4644 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4645 &error);
4646 if (error)
4647 return FAIL;
4648 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004649 cctx->ctx_skip = save_skip == SKIP_YES ||
4650 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4651
4652 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4653 // "left ?? right" and "left" is truthy: produce "left"
4654 generate_ppconst(cctx, ppconst);
4655 else
4656 {
4657 clear_tv(&ppconst->pp_tv[ppconst_used]);
4658 --ppconst->pp_used;
4659 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 }
4661 else
4662 {
4663 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004664 if (op_falsy)
4665 end_idx = instr->ga_len;
4666 generate_JUMP(cctx, op_falsy
4667 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4668 if (op_falsy)
4669 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671
4672 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004673 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004674 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004675 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004676 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004677 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679 if (!has_const_expr)
4680 {
4681 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004683 if (!op_falsy)
4684 {
4685 // remember the type and drop it
4686 --stack->ga_len;
4687 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004689 end_idx = instr->ga_len;
4690 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004692 // jump here from JUMP_IF_FALSE
4693 isn = ((isn_T *)instr->ga_data) + alt_idx;
4694 isn->isn_arg.jump.jump_where = instr->ga_len;
4695 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004698 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004700 // Check for the ":".
4701 p = may_peek_next_line(cctx, *arg, &next);
4702 if (*p != ':')
4703 {
4704 emsg(_(e_missing_colon));
4705 return FAIL;
4706 }
4707 if (next != NULL)
4708 {
4709 *arg = next_line_from_context(cctx, TRUE);
4710 p = skipwhite(*arg);
4711 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004712
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004713 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4714 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004715 semsg(_(e_white_space_required_before_and_after_str_at_str),
4716 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004717 return FAIL;
4718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004720 // evaluate the third expression
4721 if (has_const_expr)
4722 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004723 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004724 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004725 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004726 return FAIL;
4727 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4728 return FAIL;
4729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaara5565e42020-05-09 15:44:01 +02004731 if (!has_const_expr)
4732 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004733 type_T **typep;
4734
Bram Moolenaara5565e42020-05-09 15:44:01 +02004735 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736
Bram Moolenaara5565e42020-05-09 15:44:01 +02004737 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004738 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4739 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004740
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004741 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004742 isn = ((isn_T *)instr->ga_data) + end_idx;
4743 isn->isn_arg.jump.jump_where = instr->ga_len;
4744 }
4745
4746 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 }
4748 return OK;
4749}
4750
4751/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004752 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004753 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4754 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004755 */
4756 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004757compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758{
4759 ppconst_T ppconst;
4760
4761 CLEAR_FIELD(ppconst);
4762 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4763 {
4764 clear_ppconst(&ppconst);
4765 return FAIL;
4766 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004767 if (is_const != NULL)
4768 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004769 if (generate_ppconst(cctx, &ppconst) == FAIL)
4770 return FAIL;
4771 return OK;
4772}
4773
4774/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004775 * Toplevel expression.
4776 */
4777 static int
4778compile_expr0(char_u **arg, cctx_T *cctx)
4779{
4780 return compile_expr0_ext(arg, cctx, NULL);
4781}
4782
4783/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 * compile "return [expr]"
4785 */
4786 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004787compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788{
4789 char_u *p = arg;
4790 garray_T *stack = &cctx->ctx_type_stack;
4791 type_T *stack_type;
4792
4793 if (*p != NUL && *p != '|' && *p != '\n')
4794 {
4795 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004796 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 return NULL;
4798
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004799 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004800 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004801 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004802 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
4803 || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004804 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004805 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004806 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004807 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004808 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004809 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4810 && stack_type->tt_type != VAR_VOID
4811 && stack_type->tt_type != VAR_UNKNOWN)
4812 {
4813 emsg(_(e_returning_value_in_function_without_return_type));
4814 return NULL;
4815 }
4816 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004817 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004818 return NULL;
4819 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 }
4822 else
4823 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004824 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004825 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004826 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4827 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004829 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 return NULL;
4831 }
4832
4833 // No argument, return zero.
4834 generate_PUSHNR(cctx, 0);
4835 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004836 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 return NULL;
4838
4839 // "return val | endif" is possible
4840 return skipwhite(p);
4841}
4842
4843/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004844 * Get a line from the compilation context, compatible with exarg_T getline().
4845 * Return a pointer to the line in allocated memory.
4846 * Return NULL for end-of-file or some error.
4847 */
4848 static char_u *
4849exarg_getline(
4850 int c UNUSED,
4851 void *cookie,
4852 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004853 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004854{
4855 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004856 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004857
Bram Moolenaar66250c92020-08-20 15:02:42 +02004858 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004859 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004860 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004861 return NULL;
4862 ++cctx->ctx_lnum;
4863 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4864 // Comment lines result in NULL pointers, skip them.
4865 if (p != NULL)
4866 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004867 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004868}
4869
4870/*
4871 * Compile a nested :def command.
4872 */
4873 static char_u *
4874compile_nested_function(exarg_T *eap, cctx_T *cctx)
4875{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004876 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004877 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004878 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004879 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004881 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004882
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004883 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004884 {
4885 emsg(_(e_cannot_use_bang_with_nested_def));
4886 return NULL;
4887 }
4888
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004889 if (*name_start == '/')
4890 {
4891 name_end = skip_regexp(name_start + 1, '/', TRUE);
4892 if (*name_end == '/')
4893 ++name_end;
4894 eap->nextcmd = check_nextcmd(name_end);
4895 }
4896 if (name_end == name_start || *skipwhite(name_end) != '(')
4897 {
4898 if (!ends_excmd2(name_start, name_end))
4899 {
4900 semsg(_(e_invalid_command_str), eap->cmd);
4901 return NULL;
4902 }
4903
4904 // "def" or "def Name": list functions
4905 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4906 return NULL;
4907 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4908 }
4909
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004910 // Only g:Func() can use a namespace.
4911 if (name_start[1] == ':' && !is_global)
4912 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004913 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004914 return NULL;
4915 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004916 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4917 return NULL;
4918
Bram Moolenaar04b12692020-05-04 23:24:44 +02004919 eap->arg = name_end;
4920 eap->getline = exarg_getline;
4921 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004922 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004923 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004924 lambda_name = vim_strsave(get_lambda_name());
4925 if (lambda_name == NULL)
4926 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004927 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004928
Bram Moolenaar822ba242020-05-24 23:00:18 +02004929 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004930 {
4931 r = eap->skip ? OK : FAIL;
4932 goto theend;
4933 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004934 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004935 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004936 {
4937 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004938 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004939 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004940
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004941 if (is_global)
4942 {
4943 char_u *func_name = vim_strnsave(name_start + 2,
4944 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004945
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004946 if (func_name == NULL)
4947 r = FAIL;
4948 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004949 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004950 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004951 lambda_name = NULL;
4952 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004953 }
4954 else
4955 {
4956 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004957 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004958 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004959 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004960
Bram Moolenaareef21022020-08-01 22:16:43 +02004961 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004962 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004963 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004964 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004965 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004966
4967 // copy over the block scope IDs
4968 if (block_depth > 0)
4969 {
4970 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4971 if (ufunc->uf_block_ids != NULL)
4972 {
4973 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4974 sizeof(int) * block_depth);
4975 ufunc->uf_block_depth = block_depth;
4976 }
4977 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004978 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02004979 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004980 r = OK;
4981
4982theend:
4983 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004984 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004985}
4986
4987/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 * Return the length of an assignment operator, or zero if there isn't one.
4989 */
4990 int
4991assignment_len(char_u *p, int *heredoc)
4992{
4993 if (*p == '=')
4994 {
4995 if (p[1] == '<' && p[2] == '<')
4996 {
4997 *heredoc = TRUE;
4998 return 3;
4999 }
5000 return 1;
5001 }
5002 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5003 return 2;
5004 if (STRNCMP(p, "..=", 3) == 0)
5005 return 3;
5006 return 0;
5007}
5008
5009// words that cannot be used as a variable
5010static char *reserved[] = {
5011 "true",
5012 "false",
5013 NULL
5014};
5015
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005016typedef enum {
5017 dest_local,
5018 dest_option,
5019 dest_env,
5020 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005021 dest_buffer,
5022 dest_window,
5023 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005024 dest_vimvar,
5025 dest_script,
5026 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005027 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005028} assign_dest_T;
5029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005031 * Generate the load instruction for "name".
5032 */
5033 static void
5034generate_loadvar(
5035 cctx_T *cctx,
5036 assign_dest_T dest,
5037 char_u *name,
5038 lvar_T *lvar,
5039 type_T *type)
5040{
5041 switch (dest)
5042 {
5043 case dest_option:
5044 // TODO: check the option exists
5045 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5046 break;
5047 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005048 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5049 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5050 else
5051 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005052 break;
5053 case dest_buffer:
5054 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5055 break;
5056 case dest_window:
5057 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5058 break;
5059 case dest_tab:
5060 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5061 break;
5062 case dest_script:
5063 compile_load_scriptvar(cctx,
5064 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5065 break;
5066 case dest_env:
5067 // Include $ in the name here
5068 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5069 break;
5070 case dest_reg:
5071 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5072 break;
5073 case dest_vimvar:
5074 generate_LOADV(cctx, name + 2, TRUE);
5075 break;
5076 case dest_local:
5077 if (lvar->lv_from_outer)
5078 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5079 NULL, type);
5080 else
5081 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5082 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005083 case dest_expr:
5084 // list or dict value should already be on the stack.
5085 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005086 }
5087}
5088
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005089/*
5090 * Skip over "[expr]" or ".member".
5091 * Does not check for any errors.
5092 */
5093 static char_u *
5094skip_index(char_u *start)
5095{
5096 char_u *p = start;
5097
5098 if (*p == '[')
5099 {
5100 p = skipwhite(p + 1);
5101 (void)skip_expr(&p, NULL);
5102 p = skipwhite(p);
5103 if (*p == ']')
5104 return p + 1;
5105 return p;
5106 }
5107 // if (*p == '.')
5108 return to_name_end(p + 1, TRUE);
5109}
5110
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005111 void
5112vim9_declare_error(char_u *name)
5113{
5114 char *scope = "";
5115
5116 switch (*name)
5117 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005118 case 'g': scope = _("global"); break;
5119 case 'b': scope = _("buffer"); break;
5120 case 'w': scope = _("window"); break;
5121 case 't': scope = _("tab"); break;
5122 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005123 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005124 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005125 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005126 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005127 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005128 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005129 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005130 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005131 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005132}
5133
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005134/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005135 * For one assignment figure out the type of destination. Return it in "dest".
5136 * When not recognized "dest" is not set.
5137 * For an option "opt_flags" is set.
5138 * For a v:var "vimvaridx" is set.
5139 * "type" is set to the destination type if known, unchanted otherwise.
5140 * Return FAIL if an error message was given.
5141 */
5142 static int
5143get_var_dest(
5144 char_u *name,
5145 assign_dest_T *dest,
5146 int cmdidx,
5147 int *opt_flags,
5148 int *vimvaridx,
5149 type_T **type,
5150 cctx_T *cctx)
5151{
5152 char_u *p;
5153
5154 if (*name == '&')
5155 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005156 int cc;
5157 long numval;
5158 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005159
5160 *dest = dest_option;
5161 if (cmdidx == CMD_final || cmdidx == CMD_const)
5162 {
5163 emsg(_(e_const_option));
5164 return FAIL;
5165 }
5166 p = name;
5167 p = find_option_end(&p, opt_flags);
5168 if (p == NULL)
5169 {
5170 // cannot happen?
5171 emsg(_(e_letunexp));
5172 return FAIL;
5173 }
5174 cc = *p;
5175 *p = NUL;
5176 opt_type = get_option_value(skip_option_env_lead(name),
5177 &numval, NULL, *opt_flags);
5178 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005179 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005180 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005181 case gov_unknown:
5182 semsg(_(e_unknown_option), name);
5183 return FAIL;
5184 case gov_string:
5185 case gov_hidden_string:
5186 *type = &t_string;
5187 break;
5188 case gov_bool:
5189 case gov_hidden_bool:
5190 *type = &t_bool;
5191 break;
5192 case gov_number:
5193 case gov_hidden_number:
5194 *type = &t_number;
5195 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005196 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005197 }
5198 else if (*name == '$')
5199 {
5200 *dest = dest_env;
5201 *type = &t_string;
5202 }
5203 else if (*name == '@')
5204 {
5205 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5206 {
5207 emsg_invreg(name[1]);
5208 return FAIL;
5209 }
5210 *dest = dest_reg;
5211 *type = &t_string;
5212 }
5213 else if (STRNCMP(name, "g:", 2) == 0)
5214 {
5215 *dest = dest_global;
5216 }
5217 else if (STRNCMP(name, "b:", 2) == 0)
5218 {
5219 *dest = dest_buffer;
5220 }
5221 else if (STRNCMP(name, "w:", 2) == 0)
5222 {
5223 *dest = dest_window;
5224 }
5225 else if (STRNCMP(name, "t:", 2) == 0)
5226 {
5227 *dest = dest_tab;
5228 }
5229 else if (STRNCMP(name, "v:", 2) == 0)
5230 {
5231 typval_T *vtv;
5232 int di_flags;
5233
5234 *vimvaridx = find_vim_var(name + 2, &di_flags);
5235 if (*vimvaridx < 0)
5236 {
5237 semsg(_(e_variable_not_found_str), name);
5238 return FAIL;
5239 }
5240 // We use the current value of "sandbox" here, is that OK?
5241 if (var_check_ro(di_flags, name, FALSE))
5242 return FAIL;
5243 *dest = dest_vimvar;
5244 vtv = get_vim_var_tv(*vimvaridx);
5245 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5246 }
5247 return OK;
5248}
5249
5250/*
5251 * Generate a STORE instruction for "dest", not being "dest_local".
5252 * Return FAIL when out of memory.
5253 */
5254 static int
5255generate_store_var(
5256 cctx_T *cctx,
5257 assign_dest_T dest,
5258 int opt_flags,
5259 int vimvaridx,
5260 int scriptvar_idx,
5261 int scriptvar_sid,
5262 type_T *type,
5263 char_u *name)
5264{
5265 switch (dest)
5266 {
5267 case dest_option:
5268 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5269 opt_flags);
5270 case dest_global:
5271 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005272 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5273 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005274 case dest_buffer:
5275 // include b: with the name, easier to execute that way
5276 return generate_STORE(cctx, ISN_STOREB, 0, name);
5277 case dest_window:
5278 // include w: with the name, easier to execute that way
5279 return generate_STORE(cctx, ISN_STOREW, 0, name);
5280 case dest_tab:
5281 // include t: with the name, easier to execute that way
5282 return generate_STORE(cctx, ISN_STORET, 0, name);
5283 case dest_env:
5284 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5285 case dest_reg:
5286 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5287 case dest_vimvar:
5288 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5289 case dest_script:
5290 if (scriptvar_idx < 0)
5291 {
5292 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005293 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005294
Bram Moolenaar41d61962020-12-06 20:12:43 +01005295 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005296 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5297 scriptvar_sid, type);
5298 if (name_s != name)
5299 vim_free(name_s);
5300 return r;
5301 }
5302 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5303 scriptvar_sid, scriptvar_idx, type);
5304 case dest_local:
5305 case dest_expr:
5306 // cannot happen
5307 break;
5308 }
5309 return FAIL;
5310}
5311
5312/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005313 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005314 * "let name"
5315 * "var name = expr"
5316 * "final name = expr"
5317 * "const name = expr"
5318 * "name = expr"
5319 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005320 * Return NULL for an error.
5321 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322 */
5323 static char_u *
5324compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5325{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005326 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005328 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329 char_u *ret = NULL;
5330 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005331 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005332 int scriptvar_sid = 0;
5333 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005336 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 int oplen = 0;
5339 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005340 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005342 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005345 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5346 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 // Skip over the "var" or "[var, var]" to get to any "=".
5349 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5350 if (p == NULL)
5351 return *arg == '[' ? arg : NULL;
5352
5353 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005355 // TODO: should we allow this, and figure out type inference from list
5356 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005357 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 return NULL;
5359 }
5360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 sp = p;
5362 p = skipwhite(p);
5363 op = p;
5364 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365
5366 if (var_count > 0 && oplen == 0)
5367 // can be something like "[1, 2]->func()"
5368 return arg;
5369
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005370 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005372 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005374 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376 if (heredoc)
5377 {
5378 list_T *l;
5379 listitem_T *li;
5380
5381 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005382 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005383 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005384 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005385 if (l == NULL)
5386 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387
Bram Moolenaar078269b2020-09-21 20:35:55 +02005388 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005390 // Push each line and the create the list.
5391 FOR_ALL_LIST_ITEMS(l, li)
5392 {
5393 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5394 li->li_tv.vval.v_string = NULL;
5395 }
5396 generate_NEWLIST(cctx, l->lv_len);
5397 type = &t_list_string;
5398 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 list_free(l);
5401 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005402 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005404 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005406 char_u *wp;
5407
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 // for "[var, var] = expr" evaluate the expression here, loop over the
5409 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005410 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005411
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005412 wp = op + oplen;
5413 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005414 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005415 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416 if (compile_expr0(&p, cctx) == FAIL)
5417 return NULL;
5418 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005420 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005422 type_T *stacktype;
5423
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005424 stacktype = stack->ga_len == 0 ? &t_void
5425 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005426 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005428 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 goto theend;
5430 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005431 if (need_type(stacktype, &t_list_any, -1, cctx,
5432 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005434 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005435 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5436 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005437 if (stacktype->tt_member != NULL)
5438 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005439 }
5440 }
5441
5442 /*
5443 * Loop over variables in "[var, var] = expr".
5444 * For "var = expr" and "let var: type" this is done only once.
5445 */
5446 if (var_count > 0)
5447 var_start = skipwhite(arg + 1); // skip over the "["
5448 else
5449 var_start = arg;
5450 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5451 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005452 char_u *var_end;
5453 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005454 size_t varlen;
5455 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005456 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005457 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005458 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005459 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 lvar_T *lvar = NULL;
5461 lvar_T arg_lvar;
5462 int has_type = FALSE;
5463 int has_index = FALSE;
5464 int instr_count = -1;
5465
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005466 // "dest_end" is the end of the destination, including "[expr]" or
5467 // ".name".
5468 // "var_end" is the end of the variable/option/etc. name.
5469 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005470 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005471 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005472 else
5473 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005474 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005475 var_end = skip_option_env_lead(var_start);
5476 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005477 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005478
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005479 // "a: type" is declaring variable "a" with a type, not dict "a:".
5480 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5481 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5483 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005484
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005485 // compute the length of the destination without "[expr]" or ".name"
5486 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005487 vim_free(name);
5488 name = vim_strnsave(var_start, varlen);
5489 if (name == NULL)
5490 return NULL;
5491 if (!heredoc)
5492 type = &t_any;
5493
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005494 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005495 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005496 int declare_error = FALSE;
5497
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005498 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5499 &vimvaridx, &type, cctx) == FAIL)
5500 goto theend;
5501 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005502 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005503 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005504 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005505 }
5506 else
5507 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005508 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005509
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005510 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005511 for (idx = 0; reserved[idx] != NULL; ++idx)
5512 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005513 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005514 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005515 goto theend;
5516 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517
Bram Moolenaar709664c2020-12-12 14:33:41 +01005518
5519 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5520 lvar = &local_lvar;
5521 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005522 {
5523 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005524 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005525 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5526 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005527 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 if (is_decl)
5529 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005530 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005531 goto theend;
5532 }
5533 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005534 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005535 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005536 if (lvar != NULL)
5537 {
5538 if (is_decl)
5539 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005540 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005541 goto theend;
5542 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005544 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005546 int script_namespace = varlen > 1
5547 && STRNCMP(var_start, "s:", 2) == 0;
5548 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005549 ? script_var_exists(var_start + 2, varlen - 2,
5550 FALSE, cctx)
5551 : script_var_exists(var_start, varlen,
5552 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005553 imported_T *import =
5554 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005555
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005556 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005557 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005558 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5559
5560 if (is_decl)
5561 {
5562 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005563 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005564 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005565 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005566 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005567 name);
5568 goto theend;
5569 }
5570 else if (cctx->ctx_ufunc->uf_script_ctx_version
5571 == SCRIPT_VERSION_VIM9
5572 && script_namespace
5573 && !script_var && import == NULL)
5574 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005575 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005576 goto theend;
5577 }
5578
5579 dest = dest_script;
5580
5581 // existing script-local variables should have a type
5582 scriptvar_sid = current_sctx.sc_sid;
5583 if (import != NULL)
5584 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005585 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005586 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005587 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005588 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005589 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005590 {
5591 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5592 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005593 ((svar_T *)si->sn_var_vals.ga_data)
5594 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005595 type = sv->sv_type;
5596 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005597 }
5598 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005599 else if (check_defined(var_start, varlen, cctx) == FAIL)
5600 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005601 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005602 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005603
5604 if (declare_error)
5605 {
5606 vim9_declare_error(name);
5607 goto theend;
5608 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005609 }
5610
5611 // handle "a:name" as a name, not index "name" on "a"
5612 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005613 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614
5615 if (dest != dest_option)
5616 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005617 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618 {
5619 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005620 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005621 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005622 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005623 goto theend;
5624 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005625 p = skipwhite(var_end + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005626 type = parse_type(&p, cctx->ctx_type_list, TRUE);
5627 if (type == NULL)
5628 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005629 has_type = TRUE;
5630 }
5631 else if (lvar != NULL)
5632 type = lvar->lv_type;
5633 }
5634
5635 if (oplen == 3 && !heredoc && dest != dest_global
5636 && type->tt_type != VAR_STRING
5637 && type->tt_type != VAR_ANY)
5638 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005639 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005640 goto theend;
5641 }
5642
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005643 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005644 {
5645 if (oplen > 1 && !heredoc)
5646 {
5647 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005648 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005649 goto theend;
5650 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01005651 if (!is_decl)
5652 {
5653 semsg(_(e_unknown_variable_str), name);
5654 goto theend;
5655 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005656
5657 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005658 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5659 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005660 goto theend;
5661 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005662 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005663 if (lvar == NULL)
5664 goto theend;
5665 new_local = TRUE;
5666 }
5667
5668 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005669 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005670 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005671 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005672 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005673 if (is_decl)
5674 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005675 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005676 goto theend;
5677 }
5678
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005679 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005680 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005681 char_u *after = var_start + varlen;
5682
5683 // Only the last index is used below, if there are others
5684 // before it generate code for the expression. Thus for
5685 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5686 for (;;)
5687 {
5688 p = skip_index(after);
5689 if (*p != '[' && *p != '.')
5690 break;
5691 after = p;
5692 }
5693 if (after > var_start + varlen)
5694 {
5695 varlen = after - var_start;
5696 dest = dest_expr;
5697 // We don't know the type before evaluating the expression,
5698 // use "any" until then.
5699 type = &t_any;
5700 }
5701
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 has_index = TRUE;
5703 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005704 member_type = &t_any;
5705 else
5706 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005707 }
5708 else
5709 {
5710 semsg("Not supported yet: %s", var_start);
5711 goto theend;
5712 }
5713 }
5714 else if (lvar == &arg_lvar)
5715 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005716 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005717 goto theend;
5718 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005719 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5720 {
5721 semsg(_(e_cannot_assign_to_constant), name);
5722 goto theend;
5723 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005724
5725 if (!heredoc)
5726 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005727 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005728 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005729 if (oplen > 0 && var_count == 0)
5730 {
5731 // skip over the "=" and the expression
5732 p = skipwhite(op + oplen);
5733 compile_expr0(&p, cctx);
5734 }
5735 }
5736 else if (oplen > 0)
5737 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005738 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005739 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005740
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005741 // For "var = expr" evaluate the expression.
5742 if (var_count == 0)
5743 {
5744 int r;
5745
5746 // for "+=", "*=", "..=" etc. first load the current value
5747 if (*op != '=')
5748 {
5749 generate_loadvar(cctx, dest, name, lvar, type);
5750
5751 if (has_index)
5752 {
5753 // TODO: get member from list or dict
5754 emsg("Index with operation not supported yet");
5755 goto theend;
5756 }
5757 }
5758
5759 // Compile the expression. Temporarily hide the new local
5760 // variable here, it is not available to this expression.
5761 if (new_local)
5762 --cctx->ctx_locals.ga_len;
5763 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005764 wp = op + oplen;
5765 p = skipwhite(wp);
5766 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005767 {
5768 if (new_local)
5769 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005770 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005771 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005772 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005773 if (new_local)
5774 ++cctx->ctx_locals.ga_len;
5775 if (r == FAIL)
5776 goto theend;
5777 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005778 else if (semicolon && var_idx == var_count - 1)
5779 {
5780 // For "[var; var] = expr" get the rest of the list
5781 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5782 goto theend;
5783 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005784 else
5785 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005786 // For "[var, var] = expr" get the "var_idx" item from the
5787 // list.
5788 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005789 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005790 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005791
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005792 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005793 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005794 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005795 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005796 if ((rhs_type->tt_type == VAR_FUNC
5797 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005798 && var_wrong_func_name(name, TRUE))
5799 goto theend;
5800
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005801 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005802 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005803 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005804 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005805 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005806 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005807 }
5808 else
5809 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005810 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005811 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005812 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005813 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005814 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005815 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005816 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005817 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005818 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005819 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005820 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005821 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005822 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005823 {
5824 type_T *use_type = lvar->lv_type;
5825
Bram Moolenaar71abe482020-09-14 22:28:30 +02005826 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005827 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005828 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005829 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005830 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005831 goto theend;
5832 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005833 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005834 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005835 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005836 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005838 else if (cmdidx == CMD_final)
5839 {
5840 emsg(_(e_final_requires_a_value));
5841 goto theend;
5842 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005843 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005845 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005846 goto theend;
5847 }
5848 else if (!has_type || dest == dest_option)
5849 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005850 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005851 goto theend;
5852 }
5853 else
5854 {
5855 // variables are always initialized
5856 if (ga_grow(instr, 1) == FAIL)
5857 goto theend;
5858 switch (member_type->tt_type)
5859 {
5860 case VAR_BOOL:
5861 generate_PUSHBOOL(cctx, VVAL_FALSE);
5862 break;
5863 case VAR_FLOAT:
5864#ifdef FEAT_FLOAT
5865 generate_PUSHF(cctx, 0.0);
5866#endif
5867 break;
5868 case VAR_STRING:
5869 generate_PUSHS(cctx, NULL);
5870 break;
5871 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005872 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005873 break;
5874 case VAR_FUNC:
5875 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5876 break;
5877 case VAR_LIST:
5878 generate_NEWLIST(cctx, 0);
5879 break;
5880 case VAR_DICT:
5881 generate_NEWDICT(cctx, 0);
5882 break;
5883 case VAR_JOB:
5884 generate_PUSHJOB(cctx, NULL);
5885 break;
5886 case VAR_CHANNEL:
5887 generate_PUSHCHANNEL(cctx, NULL);
5888 break;
5889 case VAR_NUMBER:
5890 case VAR_UNKNOWN:
5891 case VAR_ANY:
5892 case VAR_PARTIAL:
5893 case VAR_VOID:
5894 case VAR_SPECIAL: // cannot happen
5895 generate_PUSHNR(cctx, 0);
5896 break;
5897 }
5898 }
5899 if (var_count == 0)
5900 end = p;
5901 }
5902
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005903 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005904 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005905 break;
5906
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005907 if (oplen > 0 && *op != '=')
5908 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005909 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005910 type_T *stacktype;
5911
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005912 if (*op == '.')
5913 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005914 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005915 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005916 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005917 if (
5918#ifdef FEAT_FLOAT
5919 // If variable is float operation with number is OK.
5920 !(expected == &t_float && stacktype == &t_number) &&
5921#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005922 need_type(stacktype, expected, -1, cctx,
5923 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005924 goto theend;
5925
5926 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005927 {
5928 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5929 goto theend;
5930 }
5931 else if (*op == '+')
5932 {
5933 if (generate_add_instr(cctx,
5934 operator_type(member_type, stacktype),
5935 member_type, stacktype) == FAIL)
5936 goto theend;
5937 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005938 else if (generate_two_op(cctx, op) == FAIL)
5939 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005942 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005943 {
Bram Moolenaard24602f2020-12-20 15:20:56 +01005944 int r;
5945 vartype_T dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005946
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005947 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005948 p = var_start + varlen;
5949 if (*p == '[')
5950 {
5951 p = skipwhite(p + 1);
5952 r = compile_expr0(&p, cctx);
5953 if (r == OK && *skipwhite(p) != ']')
5954 {
5955 // this should not happen
5956 emsg(_(e_missbrac));
5957 r = FAIL;
5958 }
5959 }
5960 else // if (*p == '.')
5961 {
5962 char_u *key_end = to_name_end(p + 1, TRUE);
5963 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5964
5965 r = generate_PUSHS(cctx, key);
5966 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005967 if (r == FAIL)
5968 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005969
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005970 if (type == &t_any)
5971 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005972 // Index on variable of unknown type: check at runtime.
5973 dest_type = VAR_ANY;
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005974 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005975 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005976 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005977 dest_type = type->tt_type;
5978 if (dest_type == VAR_DICT
5979 && may_generate_2STRING(-1, cctx) == FAIL)
5980 goto theend;
5981 if (dest_type == VAR_LIST
5982 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5983 != VAR_NUMBER)
5984 {
5985 emsg(_(e_number_exp));
5986 goto theend;
5987 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005988 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005989
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005990 // Load the dict or list. On the stack we then have:
5991 // - value
5992 // - index
5993 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005994 if (dest == dest_expr)
5995 {
5996 int c = var_start[varlen];
5997
5998 // Evaluate "ll[expr]" of "ll[expr][idx]"
5999 p = var_start;
6000 var_start[varlen] = NUL;
6001 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6002 {
6003 // this should not happen
6004 emsg(_(e_missbrac));
6005 goto theend;
6006 }
6007 var_start[varlen] = c;
6008
6009 type = stack->ga_len == 0 ? &t_void
6010 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6011 // now we can properly check the type
6012 if (type->tt_member != NULL
6013 && need_type(rhs_type, type->tt_member, -2, cctx,
6014 FALSE, FALSE) == FAIL)
6015 goto theend;
6016 }
6017 else
6018 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006019
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006020 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6021 || dest_type == VAR_ANY)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006022 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006023 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6024
6025 if (isn == NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006026 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006027 isn->isn_arg.vartype = dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006028 }
6029 else
6030 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006031 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006032 goto theend;
6033 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006034 }
6035 else
6036 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006037 if (is_decl && cmdidx == CMD_const
6038 && (dest == dest_script || dest == dest_local))
6039 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006040 generate_LOCKCONST(cctx);
6041
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006042 if (is_decl
6043 && (type->tt_type == VAR_DICT || type->tt_type == VAR_LIST)
6044 && type->tt_member != NULL
6045 && type->tt_member != &t_any
6046 && type->tt_member != &t_unknown)
6047 // Set the type in the list or dict, so that it can be checked,
6048 // also in legacy script.
6049 generate_SETTYPE(cctx, type);
6050
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006051 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006052 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006053 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6054 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
6055 goto theend;
6056 }
6057 else if (lvar != NULL)
6058 {
6059 isn_T *isn = ((isn_T *)instr->ga_data)
6060 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006061
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006062 // optimization: turn "var = 123" from ISN_PUSHNR +
6063 // ISN_STORE into ISN_STORENR
6064 if (!lvar->lv_from_outer
6065 && instr->ga_len == instr_count + 1
6066 && isn->isn_type == ISN_PUSHNR)
6067 {
6068 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006069
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006070 isn->isn_type = ISN_STORENR;
6071 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
6072 isn->isn_arg.storenr.stnr_val = val;
6073 if (stack->ga_len > 0)
6074 --stack->ga_len;
6075 }
6076 else if (lvar->lv_from_outer)
6077 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
6078 else
6079 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006080 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006081 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006082
6083 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006084 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006086
6087 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006088 if (var_count > 0 && !semicolon)
6089 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006090 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006091 goto theend;
6092 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006093
Bram Moolenaarb2097502020-07-19 17:17:02 +02006094 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095
6096theend:
6097 vim_free(name);
6098 return ret;
6099}
6100
6101/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006102 * Check if "name" can be "unlet".
6103 */
6104 int
6105check_vim9_unlet(char_u *name)
6106{
6107 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6108 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006109 // "unlet s:var" is allowed in legacy script.
6110 if (*name == 's' && !script_is_vim9())
6111 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006112 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006113 return FAIL;
6114 }
6115 return OK;
6116}
6117
6118/*
6119 * Callback passed to ex_unletlock().
6120 */
6121 static int
6122compile_unlet(
6123 lval_T *lvp,
6124 char_u *name_end,
6125 exarg_T *eap,
6126 int deep UNUSED,
6127 void *coookie)
6128{
6129 cctx_T *cctx = coookie;
6130
6131 if (lvp->ll_tv == NULL)
6132 {
6133 char_u *p = lvp->ll_name;
6134 int cc = *name_end;
6135 int ret = OK;
6136
6137 // Normal name. Only supports g:, w:, t: and b: namespaces.
6138 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006139 if (*p == '$')
6140 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6141 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006142 ret = FAIL;
6143 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006144 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006145
6146 *name_end = cc;
6147 return ret;
6148 }
6149
6150 // TODO: unlet {list}[idx]
6151 // TODO: unlet {dict}[key]
6152 emsg("Sorry, :unlet not fully implemented yet");
6153 return FAIL;
6154}
6155
6156/*
6157 * compile "unlet var", "lock var" and "unlock var"
6158 * "arg" points to "var".
6159 */
6160 static char_u *
6161compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6162{
6163 char_u *p = arg;
6164
6165 if (eap->cmdidx != CMD_unlet)
6166 {
6167 emsg("Sorry, :lock and unlock not implemented yet");
6168 return NULL;
6169 }
6170
Bram Moolenaar3862ea32021-01-01 21:05:55 +01006171 // TODO: this doesn't work for local variables
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006172 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6173 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6174}
6175
6176/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 * Compile an :import command.
6178 */
6179 static char_u *
6180compile_import(char_u *arg, cctx_T *cctx)
6181{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006182 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183}
6184
6185/*
6186 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6187 */
6188 static int
6189compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6190{
6191 garray_T *instr = &cctx->ctx_instr;
6192 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6193
6194 if (endlabel == NULL)
6195 return FAIL;
6196 endlabel->el_next = *el;
6197 *el = endlabel;
6198 endlabel->el_end_label = instr->ga_len;
6199
6200 generate_JUMP(cctx, when, 0);
6201 return OK;
6202}
6203
6204 static void
6205compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6206{
6207 garray_T *instr = &cctx->ctx_instr;
6208
6209 while (*el != NULL)
6210 {
6211 endlabel_T *cur = (*el);
6212 isn_T *isn;
6213
6214 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6215 isn->isn_arg.jump.jump_where = instr->ga_len;
6216 *el = cur->el_next;
6217 vim_free(cur);
6218 }
6219}
6220
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006221 static void
6222compile_free_jump_to_end(endlabel_T **el)
6223{
6224 while (*el != NULL)
6225 {
6226 endlabel_T *cur = (*el);
6227
6228 *el = cur->el_next;
6229 vim_free(cur);
6230 }
6231}
6232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233/*
6234 * Create a new scope and set up the generic items.
6235 */
6236 static scope_T *
6237new_scope(cctx_T *cctx, scopetype_T type)
6238{
6239 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6240
6241 if (scope == NULL)
6242 return NULL;
6243 scope->se_outer = cctx->ctx_scope;
6244 cctx->ctx_scope = scope;
6245 scope->se_type = type;
6246 scope->se_local_count = cctx->ctx_locals.ga_len;
6247 return scope;
6248}
6249
6250/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006251 * Free the current scope and go back to the outer scope.
6252 */
6253 static void
6254drop_scope(cctx_T *cctx)
6255{
6256 scope_T *scope = cctx->ctx_scope;
6257
6258 if (scope == NULL)
6259 {
6260 iemsg("calling drop_scope() without a scope");
6261 return;
6262 }
6263 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006264 switch (scope->se_type)
6265 {
6266 case IF_SCOPE:
6267 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6268 case FOR_SCOPE:
6269 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6270 case WHILE_SCOPE:
6271 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6272 case TRY_SCOPE:
6273 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6274 case NO_SCOPE:
6275 case BLOCK_SCOPE:
6276 break;
6277 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006278 vim_free(scope);
6279}
6280
6281/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 * compile "if expr"
6283 *
6284 * "if expr" Produces instructions:
6285 * EVAL expr Push result of "expr"
6286 * JUMP_IF_FALSE end
6287 * ... body ...
6288 * end:
6289 *
6290 * "if expr | else" Produces instructions:
6291 * EVAL expr Push result of "expr"
6292 * JUMP_IF_FALSE else
6293 * ... body ...
6294 * JUMP_ALWAYS end
6295 * else:
6296 * ... body ...
6297 * end:
6298 *
6299 * "if expr1 | elseif expr2 | else" Produces instructions:
6300 * EVAL expr Push result of "expr"
6301 * JUMP_IF_FALSE elseif
6302 * ... body ...
6303 * JUMP_ALWAYS end
6304 * elseif:
6305 * EVAL expr Push result of "expr"
6306 * JUMP_IF_FALSE else
6307 * ... body ...
6308 * JUMP_ALWAYS end
6309 * else:
6310 * ... body ...
6311 * end:
6312 */
6313 static char_u *
6314compile_if(char_u *arg, cctx_T *cctx)
6315{
6316 char_u *p = arg;
6317 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006318 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006320 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006321 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322
Bram Moolenaara5565e42020-05-09 15:44:01 +02006323 CLEAR_FIELD(ppconst);
6324 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006325 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006326 clear_ppconst(&ppconst);
6327 return NULL;
6328 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006329 if (cctx->ctx_skip == SKIP_YES)
6330 clear_ppconst(&ppconst);
6331 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006332 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006333 int error = FALSE;
6334 int v;
6335
Bram Moolenaara5565e42020-05-09 15:44:01 +02006336 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006337 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006338 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006339 if (error)
6340 return NULL;
6341 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006342 }
6343 else
6344 {
6345 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006346 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006347 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006348 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006349 if (bool_on_stack(cctx) == FAIL)
6350 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352
6353 scope = new_scope(cctx, IF_SCOPE);
6354 if (scope == NULL)
6355 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006356 scope->se_skip_save = skip_save;
6357 // "is_had_return" will be reset if any block does not end in :return
6358 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006360 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006361 {
6362 // "where" is set when ":elseif", "else" or ":endif" is found
6363 scope->se_u.se_if.is_if_label = instr->ga_len;
6364 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6365 }
6366 else
6367 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368
6369 return p;
6370}
6371
6372 static char_u *
6373compile_elseif(char_u *arg, cctx_T *cctx)
6374{
6375 char_u *p = arg;
6376 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006377 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378 isn_T *isn;
6379 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006380 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006381 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382
6383 if (scope == NULL || scope->se_type != IF_SCOPE)
6384 {
6385 emsg(_(e_elseif_without_if));
6386 return NULL;
6387 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006388 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006389 if (!cctx->ctx_had_return)
6390 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006391
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006392 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006393 {
6394 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006396 return NULL;
6397 // previous "if" or "elseif" jumps here
6398 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6399 isn->isn_arg.jump.jump_where = instr->ga_len;
6400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006402 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006403 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006404 if (cctx->ctx_skip == SKIP_YES)
6405 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006406 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006407 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006408 clear_ppconst(&ppconst);
6409 return NULL;
6410 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006411 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006412 if (scope->se_skip_save == SKIP_YES)
6413 clear_ppconst(&ppconst);
6414 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006415 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006416 int error = FALSE;
6417 int v;
6418
Bram Moolenaar7f141552020-05-09 17:35:53 +02006419 // The expression results in a constant.
6420 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006421 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6422 if (error)
6423 return NULL;
6424 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006425 clear_ppconst(&ppconst);
6426 scope->se_u.se_if.is_if_label = -1;
6427 }
6428 else
6429 {
6430 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006431 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006432 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006433 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006434 if (bool_on_stack(cctx) == FAIL)
6435 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006437 // "where" is set when ":elseif", "else" or ":endif" is found
6438 scope->se_u.se_if.is_if_label = instr->ga_len;
6439 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441
6442 return p;
6443}
6444
6445 static char_u *
6446compile_else(char_u *arg, cctx_T *cctx)
6447{
6448 char_u *p = arg;
6449 garray_T *instr = &cctx->ctx_instr;
6450 isn_T *isn;
6451 scope_T *scope = cctx->ctx_scope;
6452
6453 if (scope == NULL || scope->se_type != IF_SCOPE)
6454 {
6455 emsg(_(e_else_without_if));
6456 return NULL;
6457 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006458 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006459 if (!cctx->ctx_had_return)
6460 scope->se_u.se_if.is_had_return = FALSE;
6461 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462
Bram Moolenaarefd88552020-06-18 20:50:10 +02006463 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006464 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006465 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006466 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006467 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006468 if (!cctx->ctx_had_return
6469 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6470 JUMP_ALWAYS, cctx) == FAIL)
6471 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006472 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006473
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006474 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006475 {
6476 if (scope->se_u.se_if.is_if_label >= 0)
6477 {
6478 // previous "if" or "elseif" jumps here
6479 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6480 isn->isn_arg.jump.jump_where = instr->ga_len;
6481 scope->se_u.se_if.is_if_label = -1;
6482 }
6483 }
6484
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006485 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006486 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488
6489 return p;
6490}
6491
6492 static char_u *
6493compile_endif(char_u *arg, cctx_T *cctx)
6494{
6495 scope_T *scope = cctx->ctx_scope;
6496 ifscope_T *ifscope;
6497 garray_T *instr = &cctx->ctx_instr;
6498 isn_T *isn;
6499
6500 if (scope == NULL || scope->se_type != IF_SCOPE)
6501 {
6502 emsg(_(e_endif_without_if));
6503 return NULL;
6504 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006505 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006506 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006507 if (!cctx->ctx_had_return)
6508 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006510 if (scope->se_u.se_if.is_if_label >= 0)
6511 {
6512 // previous "if" or "elseif" jumps here
6513 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6514 isn->isn_arg.jump.jump_where = instr->ga_len;
6515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 // Fill in the "end" label in jumps at the end of the blocks.
6517 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006518 cctx->ctx_skip = scope->se_skip_save;
6519
6520 // If all the blocks end in :return and there is an :else then the
6521 // had_return flag is set.
6522 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006524 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 return arg;
6526}
6527
6528/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006529 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 *
6531 * Produces instructions:
6532 * PUSHNR -1
6533 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006534 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6536 * - if beyond end, jump to "end"
6537 * - otherwise get item from list and push it
6538 * STORE var Store item in "var"
6539 * ... body ...
6540 * JUMP top Jump back to repeat
6541 * end: DROP Drop the result of "expr"
6542 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006543 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6544 * UNPACK 2 Split item in 2
6545 * STORE var1 Store item in "var1"
6546 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 */
6548 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006549compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006551 char_u *arg;
6552 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006553 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006555 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006556 int var_count = 0;
6557 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 size_t varlen;
6559 garray_T *instr = &cctx->ctx_instr;
6560 garray_T *stack = &cctx->ctx_type_stack;
6561 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006562 lvar_T *loop_lvar; // loop iteration variable
6563 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006565 type_T *item_type = &t_any;
6566 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567
Bram Moolenaar792f7862020-11-23 08:31:18 +01006568 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6569 if (var_count == 0)
6570 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571
6572 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006573 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006575 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6576 return NULL;
6577 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 {
6579 emsg(_(e_missing_in));
6580 return NULL;
6581 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006582 wp = p + 2;
6583 p = skipwhite(wp);
6584 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6585 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 scope = new_scope(cctx, FOR_SCOPE);
6588 if (scope == NULL)
6589 return NULL;
6590
Bram Moolenaar792f7862020-11-23 08:31:18 +01006591 // Reserve a variable to store the loop iteration counter and initialize it
6592 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006593 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6594 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006595 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006596 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006597 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006599 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006600 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601
6602 // compile "expr", it remains on the stack until "endfor"
6603 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006604 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006605 {
6606 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006608 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006609 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006611 // Now that we know the type of "var", check that it is a list, now or at
6612 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006614 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006616 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 return NULL;
6618 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006619
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006620 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006621 {
6622 if (var_count == 1)
6623 item_type = vartype->tt_member;
6624 else if (vartype->tt_member->tt_type == VAR_LIST
6625 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6626 item_type = vartype->tt_member->tt_member;
6627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628
6629 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006630 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006631 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632
Bram Moolenaar792f7862020-11-23 08:31:18 +01006633 arg = arg_start;
6634 if (var_count > 1)
6635 {
6636 generate_UNPACK(cctx, var_count, semicolon);
6637 arg = skipwhite(arg + 1); // skip white after '['
6638
6639 // the list item is replaced by a number of items
6640 if (ga_grow(stack, var_count - 1) == FAIL)
6641 {
6642 drop_scope(cctx);
6643 return NULL;
6644 }
6645 --stack->ga_len;
6646 for (idx = 0; idx < var_count; ++idx)
6647 {
6648 ((type_T **)stack->ga_data)[stack->ga_len] =
6649 (semicolon && idx == 0) ? vartype : item_type;
6650 ++stack->ga_len;
6651 }
6652 }
6653
6654 for (idx = 0; idx < var_count; ++idx)
6655 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006656 assign_dest_T dest = dest_local;
6657 int opt_flags = 0;
6658 int vimvaridx = -1;
6659 type_T *type = &t_any;
6660
6661 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006662 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006663 name = vim_strnsave(arg, varlen);
6664 if (name == NULL)
6665 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006666
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006667 // TODO: script var not supported?
6668 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6669 &vimvaridx, &type, cctx) == FAIL)
6670 goto failed;
6671 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006672 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006673 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6674 0, 0, type, name) == FAIL)
6675 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006676 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006677 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006678 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006679 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006680 {
6681 semsg(_(e_variable_already_declared), arg);
6682 goto failed;
6683 }
6684
Bram Moolenaarea870692020-12-02 14:24:30 +01006685 if (STRNCMP(name, "s:", 2) == 0)
6686 {
6687 semsg(_(e_cannot_declare_script_variable_in_function), name);
6688 goto failed;
6689 }
6690
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006691 // Reserve a variable to store "var".
6692 // TODO: check for type
6693 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6694 if (var_lvar == NULL)
6695 // out of memory or used as an argument
6696 goto failed;
6697
6698 if (semicolon && idx == var_count - 1)
6699 var_lvar->lv_type = vartype;
6700 else
6701 var_lvar->lv_type = item_type;
6702 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6703 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006704
6705 if (*p == ',' || *p == ';')
6706 ++p;
6707 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006708 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006709 }
6710
6711 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006712
6713failed:
6714 vim_free(name);
6715 drop_scope(cctx);
6716 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717}
6718
6719/*
6720 * compile "endfor"
6721 */
6722 static char_u *
6723compile_endfor(char_u *arg, cctx_T *cctx)
6724{
6725 garray_T *instr = &cctx->ctx_instr;
6726 scope_T *scope = cctx->ctx_scope;
6727 forscope_T *forscope;
6728 isn_T *isn;
6729
6730 if (scope == NULL || scope->se_type != FOR_SCOPE)
6731 {
6732 emsg(_(e_for));
6733 return NULL;
6734 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006735 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006737 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738
6739 // At end of ":for" scope jump back to the FOR instruction.
6740 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6741
6742 // Fill in the "end" label in the FOR statement so it can jump here
6743 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6744 isn->isn_arg.forloop.for_end = instr->ga_len;
6745
6746 // Fill in the "end" label any BREAK statements
6747 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6748
6749 // Below the ":for" scope drop the "expr" list from the stack.
6750 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6751 return NULL;
6752
6753 vim_free(scope);
6754
6755 return arg;
6756}
6757
6758/*
6759 * compile "while expr"
6760 *
6761 * Produces instructions:
6762 * top: EVAL expr Push result of "expr"
6763 * JUMP_IF_FALSE end jump if false
6764 * ... body ...
6765 * JUMP top Jump back to repeat
6766 * end:
6767 *
6768 */
6769 static char_u *
6770compile_while(char_u *arg, cctx_T *cctx)
6771{
6772 char_u *p = arg;
6773 garray_T *instr = &cctx->ctx_instr;
6774 scope_T *scope;
6775
6776 scope = new_scope(cctx, WHILE_SCOPE);
6777 if (scope == NULL)
6778 return NULL;
6779
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006780 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781
6782 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006783 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 return NULL;
6785
Bram Moolenaar13106602020-10-04 16:06:05 +02006786 if (bool_on_stack(cctx) == FAIL)
6787 return FAIL;
6788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006790 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 JUMP_IF_FALSE, cctx) == FAIL)
6792 return FAIL;
6793
6794 return p;
6795}
6796
6797/*
6798 * compile "endwhile"
6799 */
6800 static char_u *
6801compile_endwhile(char_u *arg, cctx_T *cctx)
6802{
6803 scope_T *scope = cctx->ctx_scope;
6804
6805 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6806 {
6807 emsg(_(e_while));
6808 return NULL;
6809 }
6810 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006811 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812
6813 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006814 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815
6816 // Fill in the "end" label in the WHILE statement so it can jump here.
6817 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006818 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819
6820 vim_free(scope);
6821
6822 return arg;
6823}
6824
6825/*
6826 * compile "continue"
6827 */
6828 static char_u *
6829compile_continue(char_u *arg, cctx_T *cctx)
6830{
6831 scope_T *scope = cctx->ctx_scope;
6832
6833 for (;;)
6834 {
6835 if (scope == NULL)
6836 {
6837 emsg(_(e_continue));
6838 return NULL;
6839 }
6840 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6841 break;
6842 scope = scope->se_outer;
6843 }
6844
6845 // Jump back to the FOR or WHILE instruction.
6846 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006847 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6848 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 return arg;
6850}
6851
6852/*
6853 * compile "break"
6854 */
6855 static char_u *
6856compile_break(char_u *arg, cctx_T *cctx)
6857{
6858 scope_T *scope = cctx->ctx_scope;
6859 endlabel_T **el;
6860
6861 for (;;)
6862 {
6863 if (scope == NULL)
6864 {
6865 emsg(_(e_break));
6866 return NULL;
6867 }
6868 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6869 break;
6870 scope = scope->se_outer;
6871 }
6872
6873 // Jump to the end of the FOR or WHILE loop.
6874 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006875 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006877 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6879 return FAIL;
6880
6881 return arg;
6882}
6883
6884/*
6885 * compile "{" start of block
6886 */
6887 static char_u *
6888compile_block(char_u *arg, cctx_T *cctx)
6889{
6890 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6891 return NULL;
6892 return skipwhite(arg + 1);
6893}
6894
6895/*
6896 * compile end of block: drop one scope
6897 */
6898 static void
6899compile_endblock(cctx_T *cctx)
6900{
6901 scope_T *scope = cctx->ctx_scope;
6902
6903 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006904 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 vim_free(scope);
6906}
6907
6908/*
6909 * compile "try"
6910 * Creates a new scope for the try-endtry, pointing to the first catch and
6911 * finally.
6912 * Creates another scope for the "try" block itself.
6913 * TRY instruction sets up exception handling at runtime.
6914 *
6915 * "try"
6916 * TRY -> catch1, -> finally push trystack entry
6917 * ... try block
6918 * "throw {exception}"
6919 * EVAL {exception}
6920 * THROW create exception
6921 * ... try block
6922 * " catch {expr}"
6923 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006924 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925 * EVAL {expr}
6926 * MATCH
6927 * JUMP nomatch -> catch2
6928 * CATCH remove exception
6929 * ... catch block
6930 * " catch"
6931 * JUMP -> finally
6932 * catch2: CATCH remove exception
6933 * ... catch block
6934 * " finally"
6935 * finally:
6936 * ... finally block
6937 * " endtry"
6938 * ENDTRY pop trystack entry, may rethrow
6939 */
6940 static char_u *
6941compile_try(char_u *arg, cctx_T *cctx)
6942{
6943 garray_T *instr = &cctx->ctx_instr;
6944 scope_T *try_scope;
6945 scope_T *scope;
6946
6947 // scope that holds the jumps that go to catch/finally/endtry
6948 try_scope = new_scope(cctx, TRY_SCOPE);
6949 if (try_scope == NULL)
6950 return NULL;
6951
Bram Moolenaar69f70502021-01-01 16:10:46 +01006952 if (cctx->ctx_skip != SKIP_YES)
6953 {
6954 // "catch" is set when the first ":catch" is found.
6955 // "finally" is set when ":finally" or ":endtry" is found
6956 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
6957 if (generate_instr(cctx, ISN_TRY) == NULL)
6958 return NULL;
6959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960
6961 // scope for the try block itself
6962 scope = new_scope(cctx, BLOCK_SCOPE);
6963 if (scope == NULL)
6964 return NULL;
6965
6966 return arg;
6967}
6968
6969/*
6970 * compile "catch {expr}"
6971 */
6972 static char_u *
6973compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6974{
6975 scope_T *scope = cctx->ctx_scope;
6976 garray_T *instr = &cctx->ctx_instr;
6977 char_u *p;
6978 isn_T *isn;
6979
6980 // end block scope from :try or :catch
6981 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6982 compile_endblock(cctx);
6983 scope = cctx->ctx_scope;
6984
6985 // Error if not in a :try scope
6986 if (scope == NULL || scope->se_type != TRY_SCOPE)
6987 {
6988 emsg(_(e_catch));
6989 return NULL;
6990 }
6991
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006992 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006994 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995 return NULL;
6996 }
6997
Bram Moolenaar69f70502021-01-01 16:10:46 +01006998 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007000 // Jump from end of previous block to :finally or :endtry
7001 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7002 JUMP_ALWAYS, cctx) == FAIL)
7003 return NULL;
7004
7005 // End :try or :catch scope: set value in ISN_TRY instruction
7006 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7007 if (isn->isn_arg.try.try_catch == 0)
7008 isn->isn_arg.try.try_catch = instr->ga_len;
7009 if (scope->se_u.se_try.ts_catch_label != 0)
7010 {
7011 // Previous catch without match jumps here
7012 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7013 isn->isn_arg.jump.jump_where = instr->ga_len;
7014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 }
7016
7017 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007018 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007020 scope->se_u.se_try.ts_caught_all = TRUE;
7021 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 }
7023 else
7024 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007025 char_u *end;
7026 char_u *pat;
7027 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007028 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007029 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 // Push v:exception, push {expr} and MATCH
7032 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7033
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007034 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007035 if (*end != *p)
7036 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007037 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007038 vim_free(tofree);
7039 return FAIL;
7040 }
7041 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007042 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007043 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007044 len = (int)(end - tofree);
7045 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007046 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007047 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007048 if (pat == NULL)
7049 return FAIL;
7050 if (generate_PUSHS(cctx, pat) == FAIL)
7051 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7054 return NULL;
7055
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007056 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7058 return NULL;
7059 }
7060
Bram Moolenaar69f70502021-01-01 16:10:46 +01007061 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 return NULL;
7063
7064 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7065 return NULL;
7066 return p;
7067}
7068
7069 static char_u *
7070compile_finally(char_u *arg, cctx_T *cctx)
7071{
7072 scope_T *scope = cctx->ctx_scope;
7073 garray_T *instr = &cctx->ctx_instr;
7074 isn_T *isn;
7075
7076 // end block scope from :try or :catch
7077 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7078 compile_endblock(cctx);
7079 scope = cctx->ctx_scope;
7080
7081 // Error if not in a :try scope
7082 if (scope == NULL || scope->se_type != TRY_SCOPE)
7083 {
7084 emsg(_(e_finally));
7085 return NULL;
7086 }
7087
7088 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007089 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 if (isn->isn_arg.try.try_finally != 0)
7091 {
7092 emsg(_(e_finally_dup));
7093 return NULL;
7094 }
7095
7096 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007097 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098
Bram Moolenaar585fea72020-04-02 22:33:21 +02007099 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007100 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 {
7102 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007103 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007105 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106 }
7107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 // TODO: set index in ts_finally_label jumps
7109
7110 return arg;
7111}
7112
7113 static char_u *
7114compile_endtry(char_u *arg, cctx_T *cctx)
7115{
7116 scope_T *scope = cctx->ctx_scope;
7117 garray_T *instr = &cctx->ctx_instr;
7118 isn_T *isn;
7119
7120 // end block scope from :catch or :finally
7121 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7122 compile_endblock(cctx);
7123 scope = cctx->ctx_scope;
7124
7125 // Error if not in a :try scope
7126 if (scope == NULL || scope->se_type != TRY_SCOPE)
7127 {
7128 if (scope == NULL)
7129 emsg(_(e_no_endtry));
7130 else if (scope->se_type == WHILE_SCOPE)
7131 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007132 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 emsg(_(e_endfor));
7134 else
7135 emsg(_(e_endif));
7136 return NULL;
7137 }
7138
Bram Moolenaar69f70502021-01-01 16:10:46 +01007139 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007141 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7142 if (isn->isn_arg.try.try_catch == 0
7143 && isn->isn_arg.try.try_finally == 0)
7144 {
7145 emsg(_(e_missing_catch_or_finally));
7146 return NULL;
7147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148
Bram Moolenaar69f70502021-01-01 16:10:46 +01007149 // Fill in the "end" label in jumps at the end of the blocks, if not
7150 // done by ":finally".
7151 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152
Bram Moolenaar69f70502021-01-01 16:10:46 +01007153 // End :catch or :finally scope: set value in ISN_TRY instruction
7154 if (isn->isn_arg.try.try_catch == 0)
7155 isn->isn_arg.try.try_catch = instr->ga_len;
7156 if (isn->isn_arg.try.try_finally == 0)
7157 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007158
Bram Moolenaar69f70502021-01-01 16:10:46 +01007159 if (scope->se_u.se_try.ts_catch_label != 0)
7160 {
7161 // Last catch without match jumps here
7162 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7163 isn->isn_arg.jump.jump_where = instr->ga_len;
7164 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007165 }
7166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 compile_endblock(cctx);
7168
Bram Moolenaar69f70502021-01-01 16:10:46 +01007169 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170 return NULL;
7171 return arg;
7172}
7173
7174/*
7175 * compile "throw {expr}"
7176 */
7177 static char_u *
7178compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7179{
7180 char_u *p = skipwhite(arg);
7181
Bram Moolenaara5565e42020-05-09 15:44:01 +02007182 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 return NULL;
7184 if (may_generate_2STRING(-1, cctx) == FAIL)
7185 return NULL;
7186 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7187 return NULL;
7188
7189 return p;
7190}
7191
7192/*
7193 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007194 * compile "echomsg expr"
7195 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007196 * compile "execute expr"
7197 */
7198 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007199compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007200{
7201 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007202 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007203 int count = 0;
7204
7205 for (;;)
7206 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007207 if (ends_excmd2(prev, p))
7208 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007209 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007210 return NULL;
7211 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007212 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007213 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007214 }
7215
Bram Moolenaare4984292020-12-13 14:19:25 +01007216 if (count > 0)
7217 {
7218 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7219 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7220 else if (cmdidx == CMD_execute)
7221 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7222 else if (cmdidx == CMD_echomsg)
7223 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7224 else
7225 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 return p;
7228}
7229
7230/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007231 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007232 * instruction to compute it and return OK.
7233 * Otherwise return FAIL, the caller must deal with any range.
7234 */
7235 static int
7236compile_variable_range(exarg_T *eap, cctx_T *cctx)
7237{
7238 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7239 char_u *p = skipdigits(eap->cmd);
7240
7241 if (p == range_end)
7242 return FAIL;
7243 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7244}
7245
7246/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007247 * :put r
7248 * :put ={expr}
7249 */
7250 static char_u *
7251compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7252{
7253 char_u *line = arg;
7254 linenr_T lnum;
7255 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007256 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007257
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007258 eap->regname = *line;
7259
7260 if (eap->regname == '=')
7261 {
7262 char_u *p = line + 1;
7263
7264 if (compile_expr0(&p, cctx) == FAIL)
7265 return NULL;
7266 line = p;
7267 }
7268 else if (eap->regname != NUL)
7269 ++line;
7270
Bram Moolenaar08597872020-12-10 19:43:40 +01007271 if (compile_variable_range(eap, cctx) == OK)
7272 {
7273 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7274 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007275 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007276 {
7277 // Either no range or a number.
7278 // "errormsg" will not be set because the range is ADDR_LINES.
7279 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007280 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007281 return NULL;
7282 if (eap->addr_count == 0)
7283 lnum = -1;
7284 else
7285 lnum = eap->line2;
7286 if (above)
7287 --lnum;
7288 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007289
7290 generate_PUT(cctx, eap->regname, lnum);
7291 return line;
7292}
7293
7294/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007295 * A command that is not compiled, execute with legacy code.
7296 */
7297 static char_u *
7298compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7299{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007300 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007301 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007302 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007303
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007304 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007305 goto theend;
7306
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007307 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007308 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007309 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007310 int usefilter = FALSE;
7311
7312 has_expr = argt & (EX_XFILE | EX_EXPAND);
7313
7314 // If the command can be followed by a bar, find the bar and truncate
7315 // it, so that the following command can be compiled.
7316 // The '|' is overwritten with a NUL, it is put back below.
7317 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7318 && *eap->arg == '!')
7319 // :w !filter or :r !filter or :r! filter
7320 usefilter = TRUE;
7321 if ((argt & EX_TRLBAR) && !usefilter)
7322 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007323 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007324 separate_nextcmd(eap);
7325 if (eap->nextcmd != NULL)
7326 nextcmd = eap->nextcmd;
7327 }
7328 }
7329
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007330 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7331 {
7332 // expand filename in "syntax include [@group] filename"
7333 has_expr = TRUE;
7334 eap->arg = skipwhite(eap->arg + 7);
7335 if (*eap->arg == '@')
7336 eap->arg = skiptowhite(eap->arg);
7337 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007338
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007339 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7340 && STRLEN(eap->arg) > 4)
7341 {
7342 int delim = *eap->arg;
7343
7344 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL);
7345 if (*p == delim)
7346 {
7347 eap->arg = p + 1;
7348 has_expr = TRUE;
7349 }
7350 }
7351
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007352 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007353 {
7354 int count = 0;
7355 char_u *start = skipwhite(line);
7356
7357 // :cmd xxx`=expr1`yyy`=expr2`zzz
7358 // PUSHS ":cmd xxx"
7359 // eval expr1
7360 // PUSHS "yyy"
7361 // eval expr2
7362 // PUSHS "zzz"
7363 // EXECCONCAT 5
7364 for (;;)
7365 {
7366 if (p > start)
7367 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007368 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007369 ++count;
7370 }
7371 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007372 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007373 return NULL;
7374 may_generate_2STRING(-1, cctx);
7375 ++count;
7376 p = skipwhite(p);
7377 if (*p != '`')
7378 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007379 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007380 return NULL;
7381 }
7382 start = p + 1;
7383
7384 p = (char_u *)strstr((char *)start, "`=");
7385 if (p == NULL)
7386 {
7387 if (*skipwhite(start) != NUL)
7388 {
7389 generate_PUSHS(cctx, vim_strsave(start));
7390 ++count;
7391 }
7392 break;
7393 }
7394 }
7395 generate_EXECCONCAT(cctx, count);
7396 }
7397 else
7398 generate_EXEC(cctx, line);
7399
7400theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007401 if (*nextcmd != NUL)
7402 {
7403 // the parser expects a pointer to the bar, put it back
7404 --nextcmd;
7405 *nextcmd = '|';
7406 }
7407
7408 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007409}
7410
7411/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007412 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007413 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007414 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007415 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007416add_def_function(ufunc_T *ufunc)
7417{
7418 dfunc_T *dfunc;
7419
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007420 if (def_functions.ga_len == 0)
7421 {
7422 // The first position is not used, so that a zero uf_dfunc_idx means it
7423 // wasn't set.
7424 if (ga_grow(&def_functions, 1) == FAIL)
7425 return FAIL;
7426 ++def_functions.ga_len;
7427 }
7428
Bram Moolenaar09689a02020-05-09 22:50:08 +02007429 // Add the function to "def_functions".
7430 if (ga_grow(&def_functions, 1) == FAIL)
7431 return FAIL;
7432 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7433 CLEAR_POINTER(dfunc);
7434 dfunc->df_idx = def_functions.ga_len;
7435 ufunc->uf_dfunc_idx = dfunc->df_idx;
7436 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007437 dfunc->df_name = vim_strsave(ufunc->uf_name);
7438 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007439 ++def_functions.ga_len;
7440 return OK;
7441}
7442
7443/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 * After ex_function() has collected all the function lines: parse and compile
7445 * the lines into instructions.
7446 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007447 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7448 * the return statement (used for lambda). When uf_ret_type is already set
7449 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007450 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007451 * This can be used recursively through compile_lambda(), which may reallocate
7452 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007453 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007455 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007456compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 char_u *line = NULL;
7459 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 cctx_T cctx;
7462 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007463 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 int ret = FAIL;
7465 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007466 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007467 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007468 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007470 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007471 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007472 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007474 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7475 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007476 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007478 else
7479 {
7480 if (add_def_function(ufunc) == FAIL)
7481 return FAIL;
7482 new_def_function = TRUE;
7483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484
Bram Moolenaar985116a2020-07-12 17:31:09 +02007485 ufunc->uf_def_status = UF_COMPILING;
7486
Bram Moolenaara80faa82020-04-12 19:37:17 +02007487 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 cctx.ctx_ufunc = ufunc;
7489 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007490 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7492 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7493 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7494 cctx.ctx_type_list = &ufunc->uf_type_list;
7495 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7496 instr = &cctx.ctx_instr;
7497
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007498 // Set the context to the function, it may be compiled when called from
7499 // another script. Set the script version to the most modern one.
7500 // The line number will be set in next_line_from_context().
7501 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7503
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007504 // Make sure error messages are OK.
7505 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7506 if (do_estack_push)
7507 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007508 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007509
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007510 if (ufunc->uf_def_args.ga_len > 0)
7511 {
7512 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007513 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007514 int i;
7515 char_u *arg;
7516 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007517 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007518
7519 // Produce instructions for the default values of optional arguments.
7520 // Store the instruction index in uf_def_arg_idx[] so that we know
7521 // where to start when the function is called, depending on the number
7522 // of arguments.
7523 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7524 if (ufunc->uf_def_arg_idx == NULL)
7525 goto erret;
7526 for (i = 0; i < count; ++i)
7527 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007528 garray_T *stack = &cctx.ctx_type_stack;
7529 type_T *val_type;
7530 int arg_idx = first_def_arg + i;
7531
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007532 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7533 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007534 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007535 goto erret;
7536
7537 // If no type specified use the type of the default value.
7538 // Otherwise check that the default value type matches the
7539 // specified type.
7540 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7541 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007542 {
7543 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007544 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007545 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007546 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7547 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007548 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007549
7550 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007551 goto erret;
7552 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007553 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007554
7555 if (did_set_arg_type)
7556 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007557 }
7558
7559 /*
7560 * Loop over all the lines of the function and generate instructions.
7561 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 for (;;)
7563 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007564 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007565 int starts_with_colon = FALSE;
7566 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007567 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007568
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007569 // Bail out on the first error to avoid a flood of errors and report
7570 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007571 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007572 goto erret;
7573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 if (line != NULL && *line == '|')
7575 // the line continues after a '|'
7576 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007577 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007578 && !(*line == '#' && (line == cctx.ctx_line_start
7579 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007581 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 goto erret;
7583 }
7584 else
7585 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007586 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007587 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007588 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 }
7591
Bram Moolenaara80faa82020-04-12 19:37:17 +02007592 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 ea.cmdlinep = &line;
7594 ea.cmd = skipwhite(line);
7595
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007596 // Some things can be recognized by the first character.
7597 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007599 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007600 // "#" starts a comment
7601 line = (char_u *)"";
7602 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007604 case '}':
7605 {
7606 // "}" ends a block scope
7607 scopetype_T stype = cctx.ctx_scope == NULL
7608 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007610 if (stype == BLOCK_SCOPE)
7611 {
7612 compile_endblock(&cctx);
7613 line = ea.cmd;
7614 }
7615 else
7616 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007617 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007618 goto erret;
7619 }
7620 if (line != NULL)
7621 line = skipwhite(ea.cmd + 1);
7622 continue;
7623 }
7624
7625 case '{':
7626 // "{" starts a block scope
7627 // "{'a': 1}->func() is something else
7628 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7629 {
7630 line = compile_block(ea.cmd, &cctx);
7631 continue;
7632 }
7633 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 }
7635
7636 /*
7637 * COMMAND MODIFIERS
7638 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007639 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007640 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7641 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 {
7643 if (errormsg != NULL)
7644 goto erret;
7645 // empty line or comment
7646 line = (char_u *)"";
7647 continue;
7648 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007649 generate_cmdmods(&cctx, &local_cmdmod);
7650 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007652 // Check if there was a colon after the last command modifier or before
7653 // the current position.
7654 for (p = ea.cmd; p >= line; --p)
7655 {
7656 if (*p == ':')
7657 starts_with_colon = TRUE;
7658 if (p < ea.cmd && !VIM_ISWHITE(*p))
7659 break;
7660 }
7661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007663 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007664 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007665 {
7666 if (*ea.cmd == '(')
7667 // not for "call()"
7668 ea.cmd = p;
7669 else
7670 ea.cmd = skipwhite(ea.cmd);
7671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007673 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007675 char_u *pskip;
7676
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007677 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007678 // find what follows.
7679 // Skip over "var.member", "var[idx]" and the like.
7680 // Also "&opt = val", "$ENV = val" and "@r = val".
7681 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007682 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007683 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007684 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007686 char_u *var_end;
7687 int oplen;
7688 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689
Bram Moolenaar65821722020-08-02 18:58:54 +02007690 if (ea.cmd[0] == '@')
7691 var_end = ea.cmd + 2;
7692 else
7693 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007694 FNE_CHECK_START | FNE_INCL_BR);
7695 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007696 if (oplen > 0)
7697 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007698 size_t len = p - ea.cmd;
7699
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007700 // Recognize an assignment if we recognize the variable
7701 // name:
7702 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007703 // "local = expr" where "local" is a local var.
7704 // "script = expr" where "script" is a script-local var.
7705 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007706 // "&opt = expr"
7707 // "$ENV = expr"
7708 // "@r = expr"
7709 if (*ea.cmd == '&'
7710 || *ea.cmd == '$'
7711 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007712 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007713 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007714 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007715 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007716 || script_var_exists(ea.cmd, len,
7717 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007718 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007719 {
7720 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007721 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007722 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007723 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 }
7726 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007727
7728 if (*ea.cmd == '[')
7729 {
7730 // [var, var] = expr
7731 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7732 if (line == NULL)
7733 goto erret;
7734 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007735 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 }
7738
7739 /*
7740 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007741 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007743 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007744 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007745 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007746 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007747 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007748 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007749 if (!starts_with_colon)
7750 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01007751 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007752 goto erret;
7753 }
7754 if (ends_excmd2(line, ea.cmd))
7755 {
7756 // A range without a command: jump to the line.
7757 // TODO: compile to a more efficient command, possibly
7758 // calling parse_cmd_address().
7759 ea.cmdidx = CMD_SIZE;
7760 line = compile_exec(line, &ea, &cctx);
7761 goto nextline;
7762 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007763 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007764 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007765 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007766 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007767 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768
7769 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7770 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007771 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007772 {
7773 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007774 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007775 }
7776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007777 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007778 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01007780 // CMD_var cannot happen, compile_assignment() above would be
7781 // used. Most likely an assignment to a non-existing variable.
7782 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007783 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 }
7786
Bram Moolenaar3988f642020-08-27 22:43:03 +02007787 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007788 && ea.cmdidx != CMD_elseif
7789 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007790 && ea.cmdidx != CMD_endif
7791 && ea.cmdidx != CMD_endfor
7792 && ea.cmdidx != CMD_endwhile
7793 && ea.cmdidx != CMD_catch
7794 && ea.cmdidx != CMD_finally
7795 && ea.cmdidx != CMD_endtry)
7796 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007797 emsg(_(e_unreachable_code_after_return));
7798 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007799 }
7800
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007801 p = skipwhite(p);
7802 if (ea.cmdidx != CMD_SIZE
7803 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7804 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007805 if (ea.cmdidx >= 0)
7806 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007807 if ((ea.argt & EX_BANG) && *p == '!')
7808 {
7809 ea.forceit = TRUE;
7810 p = skipwhite(p + 1);
7811 }
7812 }
7813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 switch (ea.cmdidx)
7815 {
7816 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007817 ea.arg = p;
7818 line = compile_nested_function(&ea, &cctx);
7819 break;
7820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007822 // TODO: should we allow this, e.g. to declare a global
7823 // function?
7824 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 goto erret;
7826
7827 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007828 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007829 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 break;
7831
7832 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007833 emsg(_(e_cannot_use_let_in_vim9_script));
7834 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007835 case CMD_var:
7836 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837 case CMD_const:
7838 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007839 if (line == p)
7840 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 break;
7842
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007843 case CMD_unlet:
7844 case CMD_unlockvar:
7845 case CMD_lockvar:
7846 line = compile_unletlock(p, &ea, &cctx);
7847 break;
7848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 case CMD_import:
7850 line = compile_import(p, &cctx);
7851 break;
7852
7853 case CMD_if:
7854 line = compile_if(p, &cctx);
7855 break;
7856 case CMD_elseif:
7857 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007858 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007859 break;
7860 case CMD_else:
7861 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007862 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 break;
7864 case CMD_endif:
7865 line = compile_endif(p, &cctx);
7866 break;
7867
7868 case CMD_while:
7869 line = compile_while(p, &cctx);
7870 break;
7871 case CMD_endwhile:
7872 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007873 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 break;
7875
7876 case CMD_for:
7877 line = compile_for(p, &cctx);
7878 break;
7879 case CMD_endfor:
7880 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007881 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 break;
7883 case CMD_continue:
7884 line = compile_continue(p, &cctx);
7885 break;
7886 case CMD_break:
7887 line = compile_break(p, &cctx);
7888 break;
7889
7890 case CMD_try:
7891 line = compile_try(p, &cctx);
7892 break;
7893 case CMD_catch:
7894 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007895 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 break;
7897 case CMD_finally:
7898 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007899 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 break;
7901 case CMD_endtry:
7902 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007903 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904 break;
7905 case CMD_throw:
7906 line = compile_throw(p, &cctx);
7907 break;
7908
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007909 case CMD_eval:
7910 if (compile_expr0(&p, &cctx) == FAIL)
7911 goto erret;
7912
Bram Moolenaar3988f642020-08-27 22:43:03 +02007913 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007914 generate_instr_drop(&cctx, ISN_DROP, 1);
7915
7916 line = skipwhite(p);
7917 break;
7918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007920 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007921 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007922 case CMD_echomsg:
7923 case CMD_echoerr:
7924 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007925 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007927 case CMD_put:
7928 ea.cmd = cmd;
7929 line = compile_put(p, &ea, &cctx);
7930 break;
7931
Bram Moolenaar3988f642020-08-27 22:43:03 +02007932 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007933
Bram Moolenaarae616492020-07-28 20:07:27 +02007934 case CMD_append:
7935 case CMD_change:
7936 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007937 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007938 case CMD_xit:
7939 not_in_vim9(&ea);
7940 goto erret;
7941
Bram Moolenaar002262f2020-07-08 17:47:57 +02007942 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007943 if (cctx.ctx_skip != SKIP_YES)
7944 {
7945 semsg(_(e_invalid_command_str), ea.cmd);
7946 goto erret;
7947 }
7948 // We don't check for a next command here.
7949 line = (char_u *)"";
7950 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007953 if (cctx.ctx_skip == SKIP_YES)
7954 {
7955 // We don't check for a next command here.
7956 line = (char_u *)"";
7957 }
7958 else
7959 {
7960 // Not recognized, execute with do_cmdline_cmd().
7961 ea.arg = p;
7962 line = compile_exec(line, &ea, &cctx);
7963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007964 break;
7965 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007966nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967 if (line == NULL)
7968 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007969 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007970
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007971 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007972 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007974 if (cctx.ctx_type_stack.ga_len < 0)
7975 {
7976 iemsg("Type stack underflow");
7977 goto erret;
7978 }
7979 }
7980
7981 if (cctx.ctx_scope != NULL)
7982 {
7983 if (cctx.ctx_scope->se_type == IF_SCOPE)
7984 emsg(_(e_endif));
7985 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7986 emsg(_(e_endwhile));
7987 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7988 emsg(_(e_endfor));
7989 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007990 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991 goto erret;
7992 }
7993
Bram Moolenaarefd88552020-06-18 20:50:10 +02007994 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995 {
7996 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7997 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007998 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007999 goto erret;
8000 }
8001
8002 // Return zero if there is no return at the end.
8003 generate_PUSHNR(&cctx, 0);
8004 generate_instr(&cctx, ISN_RETURN);
8005 }
8006
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008007 {
8008 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8009 + ufunc->uf_dfunc_idx;
8010 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008011 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008012 dfunc->df_instr = instr->ga_data;
8013 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008014 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008015 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008016 if (cctx.ctx_outer_used)
8017 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008018 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008020
8021 ret = OK;
8022
8023erret:
8024 if (ret == FAIL)
8025 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008026 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8028 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008029
8030 for (idx = 0; idx < instr->ga_len; ++idx)
8031 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008033 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008034
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008035 // If using the last entry in the table and it was added above, we
8036 // might as well remove it.
8037 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008038 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008039 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008040 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008041 ufunc->uf_dfunc_idx = 0;
8042 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008043 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008044
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008045 while (cctx.ctx_scope != NULL)
8046 drop_scope(&cctx);
8047
Bram Moolenaar20431c92020-03-20 18:39:46 +01008048 // Don't execute this function body.
8049 ga_clear_strings(&ufunc->uf_lines);
8050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051 if (errormsg != NULL)
8052 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008053 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008054 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 }
8056
8057 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008058 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008059 if (do_estack_push)
8060 estack_pop();
8061
Bram Moolenaar20431c92020-03-20 18:39:46 +01008062 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008063 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008065 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066}
8067
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008068 void
8069set_function_type(ufunc_T *ufunc)
8070{
8071 int varargs = ufunc->uf_va_name != NULL;
8072 int argcount = ufunc->uf_args.ga_len;
8073
8074 // Create a type for the function, with the return type and any
8075 // argument types.
8076 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8077 // The type is included in "tt_args".
8078 if (argcount > 0 || varargs)
8079 {
8080 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8081 argcount, &ufunc->uf_type_list);
8082 // Add argument types to the function type.
8083 if (func_type_add_arg_types(ufunc->uf_func_type,
8084 argcount + varargs,
8085 &ufunc->uf_type_list) == FAIL)
8086 return;
8087 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8088 ufunc->uf_func_type->tt_min_argcount =
8089 argcount - ufunc->uf_def_args.ga_len;
8090 if (ufunc->uf_arg_types == NULL)
8091 {
8092 int i;
8093
8094 // lambda does not have argument types.
8095 for (i = 0; i < argcount; ++i)
8096 ufunc->uf_func_type->tt_args[i] = &t_any;
8097 }
8098 else
8099 mch_memmove(ufunc->uf_func_type->tt_args,
8100 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8101 if (varargs)
8102 {
8103 ufunc->uf_func_type->tt_args[argcount] =
8104 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8105 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8106 }
8107 }
8108 else
8109 // No arguments, can use a predefined type.
8110 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8111 argcount, &ufunc->uf_type_list);
8112}
8113
8114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115/*
8116 * Delete an instruction, free what it contains.
8117 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008118 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119delete_instr(isn_T *isn)
8120{
8121 switch (isn->isn_type)
8122 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008123 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008124 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008125 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008126 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 case ISN_LOADENV:
8128 case ISN_LOADG:
8129 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008130 case ISN_LOADT:
8131 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008132 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008133 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008135 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008136 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008137 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008138 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008140 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008141 case ISN_STOREW:
8142 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 vim_free(isn->isn_arg.string);
8144 break;
8145
8146 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008147 case ISN_STORES:
8148 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008149 break;
8150
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008151 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008152 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008153 vim_free(isn->isn_arg.unlet.ul_name);
8154 break;
8155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156 case ISN_STOREOPT:
8157 vim_free(isn->isn_arg.storeopt.so_name);
8158 break;
8159
8160 case ISN_PUSHBLOB: // push blob isn_arg.blob
8161 blob_unref(isn->isn_arg.blob);
8162 break;
8163
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008164 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008165#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008166 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008167#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008168 break;
8169
8170 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008172 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008173#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008174 break;
8175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 case ISN_UCALL:
8177 vim_free(isn->isn_arg.ufunc.cuf_name);
8178 break;
8179
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008180 case ISN_FUNCREF:
8181 {
8182 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8183 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008184 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008185
Bram Moolenaar077a4232020-12-22 18:33:27 +01008186 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8187 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008188 }
8189 break;
8190
8191 case ISN_DCALL:
8192 {
8193 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8194 + isn->isn_arg.dfunc.cdf_idx;
8195
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008196 if (dfunc->df_ufunc != NULL
8197 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008198 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008199 }
8200 break;
8201
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008202 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008203 {
8204 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8205 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8206
8207 if (ufunc != NULL)
8208 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008209 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008210 func_ptr_unref(ufunc);
8211 }
8212
8213 vim_free(lambda);
8214 vim_free(isn->isn_arg.newfunc.nf_global);
8215 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008216 break;
8217
Bram Moolenaar5e654232020-09-16 15:22:00 +02008218 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008219 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008220 free_type(isn->isn_arg.type.ct_type);
8221 break;
8222
Bram Moolenaar02194d22020-10-24 23:08:38 +02008223 case ISN_CMDMOD:
8224 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8225 ->cmod_filter_regmatch.regprog);
8226 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8227 break;
8228
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008229 case ISN_LOADSCRIPT:
8230 case ISN_STORESCRIPT:
8231 vim_free(isn->isn_arg.script.scriptref);
8232 break;
8233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008234 case ISN_2BOOL:
8235 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008236 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008237 case ISN_ADDBLOB:
8238 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008239 case ISN_ANYINDEX:
8240 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008241 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008242 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008243 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008244 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008245 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008246 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 case ISN_COMPAREANY:
8248 case ISN_COMPAREBLOB:
8249 case ISN_COMPAREBOOL:
8250 case ISN_COMPAREDICT:
8251 case ISN_COMPAREFLOAT:
8252 case ISN_COMPAREFUNC:
8253 case ISN_COMPARELIST:
8254 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008255 case ISN_COMPARESPECIAL:
8256 case ISN_COMPARESTRING:
8257 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008258 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 case ISN_DROP:
8260 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008261 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008262 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008264 case ISN_EXECCONCAT:
8265 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008267 case ISN_GETITEM:
8268 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008269 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008270 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008271 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008273 case ISN_LOADBDICT:
8274 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008275 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008276 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008277 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008278 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008279 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008280 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008281 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008282 case ISN_NEGATENR:
8283 case ISN_NEWDICT:
8284 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008286 case ISN_OPFLOAT:
8287 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008288 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008289 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008290 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008291 case ISN_PUSHF:
8292 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008293 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008294 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008296 case ISN_SHUFFLE:
8297 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008298 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008299 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008300 case ISN_STORENR:
8301 case ISN_STOREOUTER:
8302 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008303 case ISN_STOREV:
8304 case ISN_STRINDEX:
8305 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 case ISN_THROW:
8307 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008308 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008309 // nothing allocated
8310 break;
8311 }
8312}
8313
8314/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008315 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008316 */
8317 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008318delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008319{
8320 int idx;
8321
8322 ga_clear(&dfunc->df_def_args_isn);
8323
8324 if (dfunc->df_instr != NULL)
8325 {
8326 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8327 delete_instr(dfunc->df_instr + idx);
8328 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008329 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008330 }
8331
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008332 if (mark_deleted)
8333 dfunc->df_deleted = TRUE;
8334 if (dfunc->df_ufunc != NULL)
8335 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008336}
8337
8338/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008339 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008340 * function, unless another user function still uses it.
8341 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008342 */
8343 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008344unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008346 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347 {
8348 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8349 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008350
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008351 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008352 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008353 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008354 ufunc->uf_dfunc_idx = 0;
8355 if (dfunc->df_ufunc == ufunc)
8356 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008357 }
8358}
8359
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008360/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008361 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008362 */
8363 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008364link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008365{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008366 if (ufunc->uf_dfunc_idx > 0)
8367 {
8368 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8369 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008370
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008371 ++dfunc->df_refcount;
8372 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008373}
8374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008375#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008376/*
8377 * Free all functions defined with ":def".
8378 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379 void
8380free_def_functions(void)
8381{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008382 int idx;
8383
8384 for (idx = 0; idx < def_functions.ga_len; ++idx)
8385 {
8386 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8387
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008388 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008389 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008390 }
8391
8392 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008393}
8394#endif
8395
8396
8397#endif // FEAT_EVAL