blob: 2fad4ac34b736dad19dc3631d672d9f429453d37 [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 Moolenaar9b634462021-01-21 22:53:38 +0100829 // Use the negated offset so that it's always positive. Some systems don't
830 // support negative numbers for "char".
831 isn->isn_arg.type.ct_off = (char)-offset;
Bram Moolenaare32e5162021-01-21 20:21:29 +0100832 isn->isn_arg.type.ct_arg_idx = argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833
Bram Moolenaar5e654232020-09-16 15:22:00 +0200834 // type becomes expected
835 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836
837 return OK;
838}
839
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100840 static int
841generate_SETTYPE(
842 cctx_T *cctx,
843 type_T *expected)
844{
845 isn_T *isn;
846
847 RETURN_OK_IF_SKIP(cctx);
848 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
849 return FAIL;
850 isn->isn_arg.type.ct_type = alloc_type(expected);
851 return OK;
852}
853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200855 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
856 * used. Return FALSE if the types will never match.
857 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100858 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200859use_typecheck(type_T *actual, type_T *expected)
860{
861 if (actual->tt_type == VAR_ANY
862 || actual->tt_type == VAR_UNKNOWN
863 || (actual->tt_type == VAR_FUNC
864 && (expected->tt_type == VAR_FUNC
865 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100866 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
867 && ((actual->tt_member == &t_void)
868 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200869 return TRUE;
870 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
871 && actual->tt_type == expected->tt_type)
872 // This takes care of a nested list or dict.
873 return use_typecheck(actual->tt_member, expected->tt_member);
874 return FALSE;
875}
876
877/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200879 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880 * - "actual" is a type that can be "expected" type: add a runtime check; or
881 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200882 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
883 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200884 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100885 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200886need_type(
887 type_T *actual,
888 type_T *expected,
889 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100890 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200891 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200892 int silent,
893 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200894{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200895 if (expected == &t_bool && actual != &t_bool
896 && (actual->tt_flags & TTFLAG_BOOL_OK))
897 {
898 // Using "0", "1" or the result of an expression with "&&" or "||" as a
899 // boolean is OK but requires a conversion.
900 generate_2BOOL(cctx, FALSE);
901 return OK;
902 }
903
Bram Moolenaar351ead02021-01-16 16:07:01 +0100904 if (check_type(expected, actual, FALSE, arg_idx) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200905 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200906
907 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200908 // If it's a constant a runtime check makes no sense.
909 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200910 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100911 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200912 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200913 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200914
915 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100916 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200917 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200918}
919
920/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100921 * Check that the top of the type stack has a type that can be used as a
922 * condition. Give an error and return FAIL if not.
923 */
924 static int
925bool_on_stack(cctx_T *cctx)
926{
927 garray_T *stack = &cctx->ctx_type_stack;
928 type_T *type;
929
930 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
931 if (type == &t_bool)
932 return OK;
933
934 if (type == &t_any || type == &t_number)
935 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
936 // This requires a runtime type check.
937 return generate_COND2BOOL(cctx);
938
Bram Moolenaar351ead02021-01-16 16:07:01 +0100939 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100940}
941
942/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 * Generate an ISN_PUSHNR instruction.
944 */
945 static int
946generate_PUSHNR(cctx_T *cctx, varnumber_T number)
947{
948 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200949 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950
Bram Moolenaar080457c2020-03-03 21:53:32 +0100951 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
953 return FAIL;
954 isn->isn_arg.number = number;
955
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200956 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200957 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100958 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 return OK;
960}
961
962/*
963 * Generate an ISN_PUSHBOOL instruction.
964 */
965 static int
966generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
967{
968 isn_T *isn;
969
Bram Moolenaar080457c2020-03-03 21:53:32 +0100970 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
972 return FAIL;
973 isn->isn_arg.number = number;
974
975 return OK;
976}
977
978/*
979 * Generate an ISN_PUSHSPEC instruction.
980 */
981 static int
982generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
983{
984 isn_T *isn;
985
Bram Moolenaar080457c2020-03-03 21:53:32 +0100986 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
988 return FAIL;
989 isn->isn_arg.number = number;
990
991 return OK;
992}
993
994#ifdef FEAT_FLOAT
995/*
996 * Generate an ISN_PUSHF instruction.
997 */
998 static int
999generate_PUSHF(cctx_T *cctx, float_T fnumber)
1000{
1001 isn_T *isn;
1002
Bram Moolenaar080457c2020-03-03 21:53:32 +01001003 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1005 return FAIL;
1006 isn->isn_arg.fnumber = fnumber;
1007
1008 return OK;
1009}
1010#endif
1011
1012/*
1013 * Generate an ISN_PUSHS instruction.
1014 * Consumes "str".
1015 */
1016 static int
1017generate_PUSHS(cctx_T *cctx, char_u *str)
1018{
1019 isn_T *isn;
1020
Bram Moolenaar508b5612021-01-02 18:17:26 +01001021 if (cctx->ctx_skip == SKIP_YES)
1022 {
1023 vim_free(str);
1024 return OK;
1025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1027 return FAIL;
1028 isn->isn_arg.string = str;
1029
1030 return OK;
1031}
1032
1033/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001034 * Generate an ISN_PUSHCHANNEL instruction.
1035 * Consumes "channel".
1036 */
1037 static int
1038generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1039{
1040 isn_T *isn;
1041
Bram Moolenaar080457c2020-03-03 21:53:32 +01001042 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001043 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1044 return FAIL;
1045 isn->isn_arg.channel = channel;
1046
1047 return OK;
1048}
1049
1050/*
1051 * Generate an ISN_PUSHJOB instruction.
1052 * Consumes "job".
1053 */
1054 static int
1055generate_PUSHJOB(cctx_T *cctx, job_T *job)
1056{
1057 isn_T *isn;
1058
Bram Moolenaar080457c2020-03-03 21:53:32 +01001059 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001060 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001061 return FAIL;
1062 isn->isn_arg.job = job;
1063
1064 return OK;
1065}
1066
1067/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 * Generate an ISN_PUSHBLOB instruction.
1069 * Consumes "blob".
1070 */
1071 static int
1072generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1073{
1074 isn_T *isn;
1075
Bram Moolenaar080457c2020-03-03 21:53:32 +01001076 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1078 return FAIL;
1079 isn->isn_arg.blob = blob;
1080
1081 return OK;
1082}
1083
1084/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001085 * Generate an ISN_PUSHFUNC instruction with name "name".
1086 * Consumes "name".
1087 */
1088 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001089generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001090{
1091 isn_T *isn;
1092
Bram Moolenaar080457c2020-03-03 21:53:32 +01001093 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001094 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001095 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001096 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001097
1098 return OK;
1099}
1100
1101/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001102 * Generate an ISN_GETITEM instruction with "index".
1103 */
1104 static int
1105generate_GETITEM(cctx_T *cctx, int index)
1106{
1107 isn_T *isn;
1108 garray_T *stack = &cctx->ctx_type_stack;
1109 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1110 type_T *item_type = &t_any;
1111
1112 RETURN_OK_IF_SKIP(cctx);
1113
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001114 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001115 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001116 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001117 emsg(_(e_listreq));
1118 return FAIL;
1119 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001120 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001121 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1122 return FAIL;
1123 isn->isn_arg.number = index;
1124
1125 // add the item type to the type stack
1126 if (ga_grow(stack, 1) == FAIL)
1127 return FAIL;
1128 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1129 ++stack->ga_len;
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001134 * Generate an ISN_SLICE instruction with "count".
1135 */
1136 static int
1137generate_SLICE(cctx_T *cctx, int count)
1138{
1139 isn_T *isn;
1140
1141 RETURN_OK_IF_SKIP(cctx);
1142 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1143 return FAIL;
1144 isn->isn_arg.number = count;
1145 return OK;
1146}
1147
1148/*
1149 * Generate an ISN_CHECKLEN instruction with "min_len".
1150 */
1151 static int
1152generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1153{
1154 isn_T *isn;
1155
1156 RETURN_OK_IF_SKIP(cctx);
1157
1158 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1159 return FAIL;
1160 isn->isn_arg.checklen.cl_min_len = min_len;
1161 isn->isn_arg.checklen.cl_more_OK = more_OK;
1162
1163 return OK;
1164}
1165
1166/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 * Generate an ISN_STORE instruction.
1168 */
1169 static int
1170generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1171{
1172 isn_T *isn;
1173
Bram Moolenaar080457c2020-03-03 21:53:32 +01001174 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1176 return FAIL;
1177 if (name != NULL)
1178 isn->isn_arg.string = vim_strsave(name);
1179 else
1180 isn->isn_arg.number = idx;
1181
1182 return OK;
1183}
1184
1185/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001186 * Generate an ISN_STOREOUTER instruction.
1187 */
1188 static int
1189generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1190{
1191 isn_T *isn;
1192
1193 RETURN_OK_IF_SKIP(cctx);
1194 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1195 return FAIL;
1196 isn->isn_arg.outer.outer_idx = idx;
1197 isn->isn_arg.outer.outer_depth = level;
1198
1199 return OK;
1200}
1201
1202/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1204 */
1205 static int
1206generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1207{
1208 isn_T *isn;
1209
Bram Moolenaar080457c2020-03-03 21:53:32 +01001210 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1212 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001213 isn->isn_arg.storenr.stnr_idx = idx;
1214 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215
1216 return OK;
1217}
1218
1219/*
1220 * Generate an ISN_STOREOPT instruction
1221 */
1222 static int
1223generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1224{
1225 isn_T *isn;
1226
Bram Moolenaar080457c2020-03-03 21:53:32 +01001227 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001228 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 return FAIL;
1230 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1231 isn->isn_arg.storeopt.so_flags = opt_flags;
1232
1233 return OK;
1234}
1235
1236/*
1237 * Generate an ISN_LOAD or similar instruction.
1238 */
1239 static int
1240generate_LOAD(
1241 cctx_T *cctx,
1242 isntype_T isn_type,
1243 int idx,
1244 char_u *name,
1245 type_T *type)
1246{
1247 isn_T *isn;
1248
Bram Moolenaar080457c2020-03-03 21:53:32 +01001249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1251 return FAIL;
1252 if (name != NULL)
1253 isn->isn_arg.string = vim_strsave(name);
1254 else
1255 isn->isn_arg.number = idx;
1256
1257 return OK;
1258}
1259
1260/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001261 * Generate an ISN_LOADOUTER instruction
1262 */
1263 static int
1264generate_LOADOUTER(
1265 cctx_T *cctx,
1266 int idx,
1267 int nesting,
1268 type_T *type)
1269{
1270 isn_T *isn;
1271
1272 RETURN_OK_IF_SKIP(cctx);
1273 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1274 return FAIL;
1275 isn->isn_arg.outer.outer_idx = idx;
1276 isn->isn_arg.outer.outer_depth = nesting;
1277
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001282 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001283 */
1284 static int
1285generate_LOADV(
1286 cctx_T *cctx,
1287 char_u *name,
1288 int error)
1289{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001290 int di_flags;
1291 int vidx = find_vim_var(name, &di_flags);
1292 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 if (vidx < 0)
1296 {
1297 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001298 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001299 return FAIL;
1300 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001301 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001303 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001304}
1305
1306/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001307 * Generate an ISN_UNLET instruction.
1308 */
1309 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001310generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001311{
1312 isn_T *isn;
1313
1314 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001315 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001316 return FAIL;
1317 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1318 isn->isn_arg.unlet.ul_forceit = forceit;
1319
1320 return OK;
1321}
1322
1323/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001324 * Generate an ISN_LOCKCONST instruction.
1325 */
1326 static int
1327generate_LOCKCONST(cctx_T *cctx)
1328{
1329 isn_T *isn;
1330
1331 RETURN_OK_IF_SKIP(cctx);
1332 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1333 return FAIL;
1334 return OK;
1335}
1336
1337/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 * Generate an ISN_LOADS instruction.
1339 */
1340 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001341generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001343 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 int sid,
1346 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347{
1348 isn_T *isn;
1349
Bram Moolenaar080457c2020-03-03 21:53:32 +01001350 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 if (isn_type == ISN_LOADS)
1352 isn = generate_instr_type(cctx, isn_type, type);
1353 else
1354 isn = generate_instr_drop(cctx, isn_type, 1);
1355 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1358 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359
1360 return OK;
1361}
1362
1363/*
1364 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1365 */
1366 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001367generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 cctx_T *cctx,
1369 isntype_T isn_type,
1370 int sid,
1371 int idx,
1372 type_T *type)
1373{
1374 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001375 scriptref_T *sref;
1376 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
Bram Moolenaar080457c2020-03-03 21:53:32 +01001378 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if (isn_type == ISN_LOADSCRIPT)
1380 isn = generate_instr_type(cctx, isn_type, type);
1381 else
1382 isn = generate_instr_drop(cctx, isn_type, 1);
1383 if (isn == NULL)
1384 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001385
1386 // This requires three arguments, which doesn't fit in an instruction, thus
1387 // we need to allocate a struct for this.
1388 sref = ALLOC_ONE(scriptref_T);
1389 if (sref == NULL)
1390 return FAIL;
1391 isn->isn_arg.script.scriptref = sref;
1392 sref->sref_sid = sid;
1393 sref->sref_idx = idx;
1394 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001395 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 return OK;
1397}
1398
1399/*
1400 * Generate an ISN_NEWLIST instruction.
1401 */
1402 static int
1403generate_NEWLIST(cctx_T *cctx, int count)
1404{
1405 isn_T *isn;
1406 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 type_T *type;
1408 type_T *member;
1409
Bram Moolenaar080457c2020-03-03 21:53:32 +01001410 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1412 return FAIL;
1413 isn->isn_arg.number = count;
1414
Bram Moolenaar127542b2020-08-09 17:22:04 +02001415 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001416 if (count == 0)
1417 member = &t_void;
1418 else
1419 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001420 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1421 cctx->ctx_type_list);
1422 type = get_list_type(member, cctx->ctx_type_list);
1423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 // drop the value types
1425 stack->ga_len -= count;
1426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 // add the list type to the type stack
1428 if (ga_grow(stack, 1) == FAIL)
1429 return FAIL;
1430 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1431 ++stack->ga_len;
1432
1433 return OK;
1434}
1435
1436/*
1437 * Generate an ISN_NEWDICT instruction.
1438 */
1439 static int
1440generate_NEWDICT(cctx_T *cctx, int count)
1441{
1442 isn_T *isn;
1443 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 type_T *type;
1445 type_T *member;
1446
Bram Moolenaar080457c2020-03-03 21:53:32 +01001447 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1449 return FAIL;
1450 isn->isn_arg.number = count;
1451
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001452 if (count == 0)
1453 member = &t_void;
1454 else
1455 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001456 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1457 cctx->ctx_type_list);
1458 type = get_dict_type(member, cctx->ctx_type_list);
1459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 // drop the key and value types
1461 stack->ga_len -= 2 * count;
1462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 // add the dict type to the type stack
1464 if (ga_grow(stack, 1) == FAIL)
1465 return FAIL;
1466 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1467 ++stack->ga_len;
1468
1469 return OK;
1470}
1471
1472/*
1473 * Generate an ISN_FUNCREF instruction.
1474 */
1475 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001476generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477{
1478 isn_T *isn;
1479 garray_T *stack = &cctx->ctx_type_stack;
1480
Bram Moolenaar080457c2020-03-03 21:53:32 +01001481 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1483 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001484 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001485 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486
Bram Moolenaarab360522021-01-10 14:02:28 +01001487 // if the referenced function is a closure, it may use items further up in
1488 // the nested context, including this one.
1489 if (ufunc->uf_flags & FC_CLOSURE)
1490 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 if (ga_grow(stack, 1) == FAIL)
1493 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001494 ((type_T **)stack->ga_data)[stack->ga_len] =
1495 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 ++stack->ga_len;
1497
1498 return OK;
1499}
1500
1501/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001502 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001503 * "lambda_name" and "func_name" must be in allocated memory and will be
1504 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001505 */
1506 static int
1507generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1508{
1509 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001510
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001511 if (cctx->ctx_skip == SKIP_YES)
1512 {
1513 vim_free(lambda_name);
1514 vim_free(func_name);
1515 return OK;
1516 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001517 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001518 {
1519 vim_free(lambda_name);
1520 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001521 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001522 }
1523 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001524 isn->isn_arg.newfunc.nf_global = func_name;
1525
1526 return OK;
1527}
1528
1529/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001530 * Generate an ISN_DEF instruction: list functions
1531 */
1532 static int
1533generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1534{
1535 isn_T *isn;
1536
1537 RETURN_OK_IF_SKIP(cctx);
1538 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1539 return FAIL;
1540 if (len > 0)
1541 {
1542 isn->isn_arg.string = vim_strnsave(name, len);
1543 if (isn->isn_arg.string == NULL)
1544 return FAIL;
1545 }
1546 return OK;
1547}
1548
1549/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 * Generate an ISN_JUMP instruction.
1551 */
1552 static int
1553generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1554{
1555 isn_T *isn;
1556 garray_T *stack = &cctx->ctx_type_stack;
1557
Bram Moolenaar080457c2020-03-03 21:53:32 +01001558 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1560 return FAIL;
1561 isn->isn_arg.jump.jump_when = when;
1562 isn->isn_arg.jump.jump_where = where;
1563
1564 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1565 --stack->ga_len;
1566
1567 return OK;
1568}
1569
1570 static int
1571generate_FOR(cctx_T *cctx, int loop_idx)
1572{
1573 isn_T *isn;
1574 garray_T *stack = &cctx->ctx_type_stack;
1575
Bram Moolenaar080457c2020-03-03 21:53:32 +01001576 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1578 return FAIL;
1579 isn->isn_arg.forloop.for_idx = loop_idx;
1580
1581 if (ga_grow(stack, 1) == FAIL)
1582 return FAIL;
1583 // type doesn't matter, will be stored next
1584 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1585 ++stack->ga_len;
1586
1587 return OK;
1588}
1589
1590/*
1591 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001592 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 * Return FAIL if the number of arguments is wrong.
1594 */
1595 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001596generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597{
1598 isn_T *isn;
1599 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001600 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001601 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001602 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603
Bram Moolenaar080457c2020-03-03 21:53:32 +01001604 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001605 argoff = check_internal_func(func_idx, argcount);
1606 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 return FAIL;
1608
Bram Moolenaar389df252020-07-09 21:20:47 +02001609 if (method_call && argoff > 1)
1610 {
1611 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1612 return FAIL;
1613 isn->isn_arg.shuffle.shfl_item = argcount;
1614 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1615 }
1616
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001617 if (argcount > 0)
1618 {
1619 // Check the types of the arguments.
1620 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001621 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1622 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001623 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001624 if (internal_func_is_map(func_idx))
1625 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001626 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1629 return FAIL;
1630 isn->isn_arg.bfunc.cbf_idx = func_idx;
1631 isn->isn_arg.bfunc.cbf_argcount = argcount;
1632
Bram Moolenaar94738d82020-10-21 14:25:07 +02001633 // Drop the argument types and push the return type.
1634 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if (ga_grow(stack, 1) == FAIL)
1636 return FAIL;
1637 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001638 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001639 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001641 if (maptype != NULL && maptype->tt_member != NULL
1642 && maptype->tt_member != &t_any)
1643 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001644 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 return OK;
1647}
1648
1649/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001650 * Generate an ISN_LISTAPPEND instruction. Works like add().
1651 * Argument count is already checked.
1652 */
1653 static int
1654generate_LISTAPPEND(cctx_T *cctx)
1655{
1656 garray_T *stack = &cctx->ctx_type_stack;
1657 type_T *list_type;
1658 type_T *item_type;
1659 type_T *expected;
1660
1661 // Caller already checked that list_type is a list.
1662 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1663 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1664 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001665 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001666 return FAIL;
1667
1668 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1669 return FAIL;
1670
1671 --stack->ga_len; // drop the argument
1672 return OK;
1673}
1674
1675/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001676 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1677 * Argument count is already checked.
1678 */
1679 static int
1680generate_BLOBAPPEND(cctx_T *cctx)
1681{
1682 garray_T *stack = &cctx->ctx_type_stack;
1683 type_T *item_type;
1684
1685 // Caller already checked that blob_type is a blob.
1686 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001687 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001688 return FAIL;
1689
1690 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1691 return FAIL;
1692
1693 --stack->ga_len; // drop the argument
1694 return OK;
1695}
1696
1697/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 * Generate an ISN_DCALL or ISN_UCALL instruction.
1699 * Return FAIL if the number of arguments is wrong.
1700 */
1701 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001702generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703{
1704 isn_T *isn;
1705 garray_T *stack = &cctx->ctx_type_stack;
1706 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001707 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708
Bram Moolenaar080457c2020-03-03 21:53:32 +01001709 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 if (argcount > regular_args && !has_varargs(ufunc))
1711 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001712 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 return FAIL;
1714 }
1715 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1716 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001717 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 return FAIL;
1719 }
1720
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001721 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001722 {
1723 int i;
1724
1725 for (i = 0; i < argcount; ++i)
1726 {
1727 type_T *expected;
1728 type_T *actual;
1729
1730 if (i < regular_args)
1731 {
1732 if (ufunc->uf_arg_types == NULL)
1733 continue;
1734 expected = ufunc->uf_arg_types[i];
1735 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001736 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1737 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001738 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001739 else
1740 expected = ufunc->uf_va_type->tt_member;
1741 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001742 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001743 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001744 {
1745 arg_type_mismatch(expected, actual, i + 1);
1746 return FAIL;
1747 }
1748 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001749 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001750 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1751 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001752 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001753 }
1754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001756 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001757 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001759 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 {
1761 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1762 isn->isn_arg.dfunc.cdf_argcount = argcount;
1763 }
1764 else
1765 {
1766 // A user function may be deleted and redefined later, can't use the
1767 // ufunc pointer, need to look it up again at runtime.
1768 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1769 isn->isn_arg.ufunc.cuf_argcount = argcount;
1770 }
1771
1772 stack->ga_len -= argcount; // drop the arguments
1773 if (ga_grow(stack, 1) == FAIL)
1774 return FAIL;
1775 // add return value
1776 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1777 ++stack->ga_len;
1778
1779 return OK;
1780}
1781
1782/*
1783 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1784 */
1785 static int
1786generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1787{
1788 isn_T *isn;
1789 garray_T *stack = &cctx->ctx_type_stack;
1790
Bram Moolenaar080457c2020-03-03 21:53:32 +01001791 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1793 return FAIL;
1794 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1795 isn->isn_arg.ufunc.cuf_argcount = argcount;
1796
1797 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001798 if (ga_grow(stack, 1) == FAIL)
1799 return FAIL;
1800 // add return value
1801 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1802 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803
1804 return OK;
1805}
1806
1807/*
1808 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001809 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 */
1811 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001812generate_PCALL(
1813 cctx_T *cctx,
1814 int argcount,
1815 char_u *name,
1816 type_T *type,
1817 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818{
1819 isn_T *isn;
1820 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001821 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
Bram Moolenaar080457c2020-03-03 21:53:32 +01001823 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001824
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001825 if (type->tt_type == VAR_ANY)
1826 ret_type = &t_any;
1827 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001828 {
1829 if (type->tt_argcount != -1)
1830 {
1831 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1832
1833 if (argcount < type->tt_min_argcount - varargs)
1834 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001835 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001836 return FAIL;
1837 }
1838 if (!varargs && argcount > type->tt_argcount)
1839 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001840 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001841 return FAIL;
1842 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001843 if (type->tt_args != NULL)
1844 {
1845 int i;
1846
1847 for (i = 0; i < argcount; ++i)
1848 {
1849 int offset = -argcount + i - 1;
1850 type_T *actual = ((type_T **)stack->ga_data)[
1851 stack->ga_len + offset];
1852 type_T *expected;
1853
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001854 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001855 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001856 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001857 else
1858 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001859 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001860 cctx, TRUE, FALSE) == FAIL)
1861 {
1862 arg_type_mismatch(expected, actual, i + 1);
1863 return FAIL;
1864 }
1865 }
1866 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001867 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001868 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001869 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001870 else
1871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001872 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001873 return FAIL;
1874 }
1875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1877 return FAIL;
1878 isn->isn_arg.pfunc.cpf_top = at_top;
1879 isn->isn_arg.pfunc.cpf_argcount = argcount;
1880
1881 stack->ga_len -= argcount; // drop the arguments
1882
1883 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001884 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001886 // If partial is above the arguments it must be cleared and replaced with
1887 // the return value.
1888 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1889 return FAIL;
1890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 return OK;
1892}
1893
1894/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001895 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 */
1897 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001898generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899{
1900 isn_T *isn;
1901 garray_T *stack = &cctx->ctx_type_stack;
1902 type_T *type;
1903
Bram Moolenaar080457c2020-03-03 21:53:32 +01001904 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001905 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001907 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001909 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001911 if (type->tt_type != VAR_DICT && type != &t_any)
1912 {
1913 emsg(_(e_dictreq));
1914 return FAIL;
1915 }
1916 // change dict type to dict member type
1917 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001918 {
1919 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1920 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922
1923 return OK;
1924}
1925
1926/*
1927 * Generate an ISN_ECHO instruction.
1928 */
1929 static int
1930generate_ECHO(cctx_T *cctx, int with_white, int count)
1931{
1932 isn_T *isn;
1933
Bram Moolenaar080457c2020-03-03 21:53:32 +01001934 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1936 return FAIL;
1937 isn->isn_arg.echo.echo_with_white = with_white;
1938 isn->isn_arg.echo.echo_count = count;
1939
1940 return OK;
1941}
1942
Bram Moolenaarad39c092020-02-26 18:23:43 +01001943/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001944 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001945 */
1946 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001947generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001948{
1949 isn_T *isn;
1950
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001951 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001952 return FAIL;
1953 isn->isn_arg.number = count;
1954
1955 return OK;
1956}
1957
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001958/*
1959 * Generate an ISN_PUT instruction.
1960 */
1961 static int
1962generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1963{
1964 isn_T *isn;
1965
1966 RETURN_OK_IF_SKIP(cctx);
1967 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1968 return FAIL;
1969 isn->isn_arg.put.put_regname = regname;
1970 isn->isn_arg.put.put_lnum = lnum;
1971 return OK;
1972}
1973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 static int
1975generate_EXEC(cctx_T *cctx, char_u *line)
1976{
1977 isn_T *isn;
1978
Bram Moolenaar080457c2020-03-03 21:53:32 +01001979 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1981 return FAIL;
1982 isn->isn_arg.string = vim_strsave(line);
1983 return OK;
1984}
1985
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001986 static int
1987generate_EXECCONCAT(cctx_T *cctx, int count)
1988{
1989 isn_T *isn;
1990
1991 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1992 return FAIL;
1993 isn->isn_arg.number = count;
1994 return OK;
1995}
1996
Bram Moolenaar08597872020-12-10 19:43:40 +01001997/*
1998 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1999 */
2000 static int
2001generate_RANGE(cctx_T *cctx, char_u *range)
2002{
2003 isn_T *isn;
2004 garray_T *stack = &cctx->ctx_type_stack;
2005
2006 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2007 return FAIL;
2008 isn->isn_arg.string = range;
2009
2010 if (ga_grow(stack, 1) == FAIL)
2011 return FAIL;
2012 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2013 ++stack->ga_len;
2014 return OK;
2015}
2016
Bram Moolenaar792f7862020-11-23 08:31:18 +01002017 static int
2018generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2019{
2020 isn_T *isn;
2021
2022 RETURN_OK_IF_SKIP(cctx);
2023 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2024 return FAIL;
2025 isn->isn_arg.unpack.unp_count = var_count;
2026 isn->isn_arg.unpack.unp_semicolon = semicolon;
2027 return OK;
2028}
2029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002031 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002032 */
2033 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002034generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002035{
2036 isn_T *isn;
2037
Bram Moolenaar02194d22020-10-24 23:08:38 +02002038 if (cmod->cmod_flags != 0
2039 || cmod->cmod_split != 0
2040 || cmod->cmod_verbose != 0
2041 || cmod->cmod_tab != 0
2042 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002043 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002044 cctx->ctx_has_cmdmod = TRUE;
2045
2046 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002047 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002048 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2049 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2050 return FAIL;
2051 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002052 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002053 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002054 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002055
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002056 return OK;
2057}
2058
2059 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002060generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002061{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002062 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2063 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002064 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002065 return OK;
2066}
2067
2068/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002070 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002072 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002073reserve_local(
2074 cctx_T *cctx,
2075 char_u *name,
2076 size_t len,
2077 int isConst,
2078 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 lvar_T *lvar;
2081
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002082 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002084 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002085 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 }
2087
2088 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002089 return NULL;
2090 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002091 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002093 // Every local variable uses the next entry on the stack. We could re-use
2094 // the last ones when leaving a scope, but then variables used in a closure
2095 // might get overwritten. To keep things simple do not re-use stack
2096 // entries. This is less efficient, but memory is cheap these days.
2097 lvar->lv_idx = cctx->ctx_locals_count++;
2098
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002099 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 lvar->lv_const = isConst;
2101 lvar->lv_type = type;
2102
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002103 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104}
2105
2106/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002107 * Remove local variables above "new_top".
2108 */
2109 static void
2110unwind_locals(cctx_T *cctx, int new_top)
2111{
2112 if (cctx->ctx_locals.ga_len > new_top)
2113 {
2114 int idx;
2115 lvar_T *lvar;
2116
2117 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2118 {
2119 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2120 vim_free(lvar->lv_name);
2121 }
2122 }
2123 cctx->ctx_locals.ga_len = new_top;
2124}
2125
2126/*
2127 * Free all local variables.
2128 */
2129 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002130free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002131{
2132 unwind_locals(cctx, 0);
2133 ga_clear(&cctx->ctx_locals);
2134}
2135
2136/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002137 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2138 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2139 * error if the variable was defined with :const.
2140 */
2141 static int
2142check_item_writable(svar_T *sv, int check_writable, char_u *name)
2143{
2144 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2145 || (check_writable == ASSIGN_FINAL
2146 && sv->sv_const == ASSIGN_CONST))
2147 {
2148 semsg(_(e_readonlyvar), name);
2149 return FAIL;
2150 }
2151 return OK;
2152}
2153
2154/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002156 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 * Returns the index in "sn_var_vals" if found.
2158 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002159 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 */
2161 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002162get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163{
2164 hashtab_T *ht;
2165 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002166 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002167 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 int idx;
2169
Bram Moolenaare3d46852020-08-29 13:39:17 +02002170 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002172 if (sid == current_sctx.sc_sid)
2173 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002174 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002175
2176 if (sav == NULL)
2177 return -2;
2178 idx = sav->sav_var_vals_idx;
2179 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002180 if (check_item_writable(sv, check_writable, name) == FAIL)
2181 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002182 return idx;
2183 }
2184
2185 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 ht = &SCRIPT_VARS(sid);
2187 di = find_var_in_ht(ht, 0, name, TRUE);
2188 if (di == NULL)
2189 return -2;
2190
2191 // Now find the svar_T index in sn_var_vals.
2192 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2193 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002194 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 if (sv->sv_tv == &di->di_tv)
2196 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002197 if (check_item_writable(sv, check_writable, name) == FAIL)
2198 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 return idx;
2200 }
2201 }
2202 return -1;
2203}
2204
2205/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002206 * Find "name" in imported items of the current script or in "cctx" if not
2207 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 */
2209 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002210find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 int idx;
2213
Bram Moolenaare3d46852020-08-29 13:39:17 +02002214 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002215 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 if (cctx != NULL)
2217 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2218 {
2219 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2220 + idx;
2221
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002222 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2223 : STRLEN(import->imp_name) == len
2224 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 return import;
2226 }
2227
Bram Moolenaarefa94442020-08-08 22:16:00 +02002228 return find_imported_in_script(name, len, current_sctx.sc_sid);
2229}
2230
2231 imported_T *
2232find_imported_in_script(char_u *name, size_t len, int sid)
2233{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002234 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002235 int idx;
2236
Bram Moolenaare3d46852020-08-29 13:39:17 +02002237 if (!SCRIPT_ID_VALID(sid))
2238 return NULL;
2239 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2241 {
2242 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2243
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002244 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2245 : STRLEN(import->imp_name) == len
2246 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 return import;
2248 }
2249 return NULL;
2250}
2251
2252/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002253 * Free all imported variables.
2254 */
2255 static void
2256free_imported(cctx_T *cctx)
2257{
2258 int idx;
2259
2260 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2261 {
2262 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2263
2264 vim_free(import->imp_name);
2265 }
2266 ga_clear(&cctx->ctx_imports);
2267}
2268
2269/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002270 * Return a pointer to the next line that isn't empty or only contains a
2271 * comment. Skips over white space.
2272 * Returns NULL if there is none.
2273 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002274 char_u *
2275peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002276{
2277 int lnum = cctx->ctx_lnum;
2278
2279 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2280 {
2281 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002282 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002283
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002284 // ignore NULLs inserted for continuation lines
2285 if (line != NULL)
2286 {
2287 p = skipwhite(line);
2288 if (*p != NUL && !vim9_comment_start(p))
2289 return p;
2290 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002291 }
2292 return NULL;
2293}
2294
2295/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002296 * Called when checking for a following operator at "arg". When the rest of
2297 * the line is empty or only a comment, peek the next line. If there is a next
2298 * line return a pointer to it and set "nextp".
2299 * Otherwise skip over white space.
2300 */
2301 static char_u *
2302may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2303{
2304 char_u *p = skipwhite(arg);
2305
2306 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002307 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002308 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002309 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002310 if (*nextp != NULL)
2311 return *nextp;
2312 }
2313 return p;
2314}
2315
2316/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002317 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002318 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002319 * Returns NULL when at the end.
2320 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002321 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002322next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002323{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002324 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002325
2326 do
2327 {
2328 ++cctx->ctx_lnum;
2329 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002330 {
2331 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002332 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002333 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002334 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002335 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002336 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002337 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002338 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002339 return line;
2340}
2341
2342/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002343 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002344 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002345 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002346 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2347 */
2348 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002349may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002350{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002351 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002352 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002353 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002354 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002355
2356 if (next == NULL)
2357 return FAIL;
2358 *arg = skipwhite(next);
2359 }
2360 return OK;
2361}
2362
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002363/*
2364 * Idem, and give an error when failed.
2365 */
2366 static int
2367may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2368{
2369 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2370 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002371 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002372 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002373 return FAIL;
2374 }
2375 return OK;
2376}
2377
2378
Bram Moolenaara5565e42020-05-09 15:44:01 +02002379// Structure passed between the compile_expr* functions to keep track of
2380// constants that have been parsed but for which no code was produced yet. If
2381// possible expressions on these constants are applied at compile time. If
2382// that is not possible, the code to push the constants needs to be generated
2383// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002384// Using 50 should be more than enough of 5 levels of ().
2385#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002386typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002387 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002388 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002389 int pp_is_const; // all generated code was constants, used for a
2390 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002391} ppconst_T;
2392
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002393static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002394static int compile_expr0(char_u **arg, cctx_T *cctx);
2395static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2396
Bram Moolenaara5565e42020-05-09 15:44:01 +02002397/*
2398 * Generate a PUSH instruction for "tv".
2399 * "tv" will be consumed or cleared.
2400 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2401 */
2402 static int
2403generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2404{
2405 if (tv != NULL)
2406 {
2407 switch (tv->v_type)
2408 {
2409 case VAR_UNKNOWN:
2410 break;
2411 case VAR_BOOL:
2412 generate_PUSHBOOL(cctx, tv->vval.v_number);
2413 break;
2414 case VAR_SPECIAL:
2415 generate_PUSHSPEC(cctx, tv->vval.v_number);
2416 break;
2417 case VAR_NUMBER:
2418 generate_PUSHNR(cctx, tv->vval.v_number);
2419 break;
2420#ifdef FEAT_FLOAT
2421 case VAR_FLOAT:
2422 generate_PUSHF(cctx, tv->vval.v_float);
2423 break;
2424#endif
2425 case VAR_BLOB:
2426 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2427 tv->vval.v_blob = NULL;
2428 break;
2429 case VAR_STRING:
2430 generate_PUSHS(cctx, tv->vval.v_string);
2431 tv->vval.v_string = NULL;
2432 break;
2433 default:
2434 iemsg("constant type not supported");
2435 clear_tv(tv);
2436 return FAIL;
2437 }
2438 tv->v_type = VAR_UNKNOWN;
2439 }
2440 return OK;
2441}
2442
2443/*
2444 * Generate code for any ppconst entries.
2445 */
2446 static int
2447generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2448{
2449 int i;
2450 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002451 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002452
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002453 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002454 for (i = 0; i < ppconst->pp_used; ++i)
2455 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2456 ret = FAIL;
2457 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002458 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002459 return ret;
2460}
2461
2462/*
2463 * Clear ppconst constants. Used when failing.
2464 */
2465 static void
2466clear_ppconst(ppconst_T *ppconst)
2467{
2468 int i;
2469
2470 for (i = 0; i < ppconst->pp_used; ++i)
2471 clear_tv(&ppconst->pp_tv[i]);
2472 ppconst->pp_used = 0;
2473}
2474
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002475/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002476 * Generate an instruction to load script-local variable "name", without the
2477 * leading "s:".
2478 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 */
2480 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002481compile_load_scriptvar(
2482 cctx_T *cctx,
2483 char_u *name, // variable NUL terminated
2484 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002485 char_u **end, // end of variable
2486 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002488 scriptitem_T *si;
2489 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 imported_T *import;
2491
Bram Moolenaare3d46852020-08-29 13:39:17 +02002492 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2493 return FAIL;
2494 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002495 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002496 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002498 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002499 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2500 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 }
2502 if (idx >= 0)
2503 {
2504 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2505
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002506 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 current_sctx.sc_sid, idx, sv->sv_type);
2508 return OK;
2509 }
2510
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002511 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 if (import != NULL)
2513 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002514 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002515 {
2516 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002517 char_u *exp_name;
2518 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002519 ufunc_T *ufunc;
2520 type_T *type;
2521
2522 // Used "import * as Name", need to lookup the member.
2523 if (*p != '.')
2524 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002525 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002526 return FAIL;
2527 }
2528 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002529 if (VIM_ISWHITE(*p))
2530 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002531 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002532 return FAIL;
2533 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002534
Bram Moolenaar1c991142020-07-04 13:15:31 +02002535 // isolate one name
2536 exp_name = p;
2537 while (eval_isnamec(*p))
2538 ++p;
2539 cc = *p;
2540 *p = NUL;
2541
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002542 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002543 *p = cc;
2544 p = skipwhite(p);
2545
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002546 // TODO: what if it is a function?
2547 if (idx < 0)
2548 return FAIL;
2549 *end = p;
2550
2551 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2552 import->imp_sid,
2553 idx,
2554 type);
2555 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002556 else if (import->imp_funcname != NULL)
2557 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002558 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002559 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2560 import->imp_sid,
2561 import->imp_var_vals_idx,
2562 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 return OK;
2564 }
2565
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002566 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002567 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 return FAIL;
2569}
2570
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002571 static int
2572generate_funcref(cctx_T *cctx, char_u *name)
2573{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002574 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002575
2576 if (ufunc == NULL)
2577 return FAIL;
2578
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002579 // Need to compile any default values to get the argument types.
2580 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2581 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2582 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002583 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002584}
2585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586/*
2587 * Compile a variable name into a load instruction.
2588 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002589 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 * When "error" is FALSE do not give an error when not found.
2591 */
2592 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002593compile_load(
2594 char_u **arg,
2595 char_u *end_arg,
2596 cctx_T *cctx,
2597 int is_expr,
2598 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599{
2600 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002601 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002602 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002604 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605
2606 if (*(*arg + 1) == ':')
2607 {
2608 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002609 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002610 {
2611 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002613 switch (**arg)
2614 {
2615 case 'g': isn_type = ISN_LOADGDICT; break;
2616 case 'w': isn_type = ISN_LOADWDICT; break;
2617 case 't': isn_type = ISN_LOADTDICT; break;
2618 case 'b': isn_type = ISN_LOADBDICT; break;
2619 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002620 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002621 goto theend;
2622 }
2623 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2624 goto theend;
2625 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 else
2628 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002629 isntype_T isn_type = ISN_DROP;
2630
2631 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2632 if (name == NULL)
2633 return FAIL;
2634
2635 switch (**arg)
2636 {
2637 case 'v': res = generate_LOADV(cctx, name, error);
2638 break;
2639 case 's': res = compile_load_scriptvar(cctx, name,
2640 NULL, NULL, error);
2641 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002642 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2643 isn_type = ISN_LOADG;
2644 else
2645 {
2646 isn_type = ISN_LOADAUTO;
2647 vim_free(name);
2648 name = vim_strnsave(*arg, end - *arg);
2649 if (name == NULL)
2650 return FAIL;
2651 }
2652 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002653 case 'w': isn_type = ISN_LOADW; break;
2654 case 't': isn_type = ISN_LOADT; break;
2655 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002656 default: // cannot happen, just in case
2657 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002658 goto theend;
2659 }
2660 if (isn_type != ISN_DROP)
2661 {
2662 // Global, Buffer-local, Window-local and Tabpage-local
2663 // variables can be defined later, thus we don't check if it
2664 // exists, give error at runtime.
2665 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 }
2668 }
2669 else
2670 {
2671 size_t len = end - *arg;
2672 int idx;
2673 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002674 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675
2676 name = vim_strnsave(*arg, end - *arg);
2677 if (name == NULL)
2678 return FAIL;
2679
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002680 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002682 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002683 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 }
2685 else
2686 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002687 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002688
Bram Moolenaar709664c2020-12-12 14:33:41 +01002689 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002691 type = lvar.lv_type;
2692 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002693 if (lvar.lv_from_outer != 0)
2694 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002695 else
2696 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 }
2698 else
2699 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002700 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002701 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002702 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002703 || find_imported(name, 0, cctx) != NULL)
2704 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002705
Bram Moolenaar0f769812020-09-12 18:32:34 +02002706 // When evaluating an expression and the name starts with an
2707 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002708 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002709 if (res == FAIL && is_expr
2710 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002711 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 }
2713 }
2714 if (gen_load)
2715 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002716 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002717 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002718 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002719 cctx->ctx_outer_used = TRUE;
2720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 }
2722
2723 *arg = end;
2724
2725theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002726 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002727 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 vim_free(name);
2729 return res;
2730}
2731
2732/*
2733 * Compile the argument expressions.
2734 * "arg" points to just after the "(" and is advanced to after the ")"
2735 */
2736 static int
2737compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2738{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002739 char_u *p = *arg;
2740 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002741 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742
Bram Moolenaare6085c52020-04-12 20:19:16 +02002743 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002745 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2746 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002747 if (*p == ')')
2748 {
2749 *arg = p + 1;
2750 return OK;
2751 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002752 if (must_end)
2753 {
2754 semsg(_(e_missing_comma_before_argument_str), p);
2755 return FAIL;
2756 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002757
Bram Moolenaara5565e42020-05-09 15:44:01 +02002758 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 return FAIL;
2760 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002761
2762 if (*p != ',' && *skipwhite(p) == ',')
2763 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002764 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002765 p = skipwhite(p);
2766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002768 {
2769 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002770 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002771 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002772 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002773 else
2774 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002775 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002776 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002778failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002779 emsg(_(e_missing_close));
2780 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781}
2782
2783/*
2784 * Compile a function call: name(arg1, arg2)
2785 * "arg" points to "name", "arg + varlen" to the "(".
2786 * "argcount_init" is 1 for "value->method()"
2787 * Instructions:
2788 * EVAL arg1
2789 * EVAL arg2
2790 * BCALL / DCALL / UCALL
2791 */
2792 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002793compile_call(
2794 char_u **arg,
2795 size_t varlen,
2796 cctx_T *cctx,
2797 ppconst_T *ppconst,
2798 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799{
2800 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002801 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 int argcount = argcount_init;
2803 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002804 char_u fname_buf[FLEN_FIXED + 1];
2805 char_u *tofree = NULL;
2806 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002807 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002808 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002809 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810
Bram Moolenaara5565e42020-05-09 15:44:01 +02002811 // we can evaluate "has('name')" at compile time
2812 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2813 {
2814 char_u *s = skipwhite(*arg + varlen + 1);
2815 typval_T argvars[2];
2816
2817 argvars[0].v_type = VAR_UNKNOWN;
2818 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002819 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002820 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002821 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002822 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002823 if (*s == ')' && argvars[0].v_type == VAR_STRING
2824 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002825 {
2826 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2827
2828 *arg = s + 1;
2829 argvars[1].v_type = VAR_UNKNOWN;
2830 tv->v_type = VAR_NUMBER;
2831 tv->vval.v_number = 0;
2832 f_has(argvars, tv);
2833 clear_tv(&argvars[0]);
2834 ++ppconst->pp_used;
2835 return OK;
2836 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002837 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002838 }
2839
2840 if (generate_ppconst(cctx, ppconst) == FAIL)
2841 return FAIL;
2842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 if (varlen >= sizeof(namebuf))
2844 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002845 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 return FAIL;
2847 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002848 vim_strncpy(namebuf, *arg, varlen);
2849 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850
2851 *arg = skipwhite(*arg + varlen + 1);
2852 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002853 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854
Bram Moolenaar03290b82020-12-19 16:30:44 +01002855 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002856 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 {
2858 int idx;
2859
2860 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002861 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002863 {
2864 if (STRCMP(name, "add") == 0 && argcount == 2)
2865 {
2866 garray_T *stack = &cctx->ctx_type_stack;
2867 type_T *type = ((type_T **)stack->ga_data)[
2868 stack->ga_len - 2];
2869
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002870 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002871 if (type->tt_type == VAR_LIST)
2872 {
2873 // inline "add(list, item)" so that the type can be checked
2874 res = generate_LISTAPPEND(cctx);
2875 idx = -1;
2876 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002877 else if (type->tt_type == VAR_BLOB)
2878 {
2879 // inline "add(blob, nr)" so that the type can be checked
2880 res = generate_BLOBAPPEND(cctx);
2881 idx = -1;
2882 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002883 }
2884
2885 if (idx >= 0)
2886 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2887 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002888 else
2889 semsg(_(e_unknownfunc), namebuf);
2890 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 }
2892
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002893 // An argument or local variable can be a function reference, this
2894 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002895 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002896 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002897 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002898 // If we can find the function by name generate the right call.
2899 // Skip global functions here, a local funcref takes precedence.
2900 ufunc = find_func(name, FALSE, cctx);
2901 if (ufunc != NULL && !func_is_global(ufunc))
2902 {
2903 res = generate_CALL(cctx, ufunc, argcount);
2904 goto theend;
2905 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907
2908 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002909 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002910 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002912 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002913 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002914 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002915 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002916 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002917
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002918 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002919 goto theend;
2920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921
Bram Moolenaar0f769812020-09-12 18:32:34 +02002922 // If we can find a global function by name generate the right call.
2923 if (ufunc != NULL)
2924 {
2925 res = generate_CALL(cctx, ufunc, argcount);
2926 goto theend;
2927 }
2928
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002929 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002930 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002931 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002932 res = generate_UCALL(cctx, name, argcount);
2933 else
2934 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002935
2936theend:
2937 vim_free(tofree);
2938 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939}
2940
2941// like NAMESPACE_CHAR but with 'a' and 'l'.
2942#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2943
2944/*
2945 * Find the end of a variable or function name. Unlike find_name_end() this
2946 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002947 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 * Return a pointer to just after the name. Equal to "arg" if there is no
2949 * valid name.
2950 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002951 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002952to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953{
2954 char_u *p;
2955
2956 // Quick check for valid starting character.
2957 if (!eval_isnamec1(*arg))
2958 return arg;
2959
2960 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2961 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2962 // and can be used in slice "[n:]".
2963 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002964 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2966 break;
2967 return p;
2968}
2969
2970/*
2971 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002972 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 */
2974 char_u *
2975to_name_const_end(char_u *arg)
2976{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002977 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 typval_T rettv;
2979
2980 if (p == arg && *arg == '[')
2981 {
2982
2983 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002984 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 p = arg;
2986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 return p;
2988}
2989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990/*
2991 * parse a list: [expr, expr]
2992 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002993 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 */
2995 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002996compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997{
2998 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002999 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003001 int is_const;
3002 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003004 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003006 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003007 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003008 semsg(_(e_list_end), *arg);
3009 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003010 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003011 if (*p == ',')
3012 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003013 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02003014 return FAIL;
3015 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003016 if (*p == ']')
3017 {
3018 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003019 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003020 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003021 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003022 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003023 if (!is_const)
3024 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 ++count;
3026 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003027 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003029 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3030 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003031 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003032 return FAIL;
3033 }
3034 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003035 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 p = skipwhite(p);
3037 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003038 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003040 ppconst->pp_is_const = is_all_const;
3041 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042}
3043
3044/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003045 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003047 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 */
3049 static int
3050compile_lambda(char_u **arg, cctx_T *cctx)
3051{
Bram Moolenaare462f522020-12-27 14:43:30 +01003052 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 typval_T rettv;
3054 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003055 evalarg_T evalarg;
3056
3057 CLEAR_FIELD(evalarg);
3058 evalarg.eval_flags = EVAL_EVALUATE;
3059 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060
3061 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003062 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3063 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003064 {
3065 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003066 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003067 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003068
Bram Moolenaar65c44152020-12-24 15:14:01 +01003069 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003071 ++ufunc->uf_refcount;
3072 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073
Bram Moolenaar65c44152020-12-24 15:14:01 +01003074 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003075 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003077 clear_evalarg(&evalarg, NULL);
3078
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003079 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003080 {
3081 // The return type will now be known.
3082 set_function_type(ufunc);
3083
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003084 // The function reference count will be 1. When the ISN_FUNCREF
3085 // instruction is deleted the reference count is decremented and the
3086 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003087 return generate_FUNCREF(cctx, ufunc);
3088 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003089
3090 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 return FAIL;
3092}
3093
3094/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003095 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003097 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 */
3099 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003100compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101{
3102 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003103 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 int count = 0;
3105 dict_T *d = dict_alloc();
3106 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003107 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003108 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003109 int is_const;
3110 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111
3112 if (d == NULL)
3113 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003114 if (generate_ppconst(cctx, ppconst) == FAIL)
3115 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003116 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003118 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119
Bram Moolenaar23c55272020-06-21 16:58:13 +02003120 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003121 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003122 *arg = NULL;
3123 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003124 }
3125
3126 if (**arg == '}')
3127 break;
3128
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003129 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003131 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003133 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003134 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003135 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3138 if (isn->isn_type == ISN_PUSHS)
3139 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003140 else
3141 {
3142 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003143 [stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003144 if (need_type(keytype, &t_string, -1, 0, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003145 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003146 return FAIL;
3147 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003148 *arg = skipwhite(*arg);
3149 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003150 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003151 emsg(_(e_missing_matching_bracket_after_dict_key));
3152 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003153 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003154 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003156 else
3157 {
3158 // {"name": value},
3159 // {'name': value},
3160 // {name: value} use "name" as a literal key
3161 key = get_literal_key(arg);
3162 if (key == NULL)
3163 return FAIL;
3164 if (generate_PUSHS(cctx, key) == FAIL)
3165 return FAIL;
3166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167
3168 // Check for duplicate keys, if using string keys.
3169 if (key != NULL)
3170 {
3171 item = dict_find(d, key, -1);
3172 if (item != NULL)
3173 {
3174 semsg(_(e_duplicate_key), key);
3175 goto failret;
3176 }
3177 item = dictitem_alloc(key);
3178 if (item != NULL)
3179 {
3180 item->di_tv.v_type = VAR_UNKNOWN;
3181 item->di_tv.v_lock = 0;
3182 if (dict_add(d, item) == FAIL)
3183 dictitem_free(item);
3184 }
3185 }
3186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 if (**arg != ':')
3188 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003189 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003190 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003191 else
3192 semsg(_(e_missing_dict_colon), *arg);
3193 return FAIL;
3194 }
3195 whitep = *arg + 1;
3196 if (!IS_WHITE_OR_NUL(*whitep))
3197 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003198 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 return FAIL;
3200 }
3201
Bram Moolenaar23c55272020-06-21 16:58:13 +02003202 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003204 *arg = NULL;
3205 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003206 }
3207
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003208 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003210 if (!is_const)
3211 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 ++count;
3213
Bram Moolenaar2c330432020-04-13 14:41:35 +02003214 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003215 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003216 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003217 *arg = NULL;
3218 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 if (**arg == '}')
3221 break;
3222 if (**arg != ',')
3223 {
3224 semsg(_(e_missing_dict_comma), *arg);
3225 goto failret;
3226 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003227 if (IS_WHITE_OR_NUL(*whitep))
3228 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003229 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003230 return FAIL;
3231 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003232 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003233 if (!IS_WHITE_OR_NUL(*whitep))
3234 {
3235 semsg(_(e_white_space_required_after_str), ",");
3236 return FAIL;
3237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 *arg = skipwhite(*arg + 1);
3239 }
3240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 *arg = *arg + 1;
3242
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003243 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003244 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003245 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003246 *arg += STRLEN(*arg);
3247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003249 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 return generate_NEWDICT(cctx, count);
3251
3252failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003253 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003254 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003255 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003256 *arg = (char_u *)"";
3257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 dict_unref(d);
3259 return FAIL;
3260}
3261
3262/*
3263 * Compile "&option".
3264 */
3265 static int
3266compile_get_option(char_u **arg, cctx_T *cctx)
3267{
3268 typval_T rettv;
3269 char_u *start = *arg;
3270 int ret;
3271
3272 // parse the option and get the current value to get the type.
3273 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003274 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 if (ret == OK)
3276 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003277 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003278 char_u *name = vim_strnsave(start, *arg - start);
3279 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3280 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
3282 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3283 vim_free(name);
3284 }
3285 clear_tv(&rettv);
3286
3287 return ret;
3288}
3289
3290/*
3291 * Compile "$VAR".
3292 */
3293 static int
3294compile_get_env(char_u **arg, cctx_T *cctx)
3295{
3296 char_u *start = *arg;
3297 int len;
3298 int ret;
3299 char_u *name;
3300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 ++*arg;
3302 len = get_env_len(arg);
3303 if (len == 0)
3304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003305 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 return FAIL;
3307 }
3308
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003309 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 name = vim_strnsave(start, len + 1);
3311 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3312 vim_free(name);
3313 return ret;
3314}
3315
3316/*
3317 * Compile "@r".
3318 */
3319 static int
3320compile_get_register(char_u **arg, cctx_T *cctx)
3321{
3322 int ret;
3323
3324 ++*arg;
3325 if (**arg == NUL)
3326 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003327 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 return FAIL;
3329 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003330 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 {
3332 emsg_invreg(**arg);
3333 return FAIL;
3334 }
3335 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3336 ++*arg;
3337 return ret;
3338}
3339
3340/*
3341 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003342 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 */
3344 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003345apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003347 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348
3349 // this works from end to start
3350 while (p > start)
3351 {
3352 --p;
3353 if (*p == '-' || *p == '+')
3354 {
3355 // only '-' has an effect, for '+' we only check the type
3356#ifdef FEAT_FLOAT
3357 if (rettv->v_type == VAR_FLOAT)
3358 {
3359 if (*p == '-')
3360 rettv->vval.v_float = -rettv->vval.v_float;
3361 }
3362 else
3363#endif
3364 {
3365 varnumber_T val;
3366 int error = FALSE;
3367
3368 // tv_get_number_chk() accepts a string, but we don't want that
3369 // here
3370 if (check_not_string(rettv) == FAIL)
3371 return FAIL;
3372 val = tv_get_number_chk(rettv, &error);
3373 clear_tv(rettv);
3374 if (error)
3375 return FAIL;
3376 if (*p == '-')
3377 val = -val;
3378 rettv->v_type = VAR_NUMBER;
3379 rettv->vval.v_number = val;
3380 }
3381 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003382 else if (numeric_only)
3383 {
3384 ++p;
3385 break;
3386 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003387 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 {
3389 int v = tv2bool(rettv);
3390
3391 // '!' is permissive in the type.
3392 clear_tv(rettv);
3393 rettv->v_type = VAR_BOOL;
3394 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3395 }
3396 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003397 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 return OK;
3399}
3400
3401/*
3402 * Recognize v: variables that are constants and set "rettv".
3403 */
3404 static void
3405get_vim_constant(char_u **arg, typval_T *rettv)
3406{
3407 if (STRNCMP(*arg, "v:true", 6) == 0)
3408 {
3409 rettv->v_type = VAR_BOOL;
3410 rettv->vval.v_number = VVAL_TRUE;
3411 *arg += 6;
3412 }
3413 else if (STRNCMP(*arg, "v:false", 7) == 0)
3414 {
3415 rettv->v_type = VAR_BOOL;
3416 rettv->vval.v_number = VVAL_FALSE;
3417 *arg += 7;
3418 }
3419 else if (STRNCMP(*arg, "v:null", 6) == 0)
3420 {
3421 rettv->v_type = VAR_SPECIAL;
3422 rettv->vval.v_number = VVAL_NULL;
3423 *arg += 6;
3424 }
3425 else if (STRNCMP(*arg, "v:none", 6) == 0)
3426 {
3427 rettv->v_type = VAR_SPECIAL;
3428 rettv->vval.v_number = VVAL_NONE;
3429 *arg += 6;
3430 }
3431}
3432
Bram Moolenaar657137c2021-01-09 15:45:23 +01003433 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003434get_compare_type(char_u *p, int *len, int *type_is)
3435{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003436 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003437 int i;
3438
3439 switch (p[0])
3440 {
3441 case '=': if (p[1] == '=')
3442 type = EXPR_EQUAL;
3443 else if (p[1] == '~')
3444 type = EXPR_MATCH;
3445 break;
3446 case '!': if (p[1] == '=')
3447 type = EXPR_NEQUAL;
3448 else if (p[1] == '~')
3449 type = EXPR_NOMATCH;
3450 break;
3451 case '>': if (p[1] != '=')
3452 {
3453 type = EXPR_GREATER;
3454 *len = 1;
3455 }
3456 else
3457 type = EXPR_GEQUAL;
3458 break;
3459 case '<': if (p[1] != '=')
3460 {
3461 type = EXPR_SMALLER;
3462 *len = 1;
3463 }
3464 else
3465 type = EXPR_SEQUAL;
3466 break;
3467 case 'i': if (p[1] == 's')
3468 {
3469 // "is" and "isnot"; but not a prefix of a name
3470 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3471 *len = 5;
3472 i = p[*len];
3473 if (!isalnum(i) && i != '_')
3474 {
3475 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3476 *type_is = TRUE;
3477 }
3478 }
3479 break;
3480 }
3481 return type;
3482}
3483
3484/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003485 * Skip over an expression, ignoring most errors.
3486 */
3487 static void
3488skip_expr_cctx(char_u **arg, cctx_T *cctx)
3489{
3490 evalarg_T evalarg;
3491
3492 CLEAR_FIELD(evalarg);
3493 evalarg.eval_cctx = cctx;
3494 skip_expr(arg, &evalarg);
3495}
3496
3497/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003499 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 */
3501 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003502compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003504 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505
3506 // this works from end to start
3507 while (p > start)
3508 {
3509 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003510 while (VIM_ISWHITE(*p))
3511 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 if (*p == '-' || *p == '+')
3513 {
3514 int negate = *p == '-';
3515 isn_T *isn;
3516
3517 // TODO: check type
3518 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3519 {
3520 --p;
3521 if (*p == '-')
3522 negate = !negate;
3523 }
3524 // only '-' has an effect, for '+' we only check the type
3525 if (negate)
3526 isn = generate_instr(cctx, ISN_NEGATENR);
3527 else
3528 isn = generate_instr(cctx, ISN_CHECKNR);
3529 if (isn == NULL)
3530 return FAIL;
3531 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003532 else if (numeric_only)
3533 {
3534 ++p;
3535 break;
3536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 else
3538 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003539 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003541 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003543 if (p[-1] == '!')
3544 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 }
3547 if (generate_2BOOL(cctx, invert) == FAIL)
3548 return FAIL;
3549 }
3550 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003551 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 return OK;
3553}
3554
3555/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003556 * Compile "(expression)": recursive!
3557 * Return FAIL/OK.
3558 */
3559 static int
3560compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3561{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003562 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003563 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003564
Bram Moolenaar24156692021-01-14 20:35:49 +01003565 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3566 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003567 if (ppconst->pp_used <= PPSIZE - 10)
3568 {
3569 ret = compile_expr1(arg, cctx, ppconst);
3570 }
3571 else
3572 {
3573 // Not enough space in ppconst, flush constants.
3574 if (generate_ppconst(cctx, ppconst) == FAIL)
3575 return FAIL;
3576 ret = compile_expr0(arg, cctx);
3577 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003578 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3579 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003580 if (**arg == ')')
3581 ++*arg;
3582 else if (ret == OK)
3583 {
3584 emsg(_(e_missing_close));
3585 ret = FAIL;
3586 }
3587 return ret;
3588}
3589
3590/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003592 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 */
3594 static int
3595compile_subscript(
3596 char_u **arg,
3597 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003598 char_u *start_leader,
3599 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003600 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003602 char_u *name_start = *end_leader;
3603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 for (;;)
3605 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003606 char_u *p = skipwhite(*arg);
3607
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003608 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003609 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003610 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003611
3612 // If a following line starts with "->{" or "->X" advance to that
3613 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003614 // Also if a following line starts with ".x".
3615 if (next != NULL &&
3616 ((next[0] == '-' && next[1] == '>'
3617 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003618 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003619 {
3620 next = next_line_from_context(cctx, TRUE);
3621 if (next == NULL)
3622 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003623 *arg = next;
3624 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003625 }
3626 }
3627
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003628 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003629 // not a function call.
3630 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003632 garray_T *stack = &cctx->ctx_type_stack;
3633 type_T *type;
3634 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003636 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003637 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003638 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003641 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3642
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003643 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3645 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003646 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 return FAIL;
3648 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003649 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003651 char_u *pstart = p;
3652
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003653 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003654 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003655 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 // something->method()
3658 // Apply the '!', '-' and '+' first:
3659 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003660 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003663 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003664 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003665 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003666 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003667 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003668 int argcount = 1;
3669 char_u *expr;
3670 garray_T *stack;
3671 type_T *type;
3672
3673 // Funcref call: list->(Refs[2])(arg)
3674 // or lambda: list->((arg) => expr)(arg)
3675 // Fist compile the arguments.
3676 expr = *arg;
3677 *arg = skipwhite(*arg + 1);
3678 skip_expr_cctx(arg, cctx);
3679 *arg = skipwhite(*arg);
3680 if (**arg != ')')
3681 {
3682 semsg(_(e_missing_paren), *arg);
3683 return FAIL;
3684 }
3685 ++*arg;
3686 if (**arg != '(')
3687 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003688 if (*skipwhite(*arg) == '(')
3689 emsg(_(e_nowhitespace));
3690 else
3691 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003692 return FAIL;
3693 }
3694
3695 *arg = skipwhite(*arg + 1);
3696 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3697 return FAIL;
3698
3699 // Compile the function expression.
3700 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3701 return FAIL;
3702
3703 stack = &cctx->ctx_type_stack;
3704 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3705 if (generate_PCALL(cctx, argcount,
3706 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 return FAIL;
3708 }
3709 else
3710 {
3711 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003712 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003713 if (!eval_isnamec1(*p))
3714 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003715 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003716 return FAIL;
3717 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003718 if (ASCII_ISALPHA(*p) && p[1] == ':')
3719 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003720 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 ;
3722 if (*p != '(')
3723 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003724 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 return FAIL;
3726 }
3727 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003728 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 return FAIL;
3730 }
3731 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003732 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003734 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003735 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003736 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003737 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003738 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003739
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003740 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003741 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003742 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003743 // TODO: blob index
3744 // TODO: more arguments
3745 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003746 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003747 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003748 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003749
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003750 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003751 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003752 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003753 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003754 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003755 // missing first index is equal to zero
3756 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003757 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003758 else
3759 {
3760 if (compile_expr0(arg, cctx) == FAIL)
3761 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003762 if (**arg == ':')
3763 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003764 semsg(_(e_white_space_required_before_and_after_str_at_str),
3765 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003766 return FAIL;
3767 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003768 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003769 return FAIL;
3770 *arg = skipwhite(*arg);
3771 }
3772 if (**arg == ':')
3773 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003774 is_slice = TRUE;
3775 ++*arg;
3776 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3777 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003778 semsg(_(e_white_space_required_before_and_after_str_at_str),
3779 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003780 return FAIL;
3781 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003782 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003783 return FAIL;
3784 if (**arg == ']')
3785 // missing second index is equal to end of string
3786 generate_PUSHNR(cctx, -1);
3787 else
3788 {
3789 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003790 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003791 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003792 return FAIL;
3793 *arg = skipwhite(*arg);
3794 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796
3797 if (**arg != ']')
3798 {
3799 emsg(_(e_missbrac));
3800 return FAIL;
3801 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003802 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003804 // We can index a list and a dict. If we don't know the type
3805 // we can use the index value type.
3806 // TODO: If we don't know use an instruction to figure it out at
3807 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003808 typep = ((type_T **)stack->ga_data) + stack->ga_len
3809 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003810 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003811 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3812 // If the index is a string, the variable must be a Dict.
3813 if (*typep == &t_any && valtype == &t_string)
3814 vtype = VAR_DICT;
3815 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003816 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003817 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003818 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003819 return FAIL;
3820 if (is_slice)
3821 {
3822 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003823 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003824 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003825 return FAIL;
3826 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003827 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003828
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003829 if (vtype == VAR_DICT)
3830 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003831 if (is_slice)
3832 {
3833 emsg(_(e_cannot_slice_dictionary));
3834 return FAIL;
3835 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003836 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003837 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003838 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003839 if (*typep == &t_unknown)
3840 // empty dict was used
3841 *typep = &t_any;
3842 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003843 else
3844 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003845 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003846 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003847 return FAIL;
3848 *typep = &t_any;
3849 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003850 if (may_generate_2STRING(-1, cctx) == FAIL)
3851 return FAIL;
3852 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3853 return FAIL;
3854 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003855 else if (vtype == VAR_STRING)
3856 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003857 *typep = &t_string;
3858 if ((is_slice
3859 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3860 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003861 return FAIL;
3862 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003863 else if (vtype == VAR_BLOB)
3864 {
3865 emsg("Sorry, blob index and slice not implemented yet");
3866 return FAIL;
3867 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003868 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003869 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003870 if (is_slice)
3871 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003872 if (generate_instr_drop(cctx,
3873 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3874 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003875 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003876 }
Bram Moolenaared591872020-08-15 22:14:53 +02003877 else
3878 {
3879 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003880 {
Bram Moolenaared591872020-08-15 22:14:53 +02003881 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003882 if (*typep == &t_unknown)
3883 // empty list was used
3884 *typep = &t_any;
3885 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003886 if (generate_instr_drop(cctx,
3887 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3888 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003889 return FAIL;
3890 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003891 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003892 else
3893 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003894 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003895 return FAIL;
3896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003898 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003900 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003901 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003902 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003903 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003904
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003905 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003906 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003907 {
3908 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003909 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003910 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003911 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003912 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 while (eval_isnamec(*p))
3914 MB_PTR_ADV(p);
3915 if (p == *arg)
3916 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003917 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 return FAIL;
3919 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003920 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 return FAIL;
3922 *arg = p;
3923 }
3924 else
3925 break;
3926 }
3927
3928 // TODO - see handle_subscript():
3929 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3930 // Don't do this when "Func" is already a partial that was bound
3931 // explicitly (pt_auto is FALSE).
3932
3933 return OK;
3934}
3935
3936/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003937 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3938 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003940 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003941 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003942 *
3943 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 */
3945
3946/*
3947 * number number constant
3948 * 0zFFFFFFFF Blob constant
3949 * "string" string constant
3950 * 'string' literal string constant
3951 * &option-name option value
3952 * @r register contents
3953 * identifier variable value
3954 * function() function call
3955 * $VAR environment variable
3956 * (expression) nested expression
3957 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003958 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 *
3960 * Also handle:
3961 * ! in front logical NOT
3962 * - in front unary minus
3963 * + in front unary plus (ignored)
3964 * trailing (arg) funcref/partial call
3965 * trailing [] subscript in String or List
3966 * trailing .name entry in Dictionary
3967 * trailing ->name() method call
3968 */
3969 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003970compile_expr7(
3971 char_u **arg,
3972 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003973 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 char_u *start_leader, *end_leader;
3976 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003977 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003978 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003980 ppconst->pp_is_const = FALSE;
3981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 /*
3983 * Skip '!', '-' and '+' characters. They are handled later.
3984 */
3985 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003986 if (eval_leader(arg, TRUE) == FAIL)
3987 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 end_leader = *arg;
3989
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003990 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 switch (**arg)
3992 {
3993 /*
3994 * Number constant.
3995 */
3996 case '0': // also for blob starting with 0z
3997 case '1':
3998 case '2':
3999 case '3':
4000 case '4':
4001 case '5':
4002 case '6':
4003 case '7':
4004 case '8':
4005 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004006 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004008 // Apply "-" and "+" just before the number now, right to
4009 // left. Matters especially when "->" follows. Stops at
4010 // '!'.
4011 if (apply_leader(rettv, TRUE,
4012 start_leader, &end_leader) == FAIL)
4013 {
4014 clear_tv(rettv);
4015 return FAIL;
4016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 break;
4018
4019 /*
4020 * String constant: "string".
4021 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004022 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 return FAIL;
4024 break;
4025
4026 /*
4027 * Literal string constant: 'str''ing'.
4028 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004029 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 return FAIL;
4031 break;
4032
4033 /*
4034 * Constant Vim variable.
4035 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004036 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 ret = NOTDONE;
4038 break;
4039
4040 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004041 * "true" constant
4042 */
4043 case 't': if (STRNCMP(*arg, "true", 4) == 0
4044 && !eval_isnamec((*arg)[4]))
4045 {
4046 *arg += 4;
4047 rettv->v_type = VAR_BOOL;
4048 rettv->vval.v_number = VVAL_TRUE;
4049 }
4050 else
4051 ret = NOTDONE;
4052 break;
4053
4054 /*
4055 * "false" constant
4056 */
4057 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4058 && !eval_isnamec((*arg)[5]))
4059 {
4060 *arg += 5;
4061 rettv->v_type = VAR_BOOL;
4062 rettv->vval.v_number = VVAL_FALSE;
4063 }
4064 else
4065 ret = NOTDONE;
4066 break;
4067
4068 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004069 * "null" constant
4070 */
4071 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4072 && !eval_isnamec((*arg)[5]))
4073 {
4074 *arg += 4;
4075 rettv->v_type = VAR_SPECIAL;
4076 rettv->vval.v_number = VVAL_NULL;
4077 }
4078 else
4079 ret = NOTDONE;
4080 break;
4081
4082 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 * List: [expr, expr]
4084 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004085 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 break;
4087
4088 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 * Dictionary: {'key': val, 'key': val}
4090 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004091 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 break;
4093
4094 /*
4095 * Option value: &name
4096 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004097 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4098 return FAIL;
4099 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 break;
4101
4102 /*
4103 * Environment variable: $VAR.
4104 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004105 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4106 return FAIL;
4107 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 break;
4109
4110 /*
4111 * Register contents: @r.
4112 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004113 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4114 return FAIL;
4115 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 break;
4117 /*
4118 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004119 * lambda: (arg, arg) => expr
4120 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004122 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4123 ret = compile_lambda(arg, cctx);
4124 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004125 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 break;
4127
4128 default: ret = NOTDONE;
4129 break;
4130 }
4131 if (ret == FAIL)
4132 return FAIL;
4133
Bram Moolenaar1c747212020-05-09 18:28:34 +02004134 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004136 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004137 clear_tv(rettv);
4138 else
4139 // A constant expression can possibly be handled compile time,
4140 // return the value instead of generating code.
4141 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 }
4143 else if (ret == NOTDONE)
4144 {
4145 char_u *p;
4146 int r;
4147
4148 if (!eval_isnamec1(**arg))
4149 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004150 if (ends_excmd(*skipwhite(*arg)))
4151 semsg(_(e_empty_expression_str), *arg);
4152 else
4153 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 return FAIL;
4155 }
4156
4157 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004158 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004160 {
4161 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164 {
4165 if (generate_ppconst(cctx, ppconst) == FAIL)
4166 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004167 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 if (r == FAIL)
4170 return FAIL;
4171 }
4172
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004173 // Handle following "[]", ".member", etc.
4174 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004175 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004176 ppconst) == FAIL)
4177 return FAIL;
4178 if (ppconst->pp_used > 0)
4179 {
4180 // apply the '!', '-' and '+' before the constant
4181 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004182 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004183 return FAIL;
4184 return OK;
4185 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004186 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004188 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189}
4190
4191/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004192 * Give the "white on both sides" error, taking the operator from "p[len]".
4193 */
4194 void
4195error_white_both(char_u *op, int len)
4196{
4197 char_u buf[10];
4198
4199 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004200 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004201}
4202
4203/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004204 * <type>expr7: runtime type check / conversion
4205 */
4206 static int
4207compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4208{
4209 type_T *want_type = NULL;
4210
4211 // Recognize <type>
4212 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4213 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004214 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004215 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4216 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004217 return FAIL;
4218
4219 if (**arg != '>')
4220 {
4221 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004222 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004223 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004224 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004225 return FAIL;
4226 }
4227 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004228 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004229 return FAIL;
4230 }
4231
4232 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4233 return FAIL;
4234
4235 if (want_type != NULL)
4236 {
4237 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004238 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004239
Bram Moolenaard1103582020-08-14 22:44:25 +02004240 generate_ppconst(cctx, ppconst);
4241 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004242 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004243 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004244 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004245 return FAIL;
4246 }
4247 }
4248
4249 return OK;
4250}
4251
4252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 * * number multiplication
4254 * / number division
4255 * % number modulo
4256 */
4257 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259{
4260 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004261 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004262 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004264 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004265 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 return FAIL;
4267
4268 /*
4269 * Repeat computing, until no "*", "/" or "%" is following.
4270 */
4271 for (;;)
4272 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004273 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 if (*op != '*' && *op != '/' && *op != '%')
4275 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004276 if (next != NULL)
4277 {
4278 *arg = next_line_from_context(cctx, TRUE);
4279 op = skipwhite(*arg);
4280 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004281
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004282 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004284 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004285 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004287 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004288 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004290 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004291 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004293
4294 if (ppconst->pp_used == ppconst_used + 2
4295 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4296 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004297 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004298 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4299 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4300 varnumber_T res = 0;
4301 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004303 // both are numbers: compute the result
4304 switch (*op)
4305 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004306 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004307 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004308 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004309 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004310 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004311 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004312 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004313 break;
4314 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004315 if (failed)
4316 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004317 tv1->vval.v_number = res;
4318 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004319 }
4320 else
4321 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004322 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004323 generate_two_op(cctx, op);
4324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 }
4326
4327 return OK;
4328}
4329
4330/*
4331 * + number addition
4332 * - number subtraction
4333 * .. string concatenation
4334 */
4335 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004336compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337{
4338 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004339 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004341 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342
4343 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 return FAIL;
4346
4347 /*
4348 * Repeat computing, until no "+", "-" or ".." is following.
4349 */
4350 for (;;)
4351 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004352 op = may_peek_next_line(cctx, *arg, &next);
4353 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 break;
4355 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004356 if (next != NULL)
4357 {
4358 *arg = next_line_from_context(cctx, TRUE);
4359 op = skipwhite(*arg);
4360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004362 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004364 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 }
4367
Bram Moolenaare0de1712020-12-02 17:36:54 +01004368 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004369 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004371 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004372 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 return FAIL;
4374
Bram Moolenaara5565e42020-05-09 15:44:01 +02004375 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004376 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004377 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4378 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4379 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4380 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004382 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4383 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004384
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004385 // concat/subtract/add constant numbers
4386 if (*op == '+')
4387 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4388 else if (*op == '-')
4389 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4390 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004391 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004392 // concatenate constant strings
4393 char_u *s1 = tv1->vval.v_string;
4394 char_u *s2 = tv2->vval.v_string;
4395 size_t len1 = STRLEN(s1);
4396
4397 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4398 if (tv1->vval.v_string == NULL)
4399 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004401 return FAIL;
4402 }
4403 mch_memmove(tv1->vval.v_string, s1, len1);
4404 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004405 vim_free(s1);
4406 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004407 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 }
4410 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004411 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004413 if (*op == '.')
4414 {
4415 if (may_generate_2STRING(-2, cctx) == FAIL
4416 || may_generate_2STRING(-1, cctx) == FAIL)
4417 return FAIL;
4418 generate_instr_drop(cctx, ISN_CONCAT, 1);
4419 }
4420 else
4421 generate_two_op(cctx, op);
4422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 }
4424
4425 return OK;
4426}
4427
4428/*
4429 * expr5a == expr5b
4430 * expr5a =~ expr5b
4431 * expr5a != expr5b
4432 * expr5a !~ expr5b
4433 * expr5a > expr5b
4434 * expr5a >= expr5b
4435 * expr5a < expr5b
4436 * expr5a <= expr5b
4437 * expr5a is expr5b
4438 * expr5a isnot expr5b
4439 *
4440 * Produces instructions:
4441 * EVAL expr5a Push result of "expr5a"
4442 * EVAL expr5b Push result of "expr5b"
4443 * COMPARE one of the compare instructions
4444 */
4445 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004448 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004450 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454
4455 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 return FAIL;
4458
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004459 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004460 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
4462 /*
4463 * If there is a comparative operator, use it.
4464 */
4465 if (type != EXPR_UNKNOWN)
4466 {
4467 int ic = FALSE; // Default: do not ignore case
4468
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004469 if (next != NULL)
4470 {
4471 *arg = next_line_from_context(cctx, TRUE);
4472 p = skipwhite(*arg);
4473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 if (type_is && (p[len] == '?' || p[len] == '#'))
4475 {
4476 semsg(_(e_invexpr2), *arg);
4477 return FAIL;
4478 }
4479 // extra question mark appended: ignore case
4480 if (p[len] == '?')
4481 {
4482 ic = TRUE;
4483 ++len;
4484 }
4485 // extra '#' appended: match case (ignored)
4486 else if (p[len] == '#')
4487 ++len;
4488 // nothing appended: match case
4489
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004490 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004492 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 }
4495
4496 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004497 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498 return FAIL;
4499
Bram Moolenaara5565e42020-05-09 15:44:01 +02004500 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 return FAIL;
4502
Bram Moolenaara5565e42020-05-09 15:44:01 +02004503 if (ppconst->pp_used == ppconst_used + 2)
4504 {
4505 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4506 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4507 int ret;
4508
4509 // Both sides are a constant, compute the result now.
4510 // First check for a valid combination of types, this is more
4511 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004512 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004513 ret = FAIL;
4514 else
4515 {
4516 ret = typval_compare(tv1, tv2, type, ic);
4517 tv1->v_type = VAR_BOOL;
4518 tv1->vval.v_number = tv1->vval.v_number
4519 ? VVAL_TRUE : VVAL_FALSE;
4520 clear_tv(tv2);
4521 --ppconst->pp_used;
4522 }
4523 return ret;
4524 }
4525
4526 generate_ppconst(cctx, ppconst);
4527 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 }
4529
4530 return OK;
4531}
4532
Bram Moolenaar7f141552020-05-09 17:35:53 +02004533static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535/*
4536 * Compile || or &&.
4537 */
4538 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004539compile_and_or(
4540 char_u **arg,
4541 cctx_T *cctx,
4542 char *op,
4543 ppconst_T *ppconst,
4544 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004546 char_u *next;
4547 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 int opchar = *op;
4549
4550 if (p[0] == opchar && p[1] == opchar)
4551 {
4552 garray_T *instr = &cctx->ctx_instr;
4553 garray_T end_ga;
4554
4555 /*
4556 * Repeat until there is no following "||" or "&&"
4557 */
4558 ga_init2(&end_ga, sizeof(int), 10);
4559 while (p[0] == opchar && p[1] == opchar)
4560 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004561 if (next != NULL)
4562 {
4563 *arg = next_line_from_context(cctx, TRUE);
4564 p = skipwhite(*arg);
4565 }
4566
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004567 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4568 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004569 semsg(_(e_white_space_required_before_and_after_str_at_str),
4570 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004571 return FAIL;
4572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004574 // TODO: use ppconst if the value is a constant and check
4575 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576 generate_ppconst(cctx, ppconst);
4577
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004578 // Every part must evaluate to a bool.
4579 if (bool_on_stack(cctx) == FAIL)
4580 {
4581 ga_clear(&end_ga);
4582 return FAIL;
4583 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 if (ga_grow(&end_ga, 1) == FAIL)
4586 {
4587 ga_clear(&end_ga);
4588 return FAIL;
4589 }
4590 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4591 ++end_ga.ga_len;
4592 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004593 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
4595 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004596 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004597 {
4598 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004599 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004600 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004601
Bram Moolenaara5565e42020-05-09 15:44:01 +02004602 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4603 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 {
4605 ga_clear(&end_ga);
4606 return FAIL;
4607 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004608
4609 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004611 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004613 // Every part must evaluate to a bool.
4614 if (bool_on_stack(cctx) == FAIL)
4615 {
4616 ga_clear(&end_ga);
4617 return FAIL;
4618 }
4619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 // Fill in the end label in all jumps.
4621 while (end_ga.ga_len > 0)
4622 {
4623 isn_T *isn;
4624
4625 --end_ga.ga_len;
4626 isn = ((isn_T *)instr->ga_data)
4627 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4628 isn->isn_arg.jump.jump_where = instr->ga_len;
4629 }
4630 ga_clear(&end_ga);
4631 }
4632
4633 return OK;
4634}
4635
4636/*
4637 * expr4a && expr4a && expr4a logical AND
4638 *
4639 * Produces instructions:
4640 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004641 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004642 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004644 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 * EVAL expr4c Push result of "expr4c"
4646 * end:
4647 */
4648 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004649compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651 int ppconst_used = ppconst->pp_used;
4652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 return FAIL;
4656
4657 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659}
4660
4661/*
4662 * expr3a || expr3b || expr3c logical OR
4663 *
4664 * Produces instructions:
4665 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004666 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004667 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004669 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 * EVAL expr3c Push result of "expr3c"
4671 * end:
4672 */
4673 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004674compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004676 int ppconst_used = ppconst->pp_used;
4677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 return FAIL;
4681
4682 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004683 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684}
4685
4686/*
4687 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004689 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 * JUMP_IF_FALSE alt jump if false
4691 * EVAL expr1a
4692 * JUMP_ALWAYS end
4693 * alt: EVAL expr1b
4694 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004695 *
4696 * Toplevel expression: expr2 ?? expr1
4697 * Produces instructions:
4698 * EVAL expr2 Push result of "expr2"
4699 * JUMP_AND_KEEP_IF_TRUE end jump if true
4700 * EVAL expr1
4701 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 */
4703 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004704compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705{
4706 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004707 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004708 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709
Bram Moolenaar3988f642020-08-27 22:43:03 +02004710 // Ignore all kinds of errors when not producing code.
4711 if (cctx->ctx_skip == SKIP_YES)
4712 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004713 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004714 return OK;
4715 }
4716
Bram Moolenaar61a89812020-05-07 16:58:17 +02004717 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 return FAIL;
4720
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004721 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 if (*p == '?')
4723 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004724 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 garray_T *instr = &cctx->ctx_instr;
4726 garray_T *stack = &cctx->ctx_type_stack;
4727 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004728 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004730 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004731 int has_const_expr = FALSE;
4732 int const_value = FALSE;
4733 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004735 if (next != NULL)
4736 {
4737 *arg = next_line_from_context(cctx, TRUE);
4738 p = skipwhite(*arg);
4739 }
4740
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004741 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004742 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004743 semsg(_(e_white_space_required_before_and_after_str_at_str),
4744 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004745 return FAIL;
4746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747
Bram Moolenaara5565e42020-05-09 15:44:01 +02004748 if (ppconst->pp_used == ppconst_used + 1)
4749 {
4750 // the condition is a constant, we know whether the ? or the :
4751 // expression is to be evaluated.
4752 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004753 if (op_falsy)
4754 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4755 else
4756 {
4757 int error = FALSE;
4758
4759 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4760 &error);
4761 if (error)
4762 return FAIL;
4763 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004764 cctx->ctx_skip = save_skip == SKIP_YES ||
4765 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4766
4767 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4768 // "left ?? right" and "left" is truthy: produce "left"
4769 generate_ppconst(cctx, ppconst);
4770 else
4771 {
4772 clear_tv(&ppconst->pp_tv[ppconst_used]);
4773 --ppconst->pp_used;
4774 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 }
4776 else
4777 {
4778 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004779 if (op_falsy)
4780 end_idx = instr->ga_len;
4781 generate_JUMP(cctx, op_falsy
4782 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4783 if (op_falsy)
4784 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004785 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786
4787 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004788 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004789 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004790 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004791 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792
Bram Moolenaara5565e42020-05-09 15:44:01 +02004793 if (!has_const_expr)
4794 {
4795 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004797 if (!op_falsy)
4798 {
4799 // remember the type and drop it
4800 --stack->ga_len;
4801 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004803 end_idx = instr->ga_len;
4804 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004805
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004806 // jump here from JUMP_IF_FALSE
4807 isn = ((isn_T *)instr->ga_data) + alt_idx;
4808 isn->isn_arg.jump.jump_where = instr->ga_len;
4809 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004812 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004814 // Check for the ":".
4815 p = may_peek_next_line(cctx, *arg, &next);
4816 if (*p != ':')
4817 {
4818 emsg(_(e_missing_colon));
4819 return FAIL;
4820 }
4821 if (next != NULL)
4822 {
4823 *arg = next_line_from_context(cctx, TRUE);
4824 p = skipwhite(*arg);
4825 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004826
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004827 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4828 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004829 semsg(_(e_white_space_required_before_and_after_str_at_str),
4830 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004831 return FAIL;
4832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004834 // evaluate the third expression
4835 if (has_const_expr)
4836 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004837 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004838 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004839 return FAIL;
4840 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4841 return FAIL;
4842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 if (!has_const_expr)
4845 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004846 type_T **typep;
4847
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849
Bram Moolenaara5565e42020-05-09 15:44:01 +02004850 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004851 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4852 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004854 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004855 isn = ((isn_T *)instr->ga_data) + end_idx;
4856 isn->isn_arg.jump.jump_where = instr->ga_len;
4857 }
4858
4859 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 }
4861 return OK;
4862}
4863
4864/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004865 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004866 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4867 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004868 */
4869 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004870compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004871{
4872 ppconst_T ppconst;
4873
4874 CLEAR_FIELD(ppconst);
4875 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4876 {
4877 clear_ppconst(&ppconst);
4878 return FAIL;
4879 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004880 if (is_const != NULL)
4881 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004882 if (generate_ppconst(cctx, &ppconst) == FAIL)
4883 return FAIL;
4884 return OK;
4885}
4886
4887/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004888 * Toplevel expression.
4889 */
4890 static int
4891compile_expr0(char_u **arg, cctx_T *cctx)
4892{
4893 return compile_expr0_ext(arg, cctx, NULL);
4894}
4895
4896/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 * compile "return [expr]"
4898 */
4899 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004900compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901{
4902 char_u *p = arg;
4903 garray_T *stack = &cctx->ctx_type_stack;
4904 type_T *stack_type;
4905
4906 if (*p != NUL && *p != '|' && *p != '\n')
4907 {
4908 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004909 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 return NULL;
4911
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004912 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004913 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004914 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004915 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004916 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4917 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004918 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004919 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004920 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004921 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004922 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004923 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4924 && stack_type->tt_type != VAR_VOID
4925 && stack_type->tt_type != VAR_UNKNOWN)
4926 {
4927 emsg(_(e_returning_value_in_function_without_return_type));
4928 return NULL;
4929 }
4930 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004931 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004932 return NULL;
4933 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 }
4936 else
4937 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004938 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004939 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004940 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4941 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004943 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 return NULL;
4945 }
4946
4947 // No argument, return zero.
4948 generate_PUSHNR(cctx, 0);
4949 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01004950
4951 // Undo any command modifiers.
4952 generate_undo_cmdmods(cctx);
4953
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004954 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 return NULL;
4956
4957 // "return val | endif" is possible
4958 return skipwhite(p);
4959}
4960
4961/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004962 * Get a line from the compilation context, compatible with exarg_T getline().
4963 * Return a pointer to the line in allocated memory.
4964 * Return NULL for end-of-file or some error.
4965 */
4966 static char_u *
4967exarg_getline(
4968 int c UNUSED,
4969 void *cookie,
4970 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004971 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004972{
4973 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004974 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004975
Bram Moolenaar66250c92020-08-20 15:02:42 +02004976 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004977 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004978 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004979 return NULL;
4980 ++cctx->ctx_lnum;
4981 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4982 // Comment lines result in NULL pointers, skip them.
4983 if (p != NULL)
4984 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004985 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004986}
4987
4988/*
4989 * Compile a nested :def command.
4990 */
4991 static char_u *
4992compile_nested_function(exarg_T *eap, cctx_T *cctx)
4993{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004994 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004995 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004996 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004997 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004998 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004999 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005000
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005001 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005002 {
5003 emsg(_(e_cannot_use_bang_with_nested_def));
5004 return NULL;
5005 }
5006
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005007 if (*name_start == '/')
5008 {
5009 name_end = skip_regexp(name_start + 1, '/', TRUE);
5010 if (*name_end == '/')
5011 ++name_end;
5012 eap->nextcmd = check_nextcmd(name_end);
5013 }
5014 if (name_end == name_start || *skipwhite(name_end) != '(')
5015 {
5016 if (!ends_excmd2(name_start, name_end))
5017 {
5018 semsg(_(e_invalid_command_str), eap->cmd);
5019 return NULL;
5020 }
5021
5022 // "def" or "def Name": list functions
5023 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5024 return NULL;
5025 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5026 }
5027
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005028 // Only g:Func() can use a namespace.
5029 if (name_start[1] == ':' && !is_global)
5030 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005031 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005032 return NULL;
5033 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005034 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5035 return NULL;
5036
Bram Moolenaar04b12692020-05-04 23:24:44 +02005037 eap->arg = name_end;
5038 eap->getline = exarg_getline;
5039 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005040 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005041 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005042 lambda_name = vim_strsave(get_lambda_name());
5043 if (lambda_name == NULL)
5044 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005045 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005046
Bram Moolenaar822ba242020-05-24 23:00:18 +02005047 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005048 {
5049 r = eap->skip ? OK : FAIL;
5050 goto theend;
5051 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005052 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005053 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005054 {
5055 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005056 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005057 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005058
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005059 if (is_global)
5060 {
5061 char_u *func_name = vim_strnsave(name_start + 2,
5062 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005063
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005064 if (func_name == NULL)
5065 r = FAIL;
5066 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005067 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005068 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005069 lambda_name = NULL;
5070 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005071 }
5072 else
5073 {
5074 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005075 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005076 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005077 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005078
Bram Moolenaareef21022020-08-01 22:16:43 +02005079 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005080 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005081 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005082 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005083 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005084
5085 // copy over the block scope IDs
5086 if (block_depth > 0)
5087 {
5088 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5089 if (ufunc->uf_block_ids != NULL)
5090 {
5091 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5092 sizeof(int) * block_depth);
5093 ufunc->uf_block_depth = block_depth;
5094 }
5095 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005096 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005097 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005098 r = OK;
5099
5100theend:
5101 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005102 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005103}
5104
5105/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 * Return the length of an assignment operator, or zero if there isn't one.
5107 */
5108 int
5109assignment_len(char_u *p, int *heredoc)
5110{
5111 if (*p == '=')
5112 {
5113 if (p[1] == '<' && p[2] == '<')
5114 {
5115 *heredoc = TRUE;
5116 return 3;
5117 }
5118 return 1;
5119 }
5120 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5121 return 2;
5122 if (STRNCMP(p, "..=", 3) == 0)
5123 return 3;
5124 return 0;
5125}
5126
5127// words that cannot be used as a variable
5128static char *reserved[] = {
5129 "true",
5130 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005131 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 NULL
5133};
5134
Bram Moolenaar752fc692021-01-04 21:57:11 +01005135// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005136typedef enum {
5137 dest_local,
5138 dest_option,
5139 dest_env,
5140 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005141 dest_buffer,
5142 dest_window,
5143 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005144 dest_vimvar,
5145 dest_script,
5146 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005147 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005148} assign_dest_T;
5149
Bram Moolenaar752fc692021-01-04 21:57:11 +01005150// Used by compile_lhs() to store information about the LHS of an assignment
5151// and one argument of ":unlet" with an index.
5152typedef struct {
5153 assign_dest_T lhs_dest; // type of destination
5154
5155 char_u *lhs_name; // allocated name including
5156 // "[expr]" or ".name".
5157 size_t lhs_varlen; // length of the variable without
5158 // "[expr]" or ".name"
5159 char_u *lhs_dest_end; // end of the destination, including
5160 // "[expr]" or ".name".
5161
5162 int lhs_has_index; // has "[expr]" or ".name"
5163
5164 int lhs_new_local; // create new local variable
5165 int lhs_opt_flags; // for when destination is an option
5166 int lhs_vimvaridx; // for when destination is a v:var
5167
5168 lvar_T lhs_local_lvar; // used for existing local destination
5169 lvar_T lhs_arg_lvar; // used for argument destination
5170 lvar_T *lhs_lvar; // points to destination lvar
5171 int lhs_scriptvar_sid;
5172 int lhs_scriptvar_idx;
5173
5174 int lhs_has_type; // type was specified
5175 type_T *lhs_type;
5176 type_T *lhs_member_type;
5177} lhs_T;
5178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005180 * Generate the load instruction for "name".
5181 */
5182 static void
5183generate_loadvar(
5184 cctx_T *cctx,
5185 assign_dest_T dest,
5186 char_u *name,
5187 lvar_T *lvar,
5188 type_T *type)
5189{
5190 switch (dest)
5191 {
5192 case dest_option:
5193 // TODO: check the option exists
5194 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5195 break;
5196 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005197 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5198 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5199 else
5200 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005201 break;
5202 case dest_buffer:
5203 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5204 break;
5205 case dest_window:
5206 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5207 break;
5208 case dest_tab:
5209 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5210 break;
5211 case dest_script:
5212 compile_load_scriptvar(cctx,
5213 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5214 break;
5215 case dest_env:
5216 // Include $ in the name here
5217 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5218 break;
5219 case dest_reg:
5220 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5221 break;
5222 case dest_vimvar:
5223 generate_LOADV(cctx, name + 2, TRUE);
5224 break;
5225 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005226 if (lvar->lv_from_outer > 0)
5227 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5228 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005229 else
5230 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5231 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005232 case dest_expr:
5233 // list or dict value should already be on the stack.
5234 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005235 }
5236}
5237
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005238/*
5239 * Skip over "[expr]" or ".member".
5240 * Does not check for any errors.
5241 */
5242 static char_u *
5243skip_index(char_u *start)
5244{
5245 char_u *p = start;
5246
5247 if (*p == '[')
5248 {
5249 p = skipwhite(p + 1);
5250 (void)skip_expr(&p, NULL);
5251 p = skipwhite(p);
5252 if (*p == ']')
5253 return p + 1;
5254 return p;
5255 }
5256 // if (*p == '.')
5257 return to_name_end(p + 1, TRUE);
5258}
5259
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005260 void
5261vim9_declare_error(char_u *name)
5262{
5263 char *scope = "";
5264
5265 switch (*name)
5266 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005267 case 'g': scope = _("global"); break;
5268 case 'b': scope = _("buffer"); break;
5269 case 'w': scope = _("window"); break;
5270 case 't': scope = _("tab"); break;
5271 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005272 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005273 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005274 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005275 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005276 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005277 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005278 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005279 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005280 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005281}
5282
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005283/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005284 * For one assignment figure out the type of destination. Return it in "dest".
5285 * When not recognized "dest" is not set.
5286 * For an option "opt_flags" is set.
5287 * For a v:var "vimvaridx" is set.
5288 * "type" is set to the destination type if known, unchanted otherwise.
5289 * Return FAIL if an error message was given.
5290 */
5291 static int
5292get_var_dest(
5293 char_u *name,
5294 assign_dest_T *dest,
5295 int cmdidx,
5296 int *opt_flags,
5297 int *vimvaridx,
5298 type_T **type,
5299 cctx_T *cctx)
5300{
5301 char_u *p;
5302
5303 if (*name == '&')
5304 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005305 int cc;
5306 long numval;
5307 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005308
5309 *dest = dest_option;
5310 if (cmdidx == CMD_final || cmdidx == CMD_const)
5311 {
5312 emsg(_(e_const_option));
5313 return FAIL;
5314 }
5315 p = name;
5316 p = find_option_end(&p, opt_flags);
5317 if (p == NULL)
5318 {
5319 // cannot happen?
5320 emsg(_(e_letunexp));
5321 return FAIL;
5322 }
5323 cc = *p;
5324 *p = NUL;
5325 opt_type = get_option_value(skip_option_env_lead(name),
5326 &numval, NULL, *opt_flags);
5327 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005328 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005329 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005330 case gov_unknown:
5331 semsg(_(e_unknown_option), name);
5332 return FAIL;
5333 case gov_string:
5334 case gov_hidden_string:
5335 *type = &t_string;
5336 break;
5337 case gov_bool:
5338 case gov_hidden_bool:
5339 *type = &t_bool;
5340 break;
5341 case gov_number:
5342 case gov_hidden_number:
5343 *type = &t_number;
5344 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005345 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005346 }
5347 else if (*name == '$')
5348 {
5349 *dest = dest_env;
5350 *type = &t_string;
5351 }
5352 else if (*name == '@')
5353 {
5354 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5355 {
5356 emsg_invreg(name[1]);
5357 return FAIL;
5358 }
5359 *dest = dest_reg;
5360 *type = &t_string;
5361 }
5362 else if (STRNCMP(name, "g:", 2) == 0)
5363 {
5364 *dest = dest_global;
5365 }
5366 else if (STRNCMP(name, "b:", 2) == 0)
5367 {
5368 *dest = dest_buffer;
5369 }
5370 else if (STRNCMP(name, "w:", 2) == 0)
5371 {
5372 *dest = dest_window;
5373 }
5374 else if (STRNCMP(name, "t:", 2) == 0)
5375 {
5376 *dest = dest_tab;
5377 }
5378 else if (STRNCMP(name, "v:", 2) == 0)
5379 {
5380 typval_T *vtv;
5381 int di_flags;
5382
5383 *vimvaridx = find_vim_var(name + 2, &di_flags);
5384 if (*vimvaridx < 0)
5385 {
5386 semsg(_(e_variable_not_found_str), name);
5387 return FAIL;
5388 }
5389 // We use the current value of "sandbox" here, is that OK?
5390 if (var_check_ro(di_flags, name, FALSE))
5391 return FAIL;
5392 *dest = dest_vimvar;
5393 vtv = get_vim_var_tv(*vimvaridx);
5394 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5395 }
5396 return OK;
5397}
5398
5399/*
5400 * Generate a STORE instruction for "dest", not being "dest_local".
5401 * Return FAIL when out of memory.
5402 */
5403 static int
5404generate_store_var(
5405 cctx_T *cctx,
5406 assign_dest_T dest,
5407 int opt_flags,
5408 int vimvaridx,
5409 int scriptvar_idx,
5410 int scriptvar_sid,
5411 type_T *type,
5412 char_u *name)
5413{
5414 switch (dest)
5415 {
5416 case dest_option:
5417 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5418 opt_flags);
5419 case dest_global:
5420 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005421 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5422 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005423 case dest_buffer:
5424 // include b: with the name, easier to execute that way
5425 return generate_STORE(cctx, ISN_STOREB, 0, name);
5426 case dest_window:
5427 // include w: with the name, easier to execute that way
5428 return generate_STORE(cctx, ISN_STOREW, 0, name);
5429 case dest_tab:
5430 // include t: with the name, easier to execute that way
5431 return generate_STORE(cctx, ISN_STORET, 0, name);
5432 case dest_env:
5433 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5434 case dest_reg:
5435 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5436 case dest_vimvar:
5437 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5438 case dest_script:
5439 if (scriptvar_idx < 0)
5440 {
5441 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005442 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005443
Bram Moolenaar41d61962020-12-06 20:12:43 +01005444 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005445 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5446 scriptvar_sid, type);
5447 if (name_s != name)
5448 vim_free(name_s);
5449 return r;
5450 }
5451 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5452 scriptvar_sid, scriptvar_idx, type);
5453 case dest_local:
5454 case dest_expr:
5455 // cannot happen
5456 break;
5457 }
5458 return FAIL;
5459}
5460
Bram Moolenaar752fc692021-01-04 21:57:11 +01005461 static int
5462is_decl_command(int cmdidx)
5463{
5464 return cmdidx == CMD_let || cmdidx == CMD_var
5465 || cmdidx == CMD_final || cmdidx == CMD_const;
5466}
5467
5468/*
5469 * Figure out the LHS type and other properties for an assignment or one item
5470 * of ":unlet" with an index.
5471 * Returns OK or FAIL.
5472 */
5473 static int
5474compile_lhs(
5475 char_u *var_start,
5476 lhs_T *lhs,
5477 int cmdidx,
5478 int heredoc,
5479 int oplen,
5480 cctx_T *cctx)
5481{
5482 char_u *var_end;
5483 int is_decl = is_decl_command(cmdidx);
5484
5485 CLEAR_POINTER(lhs);
5486 lhs->lhs_dest = dest_local;
5487 lhs->lhs_vimvaridx = -1;
5488 lhs->lhs_scriptvar_idx = -1;
5489
5490 // "dest_end" is the end of the destination, including "[expr]" or
5491 // ".name".
5492 // "var_end" is the end of the variable/option/etc. name.
5493 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5494 if (*var_start == '@')
5495 var_end = var_start + 2;
5496 else
5497 {
5498 // skip over the leading "&", "&l:", "&g:" and "$"
5499 var_end = skip_option_env_lead(var_start);
5500 var_end = to_name_end(var_end, TRUE);
5501 }
5502
5503 // "a: type" is declaring variable "a" with a type, not dict "a:".
5504 if (is_decl && lhs->lhs_dest_end == var_start + 2
5505 && lhs->lhs_dest_end[-1] == ':')
5506 --lhs->lhs_dest_end;
5507 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5508 --var_end;
5509
5510 // compute the length of the destination without "[expr]" or ".name"
5511 lhs->lhs_varlen = var_end - var_start;
5512 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5513 if (lhs->lhs_name == NULL)
5514 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005515
5516 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5517 // Something follows after the variable: "var[idx]" or "var.key".
5518 lhs->lhs_has_index = TRUE;
5519
Bram Moolenaar752fc692021-01-04 21:57:11 +01005520 if (heredoc)
5521 lhs->lhs_type = &t_list_string;
5522 else
5523 lhs->lhs_type = &t_any;
5524
5525 if (cctx->ctx_skip != SKIP_YES)
5526 {
5527 int declare_error = FALSE;
5528
5529 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5530 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5531 &lhs->lhs_type, cctx) == FAIL)
5532 return FAIL;
5533 if (lhs->lhs_dest != dest_local)
5534 {
5535 // Specific kind of variable recognized.
5536 declare_error = is_decl;
5537 }
5538 else
5539 {
5540 int idx;
5541
5542 // No specific kind of variable recognized, just a name.
5543 for (idx = 0; reserved[idx] != NULL; ++idx)
5544 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5545 {
5546 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5547 return FAIL;
5548 }
5549
5550
5551 if (lookup_local(var_start, lhs->lhs_varlen,
5552 &lhs->lhs_local_lvar, cctx) == OK)
5553 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5554 else
5555 {
5556 CLEAR_FIELD(lhs->lhs_arg_lvar);
5557 if (arg_exists(var_start, lhs->lhs_varlen,
5558 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5559 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5560 {
5561 if (is_decl)
5562 {
5563 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5564 return FAIL;
5565 }
5566 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5567 }
5568 }
5569 if (lhs->lhs_lvar != NULL)
5570 {
5571 if (is_decl)
5572 {
5573 semsg(_(e_variable_already_declared), lhs->lhs_name);
5574 return FAIL;
5575 }
5576 }
5577 else
5578 {
5579 int script_namespace = lhs->lhs_varlen > 1
5580 && STRNCMP(var_start, "s:", 2) == 0;
5581 int script_var = (script_namespace
5582 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5583 FALSE, cctx)
5584 : script_var_exists(var_start, lhs->lhs_varlen,
5585 TRUE, cctx)) == OK;
5586 imported_T *import =
5587 find_imported(var_start, lhs->lhs_varlen, cctx);
5588
5589 if (script_namespace || script_var || import != NULL)
5590 {
5591 char_u *rawname = lhs->lhs_name
5592 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5593
5594 if (is_decl)
5595 {
5596 if (script_namespace)
5597 semsg(_(e_cannot_declare_script_variable_in_function),
5598 lhs->lhs_name);
5599 else
5600 semsg(_(e_variable_already_declared_in_script),
5601 lhs->lhs_name);
5602 return FAIL;
5603 }
5604 else if (cctx->ctx_ufunc->uf_script_ctx_version
5605 == SCRIPT_VERSION_VIM9
5606 && script_namespace
5607 && !script_var && import == NULL)
5608 {
5609 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5610 return FAIL;
5611 }
5612
5613 lhs->lhs_dest = dest_script;
5614
5615 // existing script-local variables should have a type
5616 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5617 if (import != NULL)
5618 lhs->lhs_scriptvar_sid = import->imp_sid;
5619 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5620 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005621 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005622 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005623 lhs->lhs_scriptvar_sid, rawname,
5624 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5625 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005626 if (lhs->lhs_scriptvar_idx >= 0)
5627 {
5628 scriptitem_T *si = SCRIPT_ITEM(
5629 lhs->lhs_scriptvar_sid);
5630 svar_T *sv =
5631 ((svar_T *)si->sn_var_vals.ga_data)
5632 + lhs->lhs_scriptvar_idx;
5633 lhs->lhs_type = sv->sv_type;
5634 }
5635 }
5636 }
5637 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5638 == FAIL)
5639 return FAIL;
5640 }
5641 }
5642
5643 if (declare_error)
5644 {
5645 vim9_declare_error(lhs->lhs_name);
5646 return FAIL;
5647 }
5648 }
5649
5650 // handle "a:name" as a name, not index "name" on "a"
5651 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5652 var_end = lhs->lhs_dest_end;
5653
5654 if (lhs->lhs_dest != dest_option)
5655 {
5656 if (is_decl && *var_end == ':')
5657 {
5658 char_u *p;
5659
5660 // parse optional type: "let var: type = expr"
5661 if (!VIM_ISWHITE(var_end[1]))
5662 {
5663 semsg(_(e_white_space_required_after_str), ":");
5664 return FAIL;
5665 }
5666 p = skipwhite(var_end + 1);
5667 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5668 if (lhs->lhs_type == NULL)
5669 return FAIL;
5670 lhs->lhs_has_type = TRUE;
5671 }
5672 else if (lhs->lhs_lvar != NULL)
5673 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5674 }
5675
5676 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5677 && lhs->lhs_type->tt_type != VAR_STRING
5678 && lhs->lhs_type->tt_type != VAR_ANY)
5679 {
5680 emsg(_(e_can_only_concatenate_to_string));
5681 return FAIL;
5682 }
5683
5684 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5685 && cctx->ctx_skip != SKIP_YES)
5686 {
5687 if (oplen > 1 && !heredoc)
5688 {
5689 // +=, /=, etc. require an existing variable
5690 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5691 return FAIL;
5692 }
5693 if (!is_decl)
5694 {
5695 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5696 return FAIL;
5697 }
5698
5699 // new local variable
5700 if ((lhs->lhs_type->tt_type == VAR_FUNC
5701 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5702 && var_wrong_func_name(lhs->lhs_name, TRUE))
5703 return FAIL;
5704 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5705 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5706 if (lhs->lhs_lvar == NULL)
5707 return FAIL;
5708 lhs->lhs_new_local = TRUE;
5709 }
5710
5711 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005712 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005713 {
5714 // Something follows after the variable: "var[idx]" or "var.key".
5715 // TODO: should we also handle "->func()" here?
5716 if (is_decl)
5717 {
5718 emsg(_(e_cannot_use_index_when_declaring_variable));
5719 return FAIL;
5720 }
5721
5722 if (var_start[lhs->lhs_varlen] == '['
5723 || var_start[lhs->lhs_varlen] == '.')
5724 {
5725 char_u *after = var_start + lhs->lhs_varlen;
5726 char_u *p;
5727
5728 // Only the last index is used below, if there are others
5729 // before it generate code for the expression. Thus for
5730 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5731 for (;;)
5732 {
5733 p = skip_index(after);
5734 if (*p != '[' && *p != '.')
5735 break;
5736 after = p;
5737 }
5738 if (after > var_start + lhs->lhs_varlen)
5739 {
5740 lhs->lhs_varlen = after - var_start;
5741 lhs->lhs_dest = dest_expr;
5742 // We don't know the type before evaluating the expression,
5743 // use "any" until then.
5744 lhs->lhs_type = &t_any;
5745 }
5746
Bram Moolenaar752fc692021-01-04 21:57:11 +01005747 if (lhs->lhs_type->tt_member == NULL)
5748 lhs->lhs_member_type = &t_any;
5749 else
5750 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5751 }
5752 else
5753 {
5754 semsg("Not supported yet: %s", var_start);
5755 return FAIL;
5756 }
5757 }
5758 return OK;
5759}
5760
5761/*
5762 * Assignment to a list or dict member, or ":unlet" for the item, using the
5763 * information in "lhs".
5764 * Returns OK or FAIL.
5765 */
5766 static int
5767compile_assign_unlet(
5768 char_u *var_start,
5769 lhs_T *lhs,
5770 int is_assign,
5771 type_T *rhs_type,
5772 cctx_T *cctx)
5773{
5774 char_u *p;
5775 int r;
5776 vartype_T dest_type;
5777 size_t varlen = lhs->lhs_varlen;
5778 garray_T *stack = &cctx->ctx_type_stack;
5779
5780 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5781 p = var_start + varlen;
5782 if (*p == '[')
5783 {
5784 p = skipwhite(p + 1);
5785 r = compile_expr0(&p, cctx);
5786 if (r == OK && *skipwhite(p) != ']')
5787 {
5788 // this should not happen
5789 emsg(_(e_missbrac));
5790 r = FAIL;
5791 }
5792 }
5793 else // if (*p == '.')
5794 {
5795 char_u *key_end = to_name_end(p + 1, TRUE);
5796 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5797
5798 r = generate_PUSHS(cctx, key);
5799 }
5800 if (r == FAIL)
5801 return FAIL;
5802
5803 if (lhs->lhs_type == &t_any)
5804 {
5805 // Index on variable of unknown type: check at runtime.
5806 dest_type = VAR_ANY;
5807 }
5808 else
5809 {
5810 dest_type = lhs->lhs_type->tt_type;
5811 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5812 return FAIL;
5813 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005814 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5815 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005816 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005817 }
5818
5819 // Load the dict or list. On the stack we then have:
5820 // - value (for assignment, not for :unlet)
5821 // - index
5822 // - variable
5823 if (lhs->lhs_dest == dest_expr)
5824 {
5825 int c = var_start[varlen];
5826
5827 // Evaluate "ll[expr]" of "ll[expr][idx]"
5828 p = var_start;
5829 var_start[varlen] = NUL;
5830 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5831 {
5832 // this should not happen
5833 emsg(_(e_missbrac));
5834 return FAIL;
5835 }
5836 var_start[varlen] = c;
5837
5838 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5839 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5840 // now we can properly check the type
5841 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005842 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005843 FALSE, FALSE) == FAIL)
5844 return FAIL;
5845 }
5846 else
5847 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5848 lhs->lhs_lvar, lhs->lhs_type);
5849
5850 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5851 {
5852 if (is_assign)
5853 {
5854 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5855
5856 if (isn == NULL)
5857 return FAIL;
5858 isn->isn_arg.vartype = dest_type;
5859 }
5860 else
5861 {
5862 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5863 return FAIL;
5864 }
5865 }
5866 else
5867 {
5868 emsg(_(e_indexable_type_required));
5869 return FAIL;
5870 }
5871
5872 return OK;
5873}
5874
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005875/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005876 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005877 * "let name"
5878 * "var name = expr"
5879 * "final name = expr"
5880 * "const name = expr"
5881 * "name = expr"
5882 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005883 * Return NULL for an error.
5884 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885 */
5886 static char_u *
5887compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5888{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005889 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005891 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 char_u *ret = NULL;
5893 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005894 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005897 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 int oplen = 0;
5900 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005901 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005903 int is_decl = is_decl_command(cmdidx);
5904 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005906 // Skip over the "var" or "[var, var]" to get to any "=".
5907 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5908 if (p == NULL)
5909 return *arg == '[' ? arg : NULL;
5910
5911 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005913 // TODO: should we allow this, and figure out type inference from list
5914 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005915 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 return NULL;
5917 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005918 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 sp = p;
5921 p = skipwhite(p);
5922 op = p;
5923 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005924
5925 if (var_count > 0 && oplen == 0)
5926 // can be something like "[1, 2]->func()"
5927 return arg;
5928
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005929 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005931 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005932 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005933 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 if (heredoc)
5936 {
5937 list_T *l;
5938 listitem_T *li;
5939
5940 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005941 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005943 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005944 if (l == NULL)
5945 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005946
Bram Moolenaar078269b2020-09-21 20:35:55 +02005947 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005949 // Push each line and the create the list.
5950 FOR_ALL_LIST_ITEMS(l, li)
5951 {
5952 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5953 li->li_tv.vval.v_string = NULL;
5954 }
5955 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957 list_free(l);
5958 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005959 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005961 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005963 char_u *wp;
5964
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005965 // for "[var, var] = expr" evaluate the expression here, loop over the
5966 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005967 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005968
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005969 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005970 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005971 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005972 if (compile_expr0(&p, cctx) == FAIL)
5973 return NULL;
5974 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005976 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005978 type_T *stacktype;
5979
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005980 stacktype = stack->ga_len == 0 ? &t_void
5981 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005982 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005984 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005985 goto theend;
5986 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01005987 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005988 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005989 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005990 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005991 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5992 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005993 if (stacktype->tt_member != NULL)
5994 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005995 }
5996 }
5997
5998 /*
5999 * Loop over variables in "[var, var] = expr".
6000 * For "var = expr" and "let var: type" this is done only once.
6001 */
6002 if (var_count > 0)
6003 var_start = skipwhite(arg + 1); // skip over the "["
6004 else
6005 var_start = arg;
6006 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6007 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006008 int instr_count = -1;
6009
Bram Moolenaar752fc692021-01-04 21:57:11 +01006010 vim_free(lhs.lhs_name);
6011
6012 /*
6013 * Figure out the LHS type and other properties.
6014 */
6015 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6016 goto theend;
6017
6018 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006019 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006020 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006021 goto theend;
6022 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006023 if (!is_decl && lhs.lhs_lvar != NULL
6024 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006025 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006026 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006027 goto theend;
6028 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006029
6030 if (!heredoc)
6031 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006032 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006033 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006034 if (oplen > 0 && var_count == 0)
6035 {
6036 // skip over the "=" and the expression
6037 p = skipwhite(op + oplen);
6038 compile_expr0(&p, cctx);
6039 }
6040 }
6041 else if (oplen > 0)
6042 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006043 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006044 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006045
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006046 // For "var = expr" evaluate the expression.
6047 if (var_count == 0)
6048 {
6049 int r;
6050
6051 // for "+=", "*=", "..=" etc. first load the current value
6052 if (*op != '=')
6053 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006054 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6055 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006056
Bram Moolenaar752fc692021-01-04 21:57:11 +01006057 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006058 {
6059 // TODO: get member from list or dict
6060 emsg("Index with operation not supported yet");
6061 goto theend;
6062 }
6063 }
6064
6065 // Compile the expression. Temporarily hide the new local
6066 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006067 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006068 --cctx->ctx_locals.ga_len;
6069 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006070 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006071 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006072 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006073 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006074 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006075 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006076 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006077 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006078 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006079 ++cctx->ctx_locals.ga_len;
6080 if (r == FAIL)
6081 goto theend;
6082 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006083 else if (semicolon && var_idx == var_count - 1)
6084 {
6085 // For "[var; var] = expr" get the rest of the list
6086 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6087 goto theend;
6088 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006089 else
6090 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006091 // For "[var, var] = expr" get the "var_idx" item from the
6092 // list.
6093 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006094 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006095 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006096
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006097 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006098 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006100 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006101 if ((rhs_type->tt_type == VAR_FUNC
6102 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006103 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006104 goto theend;
6105
Bram Moolenaar752fc692021-01-04 21:57:11 +01006106 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006107 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006108 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006109 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006110 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006111 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006112 }
6113 else
6114 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006115 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006116 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006117 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006119 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006120 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006121 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006122 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006123 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006124 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006125 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006126 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006127 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006128 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006129 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006130
Bram Moolenaar71abe482020-09-14 22:28:30 +02006131 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 if (lhs.lhs_has_index)
6133 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006134 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006135 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006136 goto theend;
6137 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006138 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006139 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006140 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006141 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006143 else if (cmdidx == CMD_final)
6144 {
6145 emsg(_(e_final_requires_a_value));
6146 goto theend;
6147 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006148 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006150 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006151 goto theend;
6152 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006153 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006154 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006155 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006156 goto theend;
6157 }
6158 else
6159 {
6160 // variables are always initialized
6161 if (ga_grow(instr, 1) == FAIL)
6162 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006163 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006164 {
6165 case VAR_BOOL:
6166 generate_PUSHBOOL(cctx, VVAL_FALSE);
6167 break;
6168 case VAR_FLOAT:
6169#ifdef FEAT_FLOAT
6170 generate_PUSHF(cctx, 0.0);
6171#endif
6172 break;
6173 case VAR_STRING:
6174 generate_PUSHS(cctx, NULL);
6175 break;
6176 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006177 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006178 break;
6179 case VAR_FUNC:
6180 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6181 break;
6182 case VAR_LIST:
6183 generate_NEWLIST(cctx, 0);
6184 break;
6185 case VAR_DICT:
6186 generate_NEWDICT(cctx, 0);
6187 break;
6188 case VAR_JOB:
6189 generate_PUSHJOB(cctx, NULL);
6190 break;
6191 case VAR_CHANNEL:
6192 generate_PUSHCHANNEL(cctx, NULL);
6193 break;
6194 case VAR_NUMBER:
6195 case VAR_UNKNOWN:
6196 case VAR_ANY:
6197 case VAR_PARTIAL:
6198 case VAR_VOID:
6199 case VAR_SPECIAL: // cannot happen
6200 generate_PUSHNR(cctx, 0);
6201 break;
6202 }
6203 }
6204 if (var_count == 0)
6205 end = p;
6206 }
6207
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006208 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006209 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006210 break;
6211
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006212 if (oplen > 0 && *op != '=')
6213 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006214 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006215 type_T *stacktype;
6216
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006217 if (*op == '.')
6218 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006219 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006220 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006221 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006222 if (
6223#ifdef FEAT_FLOAT
6224 // If variable is float operation with number is OK.
6225 !(expected == &t_float && stacktype == &t_number) &&
6226#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006227 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006228 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006229 goto theend;
6230
6231 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006232 {
6233 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6234 goto theend;
6235 }
6236 else if (*op == '+')
6237 {
6238 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006239 operator_type(lhs.lhs_member_type, stacktype),
6240 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006241 goto theend;
6242 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006243 else if (generate_two_op(cctx, op) == FAIL)
6244 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246
Bram Moolenaar752fc692021-01-04 21:57:11 +01006247 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006248 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006249 // Use the info in "lhs" to store the value at the index in the
6250 // list or dict.
6251 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6252 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006253 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006254 }
6255 else
6256 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006257 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6258 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006259 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006260 generate_LOCKCONST(cctx);
6261
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006262 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006263 && (lhs.lhs_type->tt_type == VAR_DICT
6264 || lhs.lhs_type->tt_type == VAR_LIST)
6265 && lhs.lhs_type->tt_member != NULL
6266 && lhs.lhs_type->tt_member != &t_any
6267 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006268 // Set the type in the list or dict, so that it can be checked,
6269 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006271
Bram Moolenaar752fc692021-01-04 21:57:11 +01006272 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006273 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006274 if (generate_store_var(cctx, lhs.lhs_dest,
6275 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6276 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6277 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006278 goto theend;
6279 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006280 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006281 {
6282 isn_T *isn = ((isn_T *)instr->ga_data)
6283 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006284
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006285 // optimization: turn "var = 123" from ISN_PUSHNR +
6286 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006287 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006288 && instr->ga_len == instr_count + 1
6289 && isn->isn_type == ISN_PUSHNR)
6290 {
6291 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006292
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006293 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006294 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006295 isn->isn_arg.storenr.stnr_val = val;
6296 if (stack->ga_len > 0)
6297 --stack->ga_len;
6298 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006299 else if (lhs.lhs_lvar->lv_from_outer > 0)
6300 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6301 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006302 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006303 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006304 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006305 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006306
6307 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006310
6311 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006312 if (var_count > 0 && !semicolon)
6313 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006314 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006315 goto theend;
6316 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006317
Bram Moolenaarb2097502020-07-19 17:17:02 +02006318 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319
6320theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 return ret;
6323}
6324
6325/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006326 * Check for an assignment at "eap->cmd", compile it if found.
6327 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6328 */
6329 static int
6330may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6331{
6332 char_u *pskip;
6333 char_u *p;
6334
6335 // Assuming the command starts with a variable or function name,
6336 // find what follows.
6337 // Skip over "var.member", "var[idx]" and the like.
6338 // Also "&opt = val", "$ENV = val" and "@r = val".
6339 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6340 ? eap->cmd + 1 : eap->cmd;
6341 p = to_name_end(pskip, TRUE);
6342 if (p > eap->cmd && *p != NUL)
6343 {
6344 char_u *var_end;
6345 int oplen;
6346 int heredoc;
6347
6348 if (eap->cmd[0] == '@')
6349 var_end = eap->cmd + 2;
6350 else
6351 var_end = find_name_end(pskip, NULL, NULL,
6352 FNE_CHECK_START | FNE_INCL_BR);
6353 oplen = assignment_len(skipwhite(var_end), &heredoc);
6354 if (oplen > 0)
6355 {
6356 size_t len = p - eap->cmd;
6357
6358 // Recognize an assignment if we recognize the variable
6359 // name:
6360 // "g:var = expr"
6361 // "local = expr" where "local" is a local var.
6362 // "script = expr" where "script" is a script-local var.
6363 // "import = expr" where "import" is an imported var
6364 // "&opt = expr"
6365 // "$ENV = expr"
6366 // "@r = expr"
6367 if (*eap->cmd == '&'
6368 || *eap->cmd == '$'
6369 || *eap->cmd == '@'
6370 || ((len) > 2 && eap->cmd[1] == ':')
6371 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6372 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6373 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6374 || find_imported(eap->cmd, len, cctx) != NULL)
6375 {
6376 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6377 if (*line == NULL || *line == eap->cmd)
6378 return FAIL;
6379 return OK;
6380 }
6381 }
6382 }
6383
6384 if (*eap->cmd == '[')
6385 {
6386 // [var, var] = expr
6387 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6388 if (*line == NULL)
6389 return FAIL;
6390 if (*line != eap->cmd)
6391 return OK;
6392 }
6393 return NOTDONE;
6394}
6395
6396/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006397 * Check if "name" can be "unlet".
6398 */
6399 int
6400check_vim9_unlet(char_u *name)
6401{
6402 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6403 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006404 // "unlet s:var" is allowed in legacy script.
6405 if (*name == 's' && !script_is_vim9())
6406 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006407 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006408 return FAIL;
6409 }
6410 return OK;
6411}
6412
6413/*
6414 * Callback passed to ex_unletlock().
6415 */
6416 static int
6417compile_unlet(
6418 lval_T *lvp,
6419 char_u *name_end,
6420 exarg_T *eap,
6421 int deep UNUSED,
6422 void *coookie)
6423{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006424 cctx_T *cctx = coookie;
6425 char_u *p = lvp->ll_name;
6426 int cc = *name_end;
6427 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006428
Bram Moolenaar752fc692021-01-04 21:57:11 +01006429 if (cctx->ctx_skip == SKIP_YES)
6430 return OK;
6431
6432 *name_end = NUL;
6433 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006434 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006435 // :unlet $ENV_VAR
6436 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6437 }
6438 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6439 {
6440 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006441
Bram Moolenaar752fc692021-01-04 21:57:11 +01006442 // This is similar to assigning: lookup the list/dict, compile the
6443 // idx/key. Then instead of storing the value unlet the item.
6444 // unlet {list}[idx]
6445 // unlet {dict}[key] dict.key
6446 //
6447 // Figure out the LHS type and other properties.
6448 //
6449 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6450
6451 // : unlet an indexed item
6452 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006453 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006454 iemsg("called compile_lhs() without an index");
6455 ret = FAIL;
6456 }
6457 else
6458 {
6459 // Use the info in "lhs" to unlet the item at the index in the
6460 // list or dict.
6461 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006462 }
6463
Bram Moolenaar752fc692021-01-04 21:57:11 +01006464 vim_free(lhs.lhs_name);
6465 }
6466 else if (check_vim9_unlet(p) == FAIL)
6467 {
6468 ret = FAIL;
6469 }
6470 else
6471 {
6472 // Normal name. Only supports g:, w:, t: and b: namespaces.
6473 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006474 }
6475
Bram Moolenaar752fc692021-01-04 21:57:11 +01006476 *name_end = cc;
6477 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006478}
6479
6480/*
6481 * compile "unlet var", "lock var" and "unlock var"
6482 * "arg" points to "var".
6483 */
6484 static char_u *
6485compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6486{
6487 char_u *p = arg;
6488
6489 if (eap->cmdidx != CMD_unlet)
6490 {
6491 emsg("Sorry, :lock and unlock not implemented yet");
6492 return NULL;
6493 }
6494
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006495 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6496 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006497 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6498}
6499
6500/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 * Compile an :import command.
6502 */
6503 static char_u *
6504compile_import(char_u *arg, cctx_T *cctx)
6505{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006506 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507}
6508
6509/*
6510 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6511 */
6512 static int
6513compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6514{
6515 garray_T *instr = &cctx->ctx_instr;
6516 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6517
6518 if (endlabel == NULL)
6519 return FAIL;
6520 endlabel->el_next = *el;
6521 *el = endlabel;
6522 endlabel->el_end_label = instr->ga_len;
6523
6524 generate_JUMP(cctx, when, 0);
6525 return OK;
6526}
6527
6528 static void
6529compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6530{
6531 garray_T *instr = &cctx->ctx_instr;
6532
6533 while (*el != NULL)
6534 {
6535 endlabel_T *cur = (*el);
6536 isn_T *isn;
6537
6538 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6539 isn->isn_arg.jump.jump_where = instr->ga_len;
6540 *el = cur->el_next;
6541 vim_free(cur);
6542 }
6543}
6544
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006545 static void
6546compile_free_jump_to_end(endlabel_T **el)
6547{
6548 while (*el != NULL)
6549 {
6550 endlabel_T *cur = (*el);
6551
6552 *el = cur->el_next;
6553 vim_free(cur);
6554 }
6555}
6556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557/*
6558 * Create a new scope and set up the generic items.
6559 */
6560 static scope_T *
6561new_scope(cctx_T *cctx, scopetype_T type)
6562{
6563 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6564
6565 if (scope == NULL)
6566 return NULL;
6567 scope->se_outer = cctx->ctx_scope;
6568 cctx->ctx_scope = scope;
6569 scope->se_type = type;
6570 scope->se_local_count = cctx->ctx_locals.ga_len;
6571 return scope;
6572}
6573
6574/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006575 * Free the current scope and go back to the outer scope.
6576 */
6577 static void
6578drop_scope(cctx_T *cctx)
6579{
6580 scope_T *scope = cctx->ctx_scope;
6581
6582 if (scope == NULL)
6583 {
6584 iemsg("calling drop_scope() without a scope");
6585 return;
6586 }
6587 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006588 switch (scope->se_type)
6589 {
6590 case IF_SCOPE:
6591 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6592 case FOR_SCOPE:
6593 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6594 case WHILE_SCOPE:
6595 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6596 case TRY_SCOPE:
6597 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6598 case NO_SCOPE:
6599 case BLOCK_SCOPE:
6600 break;
6601 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006602 vim_free(scope);
6603}
6604
6605/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 * compile "if expr"
6607 *
6608 * "if expr" Produces instructions:
6609 * EVAL expr Push result of "expr"
6610 * JUMP_IF_FALSE end
6611 * ... body ...
6612 * end:
6613 *
6614 * "if expr | else" Produces instructions:
6615 * EVAL expr Push result of "expr"
6616 * JUMP_IF_FALSE else
6617 * ... body ...
6618 * JUMP_ALWAYS end
6619 * else:
6620 * ... body ...
6621 * end:
6622 *
6623 * "if expr1 | elseif expr2 | else" Produces instructions:
6624 * EVAL expr Push result of "expr"
6625 * JUMP_IF_FALSE elseif
6626 * ... body ...
6627 * JUMP_ALWAYS end
6628 * elseif:
6629 * EVAL expr Push result of "expr"
6630 * JUMP_IF_FALSE else
6631 * ... body ...
6632 * JUMP_ALWAYS end
6633 * else:
6634 * ... body ...
6635 * end:
6636 */
6637 static char_u *
6638compile_if(char_u *arg, cctx_T *cctx)
6639{
6640 char_u *p = arg;
6641 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006642 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006644 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006645 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646
Bram Moolenaara5565e42020-05-09 15:44:01 +02006647 CLEAR_FIELD(ppconst);
6648 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006649 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006650 clear_ppconst(&ppconst);
6651 return NULL;
6652 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006653 if (cctx->ctx_skip == SKIP_YES)
6654 clear_ppconst(&ppconst);
6655 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006656 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006657 int error = FALSE;
6658 int v;
6659
Bram Moolenaara5565e42020-05-09 15:44:01 +02006660 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006661 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006662 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006663 if (error)
6664 return NULL;
6665 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006666 }
6667 else
6668 {
6669 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006670 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006671 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006672 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006673 if (bool_on_stack(cctx) == FAIL)
6674 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676
6677 scope = new_scope(cctx, IF_SCOPE);
6678 if (scope == NULL)
6679 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006680 scope->se_skip_save = skip_save;
6681 // "is_had_return" will be reset if any block does not end in :return
6682 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006684 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006685 {
6686 // "where" is set when ":elseif", "else" or ":endif" is found
6687 scope->se_u.se_if.is_if_label = instr->ga_len;
6688 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6689 }
6690 else
6691 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692
6693 return p;
6694}
6695
6696 static char_u *
6697compile_elseif(char_u *arg, cctx_T *cctx)
6698{
6699 char_u *p = arg;
6700 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006701 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 isn_T *isn;
6703 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006704 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006705 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706
6707 if (scope == NULL || scope->se_type != IF_SCOPE)
6708 {
6709 emsg(_(e_elseif_without_if));
6710 return NULL;
6711 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006712 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006713 if (!cctx->ctx_had_return)
6714 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006716 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006717 {
6718 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006720 return NULL;
6721 // previous "if" or "elseif" jumps here
6722 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6723 isn->isn_arg.jump.jump_where = instr->ga_len;
6724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006726 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006727 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006728 if (cctx->ctx_skip == SKIP_YES)
6729 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006730 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006731 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006732 clear_ppconst(&ppconst);
6733 return NULL;
6734 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006735 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006736 if (scope->se_skip_save == SKIP_YES)
6737 clear_ppconst(&ppconst);
6738 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006739 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006740 int error = FALSE;
6741 int v;
6742
Bram Moolenaar7f141552020-05-09 17:35:53 +02006743 // The expression results in a constant.
6744 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006745 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6746 if (error)
6747 return NULL;
6748 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006749 clear_ppconst(&ppconst);
6750 scope->se_u.se_if.is_if_label = -1;
6751 }
6752 else
6753 {
6754 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006755 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006756 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006757 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006758 if (bool_on_stack(cctx) == FAIL)
6759 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006761 // "where" is set when ":elseif", "else" or ":endif" is found
6762 scope->se_u.se_if.is_if_label = instr->ga_len;
6763 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765
6766 return p;
6767}
6768
6769 static char_u *
6770compile_else(char_u *arg, cctx_T *cctx)
6771{
6772 char_u *p = arg;
6773 garray_T *instr = &cctx->ctx_instr;
6774 isn_T *isn;
6775 scope_T *scope = cctx->ctx_scope;
6776
6777 if (scope == NULL || scope->se_type != IF_SCOPE)
6778 {
6779 emsg(_(e_else_without_if));
6780 return NULL;
6781 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006782 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006783 if (!cctx->ctx_had_return)
6784 scope->se_u.se_if.is_had_return = FALSE;
6785 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786
Bram Moolenaarefd88552020-06-18 20:50:10 +02006787 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006788 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006789 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006790 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006791 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006792 if (!cctx->ctx_had_return
6793 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6794 JUMP_ALWAYS, cctx) == FAIL)
6795 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006796 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006797
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006798 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006799 {
6800 if (scope->se_u.se_if.is_if_label >= 0)
6801 {
6802 // previous "if" or "elseif" jumps here
6803 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6804 isn->isn_arg.jump.jump_where = instr->ga_len;
6805 scope->se_u.se_if.is_if_label = -1;
6806 }
6807 }
6808
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006809 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006810 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812
6813 return p;
6814}
6815
6816 static char_u *
6817compile_endif(char_u *arg, cctx_T *cctx)
6818{
6819 scope_T *scope = cctx->ctx_scope;
6820 ifscope_T *ifscope;
6821 garray_T *instr = &cctx->ctx_instr;
6822 isn_T *isn;
6823
6824 if (scope == NULL || scope->se_type != IF_SCOPE)
6825 {
6826 emsg(_(e_endif_without_if));
6827 return NULL;
6828 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006829 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006830 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006831 if (!cctx->ctx_had_return)
6832 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006834 if (scope->se_u.se_if.is_if_label >= 0)
6835 {
6836 // previous "if" or "elseif" jumps here
6837 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6838 isn->isn_arg.jump.jump_where = instr->ga_len;
6839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 // Fill in the "end" label in jumps at the end of the blocks.
6841 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006842 cctx->ctx_skip = scope->se_skip_save;
6843
6844 // If all the blocks end in :return and there is an :else then the
6845 // had_return flag is set.
6846 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006848 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 return arg;
6850}
6851
6852/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006853 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 *
6855 * Produces instructions:
6856 * PUSHNR -1
6857 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006858 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006859 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6860 * - if beyond end, jump to "end"
6861 * - otherwise get item from list and push it
6862 * STORE var Store item in "var"
6863 * ... body ...
6864 * JUMP top Jump back to repeat
6865 * end: DROP Drop the result of "expr"
6866 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006867 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6868 * UNPACK 2 Split item in 2
6869 * STORE var1 Store item in "var1"
6870 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871 */
6872 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006873compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006875 char_u *arg;
6876 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006877 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006879 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006880 int var_count = 0;
6881 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 size_t varlen;
6883 garray_T *instr = &cctx->ctx_instr;
6884 garray_T *stack = &cctx->ctx_type_stack;
6885 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006886 lvar_T *loop_lvar; // loop iteration variable
6887 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006889 type_T *item_type = &t_any;
6890 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006891
Bram Moolenaar792f7862020-11-23 08:31:18 +01006892 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01006893 if (p == NULL)
6894 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006895 if (var_count == 0)
6896 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897
6898 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006899 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006900 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6901 return NULL;
6902 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 {
6904 emsg(_(e_missing_in));
6905 return NULL;
6906 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006907 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006908 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6909 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 scope = new_scope(cctx, FOR_SCOPE);
6912 if (scope == NULL)
6913 return NULL;
6914
Bram Moolenaar792f7862020-11-23 08:31:18 +01006915 // Reserve a variable to store the loop iteration counter and initialize it
6916 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006917 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6918 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006919 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006920 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006921 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006923 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006924 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925
6926 // compile "expr", it remains on the stack until "endfor"
6927 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006928 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006929 {
6930 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006932 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006933 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006935 // Now that we know the type of "var", check that it is a list, now or at
6936 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01006938 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006940 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 return NULL;
6942 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006943
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006944 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006945 {
6946 if (var_count == 1)
6947 item_type = vartype->tt_member;
6948 else if (vartype->tt_member->tt_type == VAR_LIST
6949 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6950 item_type = vartype->tt_member->tt_member;
6951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952
6953 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006954 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006955 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956
Bram Moolenaar792f7862020-11-23 08:31:18 +01006957 arg = arg_start;
6958 if (var_count > 1)
6959 {
6960 generate_UNPACK(cctx, var_count, semicolon);
6961 arg = skipwhite(arg + 1); // skip white after '['
6962
6963 // the list item is replaced by a number of items
6964 if (ga_grow(stack, var_count - 1) == FAIL)
6965 {
6966 drop_scope(cctx);
6967 return NULL;
6968 }
6969 --stack->ga_len;
6970 for (idx = 0; idx < var_count; ++idx)
6971 {
6972 ((type_T **)stack->ga_data)[stack->ga_len] =
6973 (semicolon && idx == 0) ? vartype : item_type;
6974 ++stack->ga_len;
6975 }
6976 }
6977
6978 for (idx = 0; idx < var_count; ++idx)
6979 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006980 assign_dest_T dest = dest_local;
6981 int opt_flags = 0;
6982 int vimvaridx = -1;
6983 type_T *type = &t_any;
6984
6985 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006986 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006987 name = vim_strnsave(arg, varlen);
6988 if (name == NULL)
6989 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006990
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006991 // TODO: script var not supported?
6992 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6993 &vimvaridx, &type, cctx) == FAIL)
6994 goto failed;
6995 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006996 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006997 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6998 0, 0, type, name) == FAIL)
6999 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007000 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007001 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007002 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007003 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007004 {
7005 semsg(_(e_variable_already_declared), arg);
7006 goto failed;
7007 }
7008
Bram Moolenaarea870692020-12-02 14:24:30 +01007009 if (STRNCMP(name, "s:", 2) == 0)
7010 {
7011 semsg(_(e_cannot_declare_script_variable_in_function), name);
7012 goto failed;
7013 }
7014
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007015 // Reserve a variable to store "var".
7016 // TODO: check for type
7017 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7018 if (var_lvar == NULL)
7019 // out of memory or used as an argument
7020 goto failed;
7021
7022 if (semicolon && idx == var_count - 1)
7023 var_lvar->lv_type = vartype;
7024 else
7025 var_lvar->lv_type = item_type;
7026 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7027 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007028
Bram Moolenaar036d0712021-01-17 20:23:38 +01007029 if (*p == ':')
7030 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007031 if (*p == ',' || *p == ';')
7032 ++p;
7033 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007034 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007035 }
7036
7037 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007038
7039failed:
7040 vim_free(name);
7041 drop_scope(cctx);
7042 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043}
7044
7045/*
7046 * compile "endfor"
7047 */
7048 static char_u *
7049compile_endfor(char_u *arg, cctx_T *cctx)
7050{
7051 garray_T *instr = &cctx->ctx_instr;
7052 scope_T *scope = cctx->ctx_scope;
7053 forscope_T *forscope;
7054 isn_T *isn;
7055
7056 if (scope == NULL || scope->se_type != FOR_SCOPE)
7057 {
7058 emsg(_(e_for));
7059 return NULL;
7060 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007061 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007063 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064
7065 // At end of ":for" scope jump back to the FOR instruction.
7066 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7067
7068 // Fill in the "end" label in the FOR statement so it can jump here
7069 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7070 isn->isn_arg.forloop.for_end = instr->ga_len;
7071
7072 // Fill in the "end" label any BREAK statements
7073 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7074
7075 // Below the ":for" scope drop the "expr" list from the stack.
7076 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7077 return NULL;
7078
7079 vim_free(scope);
7080
7081 return arg;
7082}
7083
7084/*
7085 * compile "while expr"
7086 *
7087 * Produces instructions:
7088 * top: EVAL expr Push result of "expr"
7089 * JUMP_IF_FALSE end jump if false
7090 * ... body ...
7091 * JUMP top Jump back to repeat
7092 * end:
7093 *
7094 */
7095 static char_u *
7096compile_while(char_u *arg, cctx_T *cctx)
7097{
7098 char_u *p = arg;
7099 garray_T *instr = &cctx->ctx_instr;
7100 scope_T *scope;
7101
7102 scope = new_scope(cctx, WHILE_SCOPE);
7103 if (scope == NULL)
7104 return NULL;
7105
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007106 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107
7108 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007109 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 return NULL;
7111
Bram Moolenaar13106602020-10-04 16:06:05 +02007112 if (bool_on_stack(cctx) == FAIL)
7113 return FAIL;
7114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007116 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 JUMP_IF_FALSE, cctx) == FAIL)
7118 return FAIL;
7119
7120 return p;
7121}
7122
7123/*
7124 * compile "endwhile"
7125 */
7126 static char_u *
7127compile_endwhile(char_u *arg, cctx_T *cctx)
7128{
7129 scope_T *scope = cctx->ctx_scope;
7130
7131 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7132 {
7133 emsg(_(e_while));
7134 return NULL;
7135 }
7136 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007137 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138
7139 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007140 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141
7142 // Fill in the "end" label in the WHILE statement so it can jump here.
7143 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007144 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145
7146 vim_free(scope);
7147
7148 return arg;
7149}
7150
7151/*
7152 * compile "continue"
7153 */
7154 static char_u *
7155compile_continue(char_u *arg, cctx_T *cctx)
7156{
7157 scope_T *scope = cctx->ctx_scope;
7158
7159 for (;;)
7160 {
7161 if (scope == NULL)
7162 {
7163 emsg(_(e_continue));
7164 return NULL;
7165 }
7166 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7167 break;
7168 scope = scope->se_outer;
7169 }
7170
7171 // Jump back to the FOR or WHILE instruction.
7172 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007173 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7174 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 return arg;
7176}
7177
7178/*
7179 * compile "break"
7180 */
7181 static char_u *
7182compile_break(char_u *arg, cctx_T *cctx)
7183{
7184 scope_T *scope = cctx->ctx_scope;
7185 endlabel_T **el;
7186
7187 for (;;)
7188 {
7189 if (scope == NULL)
7190 {
7191 emsg(_(e_break));
7192 return NULL;
7193 }
7194 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7195 break;
7196 scope = scope->se_outer;
7197 }
7198
7199 // Jump to the end of the FOR or WHILE loop.
7200 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007201 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007203 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7205 return FAIL;
7206
7207 return arg;
7208}
7209
7210/*
7211 * compile "{" start of block
7212 */
7213 static char_u *
7214compile_block(char_u *arg, cctx_T *cctx)
7215{
7216 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7217 return NULL;
7218 return skipwhite(arg + 1);
7219}
7220
7221/*
7222 * compile end of block: drop one scope
7223 */
7224 static void
7225compile_endblock(cctx_T *cctx)
7226{
7227 scope_T *scope = cctx->ctx_scope;
7228
7229 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007230 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 vim_free(scope);
7232}
7233
7234/*
7235 * compile "try"
7236 * Creates a new scope for the try-endtry, pointing to the first catch and
7237 * finally.
7238 * Creates another scope for the "try" block itself.
7239 * TRY instruction sets up exception handling at runtime.
7240 *
7241 * "try"
7242 * TRY -> catch1, -> finally push trystack entry
7243 * ... try block
7244 * "throw {exception}"
7245 * EVAL {exception}
7246 * THROW create exception
7247 * ... try block
7248 * " catch {expr}"
7249 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007250 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 * EVAL {expr}
7252 * MATCH
7253 * JUMP nomatch -> catch2
7254 * CATCH remove exception
7255 * ... catch block
7256 * " catch"
7257 * JUMP -> finally
7258 * catch2: CATCH remove exception
7259 * ... catch block
7260 * " finally"
7261 * finally:
7262 * ... finally block
7263 * " endtry"
7264 * ENDTRY pop trystack entry, may rethrow
7265 */
7266 static char_u *
7267compile_try(char_u *arg, cctx_T *cctx)
7268{
7269 garray_T *instr = &cctx->ctx_instr;
7270 scope_T *try_scope;
7271 scope_T *scope;
7272
7273 // scope that holds the jumps that go to catch/finally/endtry
7274 try_scope = new_scope(cctx, TRY_SCOPE);
7275 if (try_scope == NULL)
7276 return NULL;
7277
Bram Moolenaar69f70502021-01-01 16:10:46 +01007278 if (cctx->ctx_skip != SKIP_YES)
7279 {
7280 // "catch" is set when the first ":catch" is found.
7281 // "finally" is set when ":finally" or ":endtry" is found
7282 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7283 if (generate_instr(cctx, ISN_TRY) == NULL)
7284 return NULL;
7285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286
7287 // scope for the try block itself
7288 scope = new_scope(cctx, BLOCK_SCOPE);
7289 if (scope == NULL)
7290 return NULL;
7291
7292 return arg;
7293}
7294
7295/*
7296 * compile "catch {expr}"
7297 */
7298 static char_u *
7299compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7300{
7301 scope_T *scope = cctx->ctx_scope;
7302 garray_T *instr = &cctx->ctx_instr;
7303 char_u *p;
7304 isn_T *isn;
7305
7306 // end block scope from :try or :catch
7307 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7308 compile_endblock(cctx);
7309 scope = cctx->ctx_scope;
7310
7311 // Error if not in a :try scope
7312 if (scope == NULL || scope->se_type != TRY_SCOPE)
7313 {
7314 emsg(_(e_catch));
7315 return NULL;
7316 }
7317
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007318 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007320 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 return NULL;
7322 }
7323
Bram Moolenaar69f70502021-01-01 16:10:46 +01007324 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007326 // Jump from end of previous block to :finally or :endtry
7327 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7328 JUMP_ALWAYS, cctx) == FAIL)
7329 return NULL;
7330
7331 // End :try or :catch scope: set value in ISN_TRY instruction
7332 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7333 if (isn->isn_arg.try.try_catch == 0)
7334 isn->isn_arg.try.try_catch = instr->ga_len;
7335 if (scope->se_u.se_try.ts_catch_label != 0)
7336 {
7337 // Previous catch without match jumps here
7338 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7339 isn->isn_arg.jump.jump_where = instr->ga_len;
7340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 }
7342
7343 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007344 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007346 scope->se_u.se_try.ts_caught_all = TRUE;
7347 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 }
7349 else
7350 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007351 char_u *end;
7352 char_u *pat;
7353 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007354 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007355 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 // Push v:exception, push {expr} and MATCH
7358 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7359
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007360 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007361 if (*end != *p)
7362 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007363 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007364 vim_free(tofree);
7365 return FAIL;
7366 }
7367 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007368 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007369 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007370 len = (int)(end - tofree);
7371 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007372 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007373 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007374 if (pat == NULL)
7375 return FAIL;
7376 if (generate_PUSHS(cctx, pat) == FAIL)
7377 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7380 return NULL;
7381
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007382 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7384 return NULL;
7385 }
7386
Bram Moolenaar69f70502021-01-01 16:10:46 +01007387 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 return NULL;
7389
7390 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7391 return NULL;
7392 return p;
7393}
7394
7395 static char_u *
7396compile_finally(char_u *arg, cctx_T *cctx)
7397{
7398 scope_T *scope = cctx->ctx_scope;
7399 garray_T *instr = &cctx->ctx_instr;
7400 isn_T *isn;
7401
7402 // end block scope from :try or :catch
7403 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7404 compile_endblock(cctx);
7405 scope = cctx->ctx_scope;
7406
7407 // Error if not in a :try scope
7408 if (scope == NULL || scope->se_type != TRY_SCOPE)
7409 {
7410 emsg(_(e_finally));
7411 return NULL;
7412 }
7413
7414 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007415 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 if (isn->isn_arg.try.try_finally != 0)
7417 {
7418 emsg(_(e_finally_dup));
7419 return NULL;
7420 }
7421
7422 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007423 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424
Bram Moolenaar585fea72020-04-02 22:33:21 +02007425 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007426 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 {
7428 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007429 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007431 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 }
7433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 // TODO: set index in ts_finally_label jumps
7435
7436 return arg;
7437}
7438
7439 static char_u *
7440compile_endtry(char_u *arg, cctx_T *cctx)
7441{
7442 scope_T *scope = cctx->ctx_scope;
7443 garray_T *instr = &cctx->ctx_instr;
7444 isn_T *isn;
7445
7446 // end block scope from :catch or :finally
7447 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7448 compile_endblock(cctx);
7449 scope = cctx->ctx_scope;
7450
7451 // Error if not in a :try scope
7452 if (scope == NULL || scope->se_type != TRY_SCOPE)
7453 {
7454 if (scope == NULL)
7455 emsg(_(e_no_endtry));
7456 else if (scope->se_type == WHILE_SCOPE)
7457 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007458 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 emsg(_(e_endfor));
7460 else
7461 emsg(_(e_endif));
7462 return NULL;
7463 }
7464
Bram Moolenaar69f70502021-01-01 16:10:46 +01007465 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007467 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7468 if (isn->isn_arg.try.try_catch == 0
7469 && isn->isn_arg.try.try_finally == 0)
7470 {
7471 emsg(_(e_missing_catch_or_finally));
7472 return NULL;
7473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474
Bram Moolenaar69f70502021-01-01 16:10:46 +01007475 // Fill in the "end" label in jumps at the end of the blocks, if not
7476 // done by ":finally".
7477 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478
Bram Moolenaar69f70502021-01-01 16:10:46 +01007479 // End :catch or :finally scope: set value in ISN_TRY instruction
7480 if (isn->isn_arg.try.try_catch == 0)
7481 isn->isn_arg.try.try_catch = instr->ga_len;
7482 if (isn->isn_arg.try.try_finally == 0)
7483 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007484
Bram Moolenaar69f70502021-01-01 16:10:46 +01007485 if (scope->se_u.se_try.ts_catch_label != 0)
7486 {
7487 // Last catch without match jumps here
7488 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7489 isn->isn_arg.jump.jump_where = instr->ga_len;
7490 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007491 }
7492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493 compile_endblock(cctx);
7494
Bram Moolenaar69f70502021-01-01 16:10:46 +01007495 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 return NULL;
7497 return arg;
7498}
7499
7500/*
7501 * compile "throw {expr}"
7502 */
7503 static char_u *
7504compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7505{
7506 char_u *p = skipwhite(arg);
7507
Bram Moolenaara5565e42020-05-09 15:44:01 +02007508 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007510 if (cctx->ctx_skip == SKIP_YES)
7511 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512 if (may_generate_2STRING(-1, cctx) == FAIL)
7513 return NULL;
7514 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7515 return NULL;
7516
7517 return p;
7518}
7519
7520/*
7521 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007522 * compile "echomsg expr"
7523 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007524 * compile "execute expr"
7525 */
7526 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007527compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007528{
7529 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007530 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007531 int count = 0;
7532
7533 for (;;)
7534 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007535 if (ends_excmd2(prev, p))
7536 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007537 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007538 return NULL;
7539 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007540 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007541 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007542 }
7543
Bram Moolenaare4984292020-12-13 14:19:25 +01007544 if (count > 0)
7545 {
7546 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7547 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7548 else if (cmdidx == CMD_execute)
7549 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7550 else if (cmdidx == CMD_echomsg)
7551 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7552 else
7553 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 return p;
7556}
7557
7558/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007559 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007560 * instruction to compute it and return OK.
7561 * Otherwise return FAIL, the caller must deal with any range.
7562 */
7563 static int
7564compile_variable_range(exarg_T *eap, cctx_T *cctx)
7565{
7566 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7567 char_u *p = skipdigits(eap->cmd);
7568
7569 if (p == range_end)
7570 return FAIL;
7571 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7572}
7573
7574/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007575 * :put r
7576 * :put ={expr}
7577 */
7578 static char_u *
7579compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7580{
7581 char_u *line = arg;
7582 linenr_T lnum;
7583 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007584 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007585
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007586 eap->regname = *line;
7587
7588 if (eap->regname == '=')
7589 {
7590 char_u *p = line + 1;
7591
7592 if (compile_expr0(&p, cctx) == FAIL)
7593 return NULL;
7594 line = p;
7595 }
7596 else if (eap->regname != NUL)
7597 ++line;
7598
Bram Moolenaar08597872020-12-10 19:43:40 +01007599 if (compile_variable_range(eap, cctx) == OK)
7600 {
7601 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7602 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007603 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007604 {
7605 // Either no range or a number.
7606 // "errormsg" will not be set because the range is ADDR_LINES.
7607 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007608 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007609 return NULL;
7610 if (eap->addr_count == 0)
7611 lnum = -1;
7612 else
7613 lnum = eap->line2;
7614 if (above)
7615 --lnum;
7616 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007617
7618 generate_PUT(cctx, eap->regname, lnum);
7619 return line;
7620}
7621
7622/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007623 * A command that is not compiled, execute with legacy code.
7624 */
7625 static char_u *
7626compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7627{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007628 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007629 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007630 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007631
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007632 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007633 goto theend;
7634
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007635 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007636 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007637 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007638 int usefilter = FALSE;
7639
7640 has_expr = argt & (EX_XFILE | EX_EXPAND);
7641
7642 // If the command can be followed by a bar, find the bar and truncate
7643 // it, so that the following command can be compiled.
7644 // The '|' is overwritten with a NUL, it is put back below.
7645 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7646 && *eap->arg == '!')
7647 // :w !filter or :r !filter or :r! filter
7648 usefilter = TRUE;
7649 if ((argt & EX_TRLBAR) && !usefilter)
7650 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007651 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007652 separate_nextcmd(eap);
7653 if (eap->nextcmd != NULL)
7654 nextcmd = eap->nextcmd;
7655 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007656 else if (eap->cmdidx == CMD_wincmd)
7657 {
7658 p = eap->arg;
7659 if (*p != NUL)
7660 ++p;
7661 if (*p == 'g' || *p == Ctrl_G)
7662 ++p;
7663 p = skipwhite(p);
7664 if (*p == '|')
7665 {
7666 *p = NUL;
7667 nextcmd = p + 1;
7668 }
7669 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007670 }
7671
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007672 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7673 {
7674 // expand filename in "syntax include [@group] filename"
7675 has_expr = TRUE;
7676 eap->arg = skipwhite(eap->arg + 7);
7677 if (*eap->arg == '@')
7678 eap->arg = skiptowhite(eap->arg);
7679 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007680
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007681 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7682 && STRLEN(eap->arg) > 4)
7683 {
7684 int delim = *eap->arg;
7685
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007686 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007687 if (*p == delim)
7688 {
7689 eap->arg = p + 1;
7690 has_expr = TRUE;
7691 }
7692 }
7693
Bram Moolenaarecac5912021-01-05 19:23:28 +01007694 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7695 {
7696 // TODO: should only expand when appropriate for the command
7697 eap->arg = skiptowhite(eap->arg);
7698 has_expr = TRUE;
7699 }
7700
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007701 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007702 {
7703 int count = 0;
7704 char_u *start = skipwhite(line);
7705
7706 // :cmd xxx`=expr1`yyy`=expr2`zzz
7707 // PUSHS ":cmd xxx"
7708 // eval expr1
7709 // PUSHS "yyy"
7710 // eval expr2
7711 // PUSHS "zzz"
7712 // EXECCONCAT 5
7713 for (;;)
7714 {
7715 if (p > start)
7716 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007717 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007718 ++count;
7719 }
7720 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007721 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007722 return NULL;
7723 may_generate_2STRING(-1, cctx);
7724 ++count;
7725 p = skipwhite(p);
7726 if (*p != '`')
7727 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007728 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007729 return NULL;
7730 }
7731 start = p + 1;
7732
7733 p = (char_u *)strstr((char *)start, "`=");
7734 if (p == NULL)
7735 {
7736 if (*skipwhite(start) != NUL)
7737 {
7738 generate_PUSHS(cctx, vim_strsave(start));
7739 ++count;
7740 }
7741 break;
7742 }
7743 }
7744 generate_EXECCONCAT(cctx, count);
7745 }
7746 else
7747 generate_EXEC(cctx, line);
7748
7749theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007750 if (*nextcmd != NUL)
7751 {
7752 // the parser expects a pointer to the bar, put it back
7753 --nextcmd;
7754 *nextcmd = '|';
7755 }
7756
7757 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007758}
7759
7760/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007761 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007762 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007763 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007764 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007765add_def_function(ufunc_T *ufunc)
7766{
7767 dfunc_T *dfunc;
7768
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007769 if (def_functions.ga_len == 0)
7770 {
7771 // The first position is not used, so that a zero uf_dfunc_idx means it
7772 // wasn't set.
7773 if (ga_grow(&def_functions, 1) == FAIL)
7774 return FAIL;
7775 ++def_functions.ga_len;
7776 }
7777
Bram Moolenaar09689a02020-05-09 22:50:08 +02007778 // Add the function to "def_functions".
7779 if (ga_grow(&def_functions, 1) == FAIL)
7780 return FAIL;
7781 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7782 CLEAR_POINTER(dfunc);
7783 dfunc->df_idx = def_functions.ga_len;
7784 ufunc->uf_dfunc_idx = dfunc->df_idx;
7785 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007786 dfunc->df_name = vim_strsave(ufunc->uf_name);
7787 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007788 ++def_functions.ga_len;
7789 return OK;
7790}
7791
7792/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 * After ex_function() has collected all the function lines: parse and compile
7794 * the lines into instructions.
7795 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007796 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7797 * the return statement (used for lambda). When uf_ret_type is already set
7798 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007799 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007800 * This can be used recursively through compile_lambda(), which may reallocate
7801 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007802 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007804 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007805compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 char_u *line = NULL;
7808 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 cctx_T cctx;
7811 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007812 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813 int ret = FAIL;
7814 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007815 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007816 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007817 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007819 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007820 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007821 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007823 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7824 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007825 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007827 else
7828 {
7829 if (add_def_function(ufunc) == FAIL)
7830 return FAIL;
7831 new_def_function = TRUE;
7832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833
Bram Moolenaar985116a2020-07-12 17:31:09 +02007834 ufunc->uf_def_status = UF_COMPILING;
7835
Bram Moolenaara80faa82020-04-12 19:37:17 +02007836 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837 cctx.ctx_ufunc = ufunc;
7838 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007839 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7841 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7842 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7843 cctx.ctx_type_list = &ufunc->uf_type_list;
7844 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7845 instr = &cctx.ctx_instr;
7846
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007847 // Set the context to the function, it may be compiled when called from
7848 // another script. Set the script version to the most modern one.
7849 // The line number will be set in next_line_from_context().
7850 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7852
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007853 // Make sure error messages are OK.
7854 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7855 if (do_estack_push)
7856 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007857 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007858
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007859 if (ufunc->uf_def_args.ga_len > 0)
7860 {
7861 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007862 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007863 int i;
7864 char_u *arg;
7865 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007866 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007867
7868 // Produce instructions for the default values of optional arguments.
7869 // Store the instruction index in uf_def_arg_idx[] so that we know
7870 // where to start when the function is called, depending on the number
7871 // of arguments.
7872 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7873 if (ufunc->uf_def_arg_idx == NULL)
7874 goto erret;
7875 for (i = 0; i < count; ++i)
7876 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007877 garray_T *stack = &cctx.ctx_type_stack;
7878 type_T *val_type;
7879 int arg_idx = first_def_arg + i;
7880
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007881 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7882 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007883 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007884 goto erret;
7885
7886 // If no type specified use the type of the default value.
7887 // Otherwise check that the default value type matches the
7888 // specified type.
7889 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7890 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007891 {
7892 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007893 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007894 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007895 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7896 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007897 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007898
7899 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007900 goto erret;
7901 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007902 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007903
7904 if (did_set_arg_type)
7905 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007906 }
7907
7908 /*
7909 * Loop over all the lines of the function and generate instructions.
7910 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007911 for (;;)
7912 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007913 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007914 int starts_with_colon = FALSE;
7915 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007916 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007917
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007918 // Bail out on the first error to avoid a flood of errors and report
7919 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007920 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007921 goto erret;
7922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923 if (line != NULL && *line == '|')
7924 // the line continues after a '|'
7925 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007926 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007927 && !(*line == '#' && (line == cctx.ctx_line_start
7928 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007930 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931 goto erret;
7932 }
7933 else
7934 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007935 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007936 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007937 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939 }
7940
Bram Moolenaara80faa82020-04-12 19:37:17 +02007941 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942 ea.cmdlinep = &line;
7943 ea.cmd = skipwhite(line);
7944
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007945 // Some things can be recognized by the first character.
7946 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007948 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007949 // "#" starts a comment
7950 line = (char_u *)"";
7951 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007953 case '}':
7954 {
7955 // "}" ends a block scope
7956 scopetype_T stype = cctx.ctx_scope == NULL
7957 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007958
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007959 if (stype == BLOCK_SCOPE)
7960 {
7961 compile_endblock(&cctx);
7962 line = ea.cmd;
7963 }
7964 else
7965 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007966 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007967 goto erret;
7968 }
7969 if (line != NULL)
7970 line = skipwhite(ea.cmd + 1);
7971 continue;
7972 }
7973
7974 case '{':
7975 // "{" starts a block scope
7976 // "{'a': 1}->func() is something else
7977 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7978 {
7979 line = compile_block(ea.cmd, &cctx);
7980 continue;
7981 }
7982 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007983 }
7984
7985 /*
7986 * COMMAND MODIFIERS
7987 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007988 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007989 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7990 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991 {
7992 if (errormsg != NULL)
7993 goto erret;
7994 // empty line or comment
7995 line = (char_u *)"";
7996 continue;
7997 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007998 generate_cmdmods(&cctx, &local_cmdmod);
7999 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008001 // Check if there was a colon after the last command modifier or before
8002 // the current position.
8003 for (p = ea.cmd; p >= line; --p)
8004 {
8005 if (*p == ':')
8006 starts_with_colon = TRUE;
8007 if (p < ea.cmd && !VIM_ISWHITE(*p))
8008 break;
8009 }
8010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008012 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008013 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008014 {
8015 if (*ea.cmd == '(')
8016 // not for "call()"
8017 ea.cmd = p;
8018 else
8019 ea.cmd = skipwhite(ea.cmd);
8020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008021
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008022 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008024 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008025
Bram Moolenaar17126b12021-01-07 22:03:02 +01008026 // Check for assignment after command modifiers.
8027 assign = may_compile_assignment(&ea, &line, &cctx);
8028 if (assign == OK)
8029 goto nextline;
8030 if (assign == FAIL)
8031 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032 }
8033
8034 /*
8035 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008036 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008038 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008039 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008040 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008041 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008042 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008043 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008044 if (!starts_with_colon)
8045 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008046 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008047 goto erret;
8048 }
8049 if (ends_excmd2(line, ea.cmd))
8050 {
8051 // A range without a command: jump to the line.
8052 // TODO: compile to a more efficient command, possibly
8053 // calling parse_cmd_address().
8054 ea.cmdidx = CMD_SIZE;
8055 line = compile_exec(line, &ea, &cctx);
8056 goto nextline;
8057 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008058 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008059 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008060 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008061 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008062 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008063
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008064 if (p == NULL)
8065 {
8066 if (cctx.ctx_skip != SKIP_YES)
8067 emsg(_(e_ambiguous_use_of_user_defined_command));
8068 goto erret;
8069 }
8070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008071 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8072 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008073 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008074 {
8075 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008076 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008077 }
8078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008080 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008081 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008082 // CMD_var cannot happen, compile_assignment() above would be
8083 // used. Most likely an assignment to a non-existing variable.
8084 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008085 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008087 }
8088
Bram Moolenaar3988f642020-08-27 22:43:03 +02008089 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008090 && ea.cmdidx != CMD_elseif
8091 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008092 && ea.cmdidx != CMD_endif
8093 && ea.cmdidx != CMD_endfor
8094 && ea.cmdidx != CMD_endwhile
8095 && ea.cmdidx != CMD_catch
8096 && ea.cmdidx != CMD_finally
8097 && ea.cmdidx != CMD_endtry)
8098 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008099 emsg(_(e_unreachable_code_after_return));
8100 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008101 }
8102
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008103 p = skipwhite(p);
8104 if (ea.cmdidx != CMD_SIZE
8105 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8106 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008107 if (ea.cmdidx >= 0)
8108 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008109 if ((ea.argt & EX_BANG) && *p == '!')
8110 {
8111 ea.forceit = TRUE;
8112 p = skipwhite(p + 1);
8113 }
8114 }
8115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116 switch (ea.cmdidx)
8117 {
8118 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008119 ea.arg = p;
8120 line = compile_nested_function(&ea, &cctx);
8121 break;
8122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008124 // TODO: should we allow this, e.g. to declare a global
8125 // function?
8126 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008127 goto erret;
8128
8129 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008130 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008131 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008132 break;
8133
8134 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008135 emsg(_(e_cannot_use_let_in_vim9_script));
8136 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008137 case CMD_var:
8138 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 case CMD_const:
8140 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008141 if (line == p)
8142 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 break;
8144
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008145 case CMD_unlet:
8146 case CMD_unlockvar:
8147 case CMD_lockvar:
8148 line = compile_unletlock(p, &ea, &cctx);
8149 break;
8150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 case CMD_import:
8152 line = compile_import(p, &cctx);
8153 break;
8154
8155 case CMD_if:
8156 line = compile_if(p, &cctx);
8157 break;
8158 case CMD_elseif:
8159 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008160 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008161 break;
8162 case CMD_else:
8163 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008164 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165 break;
8166 case CMD_endif:
8167 line = compile_endif(p, &cctx);
8168 break;
8169
8170 case CMD_while:
8171 line = compile_while(p, &cctx);
8172 break;
8173 case CMD_endwhile:
8174 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008175 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 break;
8177
8178 case CMD_for:
8179 line = compile_for(p, &cctx);
8180 break;
8181 case CMD_endfor:
8182 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008183 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184 break;
8185 case CMD_continue:
8186 line = compile_continue(p, &cctx);
8187 break;
8188 case CMD_break:
8189 line = compile_break(p, &cctx);
8190 break;
8191
8192 case CMD_try:
8193 line = compile_try(p, &cctx);
8194 break;
8195 case CMD_catch:
8196 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008197 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008198 break;
8199 case CMD_finally:
8200 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008201 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008202 break;
8203 case CMD_endtry:
8204 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008205 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008206 break;
8207 case CMD_throw:
8208 line = compile_throw(p, &cctx);
8209 break;
8210
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008211 case CMD_eval:
8212 if (compile_expr0(&p, &cctx) == FAIL)
8213 goto erret;
8214
Bram Moolenaar3988f642020-08-27 22:43:03 +02008215 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008216 generate_instr_drop(&cctx, ISN_DROP, 1);
8217
8218 line = skipwhite(p);
8219 break;
8220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008223 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008224 case CMD_echomsg:
8225 case CMD_echoerr:
8226 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008227 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008229 case CMD_put:
8230 ea.cmd = cmd;
8231 line = compile_put(p, &ea, &cctx);
8232 break;
8233
Bram Moolenaar3988f642020-08-27 22:43:03 +02008234 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008235
Bram Moolenaarae616492020-07-28 20:07:27 +02008236 case CMD_append:
8237 case CMD_change:
8238 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008239 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008240 case CMD_xit:
8241 not_in_vim9(&ea);
8242 goto erret;
8243
Bram Moolenaar002262f2020-07-08 17:47:57 +02008244 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008245 if (cctx.ctx_skip != SKIP_YES)
8246 {
8247 semsg(_(e_invalid_command_str), ea.cmd);
8248 goto erret;
8249 }
8250 // We don't check for a next command here.
8251 line = (char_u *)"";
8252 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008254 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008255 if (cctx.ctx_skip == SKIP_YES)
8256 {
8257 // We don't check for a next command here.
8258 line = (char_u *)"";
8259 }
8260 else
8261 {
8262 // Not recognized, execute with do_cmdline_cmd().
8263 ea.arg = p;
8264 line = compile_exec(line, &ea, &cctx);
8265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266 break;
8267 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008268nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008269 if (line == NULL)
8270 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008271 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008273 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008274 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008276 if (cctx.ctx_type_stack.ga_len < 0)
8277 {
8278 iemsg("Type stack underflow");
8279 goto erret;
8280 }
8281 }
8282
8283 if (cctx.ctx_scope != NULL)
8284 {
8285 if (cctx.ctx_scope->se_type == IF_SCOPE)
8286 emsg(_(e_endif));
8287 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8288 emsg(_(e_endwhile));
8289 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8290 emsg(_(e_endfor));
8291 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008292 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008293 goto erret;
8294 }
8295
Bram Moolenaarefd88552020-06-18 20:50:10 +02008296 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008297 {
8298 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8299 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008300 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301 goto erret;
8302 }
8303
8304 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008305 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 }
8307
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008308 {
8309 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8310 + ufunc->uf_dfunc_idx;
8311 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008312 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008313 dfunc->df_instr = instr->ga_data;
8314 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008315 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008316 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008317 if (cctx.ctx_outer_used)
8318 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008319 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321
8322 ret = OK;
8323
8324erret:
8325 if (ret == FAIL)
8326 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008327 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008328 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8329 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008330
8331 for (idx = 0; idx < instr->ga_len; ++idx)
8332 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008333 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008334 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008335
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008336 // If using the last entry in the table and it was added above, we
8337 // might as well remove it.
8338 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008339 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008340 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008341 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008342 ufunc->uf_dfunc_idx = 0;
8343 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008344 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008345
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008346 while (cctx.ctx_scope != NULL)
8347 drop_scope(&cctx);
8348
Bram Moolenaar20431c92020-03-20 18:39:46 +01008349 // Don't execute this function body.
8350 ga_clear_strings(&ufunc->uf_lines);
8351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352 if (errormsg != NULL)
8353 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008354 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008355 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356 }
8357
8358 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008359 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008360 if (do_estack_push)
8361 estack_pop();
8362
Bram Moolenaar20431c92020-03-20 18:39:46 +01008363 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008364 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008365 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008366 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008367}
8368
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008369 void
8370set_function_type(ufunc_T *ufunc)
8371{
8372 int varargs = ufunc->uf_va_name != NULL;
8373 int argcount = ufunc->uf_args.ga_len;
8374
8375 // Create a type for the function, with the return type and any
8376 // argument types.
8377 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8378 // The type is included in "tt_args".
8379 if (argcount > 0 || varargs)
8380 {
8381 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8382 argcount, &ufunc->uf_type_list);
8383 // Add argument types to the function type.
8384 if (func_type_add_arg_types(ufunc->uf_func_type,
8385 argcount + varargs,
8386 &ufunc->uf_type_list) == FAIL)
8387 return;
8388 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8389 ufunc->uf_func_type->tt_min_argcount =
8390 argcount - ufunc->uf_def_args.ga_len;
8391 if (ufunc->uf_arg_types == NULL)
8392 {
8393 int i;
8394
8395 // lambda does not have argument types.
8396 for (i = 0; i < argcount; ++i)
8397 ufunc->uf_func_type->tt_args[i] = &t_any;
8398 }
8399 else
8400 mch_memmove(ufunc->uf_func_type->tt_args,
8401 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8402 if (varargs)
8403 {
8404 ufunc->uf_func_type->tt_args[argcount] =
8405 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8406 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8407 }
8408 }
8409 else
8410 // No arguments, can use a predefined type.
8411 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8412 argcount, &ufunc->uf_type_list);
8413}
8414
8415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416/*
8417 * Delete an instruction, free what it contains.
8418 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008419 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008420delete_instr(isn_T *isn)
8421{
8422 switch (isn->isn_type)
8423 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008424 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008426 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008427 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428 case ISN_LOADENV:
8429 case ISN_LOADG:
8430 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008431 case ISN_LOADT:
8432 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008434 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008435 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008436 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008437 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008438 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008439 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008441 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008442 case ISN_STOREW:
8443 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008444 vim_free(isn->isn_arg.string);
8445 break;
8446
8447 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008448 case ISN_STORES:
8449 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008450 break;
8451
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008452 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008453 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008454 vim_free(isn->isn_arg.unlet.ul_name);
8455 break;
8456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008457 case ISN_STOREOPT:
8458 vim_free(isn->isn_arg.storeopt.so_name);
8459 break;
8460
8461 case ISN_PUSHBLOB: // push blob isn_arg.blob
8462 blob_unref(isn->isn_arg.blob);
8463 break;
8464
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008465 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008466#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008467 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008468#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008469 break;
8470
8471 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008472#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008473 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008474#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008475 break;
8476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477 case ISN_UCALL:
8478 vim_free(isn->isn_arg.ufunc.cuf_name);
8479 break;
8480
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008481 case ISN_FUNCREF:
8482 {
8483 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8484 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008485 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008486
Bram Moolenaar077a4232020-12-22 18:33:27 +01008487 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8488 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008489 }
8490 break;
8491
8492 case ISN_DCALL:
8493 {
8494 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8495 + isn->isn_arg.dfunc.cdf_idx;
8496
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008497 if (dfunc->df_ufunc != NULL
8498 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008499 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008500 }
8501 break;
8502
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008503 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008504 {
8505 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8506 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8507
8508 if (ufunc != NULL)
8509 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008510 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008511 func_ptr_unref(ufunc);
8512 }
8513
8514 vim_free(lambda);
8515 vim_free(isn->isn_arg.newfunc.nf_global);
8516 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008517 break;
8518
Bram Moolenaar5e654232020-09-16 15:22:00 +02008519 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008520 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008521 free_type(isn->isn_arg.type.ct_type);
8522 break;
8523
Bram Moolenaar02194d22020-10-24 23:08:38 +02008524 case ISN_CMDMOD:
8525 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8526 ->cmod_filter_regmatch.regprog);
8527 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8528 break;
8529
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008530 case ISN_LOADSCRIPT:
8531 case ISN_STORESCRIPT:
8532 vim_free(isn->isn_arg.script.scriptref);
8533 break;
8534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008535 case ISN_2BOOL:
8536 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008537 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008538 case ISN_ADDBLOB:
8539 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008540 case ISN_ANYINDEX:
8541 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008543 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008545 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008546 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008547 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548 case ISN_COMPAREANY:
8549 case ISN_COMPAREBLOB:
8550 case ISN_COMPAREBOOL:
8551 case ISN_COMPAREDICT:
8552 case ISN_COMPAREFLOAT:
8553 case ISN_COMPAREFUNC:
8554 case ISN_COMPARELIST:
8555 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556 case ISN_COMPARESPECIAL:
8557 case ISN_COMPARESTRING:
8558 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008559 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 case ISN_DROP:
8561 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008562 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008563 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008565 case ISN_EXECCONCAT:
8566 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008568 case ISN_GETITEM:
8569 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008570 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008571 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008572 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008574 case ISN_LOADBDICT:
8575 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008576 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008577 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008578 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008580 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008581 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008582 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 case ISN_NEGATENR:
8584 case ISN_NEWDICT:
8585 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008586 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008587 case ISN_OPFLOAT:
8588 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008589 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008590 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008591 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008592 case ISN_PUSHF:
8593 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008594 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008595 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008596 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008597 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008598 case ISN_SHUFFLE:
8599 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008600 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008601 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008602 case ISN_STORENR:
8603 case ISN_STOREOUTER:
8604 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008605 case ISN_STOREV:
8606 case ISN_STRINDEX:
8607 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008608 case ISN_THROW:
8609 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008610 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008611 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612 // nothing allocated
8613 break;
8614 }
8615}
8616
8617/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008618 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008619 */
8620 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008621delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008622{
8623 int idx;
8624
8625 ga_clear(&dfunc->df_def_args_isn);
8626
8627 if (dfunc->df_instr != NULL)
8628 {
8629 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8630 delete_instr(dfunc->df_instr + idx);
8631 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008632 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008633 }
8634
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008635 if (mark_deleted)
8636 dfunc->df_deleted = TRUE;
8637 if (dfunc->df_ufunc != NULL)
8638 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008639}
8640
8641/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008642 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008643 * function, unless another user function still uses it.
8644 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008645 */
8646 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008647unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008649 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 {
8651 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8652 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008653
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008654 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008655 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008656 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008657 ufunc->uf_dfunc_idx = 0;
8658 if (dfunc->df_ufunc == ufunc)
8659 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008660 }
8661}
8662
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008663/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008664 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008665 */
8666 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008667link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008668{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008669 if (ufunc->uf_dfunc_idx > 0)
8670 {
8671 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8672 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008673
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008674 ++dfunc->df_refcount;
8675 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008676}
8677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008678#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008679/*
8680 * Free all functions defined with ":def".
8681 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008682 void
8683free_def_functions(void)
8684{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008685 int idx;
8686
8687 for (idx = 0; idx < def_functions.ga_len; ++idx)
8688 {
8689 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8690
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008691 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008692 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008693 }
8694
8695 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008696}
8697#endif
8698
8699
8700#endif // FEAT_EVAL