blob: ead971feba45f43381a36a61ee10b6e50be65c7f [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
Bram Moolenaarab360522021-01-10 14:02:28 +0100111 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200112 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".
Bram Moolenaarab360522021-01-10 14:02:28 +0100152 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100153 * 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;
Bram Moolenaarab360522021-01-10 14:02:28 +0100175 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100176 }
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;
Bram Moolenaarab360522021-01-10 14:02:28 +0100189 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100190 }
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 {
Bram Moolenaarab360522021-01-10 14:02:28 +0100261 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 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;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100477 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100479 RETURN_OK_IF_SKIP(cctx);
480 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200481 switch ((*type)->tt_type)
482 {
483 // nothing to be done
484 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200486 // conversion possible
487 case VAR_SPECIAL:
488 case VAR_BOOL:
489 case VAR_NUMBER:
490 case VAR_FLOAT:
491 break;
492
493 // conversion possible (with runtime check)
494 case VAR_ANY:
495 case VAR_UNKNOWN:
496 isntype = ISN_2STRING_ANY;
497 break;
498
499 // conversion not possible
500 case VAR_VOID:
501 case VAR_BLOB:
502 case VAR_FUNC:
503 case VAR_PARTIAL:
504 case VAR_LIST:
505 case VAR_DICT:
506 case VAR_JOB:
507 case VAR_CHANNEL:
508 to_string_error((*type)->tt_type);
509 return FAIL;
510 }
511
512 *type = &t_string;
513 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 return FAIL;
515 isn->isn_arg.number = offset;
516
517 return OK;
518}
519
520 static int
521check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
522{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200525 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 {
527 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200530 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 return FAIL;
532 }
533 return OK;
534}
535
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200536 static int
537generate_add_instr(
538 cctx_T *cctx,
539 vartype_T vartype,
540 type_T *type1,
541 type_T *type2)
542{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100543 garray_T *stack = &cctx->ctx_type_stack;
544 isn_T *isn = generate_instr_drop(cctx,
545 vartype == VAR_NUMBER ? ISN_OPNR
546 : vartype == VAR_LIST ? ISN_ADDLIST
547 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100551 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200552
553 if (vartype != VAR_LIST && vartype != VAR_BLOB
554 && type1->tt_type != VAR_ANY
555 && type2->tt_type != VAR_ANY
556 && check_number_or_float(
557 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
558 return FAIL;
559
560 if (isn != NULL)
561 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100562
563 // When concatenating two lists with different member types the member type
564 // becomes "any".
565 if (vartype == VAR_LIST
566 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
567 && type1->tt_member != type2->tt_member)
568 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
569
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200570 return isn == NULL ? FAIL : OK;
571}
572
573/*
574 * Get the type to use for an instruction for an operation on "type1" and
575 * "type2". If they are matching use a type-specific instruction. Otherwise
576 * fall back to runtime type checking.
577 */
578 static vartype_T
579operator_type(type_T *type1, type_T *type2)
580{
581 if (type1->tt_type == type2->tt_type
582 && (type1->tt_type == VAR_NUMBER
583 || type1->tt_type == VAR_LIST
584#ifdef FEAT_FLOAT
585 || type1->tt_type == VAR_FLOAT
586#endif
587 || type1->tt_type == VAR_BLOB))
588 return type1->tt_type;
589 return VAR_ANY;
590}
591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592/*
593 * Generate an instruction with two arguments. The instruction depends on the
594 * type of the arguments.
595 */
596 static int
597generate_two_op(cctx_T *cctx, char_u *op)
598{
599 garray_T *stack = &cctx->ctx_type_stack;
600 type_T *type1;
601 type_T *type2;
602 vartype_T vartype;
603 isn_T *isn;
604
Bram Moolenaar080457c2020-03-03 21:53:32 +0100605 RETURN_OK_IF_SKIP(cctx);
606
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200607 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
609 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200610 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
612 switch (*op)
613 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200614 case '+':
615 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 break;
618
619 case '-':
620 case '*':
621 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
622 op) == FAIL)
623 return FAIL;
624 if (vartype == VAR_NUMBER)
625 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
626#ifdef FEAT_FLOAT
627 else if (vartype == VAR_FLOAT)
628 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
629#endif
630 else
631 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
632 if (isn != NULL)
633 isn->isn_arg.op.op_type = *op == '*'
634 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
635 break;
636
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200639 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 && type2->tt_type != VAR_NUMBER))
641 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200642 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 return FAIL;
644 }
645 isn = generate_instr_drop(cctx,
646 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
647 if (isn != NULL)
648 isn->isn_arg.op.op_type = EXPR_REM;
649 break;
650 }
651
652 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200653 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 {
655 type_T *type = &t_any;
656
657#ifdef FEAT_FLOAT
658 // float+number and number+float results in float
659 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
660 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
661 type = &t_float;
662#endif
663 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
664 }
665
666 return OK;
667}
668
669/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200670 * Get the instruction to use for comparing "type1" with "type2"
671 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200673 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100674get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675{
676 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
Bram Moolenaar4c683752020-04-05 21:38:23 +0200678 if (type1 == VAR_UNKNOWN)
679 type1 = VAR_ANY;
680 if (type2 == VAR_UNKNOWN)
681 type2 = VAR_ANY;
682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if (type1 == type2)
684 {
685 switch (type1)
686 {
687 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
688 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
689 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
690 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
691 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
692 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
693 case VAR_LIST: isntype = ISN_COMPARELIST; break;
694 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
695 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 default: isntype = ISN_COMPAREANY; break;
697 }
698 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200699 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
701 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
702 isntype = ISN_COMPAREANY;
703
Bram Moolenaar657137c2021-01-09 15:45:23 +0100704 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 && (isntype == ISN_COMPAREBOOL
706 || isntype == ISN_COMPARESPECIAL
707 || isntype == ISN_COMPARENR
708 || isntype == ISN_COMPAREFLOAT))
709 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200710 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100711 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200712 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 }
714 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100715 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
717 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100718 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
719 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 && (type1 == VAR_BLOB || type2 == VAR_BLOB
721 || type1 == VAR_LIST || type2 == VAR_LIST))))
722 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200723 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200727 return isntype;
728}
729
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200730 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100731check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200732{
733 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
734 return FAIL;
735 return OK;
736}
737
Bram Moolenaara5565e42020-05-09 15:44:01 +0200738/*
739 * Generate an ISN_COMPARE* instruction with a boolean result.
740 */
741 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100742generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200743{
744 isntype_T isntype;
745 isn_T *isn;
746 garray_T *stack = &cctx->ctx_type_stack;
747 vartype_T type1;
748 vartype_T type2;
749
750 RETURN_OK_IF_SKIP(cctx);
751
752 // Get the known type of the two items on the stack. If they are matching
753 // use a type-specific instruction. Otherwise fall back to runtime type
754 // checking.
755 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
756 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100757 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200758 if (isntype == ISN_DROP)
759 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760
761 if ((isn = generate_instr(cctx, isntype)) == NULL)
762 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100763 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 isn->isn_arg.op.op_ic = ic;
765
766 // takes two arguments, puts one bool back
767 if (stack->ga_len >= 2)
768 {
769 --stack->ga_len;
770 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
771 }
772
773 return OK;
774}
775
776/*
777 * Generate an ISN_2BOOL instruction.
778 */
779 static int
780generate_2BOOL(cctx_T *cctx, int invert)
781{
782 isn_T *isn;
783 garray_T *stack = &cctx->ctx_type_stack;
784
Bram Moolenaar080457c2020-03-03 21:53:32 +0100785 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
787 return FAIL;
788 isn->isn_arg.number = invert;
789
790 // type becomes bool
791 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
792
793 return OK;
794}
795
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200796/*
797 * Generate an ISN_COND2BOOL instruction.
798 */
799 static int
800generate_COND2BOOL(cctx_T *cctx)
801{
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804
805 RETURN_OK_IF_SKIP(cctx);
806 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
807 return FAIL;
808
809 // type becomes bool
810 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
811
812 return OK;
813}
814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200816generate_TYPECHECK(
817 cctx_T *cctx,
818 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100819 int offset,
820 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821{
822 isn_T *isn;
823 garray_T *stack = &cctx->ctx_type_stack;
824
Bram Moolenaar080457c2020-03-03 21:53:32 +0100825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
827 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 isn->isn_arg.type.ct_off = offset;
Bram Moolenaare32e5162021-01-21 20:21:29 +0100830 isn->isn_arg.type.ct_arg_idx = argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831
Bram Moolenaar5e654232020-09-16 15:22:00 +0200832 // type becomes expected
833 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
835 return OK;
836}
837
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100838 static int
839generate_SETTYPE(
840 cctx_T *cctx,
841 type_T *expected)
842{
843 isn_T *isn;
844
845 RETURN_OK_IF_SKIP(cctx);
846 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
847 return FAIL;
848 isn->isn_arg.type.ct_type = alloc_type(expected);
849 return OK;
850}
851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200853 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
854 * used. Return FALSE if the types will never match.
855 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100856 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200857use_typecheck(type_T *actual, type_T *expected)
858{
859 if (actual->tt_type == VAR_ANY
860 || actual->tt_type == VAR_UNKNOWN
861 || (actual->tt_type == VAR_FUNC
862 && (expected->tt_type == VAR_FUNC
863 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100864 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
865 && ((actual->tt_member == &t_void)
866 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200867 return TRUE;
868 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
869 && actual->tt_type == expected->tt_type)
870 // This takes care of a nested list or dict.
871 return use_typecheck(actual->tt_member, expected->tt_member);
872 return FALSE;
873}
874
875/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200876 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200877 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878 * - "actual" is a type that can be "expected" type: add a runtime check; or
879 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200880 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
881 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200882 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100883 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200884need_type(
885 type_T *actual,
886 type_T *expected,
887 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100888 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200889 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200890 int silent,
891 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200892{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200893 if (expected == &t_bool && actual != &t_bool
894 && (actual->tt_flags & TTFLAG_BOOL_OK))
895 {
896 // Using "0", "1" or the result of an expression with "&&" or "||" as a
897 // boolean is OK but requires a conversion.
898 generate_2BOOL(cctx, FALSE);
899 return OK;
900 }
901
Bram Moolenaar351ead02021-01-16 16:07:01 +0100902 if (check_type(expected, actual, FALSE, arg_idx) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200903 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200904
905 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200906 // If it's a constant a runtime check makes no sense.
907 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200908 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100909 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200910 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200911 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200912
913 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100914 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200915 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200916}
917
918/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100919 * Check that the top of the type stack has a type that can be used as a
920 * condition. Give an error and return FAIL if not.
921 */
922 static int
923bool_on_stack(cctx_T *cctx)
924{
925 garray_T *stack = &cctx->ctx_type_stack;
926 type_T *type;
927
928 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
929 if (type == &t_bool)
930 return OK;
931
932 if (type == &t_any || type == &t_number)
933 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
934 // This requires a runtime type check.
935 return generate_COND2BOOL(cctx);
936
Bram Moolenaar351ead02021-01-16 16:07:01 +0100937 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100938}
939
940/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 * Generate an ISN_PUSHNR instruction.
942 */
943 static int
944generate_PUSHNR(cctx_T *cctx, varnumber_T number)
945{
946 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200947 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948
Bram Moolenaar080457c2020-03-03 21:53:32 +0100949 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
951 return FAIL;
952 isn->isn_arg.number = number;
953
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200954 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200955 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100956 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 return OK;
958}
959
960/*
961 * Generate an ISN_PUSHBOOL instruction.
962 */
963 static int
964generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
965{
966 isn_T *isn;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
970 return FAIL;
971 isn->isn_arg.number = number;
972
973 return OK;
974}
975
976/*
977 * Generate an ISN_PUSHSPEC instruction.
978 */
979 static int
980generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
981{
982 isn_T *isn;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
986 return FAIL;
987 isn->isn_arg.number = number;
988
989 return OK;
990}
991
992#ifdef FEAT_FLOAT
993/*
994 * Generate an ISN_PUSHF instruction.
995 */
996 static int
997generate_PUSHF(cctx_T *cctx, float_T fnumber)
998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1003 return FAIL;
1004 isn->isn_arg.fnumber = fnumber;
1005
1006 return OK;
1007}
1008#endif
1009
1010/*
1011 * Generate an ISN_PUSHS instruction.
1012 * Consumes "str".
1013 */
1014 static int
1015generate_PUSHS(cctx_T *cctx, char_u *str)
1016{
1017 isn_T *isn;
1018
Bram Moolenaar508b5612021-01-02 18:17:26 +01001019 if (cctx->ctx_skip == SKIP_YES)
1020 {
1021 vim_free(str);
1022 return OK;
1023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1025 return FAIL;
1026 isn->isn_arg.string = str;
1027
1028 return OK;
1029}
1030
1031/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001032 * Generate an ISN_PUSHCHANNEL instruction.
1033 * Consumes "channel".
1034 */
1035 static int
1036generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1037{
1038 isn_T *isn;
1039
Bram Moolenaar080457c2020-03-03 21:53:32 +01001040 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001041 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1042 return FAIL;
1043 isn->isn_arg.channel = channel;
1044
1045 return OK;
1046}
1047
1048/*
1049 * Generate an ISN_PUSHJOB instruction.
1050 * Consumes "job".
1051 */
1052 static int
1053generate_PUSHJOB(cctx_T *cctx, job_T *job)
1054{
1055 isn_T *isn;
1056
Bram Moolenaar080457c2020-03-03 21:53:32 +01001057 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001058 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001059 return FAIL;
1060 isn->isn_arg.job = job;
1061
1062 return OK;
1063}
1064
1065/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 * Generate an ISN_PUSHBLOB instruction.
1067 * Consumes "blob".
1068 */
1069 static int
1070generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1071{
1072 isn_T *isn;
1073
Bram Moolenaar080457c2020-03-03 21:53:32 +01001074 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1076 return FAIL;
1077 isn->isn_arg.blob = blob;
1078
1079 return OK;
1080}
1081
1082/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001083 * Generate an ISN_PUSHFUNC instruction with name "name".
1084 * Consumes "name".
1085 */
1086 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001087generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001088{
1089 isn_T *isn;
1090
Bram Moolenaar080457c2020-03-03 21:53:32 +01001091 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001092 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001093 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001094 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001100 * Generate an ISN_GETITEM instruction with "index".
1101 */
1102 static int
1103generate_GETITEM(cctx_T *cctx, int index)
1104{
1105 isn_T *isn;
1106 garray_T *stack = &cctx->ctx_type_stack;
1107 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1108 type_T *item_type = &t_any;
1109
1110 RETURN_OK_IF_SKIP(cctx);
1111
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001112 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001113 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001114 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001115 emsg(_(e_listreq));
1116 return FAIL;
1117 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001118 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001119 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1120 return FAIL;
1121 isn->isn_arg.number = index;
1122
1123 // add the item type to the type stack
1124 if (ga_grow(stack, 1) == FAIL)
1125 return FAIL;
1126 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1127 ++stack->ga_len;
1128 return OK;
1129}
1130
1131/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001132 * Generate an ISN_SLICE instruction with "count".
1133 */
1134 static int
1135generate_SLICE(cctx_T *cctx, int count)
1136{
1137 isn_T *isn;
1138
1139 RETURN_OK_IF_SKIP(cctx);
1140 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1141 return FAIL;
1142 isn->isn_arg.number = count;
1143 return OK;
1144}
1145
1146/*
1147 * Generate an ISN_CHECKLEN instruction with "min_len".
1148 */
1149 static int
1150generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1151{
1152 isn_T *isn;
1153
1154 RETURN_OK_IF_SKIP(cctx);
1155
1156 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1157 return FAIL;
1158 isn->isn_arg.checklen.cl_min_len = min_len;
1159 isn->isn_arg.checklen.cl_more_OK = more_OK;
1160
1161 return OK;
1162}
1163
1164/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 * Generate an ISN_STORE instruction.
1166 */
1167 static int
1168generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1169{
1170 isn_T *isn;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1174 return FAIL;
1175 if (name != NULL)
1176 isn->isn_arg.string = vim_strsave(name);
1177 else
1178 isn->isn_arg.number = idx;
1179
1180 return OK;
1181}
1182
1183/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001184 * Generate an ISN_STOREOUTER instruction.
1185 */
1186 static int
1187generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1188{
1189 isn_T *isn;
1190
1191 RETURN_OK_IF_SKIP(cctx);
1192 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1193 return FAIL;
1194 isn->isn_arg.outer.outer_idx = idx;
1195 isn->isn_arg.outer.outer_depth = level;
1196
1197 return OK;
1198}
1199
1200/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1202 */
1203 static int
1204generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1205{
1206 isn_T *isn;
1207
Bram Moolenaar080457c2020-03-03 21:53:32 +01001208 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1210 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001211 isn->isn_arg.storenr.stnr_idx = idx;
1212 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213
1214 return OK;
1215}
1216
1217/*
1218 * Generate an ISN_STOREOPT instruction
1219 */
1220 static int
1221generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1222{
1223 isn_T *isn;
1224
Bram Moolenaar080457c2020-03-03 21:53:32 +01001225 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001226 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 return FAIL;
1228 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1229 isn->isn_arg.storeopt.so_flags = opt_flags;
1230
1231 return OK;
1232}
1233
1234/*
1235 * Generate an ISN_LOAD or similar instruction.
1236 */
1237 static int
1238generate_LOAD(
1239 cctx_T *cctx,
1240 isntype_T isn_type,
1241 int idx,
1242 char_u *name,
1243 type_T *type)
1244{
1245 isn_T *isn;
1246
Bram Moolenaar080457c2020-03-03 21:53:32 +01001247 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1249 return FAIL;
1250 if (name != NULL)
1251 isn->isn_arg.string = vim_strsave(name);
1252 else
1253 isn->isn_arg.number = idx;
1254
1255 return OK;
1256}
1257
1258/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001259 * Generate an ISN_LOADOUTER instruction
1260 */
1261 static int
1262generate_LOADOUTER(
1263 cctx_T *cctx,
1264 int idx,
1265 int nesting,
1266 type_T *type)
1267{
1268 isn_T *isn;
1269
1270 RETURN_OK_IF_SKIP(cctx);
1271 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1272 return FAIL;
1273 isn->isn_arg.outer.outer_idx = idx;
1274 isn->isn_arg.outer.outer_depth = nesting;
1275
1276 return OK;
1277}
1278
1279/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001280 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001281 */
1282 static int
1283generate_LOADV(
1284 cctx_T *cctx,
1285 char_u *name,
1286 int error)
1287{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001288 int di_flags;
1289 int vidx = find_vim_var(name, &di_flags);
1290 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001291
Bram Moolenaar080457c2020-03-03 21:53:32 +01001292 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001293 if (vidx < 0)
1294 {
1295 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001296 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001297 return FAIL;
1298 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001299 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001300
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001301 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302}
1303
1304/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001305 * Generate an ISN_UNLET instruction.
1306 */
1307 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001308generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001309{
1310 isn_T *isn;
1311
1312 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001313 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001314 return FAIL;
1315 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1316 isn->isn_arg.unlet.ul_forceit = forceit;
1317
1318 return OK;
1319}
1320
1321/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001322 * Generate an ISN_LOCKCONST instruction.
1323 */
1324 static int
1325generate_LOCKCONST(cctx_T *cctx)
1326{
1327 isn_T *isn;
1328
1329 RETURN_OK_IF_SKIP(cctx);
1330 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1331 return FAIL;
1332 return OK;
1333}
1334
1335/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 * Generate an ISN_LOADS instruction.
1337 */
1338 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001339generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001341 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001343 int sid,
1344 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345{
1346 isn_T *isn;
1347
Bram Moolenaar080457c2020-03-03 21:53:32 +01001348 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001349 if (isn_type == ISN_LOADS)
1350 isn = generate_instr_type(cctx, isn_type, type);
1351 else
1352 isn = generate_instr_drop(cctx, isn_type, 1);
1353 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001355 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1356 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357
1358 return OK;
1359}
1360
1361/*
1362 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1363 */
1364 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001365generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 cctx_T *cctx,
1367 isntype_T isn_type,
1368 int sid,
1369 int idx,
1370 type_T *type)
1371{
1372 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001373 scriptref_T *sref;
1374 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375
Bram Moolenaar080457c2020-03-03 21:53:32 +01001376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 if (isn_type == ISN_LOADSCRIPT)
1378 isn = generate_instr_type(cctx, isn_type, type);
1379 else
1380 isn = generate_instr_drop(cctx, isn_type, 1);
1381 if (isn == NULL)
1382 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001383
1384 // This requires three arguments, which doesn't fit in an instruction, thus
1385 // we need to allocate a struct for this.
1386 sref = ALLOC_ONE(scriptref_T);
1387 if (sref == NULL)
1388 return FAIL;
1389 isn->isn_arg.script.scriptref = sref;
1390 sref->sref_sid = sid;
1391 sref->sref_idx = idx;
1392 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001393 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 return OK;
1395}
1396
1397/*
1398 * Generate an ISN_NEWLIST instruction.
1399 */
1400 static int
1401generate_NEWLIST(cctx_T *cctx, int count)
1402{
1403 isn_T *isn;
1404 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 type_T *type;
1406 type_T *member;
1407
Bram Moolenaar080457c2020-03-03 21:53:32 +01001408 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1410 return FAIL;
1411 isn->isn_arg.number = count;
1412
Bram Moolenaar127542b2020-08-09 17:22:04 +02001413 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001414 if (count == 0)
1415 member = &t_void;
1416 else
1417 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001418 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1419 cctx->ctx_type_list);
1420 type = get_list_type(member, cctx->ctx_type_list);
1421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 // drop the value types
1423 stack->ga_len -= count;
1424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 // add the list type to the type stack
1426 if (ga_grow(stack, 1) == FAIL)
1427 return FAIL;
1428 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1429 ++stack->ga_len;
1430
1431 return OK;
1432}
1433
1434/*
1435 * Generate an ISN_NEWDICT instruction.
1436 */
1437 static int
1438generate_NEWDICT(cctx_T *cctx, int count)
1439{
1440 isn_T *isn;
1441 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 type_T *type;
1443 type_T *member;
1444
Bram Moolenaar080457c2020-03-03 21:53:32 +01001445 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1447 return FAIL;
1448 isn->isn_arg.number = count;
1449
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001450 if (count == 0)
1451 member = &t_void;
1452 else
1453 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001454 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1455 cctx->ctx_type_list);
1456 type = get_dict_type(member, cctx->ctx_type_list);
1457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 // drop the key and value types
1459 stack->ga_len -= 2 * count;
1460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 // add the dict type to the type stack
1462 if (ga_grow(stack, 1) == FAIL)
1463 return FAIL;
1464 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1465 ++stack->ga_len;
1466
1467 return OK;
1468}
1469
1470/*
1471 * Generate an ISN_FUNCREF instruction.
1472 */
1473 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001474generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475{
1476 isn_T *isn;
1477 garray_T *stack = &cctx->ctx_type_stack;
1478
Bram Moolenaar080457c2020-03-03 21:53:32 +01001479 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1481 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001482 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001483 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484
Bram Moolenaarab360522021-01-10 14:02:28 +01001485 // if the referenced function is a closure, it may use items further up in
1486 // the nested context, including this one.
1487 if (ufunc->uf_flags & FC_CLOSURE)
1488 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 if (ga_grow(stack, 1) == FAIL)
1491 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001492 ((type_T **)stack->ga_data)[stack->ga_len] =
1493 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 ++stack->ga_len;
1495
1496 return OK;
1497}
1498
1499/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001500 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001501 * "lambda_name" and "func_name" must be in allocated memory and will be
1502 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001503 */
1504 static int
1505generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1506{
1507 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001508
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001509 if (cctx->ctx_skip == SKIP_YES)
1510 {
1511 vim_free(lambda_name);
1512 vim_free(func_name);
1513 return OK;
1514 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001515 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001516 {
1517 vim_free(lambda_name);
1518 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001519 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001520 }
1521 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001522 isn->isn_arg.newfunc.nf_global = func_name;
1523
1524 return OK;
1525}
1526
1527/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001528 * Generate an ISN_DEF instruction: list functions
1529 */
1530 static int
1531generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1532{
1533 isn_T *isn;
1534
1535 RETURN_OK_IF_SKIP(cctx);
1536 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1537 return FAIL;
1538 if (len > 0)
1539 {
1540 isn->isn_arg.string = vim_strnsave(name, len);
1541 if (isn->isn_arg.string == NULL)
1542 return FAIL;
1543 }
1544 return OK;
1545}
1546
1547/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 * Generate an ISN_JUMP instruction.
1549 */
1550 static int
1551generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1552{
1553 isn_T *isn;
1554 garray_T *stack = &cctx->ctx_type_stack;
1555
Bram Moolenaar080457c2020-03-03 21:53:32 +01001556 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1558 return FAIL;
1559 isn->isn_arg.jump.jump_when = when;
1560 isn->isn_arg.jump.jump_where = where;
1561
1562 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1563 --stack->ga_len;
1564
1565 return OK;
1566}
1567
1568 static int
1569generate_FOR(cctx_T *cctx, int loop_idx)
1570{
1571 isn_T *isn;
1572 garray_T *stack = &cctx->ctx_type_stack;
1573
Bram Moolenaar080457c2020-03-03 21:53:32 +01001574 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1576 return FAIL;
1577 isn->isn_arg.forloop.for_idx = loop_idx;
1578
1579 if (ga_grow(stack, 1) == FAIL)
1580 return FAIL;
1581 // type doesn't matter, will be stored next
1582 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1583 ++stack->ga_len;
1584
1585 return OK;
1586}
1587
1588/*
1589 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001590 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 * Return FAIL if the number of arguments is wrong.
1592 */
1593 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001594generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595{
1596 isn_T *isn;
1597 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001598 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001599 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001600 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601
Bram Moolenaar080457c2020-03-03 21:53:32 +01001602 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001603 argoff = check_internal_func(func_idx, argcount);
1604 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 return FAIL;
1606
Bram Moolenaar389df252020-07-09 21:20:47 +02001607 if (method_call && argoff > 1)
1608 {
1609 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1610 return FAIL;
1611 isn->isn_arg.shuffle.shfl_item = argcount;
1612 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1613 }
1614
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001615 if (argcount > 0)
1616 {
1617 // Check the types of the arguments.
1618 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001619 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1620 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001621 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001622 if (internal_func_is_map(func_idx))
1623 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001624 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1627 return FAIL;
1628 isn->isn_arg.bfunc.cbf_idx = func_idx;
1629 isn->isn_arg.bfunc.cbf_argcount = argcount;
1630
Bram Moolenaar94738d82020-10-21 14:25:07 +02001631 // Drop the argument types and push the return type.
1632 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 if (ga_grow(stack, 1) == FAIL)
1634 return FAIL;
1635 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001636 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001637 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001639 if (maptype != NULL && maptype->tt_member != NULL
1640 && maptype->tt_member != &t_any)
1641 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001642 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 return OK;
1645}
1646
1647/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001648 * Generate an ISN_LISTAPPEND instruction. Works like add().
1649 * Argument count is already checked.
1650 */
1651 static int
1652generate_LISTAPPEND(cctx_T *cctx)
1653{
1654 garray_T *stack = &cctx->ctx_type_stack;
1655 type_T *list_type;
1656 type_T *item_type;
1657 type_T *expected;
1658
1659 // Caller already checked that list_type is a list.
1660 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1661 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1662 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001663 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001664 return FAIL;
1665
1666 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1667 return FAIL;
1668
1669 --stack->ga_len; // drop the argument
1670 return OK;
1671}
1672
1673/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001674 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1675 * Argument count is already checked.
1676 */
1677 static int
1678generate_BLOBAPPEND(cctx_T *cctx)
1679{
1680 garray_T *stack = &cctx->ctx_type_stack;
1681 type_T *item_type;
1682
1683 // Caller already checked that blob_type is a blob.
1684 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001685 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001686 return FAIL;
1687
1688 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1689 return FAIL;
1690
1691 --stack->ga_len; // drop the argument
1692 return OK;
1693}
1694
1695/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 * Generate an ISN_DCALL or ISN_UCALL instruction.
1697 * Return FAIL if the number of arguments is wrong.
1698 */
1699 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001700generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701{
1702 isn_T *isn;
1703 garray_T *stack = &cctx->ctx_type_stack;
1704 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001705 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
Bram Moolenaar080457c2020-03-03 21:53:32 +01001707 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 if (argcount > regular_args && !has_varargs(ufunc))
1709 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001710 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 return FAIL;
1712 }
1713 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1714 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001715 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 return FAIL;
1717 }
1718
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001719 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001720 {
1721 int i;
1722
1723 for (i = 0; i < argcount; ++i)
1724 {
1725 type_T *expected;
1726 type_T *actual;
1727
1728 if (i < regular_args)
1729 {
1730 if (ufunc->uf_arg_types == NULL)
1731 continue;
1732 expected = ufunc->uf_arg_types[i];
1733 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001734 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1735 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001736 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001737 else
1738 expected = ufunc->uf_va_type->tt_member;
1739 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001740 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001741 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001742 {
1743 arg_type_mismatch(expected, actual, i + 1);
1744 return FAIL;
1745 }
1746 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001747 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001748 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1749 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001750 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001751 }
1752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001754 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001755 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001757 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 {
1759 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1760 isn->isn_arg.dfunc.cdf_argcount = argcount;
1761 }
1762 else
1763 {
1764 // A user function may be deleted and redefined later, can't use the
1765 // ufunc pointer, need to look it up again at runtime.
1766 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1767 isn->isn_arg.ufunc.cuf_argcount = argcount;
1768 }
1769
1770 stack->ga_len -= argcount; // drop the arguments
1771 if (ga_grow(stack, 1) == FAIL)
1772 return FAIL;
1773 // add return value
1774 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1775 ++stack->ga_len;
1776
1777 return OK;
1778}
1779
1780/*
1781 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1782 */
1783 static int
1784generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1785{
1786 isn_T *isn;
1787 garray_T *stack = &cctx->ctx_type_stack;
1788
Bram Moolenaar080457c2020-03-03 21:53:32 +01001789 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1791 return FAIL;
1792 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1793 isn->isn_arg.ufunc.cuf_argcount = argcount;
1794
1795 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001796 if (ga_grow(stack, 1) == FAIL)
1797 return FAIL;
1798 // add return value
1799 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1800 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801
1802 return OK;
1803}
1804
1805/*
1806 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001807 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 */
1809 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001810generate_PCALL(
1811 cctx_T *cctx,
1812 int argcount,
1813 char_u *name,
1814 type_T *type,
1815 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816{
1817 isn_T *isn;
1818 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001819 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
Bram Moolenaar080457c2020-03-03 21:53:32 +01001821 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001822
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001823 if (type->tt_type == VAR_ANY)
1824 ret_type = &t_any;
1825 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001826 {
1827 if (type->tt_argcount != -1)
1828 {
1829 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1830
1831 if (argcount < type->tt_min_argcount - varargs)
1832 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001833 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001834 return FAIL;
1835 }
1836 if (!varargs && argcount > type->tt_argcount)
1837 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001838 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001839 return FAIL;
1840 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001841 if (type->tt_args != NULL)
1842 {
1843 int i;
1844
1845 for (i = 0; i < argcount; ++i)
1846 {
1847 int offset = -argcount + i - 1;
1848 type_T *actual = ((type_T **)stack->ga_data)[
1849 stack->ga_len + offset];
1850 type_T *expected;
1851
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001852 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001853 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001854 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001855 else
1856 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001857 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001858 cctx, TRUE, FALSE) == FAIL)
1859 {
1860 arg_type_mismatch(expected, actual, i + 1);
1861 return FAIL;
1862 }
1863 }
1864 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001865 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001866 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001867 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001868 else
1869 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001870 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001871 return FAIL;
1872 }
1873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1875 return FAIL;
1876 isn->isn_arg.pfunc.cpf_top = at_top;
1877 isn->isn_arg.pfunc.cpf_argcount = argcount;
1878
1879 stack->ga_len -= argcount; // drop the arguments
1880
1881 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001882 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001884 // If partial is above the arguments it must be cleared and replaced with
1885 // the return value.
1886 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1887 return FAIL;
1888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 return OK;
1890}
1891
1892/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001893 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 */
1895 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001896generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897{
1898 isn_T *isn;
1899 garray_T *stack = &cctx->ctx_type_stack;
1900 type_T *type;
1901
Bram Moolenaar080457c2020-03-03 21:53:32 +01001902 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001903 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001905 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001907 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001909 if (type->tt_type != VAR_DICT && type != &t_any)
1910 {
1911 emsg(_(e_dictreq));
1912 return FAIL;
1913 }
1914 // change dict type to dict member type
1915 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001916 {
1917 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1918 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920
1921 return OK;
1922}
1923
1924/*
1925 * Generate an ISN_ECHO instruction.
1926 */
1927 static int
1928generate_ECHO(cctx_T *cctx, int with_white, int count)
1929{
1930 isn_T *isn;
1931
Bram Moolenaar080457c2020-03-03 21:53:32 +01001932 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1934 return FAIL;
1935 isn->isn_arg.echo.echo_with_white = with_white;
1936 isn->isn_arg.echo.echo_count = count;
1937
1938 return OK;
1939}
1940
Bram Moolenaarad39c092020-02-26 18:23:43 +01001941/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001942 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001943 */
1944 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001945generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001946{
1947 isn_T *isn;
1948
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001949 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001950 return FAIL;
1951 isn->isn_arg.number = count;
1952
1953 return OK;
1954}
1955
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001956/*
1957 * Generate an ISN_PUT instruction.
1958 */
1959 static int
1960generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1961{
1962 isn_T *isn;
1963
1964 RETURN_OK_IF_SKIP(cctx);
1965 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1966 return FAIL;
1967 isn->isn_arg.put.put_regname = regname;
1968 isn->isn_arg.put.put_lnum = lnum;
1969 return OK;
1970}
1971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 static int
1973generate_EXEC(cctx_T *cctx, char_u *line)
1974{
1975 isn_T *isn;
1976
Bram Moolenaar080457c2020-03-03 21:53:32 +01001977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1979 return FAIL;
1980 isn->isn_arg.string = vim_strsave(line);
1981 return OK;
1982}
1983
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001984 static int
1985generate_EXECCONCAT(cctx_T *cctx, int count)
1986{
1987 isn_T *isn;
1988
1989 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1990 return FAIL;
1991 isn->isn_arg.number = count;
1992 return OK;
1993}
1994
Bram Moolenaar08597872020-12-10 19:43:40 +01001995/*
1996 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1997 */
1998 static int
1999generate_RANGE(cctx_T *cctx, char_u *range)
2000{
2001 isn_T *isn;
2002 garray_T *stack = &cctx->ctx_type_stack;
2003
2004 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2005 return FAIL;
2006 isn->isn_arg.string = range;
2007
2008 if (ga_grow(stack, 1) == FAIL)
2009 return FAIL;
2010 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2011 ++stack->ga_len;
2012 return OK;
2013}
2014
Bram Moolenaar792f7862020-11-23 08:31:18 +01002015 static int
2016generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2017{
2018 isn_T *isn;
2019
2020 RETURN_OK_IF_SKIP(cctx);
2021 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2022 return FAIL;
2023 isn->isn_arg.unpack.unp_count = var_count;
2024 isn->isn_arg.unpack.unp_semicolon = semicolon;
2025 return OK;
2026}
2027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002029 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002030 */
2031 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002032generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002033{
2034 isn_T *isn;
2035
Bram Moolenaar02194d22020-10-24 23:08:38 +02002036 if (cmod->cmod_flags != 0
2037 || cmod->cmod_split != 0
2038 || cmod->cmod_verbose != 0
2039 || cmod->cmod_tab != 0
2040 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002041 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002042 cctx->ctx_has_cmdmod = TRUE;
2043
2044 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002045 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002046 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2047 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2048 return FAIL;
2049 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002050 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002051 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002052 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002053
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002054 return OK;
2055}
2056
2057 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002058generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002059{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002060 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2061 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002062 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002063 return OK;
2064}
2065
2066/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002068 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002070 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002071reserve_local(
2072 cctx_T *cctx,
2073 char_u *name,
2074 size_t len,
2075 int isConst,
2076 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 lvar_T *lvar;
2079
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002080 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002082 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002083 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 }
2085
2086 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002087 return NULL;
2088 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002089 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002091 // Every local variable uses the next entry on the stack. We could re-use
2092 // the last ones when leaving a scope, but then variables used in a closure
2093 // might get overwritten. To keep things simple do not re-use stack
2094 // entries. This is less efficient, but memory is cheap these days.
2095 lvar->lv_idx = cctx->ctx_locals_count++;
2096
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002097 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 lvar->lv_const = isConst;
2099 lvar->lv_type = type;
2100
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002101 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102}
2103
2104/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002105 * Remove local variables above "new_top".
2106 */
2107 static void
2108unwind_locals(cctx_T *cctx, int new_top)
2109{
2110 if (cctx->ctx_locals.ga_len > new_top)
2111 {
2112 int idx;
2113 lvar_T *lvar;
2114
2115 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2116 {
2117 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2118 vim_free(lvar->lv_name);
2119 }
2120 }
2121 cctx->ctx_locals.ga_len = new_top;
2122}
2123
2124/*
2125 * Free all local variables.
2126 */
2127 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002128free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002129{
2130 unwind_locals(cctx, 0);
2131 ga_clear(&cctx->ctx_locals);
2132}
2133
2134/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002135 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2136 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2137 * error if the variable was defined with :const.
2138 */
2139 static int
2140check_item_writable(svar_T *sv, int check_writable, char_u *name)
2141{
2142 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2143 || (check_writable == ASSIGN_FINAL
2144 && sv->sv_const == ASSIGN_CONST))
2145 {
2146 semsg(_(e_readonlyvar), name);
2147 return FAIL;
2148 }
2149 return OK;
2150}
2151
2152/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002154 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 * Returns the index in "sn_var_vals" if found.
2156 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002157 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 */
2159 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002160get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161{
2162 hashtab_T *ht;
2163 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002164 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002165 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 int idx;
2167
Bram Moolenaare3d46852020-08-29 13:39:17 +02002168 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002170 if (sid == current_sctx.sc_sid)
2171 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002172 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002173
2174 if (sav == NULL)
2175 return -2;
2176 idx = sav->sav_var_vals_idx;
2177 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002178 if (check_item_writable(sv, check_writable, name) == FAIL)
2179 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002180 return idx;
2181 }
2182
2183 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 ht = &SCRIPT_VARS(sid);
2185 di = find_var_in_ht(ht, 0, name, TRUE);
2186 if (di == NULL)
2187 return -2;
2188
2189 // Now find the svar_T index in sn_var_vals.
2190 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2191 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002192 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 if (sv->sv_tv == &di->di_tv)
2194 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002195 if (check_item_writable(sv, check_writable, name) == FAIL)
2196 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 return idx;
2198 }
2199 }
2200 return -1;
2201}
2202
2203/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002204 * Find "name" in imported items of the current script or in "cctx" if not
2205 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 */
2207 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002208find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 int idx;
2211
Bram Moolenaare3d46852020-08-29 13:39:17 +02002212 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002213 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 if (cctx != NULL)
2215 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2216 {
2217 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2218 + idx;
2219
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002220 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2221 : STRLEN(import->imp_name) == len
2222 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 return import;
2224 }
2225
Bram Moolenaarefa94442020-08-08 22:16:00 +02002226 return find_imported_in_script(name, len, current_sctx.sc_sid);
2227}
2228
2229 imported_T *
2230find_imported_in_script(char_u *name, size_t len, int sid)
2231{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002232 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002233 int idx;
2234
Bram Moolenaare3d46852020-08-29 13:39:17 +02002235 if (!SCRIPT_ID_VALID(sid))
2236 return NULL;
2237 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2239 {
2240 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2241
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002242 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2243 : STRLEN(import->imp_name) == len
2244 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 return import;
2246 }
2247 return NULL;
2248}
2249
2250/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002251 * Free all imported variables.
2252 */
2253 static void
2254free_imported(cctx_T *cctx)
2255{
2256 int idx;
2257
2258 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2259 {
2260 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2261
2262 vim_free(import->imp_name);
2263 }
2264 ga_clear(&cctx->ctx_imports);
2265}
2266
2267/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002268 * Return a pointer to the next line that isn't empty or only contains a
2269 * comment. Skips over white space.
2270 * Returns NULL if there is none.
2271 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002272 char_u *
2273peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002274{
2275 int lnum = cctx->ctx_lnum;
2276
2277 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2278 {
2279 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002280 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002281
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002282 // ignore NULLs inserted for continuation lines
2283 if (line != NULL)
2284 {
2285 p = skipwhite(line);
2286 if (*p != NUL && !vim9_comment_start(p))
2287 return p;
2288 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002289 }
2290 return NULL;
2291}
2292
2293/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002294 * Called when checking for a following operator at "arg". When the rest of
2295 * the line is empty or only a comment, peek the next line. If there is a next
2296 * line return a pointer to it and set "nextp".
2297 * Otherwise skip over white space.
2298 */
2299 static char_u *
2300may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2301{
2302 char_u *p = skipwhite(arg);
2303
2304 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002305 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002306 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002307 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002308 if (*nextp != NULL)
2309 return *nextp;
2310 }
2311 return p;
2312}
2313
2314/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002315 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002316 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002317 * Returns NULL when at the end.
2318 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002319 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002320next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002321{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002322 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002323
2324 do
2325 {
2326 ++cctx->ctx_lnum;
2327 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002328 {
2329 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002330 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002331 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002332 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002333 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002334 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002335 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002336 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002337 return line;
2338}
2339
2340/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002341 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002342 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002343 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002344 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2345 */
2346 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002347may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002348{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002349 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002350 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002351 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002352 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002353
2354 if (next == NULL)
2355 return FAIL;
2356 *arg = skipwhite(next);
2357 }
2358 return OK;
2359}
2360
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002361/*
2362 * Idem, and give an error when failed.
2363 */
2364 static int
2365may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2366{
2367 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2368 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002369 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002370 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002371 return FAIL;
2372 }
2373 return OK;
2374}
2375
2376
Bram Moolenaara5565e42020-05-09 15:44:01 +02002377// Structure passed between the compile_expr* functions to keep track of
2378// constants that have been parsed but for which no code was produced yet. If
2379// possible expressions on these constants are applied at compile time. If
2380// that is not possible, the code to push the constants needs to be generated
2381// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002382// Using 50 should be more than enough of 5 levels of ().
2383#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002384typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002385 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002386 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002387 int pp_is_const; // all generated code was constants, used for a
2388 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002389} ppconst_T;
2390
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002391static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002392static int compile_expr0(char_u **arg, cctx_T *cctx);
2393static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2394
Bram Moolenaara5565e42020-05-09 15:44:01 +02002395/*
2396 * Generate a PUSH instruction for "tv".
2397 * "tv" will be consumed or cleared.
2398 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2399 */
2400 static int
2401generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2402{
2403 if (tv != NULL)
2404 {
2405 switch (tv->v_type)
2406 {
2407 case VAR_UNKNOWN:
2408 break;
2409 case VAR_BOOL:
2410 generate_PUSHBOOL(cctx, tv->vval.v_number);
2411 break;
2412 case VAR_SPECIAL:
2413 generate_PUSHSPEC(cctx, tv->vval.v_number);
2414 break;
2415 case VAR_NUMBER:
2416 generate_PUSHNR(cctx, tv->vval.v_number);
2417 break;
2418#ifdef FEAT_FLOAT
2419 case VAR_FLOAT:
2420 generate_PUSHF(cctx, tv->vval.v_float);
2421 break;
2422#endif
2423 case VAR_BLOB:
2424 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2425 tv->vval.v_blob = NULL;
2426 break;
2427 case VAR_STRING:
2428 generate_PUSHS(cctx, tv->vval.v_string);
2429 tv->vval.v_string = NULL;
2430 break;
2431 default:
2432 iemsg("constant type not supported");
2433 clear_tv(tv);
2434 return FAIL;
2435 }
2436 tv->v_type = VAR_UNKNOWN;
2437 }
2438 return OK;
2439}
2440
2441/*
2442 * Generate code for any ppconst entries.
2443 */
2444 static int
2445generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2446{
2447 int i;
2448 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002449 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002450
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002451 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002452 for (i = 0; i < ppconst->pp_used; ++i)
2453 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2454 ret = FAIL;
2455 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002456 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002457 return ret;
2458}
2459
2460/*
2461 * Clear ppconst constants. Used when failing.
2462 */
2463 static void
2464clear_ppconst(ppconst_T *ppconst)
2465{
2466 int i;
2467
2468 for (i = 0; i < ppconst->pp_used; ++i)
2469 clear_tv(&ppconst->pp_tv[i]);
2470 ppconst->pp_used = 0;
2471}
2472
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002473/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002474 * Generate an instruction to load script-local variable "name", without the
2475 * leading "s:".
2476 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 */
2478 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002479compile_load_scriptvar(
2480 cctx_T *cctx,
2481 char_u *name, // variable NUL terminated
2482 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002483 char_u **end, // end of variable
2484 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002486 scriptitem_T *si;
2487 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 imported_T *import;
2489
Bram Moolenaare3d46852020-08-29 13:39:17 +02002490 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2491 return FAIL;
2492 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002493 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002494 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002496 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002497 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2498 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 }
2500 if (idx >= 0)
2501 {
2502 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2503
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002504 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 current_sctx.sc_sid, idx, sv->sv_type);
2506 return OK;
2507 }
2508
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002509 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 if (import != NULL)
2511 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002512 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002513 {
2514 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002515 char_u *exp_name;
2516 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002517 ufunc_T *ufunc;
2518 type_T *type;
2519
2520 // Used "import * as Name", need to lookup the member.
2521 if (*p != '.')
2522 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002523 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002524 return FAIL;
2525 }
2526 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002527 if (VIM_ISWHITE(*p))
2528 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002529 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002530 return FAIL;
2531 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002532
Bram Moolenaar1c991142020-07-04 13:15:31 +02002533 // isolate one name
2534 exp_name = p;
2535 while (eval_isnamec(*p))
2536 ++p;
2537 cc = *p;
2538 *p = NUL;
2539
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002540 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002541 *p = cc;
2542 p = skipwhite(p);
2543
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002544 // TODO: what if it is a function?
2545 if (idx < 0)
2546 return FAIL;
2547 *end = p;
2548
2549 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2550 import->imp_sid,
2551 idx,
2552 type);
2553 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002554 else if (import->imp_funcname != NULL)
2555 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002556 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002557 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2558 import->imp_sid,
2559 import->imp_var_vals_idx,
2560 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 return OK;
2562 }
2563
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002564 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002565 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 return FAIL;
2567}
2568
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002569 static int
2570generate_funcref(cctx_T *cctx, char_u *name)
2571{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002572 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002573
2574 if (ufunc == NULL)
2575 return FAIL;
2576
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002577 // Need to compile any default values to get the argument types.
2578 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2579 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2580 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002581 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002582}
2583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584/*
2585 * Compile a variable name into a load instruction.
2586 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002587 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 * When "error" is FALSE do not give an error when not found.
2589 */
2590 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002591compile_load(
2592 char_u **arg,
2593 char_u *end_arg,
2594 cctx_T *cctx,
2595 int is_expr,
2596 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597{
2598 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002599 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002600 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002602 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603
2604 if (*(*arg + 1) == ':')
2605 {
2606 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002607 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002608 {
2609 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002611 switch (**arg)
2612 {
2613 case 'g': isn_type = ISN_LOADGDICT; break;
2614 case 'w': isn_type = ISN_LOADWDICT; break;
2615 case 't': isn_type = ISN_LOADTDICT; break;
2616 case 'b': isn_type = ISN_LOADBDICT; break;
2617 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002618 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002619 goto theend;
2620 }
2621 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2622 goto theend;
2623 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 else
2626 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002627 isntype_T isn_type = ISN_DROP;
2628
2629 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2630 if (name == NULL)
2631 return FAIL;
2632
2633 switch (**arg)
2634 {
2635 case 'v': res = generate_LOADV(cctx, name, error);
2636 break;
2637 case 's': res = compile_load_scriptvar(cctx, name,
2638 NULL, NULL, error);
2639 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002640 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2641 isn_type = ISN_LOADG;
2642 else
2643 {
2644 isn_type = ISN_LOADAUTO;
2645 vim_free(name);
2646 name = vim_strnsave(*arg, end - *arg);
2647 if (name == NULL)
2648 return FAIL;
2649 }
2650 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002651 case 'w': isn_type = ISN_LOADW; break;
2652 case 't': isn_type = ISN_LOADT; break;
2653 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002654 default: // cannot happen, just in case
2655 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002656 goto theend;
2657 }
2658 if (isn_type != ISN_DROP)
2659 {
2660 // Global, Buffer-local, Window-local and Tabpage-local
2661 // variables can be defined later, thus we don't check if it
2662 // exists, give error at runtime.
2663 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 }
2666 }
2667 else
2668 {
2669 size_t len = end - *arg;
2670 int idx;
2671 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002672 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673
2674 name = vim_strnsave(*arg, end - *arg);
2675 if (name == NULL)
2676 return FAIL;
2677
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002678 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002680 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002681 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 }
2683 else
2684 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002685 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002686
Bram Moolenaar709664c2020-12-12 14:33:41 +01002687 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002689 type = lvar.lv_type;
2690 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002691 if (lvar.lv_from_outer != 0)
2692 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002693 else
2694 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 }
2696 else
2697 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002698 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002699 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002700 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002701 || find_imported(name, 0, cctx) != NULL)
2702 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002703
Bram Moolenaar0f769812020-09-12 18:32:34 +02002704 // When evaluating an expression and the name starts with an
2705 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002706 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002707 if (res == FAIL && is_expr
2708 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002709 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 }
2711 }
2712 if (gen_load)
2713 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002714 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002715 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002716 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002717 cctx->ctx_outer_used = TRUE;
2718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 }
2720
2721 *arg = end;
2722
2723theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002724 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002725 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 vim_free(name);
2727 return res;
2728}
2729
2730/*
2731 * Compile the argument expressions.
2732 * "arg" points to just after the "(" and is advanced to after the ")"
2733 */
2734 static int
2735compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2736{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002737 char_u *p = *arg;
2738 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002739 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740
Bram Moolenaare6085c52020-04-12 20:19:16 +02002741 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002743 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2744 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002745 if (*p == ')')
2746 {
2747 *arg = p + 1;
2748 return OK;
2749 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002750 if (must_end)
2751 {
2752 semsg(_(e_missing_comma_before_argument_str), p);
2753 return FAIL;
2754 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002755
Bram Moolenaara5565e42020-05-09 15:44:01 +02002756 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 return FAIL;
2758 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002759
2760 if (*p != ',' && *skipwhite(p) == ',')
2761 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002762 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002763 p = skipwhite(p);
2764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002766 {
2767 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002768 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002769 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002770 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002771 else
2772 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002773 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002774 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002776failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002777 emsg(_(e_missing_close));
2778 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779}
2780
2781/*
2782 * Compile a function call: name(arg1, arg2)
2783 * "arg" points to "name", "arg + varlen" to the "(".
2784 * "argcount_init" is 1 for "value->method()"
2785 * Instructions:
2786 * EVAL arg1
2787 * EVAL arg2
2788 * BCALL / DCALL / UCALL
2789 */
2790 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002791compile_call(
2792 char_u **arg,
2793 size_t varlen,
2794 cctx_T *cctx,
2795 ppconst_T *ppconst,
2796 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797{
2798 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002799 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 int argcount = argcount_init;
2801 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002802 char_u fname_buf[FLEN_FIXED + 1];
2803 char_u *tofree = NULL;
2804 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002805 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002806 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002807 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808
Bram Moolenaara5565e42020-05-09 15:44:01 +02002809 // we can evaluate "has('name')" at compile time
2810 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2811 {
2812 char_u *s = skipwhite(*arg + varlen + 1);
2813 typval_T argvars[2];
2814
2815 argvars[0].v_type = VAR_UNKNOWN;
2816 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002817 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002819 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002820 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002821 if (*s == ')' && argvars[0].v_type == VAR_STRING
2822 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002823 {
2824 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2825
2826 *arg = s + 1;
2827 argvars[1].v_type = VAR_UNKNOWN;
2828 tv->v_type = VAR_NUMBER;
2829 tv->vval.v_number = 0;
2830 f_has(argvars, tv);
2831 clear_tv(&argvars[0]);
2832 ++ppconst->pp_used;
2833 return OK;
2834 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002835 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002836 }
2837
2838 if (generate_ppconst(cctx, ppconst) == FAIL)
2839 return FAIL;
2840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 if (varlen >= sizeof(namebuf))
2842 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002843 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 return FAIL;
2845 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002846 vim_strncpy(namebuf, *arg, varlen);
2847 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 *arg = skipwhite(*arg + varlen + 1);
2850 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852
Bram Moolenaar03290b82020-12-19 16:30:44 +01002853 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002854 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 {
2856 int idx;
2857
2858 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002859 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002861 {
2862 if (STRCMP(name, "add") == 0 && argcount == 2)
2863 {
2864 garray_T *stack = &cctx->ctx_type_stack;
2865 type_T *type = ((type_T **)stack->ga_data)[
2866 stack->ga_len - 2];
2867
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002868 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002869 if (type->tt_type == VAR_LIST)
2870 {
2871 // inline "add(list, item)" so that the type can be checked
2872 res = generate_LISTAPPEND(cctx);
2873 idx = -1;
2874 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002875 else if (type->tt_type == VAR_BLOB)
2876 {
2877 // inline "add(blob, nr)" so that the type can be checked
2878 res = generate_BLOBAPPEND(cctx);
2879 idx = -1;
2880 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002881 }
2882
2883 if (idx >= 0)
2884 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2885 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002886 else
2887 semsg(_(e_unknownfunc), namebuf);
2888 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 }
2890
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002891 // An argument or local variable can be a function reference, this
2892 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002893 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002894 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002895 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002896 // If we can find the function by name generate the right call.
2897 // Skip global functions here, a local funcref takes precedence.
2898 ufunc = find_func(name, FALSE, cctx);
2899 if (ufunc != NULL && !func_is_global(ufunc))
2900 {
2901 res = generate_CALL(cctx, ufunc, argcount);
2902 goto theend;
2903 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905
2906 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002907 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002908 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002910 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002911 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002912 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002913 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002914 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002915
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002916 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002917 goto theend;
2918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919
Bram Moolenaar0f769812020-09-12 18:32:34 +02002920 // If we can find a global function by name generate the right call.
2921 if (ufunc != NULL)
2922 {
2923 res = generate_CALL(cctx, ufunc, argcount);
2924 goto theend;
2925 }
2926
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002927 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002928 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002929 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002930 res = generate_UCALL(cctx, name, argcount);
2931 else
2932 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002933
2934theend:
2935 vim_free(tofree);
2936 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937}
2938
2939// like NAMESPACE_CHAR but with 'a' and 'l'.
2940#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2941
2942/*
2943 * Find the end of a variable or function name. Unlike find_name_end() this
2944 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002945 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 * Return a pointer to just after the name. Equal to "arg" if there is no
2947 * valid name.
2948 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002949 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002950to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951{
2952 char_u *p;
2953
2954 // Quick check for valid starting character.
2955 if (!eval_isnamec1(*arg))
2956 return arg;
2957
2958 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2959 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2960 // and can be used in slice "[n:]".
2961 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002962 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2964 break;
2965 return p;
2966}
2967
2968/*
2969 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002970 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 */
2972 char_u *
2973to_name_const_end(char_u *arg)
2974{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002975 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 typval_T rettv;
2977
2978 if (p == arg && *arg == '[')
2979 {
2980
2981 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002982 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 p = arg;
2984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 return p;
2986}
2987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988/*
2989 * parse a list: [expr, expr]
2990 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002991 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 */
2993 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002994compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995{
2996 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002997 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002999 int is_const;
3000 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003002 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003004 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003005 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003006 semsg(_(e_list_end), *arg);
3007 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003008 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003009 if (*p == ',')
3010 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003011 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02003012 return FAIL;
3013 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003014 if (*p == ']')
3015 {
3016 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003017 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003018 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003019 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003020 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003021 if (!is_const)
3022 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 ++count;
3024 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003025 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003027 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3028 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003029 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003030 return FAIL;
3031 }
3032 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003033 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 p = skipwhite(p);
3035 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003036 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003038 ppconst->pp_is_const = is_all_const;
3039 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040}
3041
3042/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003043 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003045 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 */
3047 static int
3048compile_lambda(char_u **arg, cctx_T *cctx)
3049{
Bram Moolenaare462f522020-12-27 14:43:30 +01003050 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 typval_T rettv;
3052 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003053 evalarg_T evalarg;
3054
3055 CLEAR_FIELD(evalarg);
3056 evalarg.eval_flags = EVAL_EVALUATE;
3057 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
3059 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003060 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3061 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003062 {
3063 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003064 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003065 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003066
Bram Moolenaar65c44152020-12-24 15:14:01 +01003067 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003069 ++ufunc->uf_refcount;
3070 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071
Bram Moolenaar65c44152020-12-24 15:14:01 +01003072 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003073 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003075 clear_evalarg(&evalarg, NULL);
3076
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003077 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003078 {
3079 // The return type will now be known.
3080 set_function_type(ufunc);
3081
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003082 // The function reference count will be 1. When the ISN_FUNCREF
3083 // instruction is deleted the reference count is decremented and the
3084 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003085 return generate_FUNCREF(cctx, ufunc);
3086 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003087
3088 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 return FAIL;
3090}
3091
3092/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003093 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003095 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 */
3097 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003098compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099{
3100 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003101 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 int count = 0;
3103 dict_T *d = dict_alloc();
3104 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003105 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003106 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003107 int is_const;
3108 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109
3110 if (d == NULL)
3111 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003112 if (generate_ppconst(cctx, ppconst) == FAIL)
3113 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003114 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003116 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
Bram Moolenaar23c55272020-06-21 16:58:13 +02003118 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003119 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003120 *arg = NULL;
3121 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 }
3123
3124 if (**arg == '}')
3125 break;
3126
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003127 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003129 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003131 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003132 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003133 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3136 if (isn->isn_type == ISN_PUSHS)
3137 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003138 else
3139 {
3140 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003141 [stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003142 if (need_type(keytype, &t_string, -1, 0, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003143 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003144 return FAIL;
3145 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003146 *arg = skipwhite(*arg);
3147 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003148 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003149 emsg(_(e_missing_matching_bracket_after_dict_key));
3150 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003151 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003152 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003154 else
3155 {
3156 // {"name": value},
3157 // {'name': value},
3158 // {name: value} use "name" as a literal key
3159 key = get_literal_key(arg);
3160 if (key == NULL)
3161 return FAIL;
3162 if (generate_PUSHS(cctx, key) == FAIL)
3163 return FAIL;
3164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165
3166 // Check for duplicate keys, if using string keys.
3167 if (key != NULL)
3168 {
3169 item = dict_find(d, key, -1);
3170 if (item != NULL)
3171 {
3172 semsg(_(e_duplicate_key), key);
3173 goto failret;
3174 }
3175 item = dictitem_alloc(key);
3176 if (item != NULL)
3177 {
3178 item->di_tv.v_type = VAR_UNKNOWN;
3179 item->di_tv.v_lock = 0;
3180 if (dict_add(d, item) == FAIL)
3181 dictitem_free(item);
3182 }
3183 }
3184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 if (**arg != ':')
3186 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003187 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003188 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003189 else
3190 semsg(_(e_missing_dict_colon), *arg);
3191 return FAIL;
3192 }
3193 whitep = *arg + 1;
3194 if (!IS_WHITE_OR_NUL(*whitep))
3195 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003196 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 return FAIL;
3198 }
3199
Bram Moolenaar23c55272020-06-21 16:58:13 +02003200 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003201 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003202 *arg = NULL;
3203 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003204 }
3205
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003206 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003208 if (!is_const)
3209 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 ++count;
3211
Bram Moolenaar2c330432020-04-13 14:41:35 +02003212 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003213 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003214 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003215 *arg = NULL;
3216 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 if (**arg == '}')
3219 break;
3220 if (**arg != ',')
3221 {
3222 semsg(_(e_missing_dict_comma), *arg);
3223 goto failret;
3224 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003225 if (IS_WHITE_OR_NUL(*whitep))
3226 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003227 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003228 return FAIL;
3229 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003230 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003231 if (!IS_WHITE_OR_NUL(*whitep))
3232 {
3233 semsg(_(e_white_space_required_after_str), ",");
3234 return FAIL;
3235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 *arg = skipwhite(*arg + 1);
3237 }
3238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 *arg = *arg + 1;
3240
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003241 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003242 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003243 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003244 *arg += STRLEN(*arg);
3245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003247 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 return generate_NEWDICT(cctx, count);
3249
3250failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003251 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003252 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003253 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003254 *arg = (char_u *)"";
3255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 dict_unref(d);
3257 return FAIL;
3258}
3259
3260/*
3261 * Compile "&option".
3262 */
3263 static int
3264compile_get_option(char_u **arg, cctx_T *cctx)
3265{
3266 typval_T rettv;
3267 char_u *start = *arg;
3268 int ret;
3269
3270 // parse the option and get the current value to get the type.
3271 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003272 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 if (ret == OK)
3274 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003275 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003276 char_u *name = vim_strnsave(start, *arg - start);
3277 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3278 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
3280 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3281 vim_free(name);
3282 }
3283 clear_tv(&rettv);
3284
3285 return ret;
3286}
3287
3288/*
3289 * Compile "$VAR".
3290 */
3291 static int
3292compile_get_env(char_u **arg, cctx_T *cctx)
3293{
3294 char_u *start = *arg;
3295 int len;
3296 int ret;
3297 char_u *name;
3298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 ++*arg;
3300 len = get_env_len(arg);
3301 if (len == 0)
3302 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003303 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 return FAIL;
3305 }
3306
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003307 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 name = vim_strnsave(start, len + 1);
3309 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3310 vim_free(name);
3311 return ret;
3312}
3313
3314/*
3315 * Compile "@r".
3316 */
3317 static int
3318compile_get_register(char_u **arg, cctx_T *cctx)
3319{
3320 int ret;
3321
3322 ++*arg;
3323 if (**arg == NUL)
3324 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003325 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 return FAIL;
3327 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003328 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 {
3330 emsg_invreg(**arg);
3331 return FAIL;
3332 }
3333 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3334 ++*arg;
3335 return ret;
3336}
3337
3338/*
3339 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003340 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 */
3342 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003343apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003345 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
3347 // this works from end to start
3348 while (p > start)
3349 {
3350 --p;
3351 if (*p == '-' || *p == '+')
3352 {
3353 // only '-' has an effect, for '+' we only check the type
3354#ifdef FEAT_FLOAT
3355 if (rettv->v_type == VAR_FLOAT)
3356 {
3357 if (*p == '-')
3358 rettv->vval.v_float = -rettv->vval.v_float;
3359 }
3360 else
3361#endif
3362 {
3363 varnumber_T val;
3364 int error = FALSE;
3365
3366 // tv_get_number_chk() accepts a string, but we don't want that
3367 // here
3368 if (check_not_string(rettv) == FAIL)
3369 return FAIL;
3370 val = tv_get_number_chk(rettv, &error);
3371 clear_tv(rettv);
3372 if (error)
3373 return FAIL;
3374 if (*p == '-')
3375 val = -val;
3376 rettv->v_type = VAR_NUMBER;
3377 rettv->vval.v_number = val;
3378 }
3379 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003380 else if (numeric_only)
3381 {
3382 ++p;
3383 break;
3384 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003385 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 {
3387 int v = tv2bool(rettv);
3388
3389 // '!' is permissive in the type.
3390 clear_tv(rettv);
3391 rettv->v_type = VAR_BOOL;
3392 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3393 }
3394 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003395 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 return OK;
3397}
3398
3399/*
3400 * Recognize v: variables that are constants and set "rettv".
3401 */
3402 static void
3403get_vim_constant(char_u **arg, typval_T *rettv)
3404{
3405 if (STRNCMP(*arg, "v:true", 6) == 0)
3406 {
3407 rettv->v_type = VAR_BOOL;
3408 rettv->vval.v_number = VVAL_TRUE;
3409 *arg += 6;
3410 }
3411 else if (STRNCMP(*arg, "v:false", 7) == 0)
3412 {
3413 rettv->v_type = VAR_BOOL;
3414 rettv->vval.v_number = VVAL_FALSE;
3415 *arg += 7;
3416 }
3417 else if (STRNCMP(*arg, "v:null", 6) == 0)
3418 {
3419 rettv->v_type = VAR_SPECIAL;
3420 rettv->vval.v_number = VVAL_NULL;
3421 *arg += 6;
3422 }
3423 else if (STRNCMP(*arg, "v:none", 6) == 0)
3424 {
3425 rettv->v_type = VAR_SPECIAL;
3426 rettv->vval.v_number = VVAL_NONE;
3427 *arg += 6;
3428 }
3429}
3430
Bram Moolenaar657137c2021-01-09 15:45:23 +01003431 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003432get_compare_type(char_u *p, int *len, int *type_is)
3433{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003434 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003435 int i;
3436
3437 switch (p[0])
3438 {
3439 case '=': if (p[1] == '=')
3440 type = EXPR_EQUAL;
3441 else if (p[1] == '~')
3442 type = EXPR_MATCH;
3443 break;
3444 case '!': if (p[1] == '=')
3445 type = EXPR_NEQUAL;
3446 else if (p[1] == '~')
3447 type = EXPR_NOMATCH;
3448 break;
3449 case '>': if (p[1] != '=')
3450 {
3451 type = EXPR_GREATER;
3452 *len = 1;
3453 }
3454 else
3455 type = EXPR_GEQUAL;
3456 break;
3457 case '<': if (p[1] != '=')
3458 {
3459 type = EXPR_SMALLER;
3460 *len = 1;
3461 }
3462 else
3463 type = EXPR_SEQUAL;
3464 break;
3465 case 'i': if (p[1] == 's')
3466 {
3467 // "is" and "isnot"; but not a prefix of a name
3468 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3469 *len = 5;
3470 i = p[*len];
3471 if (!isalnum(i) && i != '_')
3472 {
3473 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3474 *type_is = TRUE;
3475 }
3476 }
3477 break;
3478 }
3479 return type;
3480}
3481
3482/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003483 * Skip over an expression, ignoring most errors.
3484 */
3485 static void
3486skip_expr_cctx(char_u **arg, cctx_T *cctx)
3487{
3488 evalarg_T evalarg;
3489
3490 CLEAR_FIELD(evalarg);
3491 evalarg.eval_cctx = cctx;
3492 skip_expr(arg, &evalarg);
3493}
3494
3495/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003497 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 */
3499 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003500compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003502 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503
3504 // this works from end to start
3505 while (p > start)
3506 {
3507 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003508 while (VIM_ISWHITE(*p))
3509 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 if (*p == '-' || *p == '+')
3511 {
3512 int negate = *p == '-';
3513 isn_T *isn;
3514
3515 // TODO: check type
3516 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3517 {
3518 --p;
3519 if (*p == '-')
3520 negate = !negate;
3521 }
3522 // only '-' has an effect, for '+' we only check the type
3523 if (negate)
3524 isn = generate_instr(cctx, ISN_NEGATENR);
3525 else
3526 isn = generate_instr(cctx, ISN_CHECKNR);
3527 if (isn == NULL)
3528 return FAIL;
3529 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003530 else if (numeric_only)
3531 {
3532 ++p;
3533 break;
3534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 else
3536 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003537 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003539 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003541 if (p[-1] == '!')
3542 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 }
3545 if (generate_2BOOL(cctx, invert) == FAIL)
3546 return FAIL;
3547 }
3548 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003549 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 return OK;
3551}
3552
3553/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003554 * Compile "(expression)": recursive!
3555 * Return FAIL/OK.
3556 */
3557 static int
3558compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3559{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003560 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003561 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003562
Bram Moolenaar24156692021-01-14 20:35:49 +01003563 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3564 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003565 if (ppconst->pp_used <= PPSIZE - 10)
3566 {
3567 ret = compile_expr1(arg, cctx, ppconst);
3568 }
3569 else
3570 {
3571 // Not enough space in ppconst, flush constants.
3572 if (generate_ppconst(cctx, ppconst) == FAIL)
3573 return FAIL;
3574 ret = compile_expr0(arg, cctx);
3575 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003576 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3577 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003578 if (**arg == ')')
3579 ++*arg;
3580 else if (ret == OK)
3581 {
3582 emsg(_(e_missing_close));
3583 ret = FAIL;
3584 }
3585 return ret;
3586}
3587
3588/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003590 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 */
3592 static int
3593compile_subscript(
3594 char_u **arg,
3595 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003596 char_u *start_leader,
3597 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003598 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003600 char_u *name_start = *end_leader;
3601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 for (;;)
3603 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003604 char_u *p = skipwhite(*arg);
3605
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003606 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003607 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003608 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003609
3610 // If a following line starts with "->{" or "->X" advance to that
3611 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003612 // Also if a following line starts with ".x".
3613 if (next != NULL &&
3614 ((next[0] == '-' && next[1] == '>'
3615 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003616 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003617 {
3618 next = next_line_from_context(cctx, TRUE);
3619 if (next == NULL)
3620 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003621 *arg = next;
3622 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003623 }
3624 }
3625
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003626 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003627 // not a function call.
3628 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003630 garray_T *stack = &cctx->ctx_type_stack;
3631 type_T *type;
3632 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003634 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003635 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003636 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003639 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3640
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003641 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3643 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003644 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 return FAIL;
3646 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003647 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003649 char_u *pstart = p;
3650
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003651 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003652 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003653 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 // something->method()
3656 // Apply the '!', '-' and '+' first:
3657 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003658 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003661 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003662 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003663 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003664 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003665 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003666 int argcount = 1;
3667 char_u *expr;
3668 garray_T *stack;
3669 type_T *type;
3670
3671 // Funcref call: list->(Refs[2])(arg)
3672 // or lambda: list->((arg) => expr)(arg)
3673 // Fist compile the arguments.
3674 expr = *arg;
3675 *arg = skipwhite(*arg + 1);
3676 skip_expr_cctx(arg, cctx);
3677 *arg = skipwhite(*arg);
3678 if (**arg != ')')
3679 {
3680 semsg(_(e_missing_paren), *arg);
3681 return FAIL;
3682 }
3683 ++*arg;
3684 if (**arg != '(')
3685 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003686 if (*skipwhite(*arg) == '(')
3687 emsg(_(e_nowhitespace));
3688 else
3689 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003690 return FAIL;
3691 }
3692
3693 *arg = skipwhite(*arg + 1);
3694 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3695 return FAIL;
3696
3697 // Compile the function expression.
3698 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3699 return FAIL;
3700
3701 stack = &cctx->ctx_type_stack;
3702 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3703 if (generate_PCALL(cctx, argcount,
3704 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
3706 }
3707 else
3708 {
3709 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003710 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003711 if (!eval_isnamec1(*p))
3712 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003713 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003714 return FAIL;
3715 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003716 if (ASCII_ISALPHA(*p) && p[1] == ':')
3717 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003718 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 ;
3720 if (*p != '(')
3721 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003722 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 return FAIL;
3724 }
3725 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003726 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 return FAIL;
3728 }
3729 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003730 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003732 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003733 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003734 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003735 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003736 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003737
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003738 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003739 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003740 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003741 // TODO: blob index
3742 // TODO: more arguments
3743 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003744 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003746 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003747
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003748 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003749 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003750 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003751 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003752 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003753 // missing first index is equal to zero
3754 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003755 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003756 else
3757 {
3758 if (compile_expr0(arg, cctx) == FAIL)
3759 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003760 if (**arg == ':')
3761 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003762 semsg(_(e_white_space_required_before_and_after_str_at_str),
3763 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003764 return FAIL;
3765 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003766 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003767 return FAIL;
3768 *arg = skipwhite(*arg);
3769 }
3770 if (**arg == ':')
3771 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003772 is_slice = TRUE;
3773 ++*arg;
3774 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3775 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003776 semsg(_(e_white_space_required_before_and_after_str_at_str),
3777 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003778 return FAIL;
3779 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003780 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003781 return FAIL;
3782 if (**arg == ']')
3783 // missing second index is equal to end of string
3784 generate_PUSHNR(cctx, -1);
3785 else
3786 {
3787 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003788 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003789 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003790 return FAIL;
3791 *arg = skipwhite(*arg);
3792 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794
3795 if (**arg != ']')
3796 {
3797 emsg(_(e_missbrac));
3798 return FAIL;
3799 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003800 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003802 // We can index a list and a dict. If we don't know the type
3803 // we can use the index value type.
3804 // TODO: If we don't know use an instruction to figure it out at
3805 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003806 typep = ((type_T **)stack->ga_data) + stack->ga_len
3807 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003808 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003809 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3810 // If the index is a string, the variable must be a Dict.
3811 if (*typep == &t_any && valtype == &t_string)
3812 vtype = VAR_DICT;
3813 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003814 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003815 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003816 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003817 return FAIL;
3818 if (is_slice)
3819 {
3820 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003821 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003822 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003823 return FAIL;
3824 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003825 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003826
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003827 if (vtype == VAR_DICT)
3828 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003829 if (is_slice)
3830 {
3831 emsg(_(e_cannot_slice_dictionary));
3832 return FAIL;
3833 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003834 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003835 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003836 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003837 if (*typep == &t_unknown)
3838 // empty dict was used
3839 *typep = &t_any;
3840 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003841 else
3842 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003843 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003844 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003845 return FAIL;
3846 *typep = &t_any;
3847 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003848 if (may_generate_2STRING(-1, cctx) == FAIL)
3849 return FAIL;
3850 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3851 return FAIL;
3852 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003853 else if (vtype == VAR_STRING)
3854 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003855 *typep = &t_string;
3856 if ((is_slice
3857 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3858 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003859 return FAIL;
3860 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003861 else if (vtype == VAR_BLOB)
3862 {
3863 emsg("Sorry, blob index and slice not implemented yet");
3864 return FAIL;
3865 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003866 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003867 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003868 if (is_slice)
3869 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003870 if (generate_instr_drop(cctx,
3871 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3872 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003873 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003874 }
Bram Moolenaared591872020-08-15 22:14:53 +02003875 else
3876 {
3877 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003878 {
Bram Moolenaared591872020-08-15 22:14:53 +02003879 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003880 if (*typep == &t_unknown)
3881 // empty list was used
3882 *typep = &t_any;
3883 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003884 if (generate_instr_drop(cctx,
3885 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3886 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003887 return FAIL;
3888 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003889 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003890 else
3891 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003892 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003893 return FAIL;
3894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003896 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003898 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003899 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003900 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003901 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003902
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003903 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003904 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003905 {
3906 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003907 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003908 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003909 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003910 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 while (eval_isnamec(*p))
3912 MB_PTR_ADV(p);
3913 if (p == *arg)
3914 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003915 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 return FAIL;
3917 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003918 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 return FAIL;
3920 *arg = p;
3921 }
3922 else
3923 break;
3924 }
3925
3926 // TODO - see handle_subscript():
3927 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3928 // Don't do this when "Func" is already a partial that was bound
3929 // explicitly (pt_auto is FALSE).
3930
3931 return OK;
3932}
3933
3934/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003935 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3936 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003938 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003939 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003940 *
3941 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 */
3943
3944/*
3945 * number number constant
3946 * 0zFFFFFFFF Blob constant
3947 * "string" string constant
3948 * 'string' literal string constant
3949 * &option-name option value
3950 * @r register contents
3951 * identifier variable value
3952 * function() function call
3953 * $VAR environment variable
3954 * (expression) nested expression
3955 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003956 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 *
3958 * Also handle:
3959 * ! in front logical NOT
3960 * - in front unary minus
3961 * + in front unary plus (ignored)
3962 * trailing (arg) funcref/partial call
3963 * trailing [] subscript in String or List
3964 * trailing .name entry in Dictionary
3965 * trailing ->name() method call
3966 */
3967 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003968compile_expr7(
3969 char_u **arg,
3970 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003971 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 char_u *start_leader, *end_leader;
3974 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003975 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003976 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003978 ppconst->pp_is_const = FALSE;
3979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 /*
3981 * Skip '!', '-' and '+' characters. They are handled later.
3982 */
3983 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003984 if (eval_leader(arg, TRUE) == FAIL)
3985 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 end_leader = *arg;
3987
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003988 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 switch (**arg)
3990 {
3991 /*
3992 * Number constant.
3993 */
3994 case '0': // also for blob starting with 0z
3995 case '1':
3996 case '2':
3997 case '3':
3998 case '4':
3999 case '5':
4000 case '6':
4001 case '7':
4002 case '8':
4003 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004004 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004006 // Apply "-" and "+" just before the number now, right to
4007 // left. Matters especially when "->" follows. Stops at
4008 // '!'.
4009 if (apply_leader(rettv, TRUE,
4010 start_leader, &end_leader) == FAIL)
4011 {
4012 clear_tv(rettv);
4013 return FAIL;
4014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 break;
4016
4017 /*
4018 * String constant: "string".
4019 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004020 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 return FAIL;
4022 break;
4023
4024 /*
4025 * Literal string constant: 'str''ing'.
4026 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004027 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 return FAIL;
4029 break;
4030
4031 /*
4032 * Constant Vim variable.
4033 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004034 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 ret = NOTDONE;
4036 break;
4037
4038 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004039 * "true" constant
4040 */
4041 case 't': if (STRNCMP(*arg, "true", 4) == 0
4042 && !eval_isnamec((*arg)[4]))
4043 {
4044 *arg += 4;
4045 rettv->v_type = VAR_BOOL;
4046 rettv->vval.v_number = VVAL_TRUE;
4047 }
4048 else
4049 ret = NOTDONE;
4050 break;
4051
4052 /*
4053 * "false" constant
4054 */
4055 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4056 && !eval_isnamec((*arg)[5]))
4057 {
4058 *arg += 5;
4059 rettv->v_type = VAR_BOOL;
4060 rettv->vval.v_number = VVAL_FALSE;
4061 }
4062 else
4063 ret = NOTDONE;
4064 break;
4065
4066 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004067 * "null" constant
4068 */
4069 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4070 && !eval_isnamec((*arg)[5]))
4071 {
4072 *arg += 4;
4073 rettv->v_type = VAR_SPECIAL;
4074 rettv->vval.v_number = VVAL_NULL;
4075 }
4076 else
4077 ret = NOTDONE;
4078 break;
4079
4080 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 * List: [expr, expr]
4082 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004083 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 break;
4085
4086 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 * Dictionary: {'key': val, 'key': val}
4088 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004089 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 break;
4091
4092 /*
4093 * Option value: &name
4094 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004095 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4096 return FAIL;
4097 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 break;
4099
4100 /*
4101 * Environment variable: $VAR.
4102 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004103 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4104 return FAIL;
4105 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 break;
4107
4108 /*
4109 * Register contents: @r.
4110 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004111 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4112 return FAIL;
4113 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 break;
4115 /*
4116 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004117 * lambda: (arg, arg) => expr
4118 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004120 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4121 ret = compile_lambda(arg, cctx);
4122 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004123 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 break;
4125
4126 default: ret = NOTDONE;
4127 break;
4128 }
4129 if (ret == FAIL)
4130 return FAIL;
4131
Bram Moolenaar1c747212020-05-09 18:28:34 +02004132 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004134 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 clear_tv(rettv);
4136 else
4137 // A constant expression can possibly be handled compile time,
4138 // return the value instead of generating code.
4139 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 }
4141 else if (ret == NOTDONE)
4142 {
4143 char_u *p;
4144 int r;
4145
4146 if (!eval_isnamec1(**arg))
4147 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004148 if (ends_excmd(*skipwhite(*arg)))
4149 semsg(_(e_empty_expression_str), *arg);
4150 else
4151 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 return FAIL;
4153 }
4154
4155 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004156 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 {
4159 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 {
4163 if (generate_ppconst(cctx, ppconst) == FAIL)
4164 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004165 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 if (r == FAIL)
4168 return FAIL;
4169 }
4170
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004171 // Handle following "[]", ".member", etc.
4172 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004173 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174 ppconst) == FAIL)
4175 return FAIL;
4176 if (ppconst->pp_used > 0)
4177 {
4178 // apply the '!', '-' and '+' before the constant
4179 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004180 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 return FAIL;
4182 return OK;
4183 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004184 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004186 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187}
4188
4189/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004190 * Give the "white on both sides" error, taking the operator from "p[len]".
4191 */
4192 void
4193error_white_both(char_u *op, int len)
4194{
4195 char_u buf[10];
4196
4197 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004198 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004199}
4200
4201/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004202 * <type>expr7: runtime type check / conversion
4203 */
4204 static int
4205compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4206{
4207 type_T *want_type = NULL;
4208
4209 // Recognize <type>
4210 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4211 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004212 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004213 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4214 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004215 return FAIL;
4216
4217 if (**arg != '>')
4218 {
4219 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004220 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004221 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004222 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004223 return FAIL;
4224 }
4225 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004226 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004227 return FAIL;
4228 }
4229
4230 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4231 return FAIL;
4232
4233 if (want_type != NULL)
4234 {
4235 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004236 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004237
Bram Moolenaard1103582020-08-14 22:44:25 +02004238 generate_ppconst(cctx, ppconst);
4239 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004240 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004241 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004242 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004243 return FAIL;
4244 }
4245 }
4246
4247 return OK;
4248}
4249
4250/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 * * number multiplication
4252 * / number division
4253 * % number modulo
4254 */
4255 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257{
4258 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004259 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004260 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004262 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004263 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 return FAIL;
4265
4266 /*
4267 * Repeat computing, until no "*", "/" or "%" is following.
4268 */
4269 for (;;)
4270 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004271 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 if (*op != '*' && *op != '/' && *op != '%')
4273 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004274 if (next != NULL)
4275 {
4276 *arg = next_line_from_context(cctx, TRUE);
4277 op = skipwhite(*arg);
4278 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004279
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004280 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004282 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004283 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004285 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004286 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004289 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004291
4292 if (ppconst->pp_used == ppconst_used + 2
4293 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4294 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004295 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004296 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4297 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4298 varnumber_T res = 0;
4299 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004301 // both are numbers: compute the result
4302 switch (*op)
4303 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004304 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004305 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004306 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004307 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004308 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004309 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004310 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004311 break;
4312 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004313 if (failed)
4314 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004315 tv1->vval.v_number = res;
4316 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004317 }
4318 else
4319 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004320 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004321 generate_two_op(cctx, op);
4322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 }
4324
4325 return OK;
4326}
4327
4328/*
4329 * + number addition
4330 * - number subtraction
4331 * .. string concatenation
4332 */
4333 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335{
4336 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004337 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340
4341 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 return FAIL;
4344
4345 /*
4346 * Repeat computing, until no "+", "-" or ".." is following.
4347 */
4348 for (;;)
4349 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004350 op = may_peek_next_line(cctx, *arg, &next);
4351 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 break;
4353 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004354 if (next != NULL)
4355 {
4356 *arg = next_line_from_context(cctx, TRUE);
4357 op = skipwhite(*arg);
4358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004360 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004362 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004363 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 }
4365
Bram Moolenaare0de1712020-12-02 17:36:54 +01004366 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004367 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004369 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004370 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 return FAIL;
4372
Bram Moolenaara5565e42020-05-09 15:44:01 +02004373 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004374 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004375 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4376 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4377 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4378 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004380 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4381 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004382
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004383 // concat/subtract/add constant numbers
4384 if (*op == '+')
4385 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4386 else if (*op == '-')
4387 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4388 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004389 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004390 // concatenate constant strings
4391 char_u *s1 = tv1->vval.v_string;
4392 char_u *s2 = tv2->vval.v_string;
4393 size_t len1 = STRLEN(s1);
4394
4395 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4396 if (tv1->vval.v_string == NULL)
4397 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004399 return FAIL;
4400 }
4401 mch_memmove(tv1->vval.v_string, s1, len1);
4402 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004403 vim_free(s1);
4404 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004405 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004406 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 }
4408 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004409 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004411 if (*op == '.')
4412 {
4413 if (may_generate_2STRING(-2, cctx) == FAIL
4414 || may_generate_2STRING(-1, cctx) == FAIL)
4415 return FAIL;
4416 generate_instr_drop(cctx, ISN_CONCAT, 1);
4417 }
4418 else
4419 generate_two_op(cctx, op);
4420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 }
4422
4423 return OK;
4424}
4425
4426/*
4427 * expr5a == expr5b
4428 * expr5a =~ expr5b
4429 * expr5a != expr5b
4430 * expr5a !~ expr5b
4431 * expr5a > expr5b
4432 * expr5a >= expr5b
4433 * expr5a < expr5b
4434 * expr5a <= expr5b
4435 * expr5a is expr5b
4436 * expr5a isnot expr5b
4437 *
4438 * Produces instructions:
4439 * EVAL expr5a Push result of "expr5a"
4440 * EVAL expr5b Push result of "expr5b"
4441 * COMPARE one of the compare instructions
4442 */
4443 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004446 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004448 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452
4453 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 return FAIL;
4456
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004457 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004458 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459
4460 /*
4461 * If there is a comparative operator, use it.
4462 */
4463 if (type != EXPR_UNKNOWN)
4464 {
4465 int ic = FALSE; // Default: do not ignore case
4466
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004467 if (next != NULL)
4468 {
4469 *arg = next_line_from_context(cctx, TRUE);
4470 p = skipwhite(*arg);
4471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 if (type_is && (p[len] == '?' || p[len] == '#'))
4473 {
4474 semsg(_(e_invexpr2), *arg);
4475 return FAIL;
4476 }
4477 // extra question mark appended: ignore case
4478 if (p[len] == '?')
4479 {
4480 ic = TRUE;
4481 ++len;
4482 }
4483 // extra '#' appended: match case (ignored)
4484 else if (p[len] == '#')
4485 ++len;
4486 // nothing appended: match case
4487
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004488 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004490 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004491 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 }
4493
4494 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004495 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004496 return FAIL;
4497
Bram Moolenaara5565e42020-05-09 15:44:01 +02004498 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 return FAIL;
4500
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 if (ppconst->pp_used == ppconst_used + 2)
4502 {
4503 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4504 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4505 int ret;
4506
4507 // Both sides are a constant, compute the result now.
4508 // First check for a valid combination of types, this is more
4509 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004510 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511 ret = FAIL;
4512 else
4513 {
4514 ret = typval_compare(tv1, tv2, type, ic);
4515 tv1->v_type = VAR_BOOL;
4516 tv1->vval.v_number = tv1->vval.v_number
4517 ? VVAL_TRUE : VVAL_FALSE;
4518 clear_tv(tv2);
4519 --ppconst->pp_used;
4520 }
4521 return ret;
4522 }
4523
4524 generate_ppconst(cctx, ppconst);
4525 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 }
4527
4528 return OK;
4529}
4530
Bram Moolenaar7f141552020-05-09 17:35:53 +02004531static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533/*
4534 * Compile || or &&.
4535 */
4536 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537compile_and_or(
4538 char_u **arg,
4539 cctx_T *cctx,
4540 char *op,
4541 ppconst_T *ppconst,
4542 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004544 char_u *next;
4545 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 int opchar = *op;
4547
4548 if (p[0] == opchar && p[1] == opchar)
4549 {
4550 garray_T *instr = &cctx->ctx_instr;
4551 garray_T end_ga;
4552
4553 /*
4554 * Repeat until there is no following "||" or "&&"
4555 */
4556 ga_init2(&end_ga, sizeof(int), 10);
4557 while (p[0] == opchar && p[1] == opchar)
4558 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004559 if (next != NULL)
4560 {
4561 *arg = next_line_from_context(cctx, TRUE);
4562 p = skipwhite(*arg);
4563 }
4564
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004565 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4566 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004567 semsg(_(e_white_space_required_before_and_after_str_at_str),
4568 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004569 return FAIL;
4570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004572 // TODO: use ppconst if the value is a constant and check
4573 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004574 generate_ppconst(cctx, ppconst);
4575
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004576 // Every part must evaluate to a bool.
4577 if (bool_on_stack(cctx) == FAIL)
4578 {
4579 ga_clear(&end_ga);
4580 return FAIL;
4581 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 if (ga_grow(&end_ga, 1) == FAIL)
4584 {
4585 ga_clear(&end_ga);
4586 return FAIL;
4587 }
4588 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4589 ++end_ga.ga_len;
4590 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004591 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592
4593 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004594 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004595 {
4596 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004597 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004598 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004599
Bram Moolenaara5565e42020-05-09 15:44:01 +02004600 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4601 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 {
4603 ga_clear(&end_ga);
4604 return FAIL;
4605 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004606
4607 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004611 // Every part must evaluate to a bool.
4612 if (bool_on_stack(cctx) == FAIL)
4613 {
4614 ga_clear(&end_ga);
4615 return FAIL;
4616 }
4617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 // Fill in the end label in all jumps.
4619 while (end_ga.ga_len > 0)
4620 {
4621 isn_T *isn;
4622
4623 --end_ga.ga_len;
4624 isn = ((isn_T *)instr->ga_data)
4625 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4626 isn->isn_arg.jump.jump_where = instr->ga_len;
4627 }
4628 ga_clear(&end_ga);
4629 }
4630
4631 return OK;
4632}
4633
4634/*
4635 * expr4a && expr4a && expr4a logical AND
4636 *
4637 * Produces instructions:
4638 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004639 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004640 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004642 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 * EVAL expr4c Push result of "expr4c"
4644 * end:
4645 */
4646 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004649 int ppconst_used = ppconst->pp_used;
4650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004652 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 return FAIL;
4654
4655 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657}
4658
4659/*
4660 * expr3a || expr3b || expr3c logical OR
4661 *
4662 * Produces instructions:
4663 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004664 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004665 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004667 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 * EVAL expr3c Push result of "expr3c"
4669 * end:
4670 */
4671 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004674 int ppconst_used = ppconst->pp_used;
4675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 return FAIL;
4679
4680 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682}
4683
4684/*
4685 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004687 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 * JUMP_IF_FALSE alt jump if false
4689 * EVAL expr1a
4690 * JUMP_ALWAYS end
4691 * alt: EVAL expr1b
4692 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004693 *
4694 * Toplevel expression: expr2 ?? expr1
4695 * Produces instructions:
4696 * EVAL expr2 Push result of "expr2"
4697 * JUMP_AND_KEEP_IF_TRUE end jump if true
4698 * EVAL expr1
4699 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 */
4701 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004702compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703{
4704 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004705 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004706 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707
Bram Moolenaar3988f642020-08-27 22:43:03 +02004708 // Ignore all kinds of errors when not producing code.
4709 if (cctx->ctx_skip == SKIP_YES)
4710 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004711 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004712 return OK;
4713 }
4714
Bram Moolenaar61a89812020-05-07 16:58:17 +02004715 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004716 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 return FAIL;
4718
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004719 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 if (*p == '?')
4721 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004722 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 garray_T *instr = &cctx->ctx_instr;
4724 garray_T *stack = &cctx->ctx_type_stack;
4725 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004726 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004728 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004729 int has_const_expr = FALSE;
4730 int const_value = FALSE;
4731 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004733 if (next != NULL)
4734 {
4735 *arg = next_line_from_context(cctx, TRUE);
4736 p = skipwhite(*arg);
4737 }
4738
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004739 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004740 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004741 semsg(_(e_white_space_required_before_and_after_str_at_str),
4742 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004743 return FAIL;
4744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 if (ppconst->pp_used == ppconst_used + 1)
4747 {
4748 // the condition is a constant, we know whether the ? or the :
4749 // expression is to be evaluated.
4750 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004751 if (op_falsy)
4752 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4753 else
4754 {
4755 int error = FALSE;
4756
4757 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4758 &error);
4759 if (error)
4760 return FAIL;
4761 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004762 cctx->ctx_skip = save_skip == SKIP_YES ||
4763 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4764
4765 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4766 // "left ?? right" and "left" is truthy: produce "left"
4767 generate_ppconst(cctx, ppconst);
4768 else
4769 {
4770 clear_tv(&ppconst->pp_tv[ppconst_used]);
4771 --ppconst->pp_used;
4772 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773 }
4774 else
4775 {
4776 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004777 if (op_falsy)
4778 end_idx = instr->ga_len;
4779 generate_JUMP(cctx, op_falsy
4780 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4781 if (op_falsy)
4782 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784
4785 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004786 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004787 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004788 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004789 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790
Bram Moolenaara5565e42020-05-09 15:44:01 +02004791 if (!has_const_expr)
4792 {
4793 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004795 if (!op_falsy)
4796 {
4797 // remember the type and drop it
4798 --stack->ga_len;
4799 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004801 end_idx = instr->ga_len;
4802 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004803
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004804 // jump here from JUMP_IF_FALSE
4805 isn = ((isn_T *)instr->ga_data) + alt_idx;
4806 isn->isn_arg.jump.jump_where = instr->ga_len;
4807 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004810 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004812 // Check for the ":".
4813 p = may_peek_next_line(cctx, *arg, &next);
4814 if (*p != ':')
4815 {
4816 emsg(_(e_missing_colon));
4817 return FAIL;
4818 }
4819 if (next != NULL)
4820 {
4821 *arg = next_line_from_context(cctx, TRUE);
4822 p = skipwhite(*arg);
4823 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004824
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004825 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4826 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004827 semsg(_(e_white_space_required_before_and_after_str_at_str),
4828 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004829 return FAIL;
4830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004832 // evaluate the third expression
4833 if (has_const_expr)
4834 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004835 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004836 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004837 return FAIL;
4838 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4839 return FAIL;
4840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841
Bram Moolenaara5565e42020-05-09 15:44:01 +02004842 if (!has_const_expr)
4843 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004844 type_T **typep;
4845
Bram Moolenaara5565e42020-05-09 15:44:01 +02004846 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004849 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4850 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004851
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004852 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853 isn = ((isn_T *)instr->ga_data) + end_idx;
4854 isn->isn_arg.jump.jump_where = instr->ga_len;
4855 }
4856
4857 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 }
4859 return OK;
4860}
4861
4862/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004863 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004864 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4865 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004866 */
4867 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004868compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869{
4870 ppconst_T ppconst;
4871
4872 CLEAR_FIELD(ppconst);
4873 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4874 {
4875 clear_ppconst(&ppconst);
4876 return FAIL;
4877 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004878 if (is_const != NULL)
4879 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004880 if (generate_ppconst(cctx, &ppconst) == FAIL)
4881 return FAIL;
4882 return OK;
4883}
4884
4885/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004886 * Toplevel expression.
4887 */
4888 static int
4889compile_expr0(char_u **arg, cctx_T *cctx)
4890{
4891 return compile_expr0_ext(arg, cctx, NULL);
4892}
4893
4894/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 * compile "return [expr]"
4896 */
4897 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004898compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899{
4900 char_u *p = arg;
4901 garray_T *stack = &cctx->ctx_type_stack;
4902 type_T *stack_type;
4903
4904 if (*p != NUL && *p != '|' && *p != '\n')
4905 {
4906 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004907 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 return NULL;
4909
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004910 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004911 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004912 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004913 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004914 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4915 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004916 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004917 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004918 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004919 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004920 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004921 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4922 && stack_type->tt_type != VAR_VOID
4923 && stack_type->tt_type != VAR_UNKNOWN)
4924 {
4925 emsg(_(e_returning_value_in_function_without_return_type));
4926 return NULL;
4927 }
4928 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004929 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004930 return NULL;
4931 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 }
4934 else
4935 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004936 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004937 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004938 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4939 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004941 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 return NULL;
4943 }
4944
4945 // No argument, return zero.
4946 generate_PUSHNR(cctx, 0);
4947 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01004948
4949 // Undo any command modifiers.
4950 generate_undo_cmdmods(cctx);
4951
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004952 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 return NULL;
4954
4955 // "return val | endif" is possible
4956 return skipwhite(p);
4957}
4958
4959/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004960 * Get a line from the compilation context, compatible with exarg_T getline().
4961 * Return a pointer to the line in allocated memory.
4962 * Return NULL for end-of-file or some error.
4963 */
4964 static char_u *
4965exarg_getline(
4966 int c UNUSED,
4967 void *cookie,
4968 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004969 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004970{
4971 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004972 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004973
Bram Moolenaar66250c92020-08-20 15:02:42 +02004974 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004975 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004976 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004977 return NULL;
4978 ++cctx->ctx_lnum;
4979 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4980 // Comment lines result in NULL pointers, skip them.
4981 if (p != NULL)
4982 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004983 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004984}
4985
4986/*
4987 * Compile a nested :def command.
4988 */
4989 static char_u *
4990compile_nested_function(exarg_T *eap, cctx_T *cctx)
4991{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004992 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004993 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004994 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004995 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004996 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004997 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004998
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004999 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005000 {
5001 emsg(_(e_cannot_use_bang_with_nested_def));
5002 return NULL;
5003 }
5004
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005005 if (*name_start == '/')
5006 {
5007 name_end = skip_regexp(name_start + 1, '/', TRUE);
5008 if (*name_end == '/')
5009 ++name_end;
5010 eap->nextcmd = check_nextcmd(name_end);
5011 }
5012 if (name_end == name_start || *skipwhite(name_end) != '(')
5013 {
5014 if (!ends_excmd2(name_start, name_end))
5015 {
5016 semsg(_(e_invalid_command_str), eap->cmd);
5017 return NULL;
5018 }
5019
5020 // "def" or "def Name": list functions
5021 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5022 return NULL;
5023 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5024 }
5025
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005026 // Only g:Func() can use a namespace.
5027 if (name_start[1] == ':' && !is_global)
5028 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005029 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005030 return NULL;
5031 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005032 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5033 return NULL;
5034
Bram Moolenaar04b12692020-05-04 23:24:44 +02005035 eap->arg = name_end;
5036 eap->getline = exarg_getline;
5037 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005038 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005039 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005040 lambda_name = vim_strsave(get_lambda_name());
5041 if (lambda_name == NULL)
5042 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005043 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005044
Bram Moolenaar822ba242020-05-24 23:00:18 +02005045 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005046 {
5047 r = eap->skip ? OK : FAIL;
5048 goto theend;
5049 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005050 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005051 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005052 {
5053 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005054 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005055 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005056
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005057 if (is_global)
5058 {
5059 char_u *func_name = vim_strnsave(name_start + 2,
5060 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005061
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005062 if (func_name == NULL)
5063 r = FAIL;
5064 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005065 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005066 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005067 lambda_name = NULL;
5068 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005069 }
5070 else
5071 {
5072 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005073 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005074 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005075 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005076
Bram Moolenaareef21022020-08-01 22:16:43 +02005077 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005078 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005079 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005080 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005081 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005082
5083 // copy over the block scope IDs
5084 if (block_depth > 0)
5085 {
5086 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5087 if (ufunc->uf_block_ids != NULL)
5088 {
5089 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5090 sizeof(int) * block_depth);
5091 ufunc->uf_block_depth = block_depth;
5092 }
5093 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005094 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005095 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005096 r = OK;
5097
5098theend:
5099 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005100 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005101}
5102
5103/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 * Return the length of an assignment operator, or zero if there isn't one.
5105 */
5106 int
5107assignment_len(char_u *p, int *heredoc)
5108{
5109 if (*p == '=')
5110 {
5111 if (p[1] == '<' && p[2] == '<')
5112 {
5113 *heredoc = TRUE;
5114 return 3;
5115 }
5116 return 1;
5117 }
5118 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5119 return 2;
5120 if (STRNCMP(p, "..=", 3) == 0)
5121 return 3;
5122 return 0;
5123}
5124
5125// words that cannot be used as a variable
5126static char *reserved[] = {
5127 "true",
5128 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005129 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 NULL
5131};
5132
Bram Moolenaar752fc692021-01-04 21:57:11 +01005133// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005134typedef enum {
5135 dest_local,
5136 dest_option,
5137 dest_env,
5138 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005139 dest_buffer,
5140 dest_window,
5141 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005142 dest_vimvar,
5143 dest_script,
5144 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005145 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005146} assign_dest_T;
5147
Bram Moolenaar752fc692021-01-04 21:57:11 +01005148// Used by compile_lhs() to store information about the LHS of an assignment
5149// and one argument of ":unlet" with an index.
5150typedef struct {
5151 assign_dest_T lhs_dest; // type of destination
5152
5153 char_u *lhs_name; // allocated name including
5154 // "[expr]" or ".name".
5155 size_t lhs_varlen; // length of the variable without
5156 // "[expr]" or ".name"
5157 char_u *lhs_dest_end; // end of the destination, including
5158 // "[expr]" or ".name".
5159
5160 int lhs_has_index; // has "[expr]" or ".name"
5161
5162 int lhs_new_local; // create new local variable
5163 int lhs_opt_flags; // for when destination is an option
5164 int lhs_vimvaridx; // for when destination is a v:var
5165
5166 lvar_T lhs_local_lvar; // used for existing local destination
5167 lvar_T lhs_arg_lvar; // used for argument destination
5168 lvar_T *lhs_lvar; // points to destination lvar
5169 int lhs_scriptvar_sid;
5170 int lhs_scriptvar_idx;
5171
5172 int lhs_has_type; // type was specified
5173 type_T *lhs_type;
5174 type_T *lhs_member_type;
5175} lhs_T;
5176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005178 * Generate the load instruction for "name".
5179 */
5180 static void
5181generate_loadvar(
5182 cctx_T *cctx,
5183 assign_dest_T dest,
5184 char_u *name,
5185 lvar_T *lvar,
5186 type_T *type)
5187{
5188 switch (dest)
5189 {
5190 case dest_option:
5191 // TODO: check the option exists
5192 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5193 break;
5194 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005195 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5196 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5197 else
5198 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005199 break;
5200 case dest_buffer:
5201 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5202 break;
5203 case dest_window:
5204 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5205 break;
5206 case dest_tab:
5207 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5208 break;
5209 case dest_script:
5210 compile_load_scriptvar(cctx,
5211 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5212 break;
5213 case dest_env:
5214 // Include $ in the name here
5215 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5216 break;
5217 case dest_reg:
5218 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5219 break;
5220 case dest_vimvar:
5221 generate_LOADV(cctx, name + 2, TRUE);
5222 break;
5223 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005224 if (lvar->lv_from_outer > 0)
5225 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5226 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005227 else
5228 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5229 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005230 case dest_expr:
5231 // list or dict value should already be on the stack.
5232 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005233 }
5234}
5235
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005236/*
5237 * Skip over "[expr]" or ".member".
5238 * Does not check for any errors.
5239 */
5240 static char_u *
5241skip_index(char_u *start)
5242{
5243 char_u *p = start;
5244
5245 if (*p == '[')
5246 {
5247 p = skipwhite(p + 1);
5248 (void)skip_expr(&p, NULL);
5249 p = skipwhite(p);
5250 if (*p == ']')
5251 return p + 1;
5252 return p;
5253 }
5254 // if (*p == '.')
5255 return to_name_end(p + 1, TRUE);
5256}
5257
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005258 void
5259vim9_declare_error(char_u *name)
5260{
5261 char *scope = "";
5262
5263 switch (*name)
5264 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005265 case 'g': scope = _("global"); break;
5266 case 'b': scope = _("buffer"); break;
5267 case 'w': scope = _("window"); break;
5268 case 't': scope = _("tab"); break;
5269 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005270 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005271 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005272 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005273 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005274 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005275 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005276 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005277 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005278 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005279}
5280
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005281/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005282 * For one assignment figure out the type of destination. Return it in "dest".
5283 * When not recognized "dest" is not set.
5284 * For an option "opt_flags" is set.
5285 * For a v:var "vimvaridx" is set.
5286 * "type" is set to the destination type if known, unchanted otherwise.
5287 * Return FAIL if an error message was given.
5288 */
5289 static int
5290get_var_dest(
5291 char_u *name,
5292 assign_dest_T *dest,
5293 int cmdidx,
5294 int *opt_flags,
5295 int *vimvaridx,
5296 type_T **type,
5297 cctx_T *cctx)
5298{
5299 char_u *p;
5300
5301 if (*name == '&')
5302 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005303 int cc;
5304 long numval;
5305 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005306
5307 *dest = dest_option;
5308 if (cmdidx == CMD_final || cmdidx == CMD_const)
5309 {
5310 emsg(_(e_const_option));
5311 return FAIL;
5312 }
5313 p = name;
5314 p = find_option_end(&p, opt_flags);
5315 if (p == NULL)
5316 {
5317 // cannot happen?
5318 emsg(_(e_letunexp));
5319 return FAIL;
5320 }
5321 cc = *p;
5322 *p = NUL;
5323 opt_type = get_option_value(skip_option_env_lead(name),
5324 &numval, NULL, *opt_flags);
5325 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005326 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005327 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005328 case gov_unknown:
5329 semsg(_(e_unknown_option), name);
5330 return FAIL;
5331 case gov_string:
5332 case gov_hidden_string:
5333 *type = &t_string;
5334 break;
5335 case gov_bool:
5336 case gov_hidden_bool:
5337 *type = &t_bool;
5338 break;
5339 case gov_number:
5340 case gov_hidden_number:
5341 *type = &t_number;
5342 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005343 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005344 }
5345 else if (*name == '$')
5346 {
5347 *dest = dest_env;
5348 *type = &t_string;
5349 }
5350 else if (*name == '@')
5351 {
5352 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5353 {
5354 emsg_invreg(name[1]);
5355 return FAIL;
5356 }
5357 *dest = dest_reg;
5358 *type = &t_string;
5359 }
5360 else if (STRNCMP(name, "g:", 2) == 0)
5361 {
5362 *dest = dest_global;
5363 }
5364 else if (STRNCMP(name, "b:", 2) == 0)
5365 {
5366 *dest = dest_buffer;
5367 }
5368 else if (STRNCMP(name, "w:", 2) == 0)
5369 {
5370 *dest = dest_window;
5371 }
5372 else if (STRNCMP(name, "t:", 2) == 0)
5373 {
5374 *dest = dest_tab;
5375 }
5376 else if (STRNCMP(name, "v:", 2) == 0)
5377 {
5378 typval_T *vtv;
5379 int di_flags;
5380
5381 *vimvaridx = find_vim_var(name + 2, &di_flags);
5382 if (*vimvaridx < 0)
5383 {
5384 semsg(_(e_variable_not_found_str), name);
5385 return FAIL;
5386 }
5387 // We use the current value of "sandbox" here, is that OK?
5388 if (var_check_ro(di_flags, name, FALSE))
5389 return FAIL;
5390 *dest = dest_vimvar;
5391 vtv = get_vim_var_tv(*vimvaridx);
5392 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5393 }
5394 return OK;
5395}
5396
5397/*
5398 * Generate a STORE instruction for "dest", not being "dest_local".
5399 * Return FAIL when out of memory.
5400 */
5401 static int
5402generate_store_var(
5403 cctx_T *cctx,
5404 assign_dest_T dest,
5405 int opt_flags,
5406 int vimvaridx,
5407 int scriptvar_idx,
5408 int scriptvar_sid,
5409 type_T *type,
5410 char_u *name)
5411{
5412 switch (dest)
5413 {
5414 case dest_option:
5415 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5416 opt_flags);
5417 case dest_global:
5418 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005419 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5420 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005421 case dest_buffer:
5422 // include b: with the name, easier to execute that way
5423 return generate_STORE(cctx, ISN_STOREB, 0, name);
5424 case dest_window:
5425 // include w: with the name, easier to execute that way
5426 return generate_STORE(cctx, ISN_STOREW, 0, name);
5427 case dest_tab:
5428 // include t: with the name, easier to execute that way
5429 return generate_STORE(cctx, ISN_STORET, 0, name);
5430 case dest_env:
5431 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5432 case dest_reg:
5433 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5434 case dest_vimvar:
5435 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5436 case dest_script:
5437 if (scriptvar_idx < 0)
5438 {
5439 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005440 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005441
Bram Moolenaar41d61962020-12-06 20:12:43 +01005442 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005443 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5444 scriptvar_sid, type);
5445 if (name_s != name)
5446 vim_free(name_s);
5447 return r;
5448 }
5449 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5450 scriptvar_sid, scriptvar_idx, type);
5451 case dest_local:
5452 case dest_expr:
5453 // cannot happen
5454 break;
5455 }
5456 return FAIL;
5457}
5458
Bram Moolenaar752fc692021-01-04 21:57:11 +01005459 static int
5460is_decl_command(int cmdidx)
5461{
5462 return cmdidx == CMD_let || cmdidx == CMD_var
5463 || cmdidx == CMD_final || cmdidx == CMD_const;
5464}
5465
5466/*
5467 * Figure out the LHS type and other properties for an assignment or one item
5468 * of ":unlet" with an index.
5469 * Returns OK or FAIL.
5470 */
5471 static int
5472compile_lhs(
5473 char_u *var_start,
5474 lhs_T *lhs,
5475 int cmdidx,
5476 int heredoc,
5477 int oplen,
5478 cctx_T *cctx)
5479{
5480 char_u *var_end;
5481 int is_decl = is_decl_command(cmdidx);
5482
5483 CLEAR_POINTER(lhs);
5484 lhs->lhs_dest = dest_local;
5485 lhs->lhs_vimvaridx = -1;
5486 lhs->lhs_scriptvar_idx = -1;
5487
5488 // "dest_end" is the end of the destination, including "[expr]" or
5489 // ".name".
5490 // "var_end" is the end of the variable/option/etc. name.
5491 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5492 if (*var_start == '@')
5493 var_end = var_start + 2;
5494 else
5495 {
5496 // skip over the leading "&", "&l:", "&g:" and "$"
5497 var_end = skip_option_env_lead(var_start);
5498 var_end = to_name_end(var_end, TRUE);
5499 }
5500
5501 // "a: type" is declaring variable "a" with a type, not dict "a:".
5502 if (is_decl && lhs->lhs_dest_end == var_start + 2
5503 && lhs->lhs_dest_end[-1] == ':')
5504 --lhs->lhs_dest_end;
5505 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5506 --var_end;
5507
5508 // compute the length of the destination without "[expr]" or ".name"
5509 lhs->lhs_varlen = var_end - var_start;
5510 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5511 if (lhs->lhs_name == NULL)
5512 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005513
5514 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5515 // Something follows after the variable: "var[idx]" or "var.key".
5516 lhs->lhs_has_index = TRUE;
5517
Bram Moolenaar752fc692021-01-04 21:57:11 +01005518 if (heredoc)
5519 lhs->lhs_type = &t_list_string;
5520 else
5521 lhs->lhs_type = &t_any;
5522
5523 if (cctx->ctx_skip != SKIP_YES)
5524 {
5525 int declare_error = FALSE;
5526
5527 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5528 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5529 &lhs->lhs_type, cctx) == FAIL)
5530 return FAIL;
5531 if (lhs->lhs_dest != dest_local)
5532 {
5533 // Specific kind of variable recognized.
5534 declare_error = is_decl;
5535 }
5536 else
5537 {
5538 int idx;
5539
5540 // No specific kind of variable recognized, just a name.
5541 for (idx = 0; reserved[idx] != NULL; ++idx)
5542 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5543 {
5544 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5545 return FAIL;
5546 }
5547
5548
5549 if (lookup_local(var_start, lhs->lhs_varlen,
5550 &lhs->lhs_local_lvar, cctx) == OK)
5551 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5552 else
5553 {
5554 CLEAR_FIELD(lhs->lhs_arg_lvar);
5555 if (arg_exists(var_start, lhs->lhs_varlen,
5556 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5557 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5558 {
5559 if (is_decl)
5560 {
5561 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5562 return FAIL;
5563 }
5564 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5565 }
5566 }
5567 if (lhs->lhs_lvar != NULL)
5568 {
5569 if (is_decl)
5570 {
5571 semsg(_(e_variable_already_declared), lhs->lhs_name);
5572 return FAIL;
5573 }
5574 }
5575 else
5576 {
5577 int script_namespace = lhs->lhs_varlen > 1
5578 && STRNCMP(var_start, "s:", 2) == 0;
5579 int script_var = (script_namespace
5580 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5581 FALSE, cctx)
5582 : script_var_exists(var_start, lhs->lhs_varlen,
5583 TRUE, cctx)) == OK;
5584 imported_T *import =
5585 find_imported(var_start, lhs->lhs_varlen, cctx);
5586
5587 if (script_namespace || script_var || import != NULL)
5588 {
5589 char_u *rawname = lhs->lhs_name
5590 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5591
5592 if (is_decl)
5593 {
5594 if (script_namespace)
5595 semsg(_(e_cannot_declare_script_variable_in_function),
5596 lhs->lhs_name);
5597 else
5598 semsg(_(e_variable_already_declared_in_script),
5599 lhs->lhs_name);
5600 return FAIL;
5601 }
5602 else if (cctx->ctx_ufunc->uf_script_ctx_version
5603 == SCRIPT_VERSION_VIM9
5604 && script_namespace
5605 && !script_var && import == NULL)
5606 {
5607 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5608 return FAIL;
5609 }
5610
5611 lhs->lhs_dest = dest_script;
5612
5613 // existing script-local variables should have a type
5614 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5615 if (import != NULL)
5616 lhs->lhs_scriptvar_sid = import->imp_sid;
5617 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5618 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005619 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005620 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005621 lhs->lhs_scriptvar_sid, rawname,
5622 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5623 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005624 if (lhs->lhs_scriptvar_idx >= 0)
5625 {
5626 scriptitem_T *si = SCRIPT_ITEM(
5627 lhs->lhs_scriptvar_sid);
5628 svar_T *sv =
5629 ((svar_T *)si->sn_var_vals.ga_data)
5630 + lhs->lhs_scriptvar_idx;
5631 lhs->lhs_type = sv->sv_type;
5632 }
5633 }
5634 }
5635 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5636 == FAIL)
5637 return FAIL;
5638 }
5639 }
5640
5641 if (declare_error)
5642 {
5643 vim9_declare_error(lhs->lhs_name);
5644 return FAIL;
5645 }
5646 }
5647
5648 // handle "a:name" as a name, not index "name" on "a"
5649 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5650 var_end = lhs->lhs_dest_end;
5651
5652 if (lhs->lhs_dest != dest_option)
5653 {
5654 if (is_decl && *var_end == ':')
5655 {
5656 char_u *p;
5657
5658 // parse optional type: "let var: type = expr"
5659 if (!VIM_ISWHITE(var_end[1]))
5660 {
5661 semsg(_(e_white_space_required_after_str), ":");
5662 return FAIL;
5663 }
5664 p = skipwhite(var_end + 1);
5665 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5666 if (lhs->lhs_type == NULL)
5667 return FAIL;
5668 lhs->lhs_has_type = TRUE;
5669 }
5670 else if (lhs->lhs_lvar != NULL)
5671 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5672 }
5673
5674 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5675 && lhs->lhs_type->tt_type != VAR_STRING
5676 && lhs->lhs_type->tt_type != VAR_ANY)
5677 {
5678 emsg(_(e_can_only_concatenate_to_string));
5679 return FAIL;
5680 }
5681
5682 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5683 && cctx->ctx_skip != SKIP_YES)
5684 {
5685 if (oplen > 1 && !heredoc)
5686 {
5687 // +=, /=, etc. require an existing variable
5688 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5689 return FAIL;
5690 }
5691 if (!is_decl)
5692 {
5693 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5694 return FAIL;
5695 }
5696
5697 // new local variable
5698 if ((lhs->lhs_type->tt_type == VAR_FUNC
5699 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5700 && var_wrong_func_name(lhs->lhs_name, TRUE))
5701 return FAIL;
5702 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5703 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5704 if (lhs->lhs_lvar == NULL)
5705 return FAIL;
5706 lhs->lhs_new_local = TRUE;
5707 }
5708
5709 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005710 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005711 {
5712 // Something follows after the variable: "var[idx]" or "var.key".
5713 // TODO: should we also handle "->func()" here?
5714 if (is_decl)
5715 {
5716 emsg(_(e_cannot_use_index_when_declaring_variable));
5717 return FAIL;
5718 }
5719
5720 if (var_start[lhs->lhs_varlen] == '['
5721 || var_start[lhs->lhs_varlen] == '.')
5722 {
5723 char_u *after = var_start + lhs->lhs_varlen;
5724 char_u *p;
5725
5726 // Only the last index is used below, if there are others
5727 // before it generate code for the expression. Thus for
5728 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5729 for (;;)
5730 {
5731 p = skip_index(after);
5732 if (*p != '[' && *p != '.')
5733 break;
5734 after = p;
5735 }
5736 if (after > var_start + lhs->lhs_varlen)
5737 {
5738 lhs->lhs_varlen = after - var_start;
5739 lhs->lhs_dest = dest_expr;
5740 // We don't know the type before evaluating the expression,
5741 // use "any" until then.
5742 lhs->lhs_type = &t_any;
5743 }
5744
Bram Moolenaar752fc692021-01-04 21:57:11 +01005745 if (lhs->lhs_type->tt_member == NULL)
5746 lhs->lhs_member_type = &t_any;
5747 else
5748 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5749 }
5750 else
5751 {
5752 semsg("Not supported yet: %s", var_start);
5753 return FAIL;
5754 }
5755 }
5756 return OK;
5757}
5758
5759/*
5760 * Assignment to a list or dict member, or ":unlet" for the item, using the
5761 * information in "lhs".
5762 * Returns OK or FAIL.
5763 */
5764 static int
5765compile_assign_unlet(
5766 char_u *var_start,
5767 lhs_T *lhs,
5768 int is_assign,
5769 type_T *rhs_type,
5770 cctx_T *cctx)
5771{
5772 char_u *p;
5773 int r;
5774 vartype_T dest_type;
5775 size_t varlen = lhs->lhs_varlen;
5776 garray_T *stack = &cctx->ctx_type_stack;
5777
5778 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5779 p = var_start + varlen;
5780 if (*p == '[')
5781 {
5782 p = skipwhite(p + 1);
5783 r = compile_expr0(&p, cctx);
5784 if (r == OK && *skipwhite(p) != ']')
5785 {
5786 // this should not happen
5787 emsg(_(e_missbrac));
5788 r = FAIL;
5789 }
5790 }
5791 else // if (*p == '.')
5792 {
5793 char_u *key_end = to_name_end(p + 1, TRUE);
5794 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5795
5796 r = generate_PUSHS(cctx, key);
5797 }
5798 if (r == FAIL)
5799 return FAIL;
5800
5801 if (lhs->lhs_type == &t_any)
5802 {
5803 // Index on variable of unknown type: check at runtime.
5804 dest_type = VAR_ANY;
5805 }
5806 else
5807 {
5808 dest_type = lhs->lhs_type->tt_type;
5809 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5810 return FAIL;
5811 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005812 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5813 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005814 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005815 }
5816
5817 // Load the dict or list. On the stack we then have:
5818 // - value (for assignment, not for :unlet)
5819 // - index
5820 // - variable
5821 if (lhs->lhs_dest == dest_expr)
5822 {
5823 int c = var_start[varlen];
5824
5825 // Evaluate "ll[expr]" of "ll[expr][idx]"
5826 p = var_start;
5827 var_start[varlen] = NUL;
5828 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5829 {
5830 // this should not happen
5831 emsg(_(e_missbrac));
5832 return FAIL;
5833 }
5834 var_start[varlen] = c;
5835
5836 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5837 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5838 // now we can properly check the type
5839 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005840 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005841 FALSE, FALSE) == FAIL)
5842 return FAIL;
5843 }
5844 else
5845 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5846 lhs->lhs_lvar, lhs->lhs_type);
5847
5848 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5849 {
5850 if (is_assign)
5851 {
5852 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5853
5854 if (isn == NULL)
5855 return FAIL;
5856 isn->isn_arg.vartype = dest_type;
5857 }
5858 else
5859 {
5860 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5861 return FAIL;
5862 }
5863 }
5864 else
5865 {
5866 emsg(_(e_indexable_type_required));
5867 return FAIL;
5868 }
5869
5870 return OK;
5871}
5872
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005873/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005874 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005875 * "let name"
5876 * "var name = expr"
5877 * "final name = expr"
5878 * "const name = expr"
5879 * "name = expr"
5880 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005881 * Return NULL for an error.
5882 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 */
5884 static char_u *
5885compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5886{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005887 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005889 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 char_u *ret = NULL;
5891 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005892 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005895 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 int oplen = 0;
5898 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005899 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005901 int is_decl = is_decl_command(cmdidx);
5902 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005904 // Skip over the "var" or "[var, var]" to get to any "=".
5905 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5906 if (p == NULL)
5907 return *arg == '[' ? arg : NULL;
5908
5909 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005911 // TODO: should we allow this, and figure out type inference from list
5912 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005913 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 return NULL;
5915 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005916 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918 sp = p;
5919 p = skipwhite(p);
5920 op = p;
5921 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005922
5923 if (var_count > 0 && oplen == 0)
5924 // can be something like "[1, 2]->func()"
5925 return arg;
5926
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005927 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005928 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005929 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005930 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005931 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933 if (heredoc)
5934 {
5935 list_T *l;
5936 listitem_T *li;
5937
5938 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005939 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005941 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005942 if (l == NULL)
5943 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944
Bram Moolenaar078269b2020-09-21 20:35:55 +02005945 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005946 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005947 // Push each line and the create the list.
5948 FOR_ALL_LIST_ITEMS(l, li)
5949 {
5950 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5951 li->li_tv.vval.v_string = NULL;
5952 }
5953 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 list_free(l);
5956 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005957 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005959 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005961 char_u *wp;
5962
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005963 // for "[var, var] = expr" evaluate the expression here, loop over the
5964 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005965 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005966
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005967 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005968 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005969 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005970 if (compile_expr0(&p, cctx) == FAIL)
5971 return NULL;
5972 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005974 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005976 type_T *stacktype;
5977
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005978 stacktype = stack->ga_len == 0 ? &t_void
5979 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005980 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005982 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005983 goto theend;
5984 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01005985 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005986 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005987 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005988 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005989 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5990 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005991 if (stacktype->tt_member != NULL)
5992 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005993 }
5994 }
5995
5996 /*
5997 * Loop over variables in "[var, var] = expr".
5998 * For "var = expr" and "let var: type" this is done only once.
5999 */
6000 if (var_count > 0)
6001 var_start = skipwhite(arg + 1); // skip over the "["
6002 else
6003 var_start = arg;
6004 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6005 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006006 int instr_count = -1;
6007
Bram Moolenaar752fc692021-01-04 21:57:11 +01006008 vim_free(lhs.lhs_name);
6009
6010 /*
6011 * Figure out the LHS type and other properties.
6012 */
6013 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6014 goto theend;
6015
6016 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006017 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006018 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006019 goto theend;
6020 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006021 if (!is_decl && lhs.lhs_lvar != NULL
6022 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006023 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006024 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006025 goto theend;
6026 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006027
6028 if (!heredoc)
6029 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006030 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006031 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006032 if (oplen > 0 && var_count == 0)
6033 {
6034 // skip over the "=" and the expression
6035 p = skipwhite(op + oplen);
6036 compile_expr0(&p, cctx);
6037 }
6038 }
6039 else if (oplen > 0)
6040 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006041 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006042 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006043
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006044 // For "var = expr" evaluate the expression.
6045 if (var_count == 0)
6046 {
6047 int r;
6048
6049 // for "+=", "*=", "..=" etc. first load the current value
6050 if (*op != '=')
6051 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006052 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6053 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006054
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006056 {
6057 // TODO: get member from list or dict
6058 emsg("Index with operation not supported yet");
6059 goto theend;
6060 }
6061 }
6062
6063 // Compile the expression. Temporarily hide the new local
6064 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006065 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006066 --cctx->ctx_locals.ga_len;
6067 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006068 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006069 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006070 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006071 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006072 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006073 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006074 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006075 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006076 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006077 ++cctx->ctx_locals.ga_len;
6078 if (r == FAIL)
6079 goto theend;
6080 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006081 else if (semicolon && var_idx == var_count - 1)
6082 {
6083 // For "[var; var] = expr" get the rest of the list
6084 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6085 goto theend;
6086 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006087 else
6088 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006089 // For "[var, var] = expr" get the "var_idx" item from the
6090 // list.
6091 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006092 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006093 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006094
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006095 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006096 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006097 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006098 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006099 if ((rhs_type->tt_type == VAR_FUNC
6100 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006101 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006102 goto theend;
6103
Bram Moolenaar752fc692021-01-04 21:57:11 +01006104 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006105 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006106 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006108 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006109 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006110 }
6111 else
6112 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006113 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006114 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006115 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006116 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006117 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006119 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006120 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006121 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006122 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006123 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006124 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006125 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006126 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006127 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006128
Bram Moolenaar71abe482020-09-14 22:28:30 +02006129 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006130 if (lhs.lhs_has_index)
6131 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006132 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006133 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006134 goto theend;
6135 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006136 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006137 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006138 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006139 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006141 else if (cmdidx == CMD_final)
6142 {
6143 emsg(_(e_final_requires_a_value));
6144 goto theend;
6145 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006146 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006148 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006149 goto theend;
6150 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006152 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006153 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006154 goto theend;
6155 }
6156 else
6157 {
6158 // variables are always initialized
6159 if (ga_grow(instr, 1) == FAIL)
6160 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006161 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006162 {
6163 case VAR_BOOL:
6164 generate_PUSHBOOL(cctx, VVAL_FALSE);
6165 break;
6166 case VAR_FLOAT:
6167#ifdef FEAT_FLOAT
6168 generate_PUSHF(cctx, 0.0);
6169#endif
6170 break;
6171 case VAR_STRING:
6172 generate_PUSHS(cctx, NULL);
6173 break;
6174 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006175 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006176 break;
6177 case VAR_FUNC:
6178 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6179 break;
6180 case VAR_LIST:
6181 generate_NEWLIST(cctx, 0);
6182 break;
6183 case VAR_DICT:
6184 generate_NEWDICT(cctx, 0);
6185 break;
6186 case VAR_JOB:
6187 generate_PUSHJOB(cctx, NULL);
6188 break;
6189 case VAR_CHANNEL:
6190 generate_PUSHCHANNEL(cctx, NULL);
6191 break;
6192 case VAR_NUMBER:
6193 case VAR_UNKNOWN:
6194 case VAR_ANY:
6195 case VAR_PARTIAL:
6196 case VAR_VOID:
6197 case VAR_SPECIAL: // cannot happen
6198 generate_PUSHNR(cctx, 0);
6199 break;
6200 }
6201 }
6202 if (var_count == 0)
6203 end = p;
6204 }
6205
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006206 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006207 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006208 break;
6209
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006210 if (oplen > 0 && *op != '=')
6211 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006212 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006213 type_T *stacktype;
6214
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006215 if (*op == '.')
6216 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006217 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006219 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006220 if (
6221#ifdef FEAT_FLOAT
6222 // If variable is float operation with number is OK.
6223 !(expected == &t_float && stacktype == &t_number) &&
6224#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006225 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006226 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006227 goto theend;
6228
6229 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006230 {
6231 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6232 goto theend;
6233 }
6234 else if (*op == '+')
6235 {
6236 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 operator_type(lhs.lhs_member_type, stacktype),
6238 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006239 goto theend;
6240 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006241 else if (generate_two_op(cctx, op) == FAIL)
6242 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244
Bram Moolenaar752fc692021-01-04 21:57:11 +01006245 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006246 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006247 // Use the info in "lhs" to store the value at the index in the
6248 // list or dict.
6249 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6250 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006251 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006252 }
6253 else
6254 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006255 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6256 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006257 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006258 generate_LOCKCONST(cctx);
6259
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006260 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006261 && (lhs.lhs_type->tt_type == VAR_DICT
6262 || lhs.lhs_type->tt_type == VAR_LIST)
6263 && lhs.lhs_type->tt_member != NULL
6264 && lhs.lhs_type->tt_member != &t_any
6265 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006266 // Set the type in the list or dict, so that it can be checked,
6267 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006268 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006269
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006271 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 if (generate_store_var(cctx, lhs.lhs_dest,
6273 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6274 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6275 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006276 goto theend;
6277 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006278 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006279 {
6280 isn_T *isn = ((isn_T *)instr->ga_data)
6281 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006282
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006283 // optimization: turn "var = 123" from ISN_PUSHNR +
6284 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006285 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006286 && instr->ga_len == instr_count + 1
6287 && isn->isn_type == ISN_PUSHNR)
6288 {
6289 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006290
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006291 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006292 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006293 isn->isn_arg.storenr.stnr_val = val;
6294 if (stack->ga_len > 0)
6295 --stack->ga_len;
6296 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006297 else if (lhs.lhs_lvar->lv_from_outer > 0)
6298 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6299 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006300 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006301 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006302 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006303 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006304
6305 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006306 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006308
6309 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006310 if (var_count > 0 && !semicolon)
6311 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006312 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006313 goto theend;
6314 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006315
Bram Moolenaarb2097502020-07-19 17:17:02 +02006316 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317
6318theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006319 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 return ret;
6321}
6322
6323/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006324 * Check for an assignment at "eap->cmd", compile it if found.
6325 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6326 */
6327 static int
6328may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6329{
6330 char_u *pskip;
6331 char_u *p;
6332
6333 // Assuming the command starts with a variable or function name,
6334 // find what follows.
6335 // Skip over "var.member", "var[idx]" and the like.
6336 // Also "&opt = val", "$ENV = val" and "@r = val".
6337 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6338 ? eap->cmd + 1 : eap->cmd;
6339 p = to_name_end(pskip, TRUE);
6340 if (p > eap->cmd && *p != NUL)
6341 {
6342 char_u *var_end;
6343 int oplen;
6344 int heredoc;
6345
6346 if (eap->cmd[0] == '@')
6347 var_end = eap->cmd + 2;
6348 else
6349 var_end = find_name_end(pskip, NULL, NULL,
6350 FNE_CHECK_START | FNE_INCL_BR);
6351 oplen = assignment_len(skipwhite(var_end), &heredoc);
6352 if (oplen > 0)
6353 {
6354 size_t len = p - eap->cmd;
6355
6356 // Recognize an assignment if we recognize the variable
6357 // name:
6358 // "g:var = expr"
6359 // "local = expr" where "local" is a local var.
6360 // "script = expr" where "script" is a script-local var.
6361 // "import = expr" where "import" is an imported var
6362 // "&opt = expr"
6363 // "$ENV = expr"
6364 // "@r = expr"
6365 if (*eap->cmd == '&'
6366 || *eap->cmd == '$'
6367 || *eap->cmd == '@'
6368 || ((len) > 2 && eap->cmd[1] == ':')
6369 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6370 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6371 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6372 || find_imported(eap->cmd, len, cctx) != NULL)
6373 {
6374 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6375 if (*line == NULL || *line == eap->cmd)
6376 return FAIL;
6377 return OK;
6378 }
6379 }
6380 }
6381
6382 if (*eap->cmd == '[')
6383 {
6384 // [var, var] = expr
6385 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6386 if (*line == NULL)
6387 return FAIL;
6388 if (*line != eap->cmd)
6389 return OK;
6390 }
6391 return NOTDONE;
6392}
6393
6394/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006395 * Check if "name" can be "unlet".
6396 */
6397 int
6398check_vim9_unlet(char_u *name)
6399{
6400 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6401 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006402 // "unlet s:var" is allowed in legacy script.
6403 if (*name == 's' && !script_is_vim9())
6404 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006405 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006406 return FAIL;
6407 }
6408 return OK;
6409}
6410
6411/*
6412 * Callback passed to ex_unletlock().
6413 */
6414 static int
6415compile_unlet(
6416 lval_T *lvp,
6417 char_u *name_end,
6418 exarg_T *eap,
6419 int deep UNUSED,
6420 void *coookie)
6421{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006422 cctx_T *cctx = coookie;
6423 char_u *p = lvp->ll_name;
6424 int cc = *name_end;
6425 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006426
Bram Moolenaar752fc692021-01-04 21:57:11 +01006427 if (cctx->ctx_skip == SKIP_YES)
6428 return OK;
6429
6430 *name_end = NUL;
6431 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006432 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006433 // :unlet $ENV_VAR
6434 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6435 }
6436 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6437 {
6438 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006439
Bram Moolenaar752fc692021-01-04 21:57:11 +01006440 // This is similar to assigning: lookup the list/dict, compile the
6441 // idx/key. Then instead of storing the value unlet the item.
6442 // unlet {list}[idx]
6443 // unlet {dict}[key] dict.key
6444 //
6445 // Figure out the LHS type and other properties.
6446 //
6447 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6448
6449 // : unlet an indexed item
6450 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006451 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006452 iemsg("called compile_lhs() without an index");
6453 ret = FAIL;
6454 }
6455 else
6456 {
6457 // Use the info in "lhs" to unlet the item at the index in the
6458 // list or dict.
6459 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006460 }
6461
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 vim_free(lhs.lhs_name);
6463 }
6464 else if (check_vim9_unlet(p) == FAIL)
6465 {
6466 ret = FAIL;
6467 }
6468 else
6469 {
6470 // Normal name. Only supports g:, w:, t: and b: namespaces.
6471 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006472 }
6473
Bram Moolenaar752fc692021-01-04 21:57:11 +01006474 *name_end = cc;
6475 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006476}
6477
6478/*
6479 * compile "unlet var", "lock var" and "unlock var"
6480 * "arg" points to "var".
6481 */
6482 static char_u *
6483compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6484{
6485 char_u *p = arg;
6486
6487 if (eap->cmdidx != CMD_unlet)
6488 {
6489 emsg("Sorry, :lock and unlock not implemented yet");
6490 return NULL;
6491 }
6492
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006493 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6494 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006495 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6496}
6497
6498/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 * Compile an :import command.
6500 */
6501 static char_u *
6502compile_import(char_u *arg, cctx_T *cctx)
6503{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006504 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505}
6506
6507/*
6508 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6509 */
6510 static int
6511compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6512{
6513 garray_T *instr = &cctx->ctx_instr;
6514 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6515
6516 if (endlabel == NULL)
6517 return FAIL;
6518 endlabel->el_next = *el;
6519 *el = endlabel;
6520 endlabel->el_end_label = instr->ga_len;
6521
6522 generate_JUMP(cctx, when, 0);
6523 return OK;
6524}
6525
6526 static void
6527compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6528{
6529 garray_T *instr = &cctx->ctx_instr;
6530
6531 while (*el != NULL)
6532 {
6533 endlabel_T *cur = (*el);
6534 isn_T *isn;
6535
6536 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6537 isn->isn_arg.jump.jump_where = instr->ga_len;
6538 *el = cur->el_next;
6539 vim_free(cur);
6540 }
6541}
6542
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006543 static void
6544compile_free_jump_to_end(endlabel_T **el)
6545{
6546 while (*el != NULL)
6547 {
6548 endlabel_T *cur = (*el);
6549
6550 *el = cur->el_next;
6551 vim_free(cur);
6552 }
6553}
6554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555/*
6556 * Create a new scope and set up the generic items.
6557 */
6558 static scope_T *
6559new_scope(cctx_T *cctx, scopetype_T type)
6560{
6561 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6562
6563 if (scope == NULL)
6564 return NULL;
6565 scope->se_outer = cctx->ctx_scope;
6566 cctx->ctx_scope = scope;
6567 scope->se_type = type;
6568 scope->se_local_count = cctx->ctx_locals.ga_len;
6569 return scope;
6570}
6571
6572/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006573 * Free the current scope and go back to the outer scope.
6574 */
6575 static void
6576drop_scope(cctx_T *cctx)
6577{
6578 scope_T *scope = cctx->ctx_scope;
6579
6580 if (scope == NULL)
6581 {
6582 iemsg("calling drop_scope() without a scope");
6583 return;
6584 }
6585 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006586 switch (scope->se_type)
6587 {
6588 case IF_SCOPE:
6589 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6590 case FOR_SCOPE:
6591 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6592 case WHILE_SCOPE:
6593 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6594 case TRY_SCOPE:
6595 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6596 case NO_SCOPE:
6597 case BLOCK_SCOPE:
6598 break;
6599 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006600 vim_free(scope);
6601}
6602
6603/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 * compile "if expr"
6605 *
6606 * "if expr" Produces instructions:
6607 * EVAL expr Push result of "expr"
6608 * JUMP_IF_FALSE end
6609 * ... body ...
6610 * end:
6611 *
6612 * "if expr | else" Produces instructions:
6613 * EVAL expr Push result of "expr"
6614 * JUMP_IF_FALSE else
6615 * ... body ...
6616 * JUMP_ALWAYS end
6617 * else:
6618 * ... body ...
6619 * end:
6620 *
6621 * "if expr1 | elseif expr2 | else" Produces instructions:
6622 * EVAL expr Push result of "expr"
6623 * JUMP_IF_FALSE elseif
6624 * ... body ...
6625 * JUMP_ALWAYS end
6626 * elseif:
6627 * EVAL expr Push result of "expr"
6628 * JUMP_IF_FALSE else
6629 * ... body ...
6630 * JUMP_ALWAYS end
6631 * else:
6632 * ... body ...
6633 * end:
6634 */
6635 static char_u *
6636compile_if(char_u *arg, cctx_T *cctx)
6637{
6638 char_u *p = arg;
6639 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006640 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006642 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006643 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644
Bram Moolenaara5565e42020-05-09 15:44:01 +02006645 CLEAR_FIELD(ppconst);
6646 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006647 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006648 clear_ppconst(&ppconst);
6649 return NULL;
6650 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006651 if (cctx->ctx_skip == SKIP_YES)
6652 clear_ppconst(&ppconst);
6653 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006654 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006655 int error = FALSE;
6656 int v;
6657
Bram Moolenaara5565e42020-05-09 15:44:01 +02006658 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006659 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006660 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006661 if (error)
6662 return NULL;
6663 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006664 }
6665 else
6666 {
6667 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006668 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006669 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006670 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006671 if (bool_on_stack(cctx) == FAIL)
6672 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674
6675 scope = new_scope(cctx, IF_SCOPE);
6676 if (scope == NULL)
6677 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006678 scope->se_skip_save = skip_save;
6679 // "is_had_return" will be reset if any block does not end in :return
6680 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006682 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006683 {
6684 // "where" is set when ":elseif", "else" or ":endif" is found
6685 scope->se_u.se_if.is_if_label = instr->ga_len;
6686 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6687 }
6688 else
6689 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690
6691 return p;
6692}
6693
6694 static char_u *
6695compile_elseif(char_u *arg, cctx_T *cctx)
6696{
6697 char_u *p = arg;
6698 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006699 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 isn_T *isn;
6701 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006702 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006703 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704
6705 if (scope == NULL || scope->se_type != IF_SCOPE)
6706 {
6707 emsg(_(e_elseif_without_if));
6708 return NULL;
6709 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006710 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006711 if (!cctx->ctx_had_return)
6712 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006714 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006715 {
6716 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006718 return NULL;
6719 // previous "if" or "elseif" jumps here
6720 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6721 isn->isn_arg.jump.jump_where = instr->ga_len;
6722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006724 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006725 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006726 if (cctx->ctx_skip == SKIP_YES)
6727 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006728 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006729 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006730 clear_ppconst(&ppconst);
6731 return NULL;
6732 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006733 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006734 if (scope->se_skip_save == SKIP_YES)
6735 clear_ppconst(&ppconst);
6736 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006737 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006738 int error = FALSE;
6739 int v;
6740
Bram Moolenaar7f141552020-05-09 17:35:53 +02006741 // The expression results in a constant.
6742 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006743 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6744 if (error)
6745 return NULL;
6746 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006747 clear_ppconst(&ppconst);
6748 scope->se_u.se_if.is_if_label = -1;
6749 }
6750 else
6751 {
6752 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006753 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006754 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006755 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006756 if (bool_on_stack(cctx) == FAIL)
6757 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006759 // "where" is set when ":elseif", "else" or ":endif" is found
6760 scope->se_u.se_if.is_if_label = instr->ga_len;
6761 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763
6764 return p;
6765}
6766
6767 static char_u *
6768compile_else(char_u *arg, cctx_T *cctx)
6769{
6770 char_u *p = arg;
6771 garray_T *instr = &cctx->ctx_instr;
6772 isn_T *isn;
6773 scope_T *scope = cctx->ctx_scope;
6774
6775 if (scope == NULL || scope->se_type != IF_SCOPE)
6776 {
6777 emsg(_(e_else_without_if));
6778 return NULL;
6779 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006780 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006781 if (!cctx->ctx_had_return)
6782 scope->se_u.se_if.is_had_return = FALSE;
6783 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784
Bram Moolenaarefd88552020-06-18 20:50:10 +02006785 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006786 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006787 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006788 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006789 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006790 if (!cctx->ctx_had_return
6791 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6792 JUMP_ALWAYS, cctx) == FAIL)
6793 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006794 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006795
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006796 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006797 {
6798 if (scope->se_u.se_if.is_if_label >= 0)
6799 {
6800 // previous "if" or "elseif" jumps here
6801 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6802 isn->isn_arg.jump.jump_where = instr->ga_len;
6803 scope->se_u.se_if.is_if_label = -1;
6804 }
6805 }
6806
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006807 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006808 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810
6811 return p;
6812}
6813
6814 static char_u *
6815compile_endif(char_u *arg, cctx_T *cctx)
6816{
6817 scope_T *scope = cctx->ctx_scope;
6818 ifscope_T *ifscope;
6819 garray_T *instr = &cctx->ctx_instr;
6820 isn_T *isn;
6821
6822 if (scope == NULL || scope->se_type != IF_SCOPE)
6823 {
6824 emsg(_(e_endif_without_if));
6825 return NULL;
6826 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006827 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006828 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006829 if (!cctx->ctx_had_return)
6830 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006832 if (scope->se_u.se_if.is_if_label >= 0)
6833 {
6834 // previous "if" or "elseif" jumps here
6835 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6836 isn->isn_arg.jump.jump_where = instr->ga_len;
6837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 // Fill in the "end" label in jumps at the end of the blocks.
6839 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006840 cctx->ctx_skip = scope->se_skip_save;
6841
6842 // If all the blocks end in :return and there is an :else then the
6843 // had_return flag is set.
6844 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006846 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 return arg;
6848}
6849
6850/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006851 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 *
6853 * Produces instructions:
6854 * PUSHNR -1
6855 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006856 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6858 * - if beyond end, jump to "end"
6859 * - otherwise get item from list and push it
6860 * STORE var Store item in "var"
6861 * ... body ...
6862 * JUMP top Jump back to repeat
6863 * end: DROP Drop the result of "expr"
6864 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006865 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6866 * UNPACK 2 Split item in 2
6867 * STORE var1 Store item in "var1"
6868 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 */
6870 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006871compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006873 char_u *arg;
6874 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006875 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006877 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006878 int var_count = 0;
6879 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 size_t varlen;
6881 garray_T *instr = &cctx->ctx_instr;
6882 garray_T *stack = &cctx->ctx_type_stack;
6883 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006884 lvar_T *loop_lvar; // loop iteration variable
6885 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006887 type_T *item_type = &t_any;
6888 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889
Bram Moolenaar792f7862020-11-23 08:31:18 +01006890 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01006891 if (p == NULL)
6892 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006893 if (var_count == 0)
6894 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895
6896 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006897 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006898 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6899 return NULL;
6900 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 {
6902 emsg(_(e_missing_in));
6903 return NULL;
6904 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006905 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006906 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6907 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 scope = new_scope(cctx, FOR_SCOPE);
6910 if (scope == NULL)
6911 return NULL;
6912
Bram Moolenaar792f7862020-11-23 08:31:18 +01006913 // Reserve a variable to store the loop iteration counter and initialize it
6914 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006915 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6916 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006917 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006918 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006919 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006921 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006922 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923
6924 // compile "expr", it remains on the stack until "endfor"
6925 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006926 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006927 {
6928 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006930 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006931 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006933 // Now that we know the type of "var", check that it is a list, now or at
6934 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01006936 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006938 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 return NULL;
6940 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006941
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006942 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006943 {
6944 if (var_count == 1)
6945 item_type = vartype->tt_member;
6946 else if (vartype->tt_member->tt_type == VAR_LIST
6947 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6948 item_type = vartype->tt_member->tt_member;
6949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950
6951 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006952 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006953 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954
Bram Moolenaar792f7862020-11-23 08:31:18 +01006955 arg = arg_start;
6956 if (var_count > 1)
6957 {
6958 generate_UNPACK(cctx, var_count, semicolon);
6959 arg = skipwhite(arg + 1); // skip white after '['
6960
6961 // the list item is replaced by a number of items
6962 if (ga_grow(stack, var_count - 1) == FAIL)
6963 {
6964 drop_scope(cctx);
6965 return NULL;
6966 }
6967 --stack->ga_len;
6968 for (idx = 0; idx < var_count; ++idx)
6969 {
6970 ((type_T **)stack->ga_data)[stack->ga_len] =
6971 (semicolon && idx == 0) ? vartype : item_type;
6972 ++stack->ga_len;
6973 }
6974 }
6975
6976 for (idx = 0; idx < var_count; ++idx)
6977 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006978 assign_dest_T dest = dest_local;
6979 int opt_flags = 0;
6980 int vimvaridx = -1;
6981 type_T *type = &t_any;
6982
6983 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006984 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006985 name = vim_strnsave(arg, varlen);
6986 if (name == NULL)
6987 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006988
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006989 // TODO: script var not supported?
6990 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6991 &vimvaridx, &type, cctx) == FAIL)
6992 goto failed;
6993 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006994 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006995 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6996 0, 0, type, name) == FAIL)
6997 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006998 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006999 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007000 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007001 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007002 {
7003 semsg(_(e_variable_already_declared), arg);
7004 goto failed;
7005 }
7006
Bram Moolenaarea870692020-12-02 14:24:30 +01007007 if (STRNCMP(name, "s:", 2) == 0)
7008 {
7009 semsg(_(e_cannot_declare_script_variable_in_function), name);
7010 goto failed;
7011 }
7012
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007013 // Reserve a variable to store "var".
7014 // TODO: check for type
7015 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7016 if (var_lvar == NULL)
7017 // out of memory or used as an argument
7018 goto failed;
7019
7020 if (semicolon && idx == var_count - 1)
7021 var_lvar->lv_type = vartype;
7022 else
7023 var_lvar->lv_type = item_type;
7024 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7025 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007026
Bram Moolenaar036d0712021-01-17 20:23:38 +01007027 if (*p == ':')
7028 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007029 if (*p == ',' || *p == ';')
7030 ++p;
7031 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007032 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007033 }
7034
7035 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007036
7037failed:
7038 vim_free(name);
7039 drop_scope(cctx);
7040 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041}
7042
7043/*
7044 * compile "endfor"
7045 */
7046 static char_u *
7047compile_endfor(char_u *arg, cctx_T *cctx)
7048{
7049 garray_T *instr = &cctx->ctx_instr;
7050 scope_T *scope = cctx->ctx_scope;
7051 forscope_T *forscope;
7052 isn_T *isn;
7053
7054 if (scope == NULL || scope->se_type != FOR_SCOPE)
7055 {
7056 emsg(_(e_for));
7057 return NULL;
7058 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007059 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007061 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062
7063 // At end of ":for" scope jump back to the FOR instruction.
7064 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7065
7066 // Fill in the "end" label in the FOR statement so it can jump here
7067 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7068 isn->isn_arg.forloop.for_end = instr->ga_len;
7069
7070 // Fill in the "end" label any BREAK statements
7071 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7072
7073 // Below the ":for" scope drop the "expr" list from the stack.
7074 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7075 return NULL;
7076
7077 vim_free(scope);
7078
7079 return arg;
7080}
7081
7082/*
7083 * compile "while expr"
7084 *
7085 * Produces instructions:
7086 * top: EVAL expr Push result of "expr"
7087 * JUMP_IF_FALSE end jump if false
7088 * ... body ...
7089 * JUMP top Jump back to repeat
7090 * end:
7091 *
7092 */
7093 static char_u *
7094compile_while(char_u *arg, cctx_T *cctx)
7095{
7096 char_u *p = arg;
7097 garray_T *instr = &cctx->ctx_instr;
7098 scope_T *scope;
7099
7100 scope = new_scope(cctx, WHILE_SCOPE);
7101 if (scope == NULL)
7102 return NULL;
7103
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007104 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105
7106 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007107 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 return NULL;
7109
Bram Moolenaar13106602020-10-04 16:06:05 +02007110 if (bool_on_stack(cctx) == FAIL)
7111 return FAIL;
7112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007114 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 JUMP_IF_FALSE, cctx) == FAIL)
7116 return FAIL;
7117
7118 return p;
7119}
7120
7121/*
7122 * compile "endwhile"
7123 */
7124 static char_u *
7125compile_endwhile(char_u *arg, cctx_T *cctx)
7126{
7127 scope_T *scope = cctx->ctx_scope;
7128
7129 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7130 {
7131 emsg(_(e_while));
7132 return NULL;
7133 }
7134 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007135 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136
7137 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007138 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139
7140 // Fill in the "end" label in the WHILE statement so it can jump here.
7141 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007142 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143
7144 vim_free(scope);
7145
7146 return arg;
7147}
7148
7149/*
7150 * compile "continue"
7151 */
7152 static char_u *
7153compile_continue(char_u *arg, cctx_T *cctx)
7154{
7155 scope_T *scope = cctx->ctx_scope;
7156
7157 for (;;)
7158 {
7159 if (scope == NULL)
7160 {
7161 emsg(_(e_continue));
7162 return NULL;
7163 }
7164 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7165 break;
7166 scope = scope->se_outer;
7167 }
7168
7169 // Jump back to the FOR or WHILE instruction.
7170 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007171 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7172 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 return arg;
7174}
7175
7176/*
7177 * compile "break"
7178 */
7179 static char_u *
7180compile_break(char_u *arg, cctx_T *cctx)
7181{
7182 scope_T *scope = cctx->ctx_scope;
7183 endlabel_T **el;
7184
7185 for (;;)
7186 {
7187 if (scope == NULL)
7188 {
7189 emsg(_(e_break));
7190 return NULL;
7191 }
7192 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7193 break;
7194 scope = scope->se_outer;
7195 }
7196
7197 // Jump to the end of the FOR or WHILE loop.
7198 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007199 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007201 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7203 return FAIL;
7204
7205 return arg;
7206}
7207
7208/*
7209 * compile "{" start of block
7210 */
7211 static char_u *
7212compile_block(char_u *arg, cctx_T *cctx)
7213{
7214 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7215 return NULL;
7216 return skipwhite(arg + 1);
7217}
7218
7219/*
7220 * compile end of block: drop one scope
7221 */
7222 static void
7223compile_endblock(cctx_T *cctx)
7224{
7225 scope_T *scope = cctx->ctx_scope;
7226
7227 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007228 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 vim_free(scope);
7230}
7231
7232/*
7233 * compile "try"
7234 * Creates a new scope for the try-endtry, pointing to the first catch and
7235 * finally.
7236 * Creates another scope for the "try" block itself.
7237 * TRY instruction sets up exception handling at runtime.
7238 *
7239 * "try"
7240 * TRY -> catch1, -> finally push trystack entry
7241 * ... try block
7242 * "throw {exception}"
7243 * EVAL {exception}
7244 * THROW create exception
7245 * ... try block
7246 * " catch {expr}"
7247 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007248 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 * EVAL {expr}
7250 * MATCH
7251 * JUMP nomatch -> catch2
7252 * CATCH remove exception
7253 * ... catch block
7254 * " catch"
7255 * JUMP -> finally
7256 * catch2: CATCH remove exception
7257 * ... catch block
7258 * " finally"
7259 * finally:
7260 * ... finally block
7261 * " endtry"
7262 * ENDTRY pop trystack entry, may rethrow
7263 */
7264 static char_u *
7265compile_try(char_u *arg, cctx_T *cctx)
7266{
7267 garray_T *instr = &cctx->ctx_instr;
7268 scope_T *try_scope;
7269 scope_T *scope;
7270
7271 // scope that holds the jumps that go to catch/finally/endtry
7272 try_scope = new_scope(cctx, TRY_SCOPE);
7273 if (try_scope == NULL)
7274 return NULL;
7275
Bram Moolenaar69f70502021-01-01 16:10:46 +01007276 if (cctx->ctx_skip != SKIP_YES)
7277 {
7278 // "catch" is set when the first ":catch" is found.
7279 // "finally" is set when ":finally" or ":endtry" is found
7280 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7281 if (generate_instr(cctx, ISN_TRY) == NULL)
7282 return NULL;
7283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284
7285 // scope for the try block itself
7286 scope = new_scope(cctx, BLOCK_SCOPE);
7287 if (scope == NULL)
7288 return NULL;
7289
7290 return arg;
7291}
7292
7293/*
7294 * compile "catch {expr}"
7295 */
7296 static char_u *
7297compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7298{
7299 scope_T *scope = cctx->ctx_scope;
7300 garray_T *instr = &cctx->ctx_instr;
7301 char_u *p;
7302 isn_T *isn;
7303
7304 // end block scope from :try or :catch
7305 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7306 compile_endblock(cctx);
7307 scope = cctx->ctx_scope;
7308
7309 // Error if not in a :try scope
7310 if (scope == NULL || scope->se_type != TRY_SCOPE)
7311 {
7312 emsg(_(e_catch));
7313 return NULL;
7314 }
7315
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007316 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007318 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 return NULL;
7320 }
7321
Bram Moolenaar69f70502021-01-01 16:10:46 +01007322 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007324 // Jump from end of previous block to :finally or :endtry
7325 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7326 JUMP_ALWAYS, cctx) == FAIL)
7327 return NULL;
7328
7329 // End :try or :catch scope: set value in ISN_TRY instruction
7330 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7331 if (isn->isn_arg.try.try_catch == 0)
7332 isn->isn_arg.try.try_catch = instr->ga_len;
7333 if (scope->se_u.se_try.ts_catch_label != 0)
7334 {
7335 // Previous catch without match jumps here
7336 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7337 isn->isn_arg.jump.jump_where = instr->ga_len;
7338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 }
7340
7341 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007342 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007344 scope->se_u.se_try.ts_caught_all = TRUE;
7345 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 }
7347 else
7348 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007349 char_u *end;
7350 char_u *pat;
7351 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007352 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007353 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 // Push v:exception, push {expr} and MATCH
7356 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7357
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007358 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007359 if (*end != *p)
7360 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007361 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007362 vim_free(tofree);
7363 return FAIL;
7364 }
7365 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007366 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007367 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007368 len = (int)(end - tofree);
7369 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007370 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007371 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007372 if (pat == NULL)
7373 return FAIL;
7374 if (generate_PUSHS(cctx, pat) == FAIL)
7375 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7378 return NULL;
7379
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007380 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7382 return NULL;
7383 }
7384
Bram Moolenaar69f70502021-01-01 16:10:46 +01007385 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 return NULL;
7387
7388 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7389 return NULL;
7390 return p;
7391}
7392
7393 static char_u *
7394compile_finally(char_u *arg, cctx_T *cctx)
7395{
7396 scope_T *scope = cctx->ctx_scope;
7397 garray_T *instr = &cctx->ctx_instr;
7398 isn_T *isn;
7399
7400 // end block scope from :try or :catch
7401 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7402 compile_endblock(cctx);
7403 scope = cctx->ctx_scope;
7404
7405 // Error if not in a :try scope
7406 if (scope == NULL || scope->se_type != TRY_SCOPE)
7407 {
7408 emsg(_(e_finally));
7409 return NULL;
7410 }
7411
7412 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007413 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 if (isn->isn_arg.try.try_finally != 0)
7415 {
7416 emsg(_(e_finally_dup));
7417 return NULL;
7418 }
7419
7420 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007421 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422
Bram Moolenaar585fea72020-04-02 22:33:21 +02007423 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007424 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 {
7426 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007427 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007429 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 }
7431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 // TODO: set index in ts_finally_label jumps
7433
7434 return arg;
7435}
7436
7437 static char_u *
7438compile_endtry(char_u *arg, cctx_T *cctx)
7439{
7440 scope_T *scope = cctx->ctx_scope;
7441 garray_T *instr = &cctx->ctx_instr;
7442 isn_T *isn;
7443
7444 // end block scope from :catch or :finally
7445 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7446 compile_endblock(cctx);
7447 scope = cctx->ctx_scope;
7448
7449 // Error if not in a :try scope
7450 if (scope == NULL || scope->se_type != TRY_SCOPE)
7451 {
7452 if (scope == NULL)
7453 emsg(_(e_no_endtry));
7454 else if (scope->se_type == WHILE_SCOPE)
7455 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007456 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 emsg(_(e_endfor));
7458 else
7459 emsg(_(e_endif));
7460 return NULL;
7461 }
7462
Bram Moolenaar69f70502021-01-01 16:10:46 +01007463 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007465 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7466 if (isn->isn_arg.try.try_catch == 0
7467 && isn->isn_arg.try.try_finally == 0)
7468 {
7469 emsg(_(e_missing_catch_or_finally));
7470 return NULL;
7471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472
Bram Moolenaar69f70502021-01-01 16:10:46 +01007473 // Fill in the "end" label in jumps at the end of the blocks, if not
7474 // done by ":finally".
7475 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476
Bram Moolenaar69f70502021-01-01 16:10:46 +01007477 // End :catch or :finally scope: set value in ISN_TRY instruction
7478 if (isn->isn_arg.try.try_catch == 0)
7479 isn->isn_arg.try.try_catch = instr->ga_len;
7480 if (isn->isn_arg.try.try_finally == 0)
7481 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007482
Bram Moolenaar69f70502021-01-01 16:10:46 +01007483 if (scope->se_u.se_try.ts_catch_label != 0)
7484 {
7485 // Last catch without match jumps here
7486 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7487 isn->isn_arg.jump.jump_where = instr->ga_len;
7488 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007489 }
7490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 compile_endblock(cctx);
7492
Bram Moolenaar69f70502021-01-01 16:10:46 +01007493 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 return NULL;
7495 return arg;
7496}
7497
7498/*
7499 * compile "throw {expr}"
7500 */
7501 static char_u *
7502compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7503{
7504 char_u *p = skipwhite(arg);
7505
Bram Moolenaara5565e42020-05-09 15:44:01 +02007506 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007507 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007508 if (cctx->ctx_skip == SKIP_YES)
7509 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 if (may_generate_2STRING(-1, cctx) == FAIL)
7511 return NULL;
7512 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7513 return NULL;
7514
7515 return p;
7516}
7517
7518/*
7519 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007520 * compile "echomsg expr"
7521 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007522 * compile "execute expr"
7523 */
7524 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007525compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007526{
7527 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007528 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007529 int count = 0;
7530
7531 for (;;)
7532 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007533 if (ends_excmd2(prev, p))
7534 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007535 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007536 return NULL;
7537 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007538 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007539 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007540 }
7541
Bram Moolenaare4984292020-12-13 14:19:25 +01007542 if (count > 0)
7543 {
7544 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7545 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7546 else if (cmdidx == CMD_execute)
7547 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7548 else if (cmdidx == CMD_echomsg)
7549 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7550 else
7551 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007553 return p;
7554}
7555
7556/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007557 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007558 * instruction to compute it and return OK.
7559 * Otherwise return FAIL, the caller must deal with any range.
7560 */
7561 static int
7562compile_variable_range(exarg_T *eap, cctx_T *cctx)
7563{
7564 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7565 char_u *p = skipdigits(eap->cmd);
7566
7567 if (p == range_end)
7568 return FAIL;
7569 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7570}
7571
7572/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007573 * :put r
7574 * :put ={expr}
7575 */
7576 static char_u *
7577compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7578{
7579 char_u *line = arg;
7580 linenr_T lnum;
7581 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007582 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007583
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007584 eap->regname = *line;
7585
7586 if (eap->regname == '=')
7587 {
7588 char_u *p = line + 1;
7589
7590 if (compile_expr0(&p, cctx) == FAIL)
7591 return NULL;
7592 line = p;
7593 }
7594 else if (eap->regname != NUL)
7595 ++line;
7596
Bram Moolenaar08597872020-12-10 19:43:40 +01007597 if (compile_variable_range(eap, cctx) == OK)
7598 {
7599 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7600 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007601 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007602 {
7603 // Either no range or a number.
7604 // "errormsg" will not be set because the range is ADDR_LINES.
7605 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007606 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007607 return NULL;
7608 if (eap->addr_count == 0)
7609 lnum = -1;
7610 else
7611 lnum = eap->line2;
7612 if (above)
7613 --lnum;
7614 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007615
7616 generate_PUT(cctx, eap->regname, lnum);
7617 return line;
7618}
7619
7620/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007621 * A command that is not compiled, execute with legacy code.
7622 */
7623 static char_u *
7624compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7625{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007626 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007627 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007628 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007629
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007630 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007631 goto theend;
7632
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007633 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007634 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007635 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007636 int usefilter = FALSE;
7637
7638 has_expr = argt & (EX_XFILE | EX_EXPAND);
7639
7640 // If the command can be followed by a bar, find the bar and truncate
7641 // it, so that the following command can be compiled.
7642 // The '|' is overwritten with a NUL, it is put back below.
7643 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7644 && *eap->arg == '!')
7645 // :w !filter or :r !filter or :r! filter
7646 usefilter = TRUE;
7647 if ((argt & EX_TRLBAR) && !usefilter)
7648 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007649 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007650 separate_nextcmd(eap);
7651 if (eap->nextcmd != NULL)
7652 nextcmd = eap->nextcmd;
7653 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007654 else if (eap->cmdidx == CMD_wincmd)
7655 {
7656 p = eap->arg;
7657 if (*p != NUL)
7658 ++p;
7659 if (*p == 'g' || *p == Ctrl_G)
7660 ++p;
7661 p = skipwhite(p);
7662 if (*p == '|')
7663 {
7664 *p = NUL;
7665 nextcmd = p + 1;
7666 }
7667 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007668 }
7669
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007670 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7671 {
7672 // expand filename in "syntax include [@group] filename"
7673 has_expr = TRUE;
7674 eap->arg = skipwhite(eap->arg + 7);
7675 if (*eap->arg == '@')
7676 eap->arg = skiptowhite(eap->arg);
7677 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007678
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007679 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7680 && STRLEN(eap->arg) > 4)
7681 {
7682 int delim = *eap->arg;
7683
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007684 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007685 if (*p == delim)
7686 {
7687 eap->arg = p + 1;
7688 has_expr = TRUE;
7689 }
7690 }
7691
Bram Moolenaarecac5912021-01-05 19:23:28 +01007692 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7693 {
7694 // TODO: should only expand when appropriate for the command
7695 eap->arg = skiptowhite(eap->arg);
7696 has_expr = TRUE;
7697 }
7698
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007699 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007700 {
7701 int count = 0;
7702 char_u *start = skipwhite(line);
7703
7704 // :cmd xxx`=expr1`yyy`=expr2`zzz
7705 // PUSHS ":cmd xxx"
7706 // eval expr1
7707 // PUSHS "yyy"
7708 // eval expr2
7709 // PUSHS "zzz"
7710 // EXECCONCAT 5
7711 for (;;)
7712 {
7713 if (p > start)
7714 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007715 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007716 ++count;
7717 }
7718 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007719 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007720 return NULL;
7721 may_generate_2STRING(-1, cctx);
7722 ++count;
7723 p = skipwhite(p);
7724 if (*p != '`')
7725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007726 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007727 return NULL;
7728 }
7729 start = p + 1;
7730
7731 p = (char_u *)strstr((char *)start, "`=");
7732 if (p == NULL)
7733 {
7734 if (*skipwhite(start) != NUL)
7735 {
7736 generate_PUSHS(cctx, vim_strsave(start));
7737 ++count;
7738 }
7739 break;
7740 }
7741 }
7742 generate_EXECCONCAT(cctx, count);
7743 }
7744 else
7745 generate_EXEC(cctx, line);
7746
7747theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007748 if (*nextcmd != NUL)
7749 {
7750 // the parser expects a pointer to the bar, put it back
7751 --nextcmd;
7752 *nextcmd = '|';
7753 }
7754
7755 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007756}
7757
7758/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007759 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007760 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007761 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007762 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007763add_def_function(ufunc_T *ufunc)
7764{
7765 dfunc_T *dfunc;
7766
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007767 if (def_functions.ga_len == 0)
7768 {
7769 // The first position is not used, so that a zero uf_dfunc_idx means it
7770 // wasn't set.
7771 if (ga_grow(&def_functions, 1) == FAIL)
7772 return FAIL;
7773 ++def_functions.ga_len;
7774 }
7775
Bram Moolenaar09689a02020-05-09 22:50:08 +02007776 // Add the function to "def_functions".
7777 if (ga_grow(&def_functions, 1) == FAIL)
7778 return FAIL;
7779 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7780 CLEAR_POINTER(dfunc);
7781 dfunc->df_idx = def_functions.ga_len;
7782 ufunc->uf_dfunc_idx = dfunc->df_idx;
7783 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007784 dfunc->df_name = vim_strsave(ufunc->uf_name);
7785 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007786 ++def_functions.ga_len;
7787 return OK;
7788}
7789
7790/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007791 * After ex_function() has collected all the function lines: parse and compile
7792 * the lines into instructions.
7793 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007794 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7795 * the return statement (used for lambda). When uf_ret_type is already set
7796 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007797 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007798 * This can be used recursively through compile_lambda(), which may reallocate
7799 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007800 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007802 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007803compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007804{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007805 char_u *line = NULL;
7806 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007808 cctx_T cctx;
7809 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007810 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811 int ret = FAIL;
7812 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007813 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007814 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007815 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007817 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007818 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007819 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007821 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7822 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007823 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007825 else
7826 {
7827 if (add_def_function(ufunc) == FAIL)
7828 return FAIL;
7829 new_def_function = TRUE;
7830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007831
Bram Moolenaar985116a2020-07-12 17:31:09 +02007832 ufunc->uf_def_status = UF_COMPILING;
7833
Bram Moolenaara80faa82020-04-12 19:37:17 +02007834 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 cctx.ctx_ufunc = ufunc;
7836 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007837 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7839 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7840 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7841 cctx.ctx_type_list = &ufunc->uf_type_list;
7842 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7843 instr = &cctx.ctx_instr;
7844
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007845 // Set the context to the function, it may be compiled when called from
7846 // another script. Set the script version to the most modern one.
7847 // The line number will be set in next_line_from_context().
7848 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7850
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007851 // Make sure error messages are OK.
7852 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7853 if (do_estack_push)
7854 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007855 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007856
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007857 if (ufunc->uf_def_args.ga_len > 0)
7858 {
7859 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007860 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007861 int i;
7862 char_u *arg;
7863 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007864 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007865
7866 // Produce instructions for the default values of optional arguments.
7867 // Store the instruction index in uf_def_arg_idx[] so that we know
7868 // where to start when the function is called, depending on the number
7869 // of arguments.
7870 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7871 if (ufunc->uf_def_arg_idx == NULL)
7872 goto erret;
7873 for (i = 0; i < count; ++i)
7874 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007875 garray_T *stack = &cctx.ctx_type_stack;
7876 type_T *val_type;
7877 int arg_idx = first_def_arg + i;
7878
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007879 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7880 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007881 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007882 goto erret;
7883
7884 // If no type specified use the type of the default value.
7885 // Otherwise check that the default value type matches the
7886 // specified type.
7887 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7888 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007889 {
7890 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007891 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007892 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007893 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7894 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007895 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007896
7897 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007898 goto erret;
7899 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007900 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007901
7902 if (did_set_arg_type)
7903 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007904 }
7905
7906 /*
7907 * Loop over all the lines of the function and generate instructions.
7908 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007909 for (;;)
7910 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007911 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007912 int starts_with_colon = FALSE;
7913 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007914 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007915
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007916 // Bail out on the first error to avoid a flood of errors and report
7917 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007918 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007919 goto erret;
7920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007921 if (line != NULL && *line == '|')
7922 // the line continues after a '|'
7923 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007924 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007925 && !(*line == '#' && (line == cctx.ctx_line_start
7926 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007928 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929 goto erret;
7930 }
7931 else
7932 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007933 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007934 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007935 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 }
7938
Bram Moolenaara80faa82020-04-12 19:37:17 +02007939 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940 ea.cmdlinep = &line;
7941 ea.cmd = skipwhite(line);
7942
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007943 // Some things can be recognized by the first character.
7944 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007946 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007947 // "#" starts a comment
7948 line = (char_u *)"";
7949 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007951 case '}':
7952 {
7953 // "}" ends a block scope
7954 scopetype_T stype = cctx.ctx_scope == NULL
7955 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007957 if (stype == BLOCK_SCOPE)
7958 {
7959 compile_endblock(&cctx);
7960 line = ea.cmd;
7961 }
7962 else
7963 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007964 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007965 goto erret;
7966 }
7967 if (line != NULL)
7968 line = skipwhite(ea.cmd + 1);
7969 continue;
7970 }
7971
7972 case '{':
7973 // "{" starts a block scope
7974 // "{'a': 1}->func() is something else
7975 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7976 {
7977 line = compile_block(ea.cmd, &cctx);
7978 continue;
7979 }
7980 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 }
7982
7983 /*
7984 * COMMAND MODIFIERS
7985 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007986 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007987 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7988 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989 {
7990 if (errormsg != NULL)
7991 goto erret;
7992 // empty line or comment
7993 line = (char_u *)"";
7994 continue;
7995 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007996 generate_cmdmods(&cctx, &local_cmdmod);
7997 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007999 // Check if there was a colon after the last command modifier or before
8000 // the current position.
8001 for (p = ea.cmd; p >= line; --p)
8002 {
8003 if (*p == ':')
8004 starts_with_colon = TRUE;
8005 if (p < ea.cmd && !VIM_ISWHITE(*p))
8006 break;
8007 }
8008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008010 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008012 {
8013 if (*ea.cmd == '(')
8014 // not for "call()"
8015 ea.cmd = p;
8016 else
8017 ea.cmd = skipwhite(ea.cmd);
8018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008020 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008021 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008022 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008023
Bram Moolenaar17126b12021-01-07 22:03:02 +01008024 // Check for assignment after command modifiers.
8025 assign = may_compile_assignment(&ea, &line, &cctx);
8026 if (assign == OK)
8027 goto nextline;
8028 if (assign == FAIL)
8029 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 }
8031
8032 /*
8033 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008034 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008036 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008037 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008038 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008039 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008040 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008041 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008042 if (!starts_with_colon)
8043 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008044 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008045 goto erret;
8046 }
8047 if (ends_excmd2(line, ea.cmd))
8048 {
8049 // A range without a command: jump to the line.
8050 // TODO: compile to a more efficient command, possibly
8051 // calling parse_cmd_address().
8052 ea.cmdidx = CMD_SIZE;
8053 line = compile_exec(line, &ea, &cctx);
8054 goto nextline;
8055 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008056 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008057 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008058 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008059 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008060 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008061
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008062 if (p == NULL)
8063 {
8064 if (cctx.ctx_skip != SKIP_YES)
8065 emsg(_(e_ambiguous_use_of_user_defined_command));
8066 goto erret;
8067 }
8068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008069 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8070 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008071 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008072 {
8073 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008074 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008075 }
8076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008077 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008078 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008080 // CMD_var cannot happen, compile_assignment() above would be
8081 // used. Most likely an assignment to a non-existing variable.
8082 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008083 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085 }
8086
Bram Moolenaar3988f642020-08-27 22:43:03 +02008087 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008088 && ea.cmdidx != CMD_elseif
8089 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008090 && ea.cmdidx != CMD_endif
8091 && ea.cmdidx != CMD_endfor
8092 && ea.cmdidx != CMD_endwhile
8093 && ea.cmdidx != CMD_catch
8094 && ea.cmdidx != CMD_finally
8095 && ea.cmdidx != CMD_endtry)
8096 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008097 emsg(_(e_unreachable_code_after_return));
8098 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008099 }
8100
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008101 p = skipwhite(p);
8102 if (ea.cmdidx != CMD_SIZE
8103 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8104 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008105 if (ea.cmdidx >= 0)
8106 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008107 if ((ea.argt & EX_BANG) && *p == '!')
8108 {
8109 ea.forceit = TRUE;
8110 p = skipwhite(p + 1);
8111 }
8112 }
8113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008114 switch (ea.cmdidx)
8115 {
8116 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008117 ea.arg = p;
8118 line = compile_nested_function(&ea, &cctx);
8119 break;
8120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008122 // TODO: should we allow this, e.g. to declare a global
8123 // function?
8124 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008125 goto erret;
8126
8127 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008128 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008129 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008130 break;
8131
8132 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008133 emsg(_(e_cannot_use_let_in_vim9_script));
8134 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008135 case CMD_var:
8136 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 case CMD_const:
8138 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008139 if (line == p)
8140 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 break;
8142
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008143 case CMD_unlet:
8144 case CMD_unlockvar:
8145 case CMD_lockvar:
8146 line = compile_unletlock(p, &ea, &cctx);
8147 break;
8148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008149 case CMD_import:
8150 line = compile_import(p, &cctx);
8151 break;
8152
8153 case CMD_if:
8154 line = compile_if(p, &cctx);
8155 break;
8156 case CMD_elseif:
8157 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008158 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159 break;
8160 case CMD_else:
8161 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008162 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008163 break;
8164 case CMD_endif:
8165 line = compile_endif(p, &cctx);
8166 break;
8167
8168 case CMD_while:
8169 line = compile_while(p, &cctx);
8170 break;
8171 case CMD_endwhile:
8172 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008173 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174 break;
8175
8176 case CMD_for:
8177 line = compile_for(p, &cctx);
8178 break;
8179 case CMD_endfor:
8180 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008181 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182 break;
8183 case CMD_continue:
8184 line = compile_continue(p, &cctx);
8185 break;
8186 case CMD_break:
8187 line = compile_break(p, &cctx);
8188 break;
8189
8190 case CMD_try:
8191 line = compile_try(p, &cctx);
8192 break;
8193 case CMD_catch:
8194 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008195 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 break;
8197 case CMD_finally:
8198 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008199 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200 break;
8201 case CMD_endtry:
8202 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008203 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008204 break;
8205 case CMD_throw:
8206 line = compile_throw(p, &cctx);
8207 break;
8208
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008209 case CMD_eval:
8210 if (compile_expr0(&p, &cctx) == FAIL)
8211 goto erret;
8212
Bram Moolenaar3988f642020-08-27 22:43:03 +02008213 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008214 generate_instr_drop(&cctx, ISN_DROP, 1);
8215
8216 line = skipwhite(p);
8217 break;
8218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008219 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008220 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008221 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008222 case CMD_echomsg:
8223 case CMD_echoerr:
8224 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008225 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008226
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008227 case CMD_put:
8228 ea.cmd = cmd;
8229 line = compile_put(p, &ea, &cctx);
8230 break;
8231
Bram Moolenaar3988f642020-08-27 22:43:03 +02008232 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008233
Bram Moolenaarae616492020-07-28 20:07:27 +02008234 case CMD_append:
8235 case CMD_change:
8236 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008237 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008238 case CMD_xit:
8239 not_in_vim9(&ea);
8240 goto erret;
8241
Bram Moolenaar002262f2020-07-08 17:47:57 +02008242 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008243 if (cctx.ctx_skip != SKIP_YES)
8244 {
8245 semsg(_(e_invalid_command_str), ea.cmd);
8246 goto erret;
8247 }
8248 // We don't check for a next command here.
8249 line = (char_u *)"";
8250 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008253 if (cctx.ctx_skip == SKIP_YES)
8254 {
8255 // We don't check for a next command here.
8256 line = (char_u *)"";
8257 }
8258 else
8259 {
8260 // Not recognized, execute with do_cmdline_cmd().
8261 ea.arg = p;
8262 line = compile_exec(line, &ea, &cctx);
8263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264 break;
8265 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008266nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 if (line == NULL)
8268 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008269 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008270
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008271 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008272 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008274 if (cctx.ctx_type_stack.ga_len < 0)
8275 {
8276 iemsg("Type stack underflow");
8277 goto erret;
8278 }
8279 }
8280
8281 if (cctx.ctx_scope != NULL)
8282 {
8283 if (cctx.ctx_scope->se_type == IF_SCOPE)
8284 emsg(_(e_endif));
8285 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8286 emsg(_(e_endwhile));
8287 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8288 emsg(_(e_endfor));
8289 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008290 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008291 goto erret;
8292 }
8293
Bram Moolenaarefd88552020-06-18 20:50:10 +02008294 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295 {
8296 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8297 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008298 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008299 goto erret;
8300 }
8301
8302 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008303 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008304 }
8305
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008306 {
8307 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8308 + ufunc->uf_dfunc_idx;
8309 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008310 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008311 dfunc->df_instr = instr->ga_data;
8312 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008313 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008314 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008315 if (cctx.ctx_outer_used)
8316 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008317 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008319
8320 ret = OK;
8321
8322erret:
8323 if (ret == FAIL)
8324 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008325 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008326 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8327 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008328
8329 for (idx = 0; idx < instr->ga_len; ++idx)
8330 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008331 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008332 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008333
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008334 // If using the last entry in the table and it was added above, we
8335 // might as well remove it.
8336 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008337 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008338 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008339 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008340 ufunc->uf_dfunc_idx = 0;
8341 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008342 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008343
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008344 while (cctx.ctx_scope != NULL)
8345 drop_scope(&cctx);
8346
Bram Moolenaar20431c92020-03-20 18:39:46 +01008347 // Don't execute this function body.
8348 ga_clear_strings(&ufunc->uf_lines);
8349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008350 if (errormsg != NULL)
8351 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008352 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008353 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354 }
8355
8356 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008357 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008358 if (do_estack_push)
8359 estack_pop();
8360
Bram Moolenaar20431c92020-03-20 18:39:46 +01008361 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008362 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008364 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008365}
8366
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008367 void
8368set_function_type(ufunc_T *ufunc)
8369{
8370 int varargs = ufunc->uf_va_name != NULL;
8371 int argcount = ufunc->uf_args.ga_len;
8372
8373 // Create a type for the function, with the return type and any
8374 // argument types.
8375 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8376 // The type is included in "tt_args".
8377 if (argcount > 0 || varargs)
8378 {
8379 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8380 argcount, &ufunc->uf_type_list);
8381 // Add argument types to the function type.
8382 if (func_type_add_arg_types(ufunc->uf_func_type,
8383 argcount + varargs,
8384 &ufunc->uf_type_list) == FAIL)
8385 return;
8386 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8387 ufunc->uf_func_type->tt_min_argcount =
8388 argcount - ufunc->uf_def_args.ga_len;
8389 if (ufunc->uf_arg_types == NULL)
8390 {
8391 int i;
8392
8393 // lambda does not have argument types.
8394 for (i = 0; i < argcount; ++i)
8395 ufunc->uf_func_type->tt_args[i] = &t_any;
8396 }
8397 else
8398 mch_memmove(ufunc->uf_func_type->tt_args,
8399 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8400 if (varargs)
8401 {
8402 ufunc->uf_func_type->tt_args[argcount] =
8403 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8404 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8405 }
8406 }
8407 else
8408 // No arguments, can use a predefined type.
8409 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8410 argcount, &ufunc->uf_type_list);
8411}
8412
8413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008414/*
8415 * Delete an instruction, free what it contains.
8416 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008417 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418delete_instr(isn_T *isn)
8419{
8420 switch (isn->isn_type)
8421 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008422 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008424 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008425 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426 case ISN_LOADENV:
8427 case ISN_LOADG:
8428 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008429 case ISN_LOADT:
8430 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008431 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008432 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008434 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008435 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008436 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008437 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008439 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008440 case ISN_STOREW:
8441 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442 vim_free(isn->isn_arg.string);
8443 break;
8444
8445 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008446 case ISN_STORES:
8447 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448 break;
8449
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008450 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008451 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008452 vim_free(isn->isn_arg.unlet.ul_name);
8453 break;
8454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008455 case ISN_STOREOPT:
8456 vim_free(isn->isn_arg.storeopt.so_name);
8457 break;
8458
8459 case ISN_PUSHBLOB: // push blob isn_arg.blob
8460 blob_unref(isn->isn_arg.blob);
8461 break;
8462
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008463 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008464#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008465 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008466#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008467 break;
8468
8469 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008470#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008471 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008472#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008473 break;
8474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008475 case ISN_UCALL:
8476 vim_free(isn->isn_arg.ufunc.cuf_name);
8477 break;
8478
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008479 case ISN_FUNCREF:
8480 {
8481 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8482 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008483 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008484
Bram Moolenaar077a4232020-12-22 18:33:27 +01008485 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8486 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008487 }
8488 break;
8489
8490 case ISN_DCALL:
8491 {
8492 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8493 + isn->isn_arg.dfunc.cdf_idx;
8494
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008495 if (dfunc->df_ufunc != NULL
8496 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008497 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008498 }
8499 break;
8500
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008501 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008502 {
8503 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8504 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8505
8506 if (ufunc != NULL)
8507 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008508 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008509 func_ptr_unref(ufunc);
8510 }
8511
8512 vim_free(lambda);
8513 vim_free(isn->isn_arg.newfunc.nf_global);
8514 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008515 break;
8516
Bram Moolenaar5e654232020-09-16 15:22:00 +02008517 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008518 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008519 free_type(isn->isn_arg.type.ct_type);
8520 break;
8521
Bram Moolenaar02194d22020-10-24 23:08:38 +02008522 case ISN_CMDMOD:
8523 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8524 ->cmod_filter_regmatch.regprog);
8525 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8526 break;
8527
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008528 case ISN_LOADSCRIPT:
8529 case ISN_STORESCRIPT:
8530 vim_free(isn->isn_arg.script.scriptref);
8531 break;
8532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533 case ISN_2BOOL:
8534 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008535 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536 case ISN_ADDBLOB:
8537 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008538 case ISN_ANYINDEX:
8539 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008540 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008541 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008543 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008545 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008546 case ISN_COMPAREANY:
8547 case ISN_COMPAREBLOB:
8548 case ISN_COMPAREBOOL:
8549 case ISN_COMPAREDICT:
8550 case ISN_COMPAREFLOAT:
8551 case ISN_COMPAREFUNC:
8552 case ISN_COMPARELIST:
8553 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008554 case ISN_COMPARESPECIAL:
8555 case ISN_COMPARESTRING:
8556 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008557 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008558 case ISN_DROP:
8559 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008560 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008561 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008562 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008563 case ISN_EXECCONCAT:
8564 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008565 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008566 case ISN_GETITEM:
8567 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008568 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008569 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008570 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008571 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008572 case ISN_LOADBDICT:
8573 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008574 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008576 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008577 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008578 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008579 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008580 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008581 case ISN_NEGATENR:
8582 case ISN_NEWDICT:
8583 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008584 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008585 case ISN_OPFLOAT:
8586 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008588 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008589 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008590 case ISN_PUSHF:
8591 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008592 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008593 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008594 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008595 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008596 case ISN_SHUFFLE:
8597 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008598 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008599 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008600 case ISN_STORENR:
8601 case ISN_STOREOUTER:
8602 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008603 case ISN_STOREV:
8604 case ISN_STRINDEX:
8605 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008606 case ISN_THROW:
8607 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008608 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008609 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008610 // nothing allocated
8611 break;
8612 }
8613}
8614
8615/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008616 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008617 */
8618 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008619delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008620{
8621 int idx;
8622
8623 ga_clear(&dfunc->df_def_args_isn);
8624
8625 if (dfunc->df_instr != NULL)
8626 {
8627 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8628 delete_instr(dfunc->df_instr + idx);
8629 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008630 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008631 }
8632
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008633 if (mark_deleted)
8634 dfunc->df_deleted = TRUE;
8635 if (dfunc->df_ufunc != NULL)
8636 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008637}
8638
8639/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008640 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008641 * function, unless another user function still uses it.
8642 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643 */
8644 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008645unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008646{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008647 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648 {
8649 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8650 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008652 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008653 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008654 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008655 ufunc->uf_dfunc_idx = 0;
8656 if (dfunc->df_ufunc == ufunc)
8657 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008658 }
8659}
8660
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008661/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008662 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008663 */
8664 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008665link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008666{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008667 if (ufunc->uf_dfunc_idx > 0)
8668 {
8669 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8670 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008671
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008672 ++dfunc->df_refcount;
8673 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008674}
8675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008676#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008677/*
8678 * Free all functions defined with ":def".
8679 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008680 void
8681free_def_functions(void)
8682{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008683 int idx;
8684
8685 for (idx = 0; idx < def_functions.ga_len; ++idx)
8686 {
8687 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8688
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008689 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008690 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008691 }
8692
8693 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008694}
8695#endif
8696
8697
8698#endif // FEAT_EVAL