blob: 36fd2534c3677367c474d15dee6276ccdf9cc99a [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,
819 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820{
821 isn_T *isn;
822 garray_T *stack = &cctx->ctx_type_stack;
823
Bram Moolenaar080457c2020-03-03 21:53:32 +0100824 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
826 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200827 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 isn->isn_arg.type.ct_off = offset;
829
Bram Moolenaar5e654232020-09-16 15:22:00 +0200830 // type becomes expected
831 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832
833 return OK;
834}
835
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100836 static int
837generate_SETTYPE(
838 cctx_T *cctx,
839 type_T *expected)
840{
841 isn_T *isn;
842
843 RETURN_OK_IF_SKIP(cctx);
844 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
845 return FAIL;
846 isn->isn_arg.type.ct_type = alloc_type(expected);
847 return OK;
848}
849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200851 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
852 * used. Return FALSE if the types will never match.
853 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100854 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200855use_typecheck(type_T *actual, type_T *expected)
856{
857 if (actual->tt_type == VAR_ANY
858 || actual->tt_type == VAR_UNKNOWN
859 || (actual->tt_type == VAR_FUNC
860 && (expected->tt_type == VAR_FUNC
861 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100862 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
863 && ((actual->tt_member == &t_void)
864 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200865 return TRUE;
866 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
867 && actual->tt_type == expected->tt_type)
868 // This takes care of a nested list or dict.
869 return use_typecheck(actual->tt_member, expected->tt_member);
870 return FALSE;
871}
872
873/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200874 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200875 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200876 * - "actual" is a type that can be "expected" type: add a runtime check; or
877 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200878 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
879 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100881 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200882need_type(
883 type_T *actual,
884 type_T *expected,
885 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100886 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200887 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200888 int silent,
889 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200890{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200891 if (expected == &t_bool && actual != &t_bool
892 && (actual->tt_flags & TTFLAG_BOOL_OK))
893 {
894 // Using "0", "1" or the result of an expression with "&&" or "||" as a
895 // boolean is OK but requires a conversion.
896 generate_2BOOL(cctx, FALSE);
897 return OK;
898 }
899
Bram Moolenaar351ead02021-01-16 16:07:01 +0100900 if (check_type(expected, actual, FALSE, arg_idx) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200901 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200902
903 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200904 // If it's a constant a runtime check makes no sense.
905 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200906 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200907 generate_TYPECHECK(cctx, expected, offset);
908 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200909 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200910
911 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100912 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200913 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200914}
915
916/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100917 * Check that the top of the type stack has a type that can be used as a
918 * condition. Give an error and return FAIL if not.
919 */
920 static int
921bool_on_stack(cctx_T *cctx)
922{
923 garray_T *stack = &cctx->ctx_type_stack;
924 type_T *type;
925
926 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
927 if (type == &t_bool)
928 return OK;
929
930 if (type == &t_any || type == &t_number)
931 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
932 // This requires a runtime type check.
933 return generate_COND2BOOL(cctx);
934
Bram Moolenaar351ead02021-01-16 16:07:01 +0100935 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100936}
937
938/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 * Generate an ISN_PUSHNR instruction.
940 */
941 static int
942generate_PUSHNR(cctx_T *cctx, varnumber_T number)
943{
944 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200945 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946
Bram Moolenaar080457c2020-03-03 21:53:32 +0100947 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
949 return FAIL;
950 isn->isn_arg.number = number;
951
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200952 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200953 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100954 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 return OK;
956}
957
958/*
959 * Generate an ISN_PUSHBOOL instruction.
960 */
961 static int
962generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
963{
964 isn_T *isn;
965
Bram Moolenaar080457c2020-03-03 21:53:32 +0100966 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
968 return FAIL;
969 isn->isn_arg.number = number;
970
971 return OK;
972}
973
974/*
975 * Generate an ISN_PUSHSPEC instruction.
976 */
977 static int
978generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
979{
980 isn_T *isn;
981
Bram Moolenaar080457c2020-03-03 21:53:32 +0100982 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
984 return FAIL;
985 isn->isn_arg.number = number;
986
987 return OK;
988}
989
990#ifdef FEAT_FLOAT
991/*
992 * Generate an ISN_PUSHF instruction.
993 */
994 static int
995generate_PUSHF(cctx_T *cctx, float_T fnumber)
996{
997 isn_T *isn;
998
Bram Moolenaar080457c2020-03-03 21:53:32 +0100999 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1001 return FAIL;
1002 isn->isn_arg.fnumber = fnumber;
1003
1004 return OK;
1005}
1006#endif
1007
1008/*
1009 * Generate an ISN_PUSHS instruction.
1010 * Consumes "str".
1011 */
1012 static int
1013generate_PUSHS(cctx_T *cctx, char_u *str)
1014{
1015 isn_T *isn;
1016
Bram Moolenaar508b5612021-01-02 18:17:26 +01001017 if (cctx->ctx_skip == SKIP_YES)
1018 {
1019 vim_free(str);
1020 return OK;
1021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.string = str;
1025
1026 return OK;
1027}
1028
1029/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001030 * Generate an ISN_PUSHCHANNEL instruction.
1031 * Consumes "channel".
1032 */
1033 static int
1034generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1035{
1036 isn_T *isn;
1037
Bram Moolenaar080457c2020-03-03 21:53:32 +01001038 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001039 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1040 return FAIL;
1041 isn->isn_arg.channel = channel;
1042
1043 return OK;
1044}
1045
1046/*
1047 * Generate an ISN_PUSHJOB instruction.
1048 * Consumes "job".
1049 */
1050 static int
1051generate_PUSHJOB(cctx_T *cctx, job_T *job)
1052{
1053 isn_T *isn;
1054
Bram Moolenaar080457c2020-03-03 21:53:32 +01001055 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001056 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001057 return FAIL;
1058 isn->isn_arg.job = job;
1059
1060 return OK;
1061}
1062
1063/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 * Generate an ISN_PUSHBLOB instruction.
1065 * Consumes "blob".
1066 */
1067 static int
1068generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1069{
1070 isn_T *isn;
1071
Bram Moolenaar080457c2020-03-03 21:53:32 +01001072 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1074 return FAIL;
1075 isn->isn_arg.blob = blob;
1076
1077 return OK;
1078}
1079
1080/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001081 * Generate an ISN_PUSHFUNC instruction with name "name".
1082 * Consumes "name".
1083 */
1084 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001085generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001086{
1087 isn_T *isn;
1088
Bram Moolenaar080457c2020-03-03 21:53:32 +01001089 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001090 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001091 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001092 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001093
1094 return OK;
1095}
1096
1097/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001098 * Generate an ISN_GETITEM instruction with "index".
1099 */
1100 static int
1101generate_GETITEM(cctx_T *cctx, int index)
1102{
1103 isn_T *isn;
1104 garray_T *stack = &cctx->ctx_type_stack;
1105 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1106 type_T *item_type = &t_any;
1107
1108 RETURN_OK_IF_SKIP(cctx);
1109
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001110 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001111 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001112 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001113 emsg(_(e_listreq));
1114 return FAIL;
1115 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001116 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001117 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1118 return FAIL;
1119 isn->isn_arg.number = index;
1120
1121 // add the item type to the type stack
1122 if (ga_grow(stack, 1) == FAIL)
1123 return FAIL;
1124 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1125 ++stack->ga_len;
1126 return OK;
1127}
1128
1129/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001130 * Generate an ISN_SLICE instruction with "count".
1131 */
1132 static int
1133generate_SLICE(cctx_T *cctx, int count)
1134{
1135 isn_T *isn;
1136
1137 RETURN_OK_IF_SKIP(cctx);
1138 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.number = count;
1141 return OK;
1142}
1143
1144/*
1145 * Generate an ISN_CHECKLEN instruction with "min_len".
1146 */
1147 static int
1148generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1149{
1150 isn_T *isn;
1151
1152 RETURN_OK_IF_SKIP(cctx);
1153
1154 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1155 return FAIL;
1156 isn->isn_arg.checklen.cl_min_len = min_len;
1157 isn->isn_arg.checklen.cl_more_OK = more_OK;
1158
1159 return OK;
1160}
1161
1162/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 * Generate an ISN_STORE instruction.
1164 */
1165 static int
1166generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1167{
1168 isn_T *isn;
1169
Bram Moolenaar080457c2020-03-03 21:53:32 +01001170 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1172 return FAIL;
1173 if (name != NULL)
1174 isn->isn_arg.string = vim_strsave(name);
1175 else
1176 isn->isn_arg.number = idx;
1177
1178 return OK;
1179}
1180
1181/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001182 * Generate an ISN_STOREOUTER instruction.
1183 */
1184 static int
1185generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1186{
1187 isn_T *isn;
1188
1189 RETURN_OK_IF_SKIP(cctx);
1190 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1191 return FAIL;
1192 isn->isn_arg.outer.outer_idx = idx;
1193 isn->isn_arg.outer.outer_depth = level;
1194
1195 return OK;
1196}
1197
1198/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1200 */
1201 static int
1202generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1203{
1204 isn_T *isn;
1205
Bram Moolenaar080457c2020-03-03 21:53:32 +01001206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1208 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001209 isn->isn_arg.storenr.stnr_idx = idx;
1210 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211
1212 return OK;
1213}
1214
1215/*
1216 * Generate an ISN_STOREOPT instruction
1217 */
1218 static int
1219generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1220{
1221 isn_T *isn;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001224 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 return FAIL;
1226 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1227 isn->isn_arg.storeopt.so_flags = opt_flags;
1228
1229 return OK;
1230}
1231
1232/*
1233 * Generate an ISN_LOAD or similar instruction.
1234 */
1235 static int
1236generate_LOAD(
1237 cctx_T *cctx,
1238 isntype_T isn_type,
1239 int idx,
1240 char_u *name,
1241 type_T *type)
1242{
1243 isn_T *isn;
1244
Bram Moolenaar080457c2020-03-03 21:53:32 +01001245 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1247 return FAIL;
1248 if (name != NULL)
1249 isn->isn_arg.string = vim_strsave(name);
1250 else
1251 isn->isn_arg.number = idx;
1252
1253 return OK;
1254}
1255
1256/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001257 * Generate an ISN_LOADOUTER instruction
1258 */
1259 static int
1260generate_LOADOUTER(
1261 cctx_T *cctx,
1262 int idx,
1263 int nesting,
1264 type_T *type)
1265{
1266 isn_T *isn;
1267
1268 RETURN_OK_IF_SKIP(cctx);
1269 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1270 return FAIL;
1271 isn->isn_arg.outer.outer_idx = idx;
1272 isn->isn_arg.outer.outer_depth = nesting;
1273
1274 return OK;
1275}
1276
1277/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001278 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001279 */
1280 static int
1281generate_LOADV(
1282 cctx_T *cctx,
1283 char_u *name,
1284 int error)
1285{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001286 int di_flags;
1287 int vidx = find_vim_var(name, &di_flags);
1288 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289
Bram Moolenaar080457c2020-03-03 21:53:32 +01001290 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001291 if (vidx < 0)
1292 {
1293 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001294 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 return FAIL;
1296 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001297 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001298
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001299 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001300}
1301
1302/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001303 * Generate an ISN_UNLET instruction.
1304 */
1305 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001306generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001307{
1308 isn_T *isn;
1309
1310 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001311 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001312 return FAIL;
1313 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1314 isn->isn_arg.unlet.ul_forceit = forceit;
1315
1316 return OK;
1317}
1318
1319/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001320 * Generate an ISN_LOCKCONST instruction.
1321 */
1322 static int
1323generate_LOCKCONST(cctx_T *cctx)
1324{
1325 isn_T *isn;
1326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1329 return FAIL;
1330 return OK;
1331}
1332
1333/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 * Generate an ISN_LOADS instruction.
1335 */
1336 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001337generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001339 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001341 int sid,
1342 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343{
1344 isn_T *isn;
1345
Bram Moolenaar080457c2020-03-03 21:53:32 +01001346 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001347 if (isn_type == ISN_LOADS)
1348 isn = generate_instr_type(cctx, isn_type, type);
1349 else
1350 isn = generate_instr_drop(cctx, isn_type, 1);
1351 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001353 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1354 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355
1356 return OK;
1357}
1358
1359/*
1360 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1361 */
1362 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001363generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 cctx_T *cctx,
1365 isntype_T isn_type,
1366 int sid,
1367 int idx,
1368 type_T *type)
1369{
1370 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001371 scriptref_T *sref;
1372 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373
Bram Moolenaar080457c2020-03-03 21:53:32 +01001374 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 if (isn_type == ISN_LOADSCRIPT)
1376 isn = generate_instr_type(cctx, isn_type, type);
1377 else
1378 isn = generate_instr_drop(cctx, isn_type, 1);
1379 if (isn == NULL)
1380 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001381
1382 // This requires three arguments, which doesn't fit in an instruction, thus
1383 // we need to allocate a struct for this.
1384 sref = ALLOC_ONE(scriptref_T);
1385 if (sref == NULL)
1386 return FAIL;
1387 isn->isn_arg.script.scriptref = sref;
1388 sref->sref_sid = sid;
1389 sref->sref_idx = idx;
1390 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001391 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 return OK;
1393}
1394
1395/*
1396 * Generate an ISN_NEWLIST instruction.
1397 */
1398 static int
1399generate_NEWLIST(cctx_T *cctx, int count)
1400{
1401 isn_T *isn;
1402 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 type_T *type;
1404 type_T *member;
1405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1408 return FAIL;
1409 isn->isn_arg.number = count;
1410
Bram Moolenaar127542b2020-08-09 17:22:04 +02001411 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001412 if (count == 0)
1413 member = &t_void;
1414 else
1415 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001416 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1417 cctx->ctx_type_list);
1418 type = get_list_type(member, cctx->ctx_type_list);
1419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 // drop the value types
1421 stack->ga_len -= count;
1422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 // add the list type to the type stack
1424 if (ga_grow(stack, 1) == FAIL)
1425 return FAIL;
1426 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1427 ++stack->ga_len;
1428
1429 return OK;
1430}
1431
1432/*
1433 * Generate an ISN_NEWDICT instruction.
1434 */
1435 static int
1436generate_NEWDICT(cctx_T *cctx, int count)
1437{
1438 isn_T *isn;
1439 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 type_T *type;
1441 type_T *member;
1442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1445 return FAIL;
1446 isn->isn_arg.number = count;
1447
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001448 if (count == 0)
1449 member = &t_void;
1450 else
1451 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001452 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1453 cctx->ctx_type_list);
1454 type = get_dict_type(member, cctx->ctx_type_list);
1455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 // drop the key and value types
1457 stack->ga_len -= 2 * count;
1458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 // add the dict type to the type stack
1460 if (ga_grow(stack, 1) == FAIL)
1461 return FAIL;
1462 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1463 ++stack->ga_len;
1464
1465 return OK;
1466}
1467
1468/*
1469 * Generate an ISN_FUNCREF instruction.
1470 */
1471 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001472generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473{
1474 isn_T *isn;
1475 garray_T *stack = &cctx->ctx_type_stack;
1476
Bram Moolenaar080457c2020-03-03 21:53:32 +01001477 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1479 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001480 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001481 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482
Bram Moolenaarab360522021-01-10 14:02:28 +01001483 // if the referenced function is a closure, it may use items further up in
1484 // the nested context, including this one.
1485 if (ufunc->uf_flags & FC_CLOSURE)
1486 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if (ga_grow(stack, 1) == FAIL)
1489 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001490 ((type_T **)stack->ga_data)[stack->ga_len] =
1491 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 ++stack->ga_len;
1493
1494 return OK;
1495}
1496
1497/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001498 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001499 * "lambda_name" and "func_name" must be in allocated memory and will be
1500 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001501 */
1502 static int
1503generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1504{
1505 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001506
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001507 if (cctx->ctx_skip == SKIP_YES)
1508 {
1509 vim_free(lambda_name);
1510 vim_free(func_name);
1511 return OK;
1512 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001513 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001514 {
1515 vim_free(lambda_name);
1516 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001517 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001518 }
1519 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001520 isn->isn_arg.newfunc.nf_global = func_name;
1521
1522 return OK;
1523}
1524
1525/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001526 * Generate an ISN_DEF instruction: list functions
1527 */
1528 static int
1529generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1530{
1531 isn_T *isn;
1532
1533 RETURN_OK_IF_SKIP(cctx);
1534 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1535 return FAIL;
1536 if (len > 0)
1537 {
1538 isn->isn_arg.string = vim_strnsave(name, len);
1539 if (isn->isn_arg.string == NULL)
1540 return FAIL;
1541 }
1542 return OK;
1543}
1544
1545/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 * Generate an ISN_JUMP instruction.
1547 */
1548 static int
1549generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1550{
1551 isn_T *isn;
1552 garray_T *stack = &cctx->ctx_type_stack;
1553
Bram Moolenaar080457c2020-03-03 21:53:32 +01001554 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1556 return FAIL;
1557 isn->isn_arg.jump.jump_when = when;
1558 isn->isn_arg.jump.jump_where = where;
1559
1560 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1561 --stack->ga_len;
1562
1563 return OK;
1564}
1565
1566 static int
1567generate_FOR(cctx_T *cctx, int loop_idx)
1568{
1569 isn_T *isn;
1570 garray_T *stack = &cctx->ctx_type_stack;
1571
Bram Moolenaar080457c2020-03-03 21:53:32 +01001572 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1574 return FAIL;
1575 isn->isn_arg.forloop.for_idx = loop_idx;
1576
1577 if (ga_grow(stack, 1) == FAIL)
1578 return FAIL;
1579 // type doesn't matter, will be stored next
1580 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1581 ++stack->ga_len;
1582
1583 return OK;
1584}
1585
1586/*
1587 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001588 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 * Return FAIL if the number of arguments is wrong.
1590 */
1591 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001592generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593{
1594 isn_T *isn;
1595 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001596 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001597 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001598 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Bram Moolenaar080457c2020-03-03 21:53:32 +01001600 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001601 argoff = check_internal_func(func_idx, argcount);
1602 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 return FAIL;
1604
Bram Moolenaar389df252020-07-09 21:20:47 +02001605 if (method_call && argoff > 1)
1606 {
1607 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1608 return FAIL;
1609 isn->isn_arg.shuffle.shfl_item = argcount;
1610 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1611 }
1612
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001613 if (argcount > 0)
1614 {
1615 // Check the types of the arguments.
1616 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001617 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1618 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001619 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001620 if (internal_func_is_map(func_idx))
1621 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001622 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1625 return FAIL;
1626 isn->isn_arg.bfunc.cbf_idx = func_idx;
1627 isn->isn_arg.bfunc.cbf_argcount = argcount;
1628
Bram Moolenaar94738d82020-10-21 14:25:07 +02001629 // Drop the argument types and push the return type.
1630 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 if (ga_grow(stack, 1) == FAIL)
1632 return FAIL;
1633 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001634 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001635 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001637 if (maptype != NULL && maptype->tt_member != NULL
1638 && maptype->tt_member != &t_any)
1639 // Check that map() didn't change the item types.
1640 generate_TYPECHECK(cctx, maptype, -1);
1641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 return OK;
1643}
1644
1645/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001646 * Generate an ISN_LISTAPPEND instruction. Works like add().
1647 * Argument count is already checked.
1648 */
1649 static int
1650generate_LISTAPPEND(cctx_T *cctx)
1651{
1652 garray_T *stack = &cctx->ctx_type_stack;
1653 type_T *list_type;
1654 type_T *item_type;
1655 type_T *expected;
1656
1657 // Caller already checked that list_type is a list.
1658 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1659 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1660 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001661 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001662 return FAIL;
1663
1664 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1665 return FAIL;
1666
1667 --stack->ga_len; // drop the argument
1668 return OK;
1669}
1670
1671/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001672 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1673 * Argument count is already checked.
1674 */
1675 static int
1676generate_BLOBAPPEND(cctx_T *cctx)
1677{
1678 garray_T *stack = &cctx->ctx_type_stack;
1679 type_T *item_type;
1680
1681 // Caller already checked that blob_type is a blob.
1682 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001683 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001684 return FAIL;
1685
1686 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1687 return FAIL;
1688
1689 --stack->ga_len; // drop the argument
1690 return OK;
1691}
1692
1693/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 * Generate an ISN_DCALL or ISN_UCALL instruction.
1695 * Return FAIL if the number of arguments is wrong.
1696 */
1697 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001698generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699{
1700 isn_T *isn;
1701 garray_T *stack = &cctx->ctx_type_stack;
1702 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001703 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704
Bram Moolenaar080457c2020-03-03 21:53:32 +01001705 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 if (argcount > regular_args && !has_varargs(ufunc))
1707 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001708 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 return FAIL;
1710 }
1711 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1712 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001713 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 return FAIL;
1715 }
1716
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001717 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001718 {
1719 int i;
1720
1721 for (i = 0; i < argcount; ++i)
1722 {
1723 type_T *expected;
1724 type_T *actual;
1725
1726 if (i < regular_args)
1727 {
1728 if (ufunc->uf_arg_types == NULL)
1729 continue;
1730 expected = ufunc->uf_arg_types[i];
1731 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001732 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1733 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001734 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001735 else
1736 expected = ufunc->uf_va_type->tt_member;
1737 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001738 if (need_type(actual, expected, -argcount + i, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001739 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001740 {
1741 arg_type_mismatch(expected, actual, i + 1);
1742 return FAIL;
1743 }
1744 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001745 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001746 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1747 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001748 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001749 }
1750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001752 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001753 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001755 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 {
1757 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1758 isn->isn_arg.dfunc.cdf_argcount = argcount;
1759 }
1760 else
1761 {
1762 // A user function may be deleted and redefined later, can't use the
1763 // ufunc pointer, need to look it up again at runtime.
1764 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1765 isn->isn_arg.ufunc.cuf_argcount = argcount;
1766 }
1767
1768 stack->ga_len -= argcount; // drop the arguments
1769 if (ga_grow(stack, 1) == FAIL)
1770 return FAIL;
1771 // add return value
1772 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1773 ++stack->ga_len;
1774
1775 return OK;
1776}
1777
1778/*
1779 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1780 */
1781 static int
1782generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1783{
1784 isn_T *isn;
1785 garray_T *stack = &cctx->ctx_type_stack;
1786
Bram Moolenaar080457c2020-03-03 21:53:32 +01001787 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1789 return FAIL;
1790 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1791 isn->isn_arg.ufunc.cuf_argcount = argcount;
1792
1793 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001794 if (ga_grow(stack, 1) == FAIL)
1795 return FAIL;
1796 // add return value
1797 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1798 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799
1800 return OK;
1801}
1802
1803/*
1804 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001805 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 */
1807 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001808generate_PCALL(
1809 cctx_T *cctx,
1810 int argcount,
1811 char_u *name,
1812 type_T *type,
1813 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814{
1815 isn_T *isn;
1816 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001817 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818
Bram Moolenaar080457c2020-03-03 21:53:32 +01001819 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001820
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001821 if (type->tt_type == VAR_ANY)
1822 ret_type = &t_any;
1823 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001824 {
1825 if (type->tt_argcount != -1)
1826 {
1827 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1828
1829 if (argcount < type->tt_min_argcount - varargs)
1830 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001831 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001832 return FAIL;
1833 }
1834 if (!varargs && argcount > type->tt_argcount)
1835 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001836 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001837 return FAIL;
1838 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001839 if (type->tt_args != NULL)
1840 {
1841 int i;
1842
1843 for (i = 0; i < argcount; ++i)
1844 {
1845 int offset = -argcount + i - 1;
1846 type_T *actual = ((type_T **)stack->ga_data)[
1847 stack->ga_len + offset];
1848 type_T *expected;
1849
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001850 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001851 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001852 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001853 else
1854 expected = type->tt_args[i];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001855 if (need_type(actual, expected, offset, 0,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001856 cctx, TRUE, FALSE) == FAIL)
1857 {
1858 arg_type_mismatch(expected, actual, i + 1);
1859 return FAIL;
1860 }
1861 }
1862 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001863 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001864 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001865 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001866 else
1867 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001868 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001869 return FAIL;
1870 }
1871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1873 return FAIL;
1874 isn->isn_arg.pfunc.cpf_top = at_top;
1875 isn->isn_arg.pfunc.cpf_argcount = argcount;
1876
1877 stack->ga_len -= argcount; // drop the arguments
1878
1879 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001880 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001882 // If partial is above the arguments it must be cleared and replaced with
1883 // the return value.
1884 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1885 return FAIL;
1886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 return OK;
1888}
1889
1890/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001891 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 */
1893 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001894generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895{
1896 isn_T *isn;
1897 garray_T *stack = &cctx->ctx_type_stack;
1898 type_T *type;
1899
Bram Moolenaar080457c2020-03-03 21:53:32 +01001900 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001901 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001903 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001905 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001907 if (type->tt_type != VAR_DICT && type != &t_any)
1908 {
1909 emsg(_(e_dictreq));
1910 return FAIL;
1911 }
1912 // change dict type to dict member type
1913 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001914 {
1915 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1916 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918
1919 return OK;
1920}
1921
1922/*
1923 * Generate an ISN_ECHO instruction.
1924 */
1925 static int
1926generate_ECHO(cctx_T *cctx, int with_white, int count)
1927{
1928 isn_T *isn;
1929
Bram Moolenaar080457c2020-03-03 21:53:32 +01001930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1932 return FAIL;
1933 isn->isn_arg.echo.echo_with_white = with_white;
1934 isn->isn_arg.echo.echo_count = count;
1935
1936 return OK;
1937}
1938
Bram Moolenaarad39c092020-02-26 18:23:43 +01001939/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001940 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001941 */
1942 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001943generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001944{
1945 isn_T *isn;
1946
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001947 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001948 return FAIL;
1949 isn->isn_arg.number = count;
1950
1951 return OK;
1952}
1953
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001954/*
1955 * Generate an ISN_PUT instruction.
1956 */
1957 static int
1958generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1959{
1960 isn_T *isn;
1961
1962 RETURN_OK_IF_SKIP(cctx);
1963 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1964 return FAIL;
1965 isn->isn_arg.put.put_regname = regname;
1966 isn->isn_arg.put.put_lnum = lnum;
1967 return OK;
1968}
1969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 static int
1971generate_EXEC(cctx_T *cctx, char_u *line)
1972{
1973 isn_T *isn;
1974
Bram Moolenaar080457c2020-03-03 21:53:32 +01001975 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1977 return FAIL;
1978 isn->isn_arg.string = vim_strsave(line);
1979 return OK;
1980}
1981
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001982 static int
1983generate_EXECCONCAT(cctx_T *cctx, int count)
1984{
1985 isn_T *isn;
1986
1987 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1988 return FAIL;
1989 isn->isn_arg.number = count;
1990 return OK;
1991}
1992
Bram Moolenaar08597872020-12-10 19:43:40 +01001993/*
1994 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1995 */
1996 static int
1997generate_RANGE(cctx_T *cctx, char_u *range)
1998{
1999 isn_T *isn;
2000 garray_T *stack = &cctx->ctx_type_stack;
2001
2002 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2003 return FAIL;
2004 isn->isn_arg.string = range;
2005
2006 if (ga_grow(stack, 1) == FAIL)
2007 return FAIL;
2008 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2009 ++stack->ga_len;
2010 return OK;
2011}
2012
Bram Moolenaar792f7862020-11-23 08:31:18 +01002013 static int
2014generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2015{
2016 isn_T *isn;
2017
2018 RETURN_OK_IF_SKIP(cctx);
2019 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2020 return FAIL;
2021 isn->isn_arg.unpack.unp_count = var_count;
2022 isn->isn_arg.unpack.unp_semicolon = semicolon;
2023 return OK;
2024}
2025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002027 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002028 */
2029 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002030generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002031{
2032 isn_T *isn;
2033
Bram Moolenaar02194d22020-10-24 23:08:38 +02002034 if (cmod->cmod_flags != 0
2035 || cmod->cmod_split != 0
2036 || cmod->cmod_verbose != 0
2037 || cmod->cmod_tab != 0
2038 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002039 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002040 cctx->ctx_has_cmdmod = TRUE;
2041
2042 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002043 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002044 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2045 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2046 return FAIL;
2047 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002048 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002049 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002050 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002051
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002052 return OK;
2053}
2054
2055 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002056generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002057{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002058 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2059 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002060 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002061 return OK;
2062}
2063
2064/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002066 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002068 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002069reserve_local(
2070 cctx_T *cctx,
2071 char_u *name,
2072 size_t len,
2073 int isConst,
2074 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 lvar_T *lvar;
2077
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002078 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002080 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002081 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 }
2083
2084 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002085 return NULL;
2086 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002087 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002089 // Every local variable uses the next entry on the stack. We could re-use
2090 // the last ones when leaving a scope, but then variables used in a closure
2091 // might get overwritten. To keep things simple do not re-use stack
2092 // entries. This is less efficient, but memory is cheap these days.
2093 lvar->lv_idx = cctx->ctx_locals_count++;
2094
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002095 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 lvar->lv_const = isConst;
2097 lvar->lv_type = type;
2098
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002099 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100}
2101
2102/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002103 * Remove local variables above "new_top".
2104 */
2105 static void
2106unwind_locals(cctx_T *cctx, int new_top)
2107{
2108 if (cctx->ctx_locals.ga_len > new_top)
2109 {
2110 int idx;
2111 lvar_T *lvar;
2112
2113 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2114 {
2115 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2116 vim_free(lvar->lv_name);
2117 }
2118 }
2119 cctx->ctx_locals.ga_len = new_top;
2120}
2121
2122/*
2123 * Free all local variables.
2124 */
2125 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002126free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002127{
2128 unwind_locals(cctx, 0);
2129 ga_clear(&cctx->ctx_locals);
2130}
2131
2132/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002133 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2134 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2135 * error if the variable was defined with :const.
2136 */
2137 static int
2138check_item_writable(svar_T *sv, int check_writable, char_u *name)
2139{
2140 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2141 || (check_writable == ASSIGN_FINAL
2142 && sv->sv_const == ASSIGN_CONST))
2143 {
2144 semsg(_(e_readonlyvar), name);
2145 return FAIL;
2146 }
2147 return OK;
2148}
2149
2150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002152 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 * Returns the index in "sn_var_vals" if found.
2154 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002155 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 */
2157 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002158get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159{
2160 hashtab_T *ht;
2161 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002162 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002163 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 int idx;
2165
Bram Moolenaare3d46852020-08-29 13:39:17 +02002166 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002168 if (sid == current_sctx.sc_sid)
2169 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002170 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002171
2172 if (sav == NULL)
2173 return -2;
2174 idx = sav->sav_var_vals_idx;
2175 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002176 if (check_item_writable(sv, check_writable, name) == FAIL)
2177 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002178 return idx;
2179 }
2180
2181 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 ht = &SCRIPT_VARS(sid);
2183 di = find_var_in_ht(ht, 0, name, TRUE);
2184 if (di == NULL)
2185 return -2;
2186
2187 // Now find the svar_T index in sn_var_vals.
2188 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2189 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002190 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 if (sv->sv_tv == &di->di_tv)
2192 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002193 if (check_item_writable(sv, check_writable, name) == FAIL)
2194 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 return idx;
2196 }
2197 }
2198 return -1;
2199}
2200
2201/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002202 * Find "name" in imported items of the current script or in "cctx" if not
2203 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 */
2205 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002206find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 int idx;
2209
Bram Moolenaare3d46852020-08-29 13:39:17 +02002210 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002211 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 if (cctx != NULL)
2213 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2214 {
2215 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2216 + idx;
2217
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002218 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2219 : STRLEN(import->imp_name) == len
2220 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 return import;
2222 }
2223
Bram Moolenaarefa94442020-08-08 22:16:00 +02002224 return find_imported_in_script(name, len, current_sctx.sc_sid);
2225}
2226
2227 imported_T *
2228find_imported_in_script(char_u *name, size_t len, int sid)
2229{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002230 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002231 int idx;
2232
Bram Moolenaare3d46852020-08-29 13:39:17 +02002233 if (!SCRIPT_ID_VALID(sid))
2234 return NULL;
2235 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2237 {
2238 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2239
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002240 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2241 : STRLEN(import->imp_name) == len
2242 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 return import;
2244 }
2245 return NULL;
2246}
2247
2248/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002249 * Free all imported variables.
2250 */
2251 static void
2252free_imported(cctx_T *cctx)
2253{
2254 int idx;
2255
2256 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2257 {
2258 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2259
2260 vim_free(import->imp_name);
2261 }
2262 ga_clear(&cctx->ctx_imports);
2263}
2264
2265/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002266 * Return a pointer to the next line that isn't empty or only contains a
2267 * comment. Skips over white space.
2268 * Returns NULL if there is none.
2269 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002270 char_u *
2271peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002272{
2273 int lnum = cctx->ctx_lnum;
2274
2275 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2276 {
2277 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002278 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002279
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002280 // ignore NULLs inserted for continuation lines
2281 if (line != NULL)
2282 {
2283 p = skipwhite(line);
2284 if (*p != NUL && !vim9_comment_start(p))
2285 return p;
2286 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002287 }
2288 return NULL;
2289}
2290
2291/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002292 * Called when checking for a following operator at "arg". When the rest of
2293 * the line is empty or only a comment, peek the next line. If there is a next
2294 * line return a pointer to it and set "nextp".
2295 * Otherwise skip over white space.
2296 */
2297 static char_u *
2298may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2299{
2300 char_u *p = skipwhite(arg);
2301
2302 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002303 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002304 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002305 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002306 if (*nextp != NULL)
2307 return *nextp;
2308 }
2309 return p;
2310}
2311
2312/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002313 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002314 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002315 * Returns NULL when at the end.
2316 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002317 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002318next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002319{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002320 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002321
2322 do
2323 {
2324 ++cctx->ctx_lnum;
2325 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002326 {
2327 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002328 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002329 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002330 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002331 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002332 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002333 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002334 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002335 return line;
2336}
2337
2338/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002339 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002340 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002341 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002342 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2343 */
2344 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002345may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002346{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002347 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002348 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002349 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002350 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002351
2352 if (next == NULL)
2353 return FAIL;
2354 *arg = skipwhite(next);
2355 }
2356 return OK;
2357}
2358
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002359/*
2360 * Idem, and give an error when failed.
2361 */
2362 static int
2363may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2364{
2365 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2366 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002367 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002368 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002369 return FAIL;
2370 }
2371 return OK;
2372}
2373
2374
Bram Moolenaara5565e42020-05-09 15:44:01 +02002375// Structure passed between the compile_expr* functions to keep track of
2376// constants that have been parsed but for which no code was produced yet. If
2377// possible expressions on these constants are applied at compile time. If
2378// that is not possible, the code to push the constants needs to be generated
2379// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002380// Using 50 should be more than enough of 5 levels of ().
2381#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002382typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002383 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002384 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002385 int pp_is_const; // all generated code was constants, used for a
2386 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002387} ppconst_T;
2388
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002389static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002390static int compile_expr0(char_u **arg, cctx_T *cctx);
2391static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2392
Bram Moolenaara5565e42020-05-09 15:44:01 +02002393/*
2394 * Generate a PUSH instruction for "tv".
2395 * "tv" will be consumed or cleared.
2396 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2397 */
2398 static int
2399generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2400{
2401 if (tv != NULL)
2402 {
2403 switch (tv->v_type)
2404 {
2405 case VAR_UNKNOWN:
2406 break;
2407 case VAR_BOOL:
2408 generate_PUSHBOOL(cctx, tv->vval.v_number);
2409 break;
2410 case VAR_SPECIAL:
2411 generate_PUSHSPEC(cctx, tv->vval.v_number);
2412 break;
2413 case VAR_NUMBER:
2414 generate_PUSHNR(cctx, tv->vval.v_number);
2415 break;
2416#ifdef FEAT_FLOAT
2417 case VAR_FLOAT:
2418 generate_PUSHF(cctx, tv->vval.v_float);
2419 break;
2420#endif
2421 case VAR_BLOB:
2422 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2423 tv->vval.v_blob = NULL;
2424 break;
2425 case VAR_STRING:
2426 generate_PUSHS(cctx, tv->vval.v_string);
2427 tv->vval.v_string = NULL;
2428 break;
2429 default:
2430 iemsg("constant type not supported");
2431 clear_tv(tv);
2432 return FAIL;
2433 }
2434 tv->v_type = VAR_UNKNOWN;
2435 }
2436 return OK;
2437}
2438
2439/*
2440 * Generate code for any ppconst entries.
2441 */
2442 static int
2443generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2444{
2445 int i;
2446 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002447 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002448
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002449 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002450 for (i = 0; i < ppconst->pp_used; ++i)
2451 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2452 ret = FAIL;
2453 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002454 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002455 return ret;
2456}
2457
2458/*
2459 * Clear ppconst constants. Used when failing.
2460 */
2461 static void
2462clear_ppconst(ppconst_T *ppconst)
2463{
2464 int i;
2465
2466 for (i = 0; i < ppconst->pp_used; ++i)
2467 clear_tv(&ppconst->pp_tv[i]);
2468 ppconst->pp_used = 0;
2469}
2470
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002471/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002472 * Generate an instruction to load script-local variable "name", without the
2473 * leading "s:".
2474 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 */
2476 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002477compile_load_scriptvar(
2478 cctx_T *cctx,
2479 char_u *name, // variable NUL terminated
2480 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002481 char_u **end, // end of variable
2482 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002484 scriptitem_T *si;
2485 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 imported_T *import;
2487
Bram Moolenaare3d46852020-08-29 13:39:17 +02002488 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2489 return FAIL;
2490 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002491 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002492 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002494 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002495 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2496 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
2498 if (idx >= 0)
2499 {
2500 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2501
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002502 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 current_sctx.sc_sid, idx, sv->sv_type);
2504 return OK;
2505 }
2506
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002507 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 if (import != NULL)
2509 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002510 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002511 {
2512 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002513 char_u *exp_name;
2514 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002515 ufunc_T *ufunc;
2516 type_T *type;
2517
2518 // Used "import * as Name", need to lookup the member.
2519 if (*p != '.')
2520 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002521 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002522 return FAIL;
2523 }
2524 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002525 if (VIM_ISWHITE(*p))
2526 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002527 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002528 return FAIL;
2529 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002530
Bram Moolenaar1c991142020-07-04 13:15:31 +02002531 // isolate one name
2532 exp_name = p;
2533 while (eval_isnamec(*p))
2534 ++p;
2535 cc = *p;
2536 *p = NUL;
2537
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002538 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002539 *p = cc;
2540 p = skipwhite(p);
2541
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002542 // TODO: what if it is a function?
2543 if (idx < 0)
2544 return FAIL;
2545 *end = p;
2546
2547 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2548 import->imp_sid,
2549 idx,
2550 type);
2551 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002552 else if (import->imp_funcname != NULL)
2553 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002554 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002555 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2556 import->imp_sid,
2557 import->imp_var_vals_idx,
2558 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 return OK;
2560 }
2561
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002562 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002563 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 return FAIL;
2565}
2566
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002567 static int
2568generate_funcref(cctx_T *cctx, char_u *name)
2569{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002570 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002571
2572 if (ufunc == NULL)
2573 return FAIL;
2574
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002575 // Need to compile any default values to get the argument types.
2576 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2577 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2578 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002579 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002580}
2581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582/*
2583 * Compile a variable name into a load instruction.
2584 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002585 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 * When "error" is FALSE do not give an error when not found.
2587 */
2588 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002589compile_load(
2590 char_u **arg,
2591 char_u *end_arg,
2592 cctx_T *cctx,
2593 int is_expr,
2594 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595{
2596 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002597 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002598 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002600 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601
2602 if (*(*arg + 1) == ':')
2603 {
2604 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002605 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002606 {
2607 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002609 switch (**arg)
2610 {
2611 case 'g': isn_type = ISN_LOADGDICT; break;
2612 case 'w': isn_type = ISN_LOADWDICT; break;
2613 case 't': isn_type = ISN_LOADTDICT; break;
2614 case 'b': isn_type = ISN_LOADBDICT; break;
2615 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002616 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002617 goto theend;
2618 }
2619 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2620 goto theend;
2621 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 else
2624 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002625 isntype_T isn_type = ISN_DROP;
2626
2627 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2628 if (name == NULL)
2629 return FAIL;
2630
2631 switch (**arg)
2632 {
2633 case 'v': res = generate_LOADV(cctx, name, error);
2634 break;
2635 case 's': res = compile_load_scriptvar(cctx, name,
2636 NULL, NULL, error);
2637 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002638 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2639 isn_type = ISN_LOADG;
2640 else
2641 {
2642 isn_type = ISN_LOADAUTO;
2643 vim_free(name);
2644 name = vim_strnsave(*arg, end - *arg);
2645 if (name == NULL)
2646 return FAIL;
2647 }
2648 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002649 case 'w': isn_type = ISN_LOADW; break;
2650 case 't': isn_type = ISN_LOADT; break;
2651 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002652 default: // cannot happen, just in case
2653 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002654 goto theend;
2655 }
2656 if (isn_type != ISN_DROP)
2657 {
2658 // Global, Buffer-local, Window-local and Tabpage-local
2659 // variables can be defined later, thus we don't check if it
2660 // exists, give error at runtime.
2661 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 }
2664 }
2665 else
2666 {
2667 size_t len = end - *arg;
2668 int idx;
2669 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002670 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671
2672 name = vim_strnsave(*arg, end - *arg);
2673 if (name == NULL)
2674 return FAIL;
2675
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002676 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002678 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002679 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 }
2681 else
2682 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002683 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002684
Bram Moolenaar709664c2020-12-12 14:33:41 +01002685 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002687 type = lvar.lv_type;
2688 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002689 if (lvar.lv_from_outer != 0)
2690 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002691 else
2692 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
2694 else
2695 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002696 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002697 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002698 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002699 || find_imported(name, 0, cctx) != NULL)
2700 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002701
Bram Moolenaar0f769812020-09-12 18:32:34 +02002702 // When evaluating an expression and the name starts with an
2703 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002704 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002705 if (res == FAIL && is_expr
2706 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002707 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 }
2709 }
2710 if (gen_load)
2711 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002712 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002713 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002714 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002715 cctx->ctx_outer_used = TRUE;
2716 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 }
2718
2719 *arg = end;
2720
2721theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002722 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002723 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 vim_free(name);
2725 return res;
2726}
2727
2728/*
2729 * Compile the argument expressions.
2730 * "arg" points to just after the "(" and is advanced to after the ")"
2731 */
2732 static int
2733compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2734{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002735 char_u *p = *arg;
2736 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002737 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738
Bram Moolenaare6085c52020-04-12 20:19:16 +02002739 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002741 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2742 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002743 if (*p == ')')
2744 {
2745 *arg = p + 1;
2746 return OK;
2747 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002748 if (must_end)
2749 {
2750 semsg(_(e_missing_comma_before_argument_str), p);
2751 return FAIL;
2752 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002753
Bram Moolenaara5565e42020-05-09 15:44:01 +02002754 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 return FAIL;
2756 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002757
2758 if (*p != ',' && *skipwhite(p) == ',')
2759 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002760 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002761 p = skipwhite(p);
2762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002764 {
2765 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002766 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002767 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002768 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002769 else
2770 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002771 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002772 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002774failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002775 emsg(_(e_missing_close));
2776 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777}
2778
2779/*
2780 * Compile a function call: name(arg1, arg2)
2781 * "arg" points to "name", "arg + varlen" to the "(".
2782 * "argcount_init" is 1 for "value->method()"
2783 * Instructions:
2784 * EVAL arg1
2785 * EVAL arg2
2786 * BCALL / DCALL / UCALL
2787 */
2788 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002789compile_call(
2790 char_u **arg,
2791 size_t varlen,
2792 cctx_T *cctx,
2793 ppconst_T *ppconst,
2794 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795{
2796 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002797 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 int argcount = argcount_init;
2799 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002800 char_u fname_buf[FLEN_FIXED + 1];
2801 char_u *tofree = NULL;
2802 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002803 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002804 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002805 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806
Bram Moolenaara5565e42020-05-09 15:44:01 +02002807 // we can evaluate "has('name')" at compile time
2808 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2809 {
2810 char_u *s = skipwhite(*arg + varlen + 1);
2811 typval_T argvars[2];
2812
2813 argvars[0].v_type = VAR_UNKNOWN;
2814 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002815 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002816 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002817 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002819 if (*s == ')' && argvars[0].v_type == VAR_STRING
2820 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002821 {
2822 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2823
2824 *arg = s + 1;
2825 argvars[1].v_type = VAR_UNKNOWN;
2826 tv->v_type = VAR_NUMBER;
2827 tv->vval.v_number = 0;
2828 f_has(argvars, tv);
2829 clear_tv(&argvars[0]);
2830 ++ppconst->pp_used;
2831 return OK;
2832 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002833 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002834 }
2835
2836 if (generate_ppconst(cctx, ppconst) == FAIL)
2837 return FAIL;
2838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 if (varlen >= sizeof(namebuf))
2840 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002841 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 return FAIL;
2843 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002844 vim_strncpy(namebuf, *arg, varlen);
2845 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846
2847 *arg = skipwhite(*arg + varlen + 1);
2848 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002849 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850
Bram Moolenaar03290b82020-12-19 16:30:44 +01002851 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002852 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 {
2854 int idx;
2855
2856 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002857 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002859 {
2860 if (STRCMP(name, "add") == 0 && argcount == 2)
2861 {
2862 garray_T *stack = &cctx->ctx_type_stack;
2863 type_T *type = ((type_T **)stack->ga_data)[
2864 stack->ga_len - 2];
2865
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002866 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002867 if (type->tt_type == VAR_LIST)
2868 {
2869 // inline "add(list, item)" so that the type can be checked
2870 res = generate_LISTAPPEND(cctx);
2871 idx = -1;
2872 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002873 else if (type->tt_type == VAR_BLOB)
2874 {
2875 // inline "add(blob, nr)" so that the type can be checked
2876 res = generate_BLOBAPPEND(cctx);
2877 idx = -1;
2878 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002879 }
2880
2881 if (idx >= 0)
2882 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2883 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002884 else
2885 semsg(_(e_unknownfunc), namebuf);
2886 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 }
2888
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002889 // An argument or local variable can be a function reference, this
2890 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002891 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002892 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002893 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002894 // If we can find the function by name generate the right call.
2895 // Skip global functions here, a local funcref takes precedence.
2896 ufunc = find_func(name, FALSE, cctx);
2897 if (ufunc != NULL && !func_is_global(ufunc))
2898 {
2899 res = generate_CALL(cctx, ufunc, argcount);
2900 goto theend;
2901 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903
2904 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002905 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002906 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002908 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002909 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002910 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002911 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002912 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002913
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002914 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002915 goto theend;
2916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
Bram Moolenaar0f769812020-09-12 18:32:34 +02002918 // If we can find a global function by name generate the right call.
2919 if (ufunc != NULL)
2920 {
2921 res = generate_CALL(cctx, ufunc, argcount);
2922 goto theend;
2923 }
2924
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002925 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002926 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002927 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002928 res = generate_UCALL(cctx, name, argcount);
2929 else
2930 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002931
2932theend:
2933 vim_free(tofree);
2934 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935}
2936
2937// like NAMESPACE_CHAR but with 'a' and 'l'.
2938#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2939
2940/*
2941 * Find the end of a variable or function name. Unlike find_name_end() this
2942 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002943 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 * Return a pointer to just after the name. Equal to "arg" if there is no
2945 * valid name.
2946 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002947 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002948to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949{
2950 char_u *p;
2951
2952 // Quick check for valid starting character.
2953 if (!eval_isnamec1(*arg))
2954 return arg;
2955
2956 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2957 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2958 // and can be used in slice "[n:]".
2959 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002960 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2962 break;
2963 return p;
2964}
2965
2966/*
2967 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002968 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 */
2970 char_u *
2971to_name_const_end(char_u *arg)
2972{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002973 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 typval_T rettv;
2975
2976 if (p == arg && *arg == '[')
2977 {
2978
2979 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002980 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 p = arg;
2982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 return p;
2984}
2985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986/*
2987 * parse a list: [expr, expr]
2988 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002989 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 */
2991 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002992compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993{
2994 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002995 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002997 int is_const;
2998 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003000 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003002 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003003 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003004 semsg(_(e_list_end), *arg);
3005 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003006 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003007 if (*p == ',')
3008 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003009 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02003010 return FAIL;
3011 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003012 if (*p == ']')
3013 {
3014 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003015 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003016 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003017 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003018 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003019 if (!is_const)
3020 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 ++count;
3022 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003023 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003025 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3026 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003027 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003028 return FAIL;
3029 }
3030 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003031 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 p = skipwhite(p);
3033 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003034 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003036 ppconst->pp_is_const = is_all_const;
3037 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038}
3039
3040/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003041 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003043 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 */
3045 static int
3046compile_lambda(char_u **arg, cctx_T *cctx)
3047{
Bram Moolenaare462f522020-12-27 14:43:30 +01003048 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 typval_T rettv;
3050 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003051 evalarg_T evalarg;
3052
3053 CLEAR_FIELD(evalarg);
3054 evalarg.eval_flags = EVAL_EVALUATE;
3055 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056
3057 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003058 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3059 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003060 {
3061 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003062 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003063 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003064
Bram Moolenaar65c44152020-12-24 15:14:01 +01003065 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003067 ++ufunc->uf_refcount;
3068 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069
Bram Moolenaar65c44152020-12-24 15:14:01 +01003070 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003071 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003073 clear_evalarg(&evalarg, NULL);
3074
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003075 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003076 {
3077 // The return type will now be known.
3078 set_function_type(ufunc);
3079
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003080 // The function reference count will be 1. When the ISN_FUNCREF
3081 // instruction is deleted the reference count is decremented and the
3082 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003083 return generate_FUNCREF(cctx, ufunc);
3084 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003085
3086 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 return FAIL;
3088}
3089
3090/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003091 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003093 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 */
3095 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003096compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097{
3098 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003099 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 int count = 0;
3101 dict_T *d = dict_alloc();
3102 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003103 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003104 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003105 int is_const;
3106 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107
3108 if (d == NULL)
3109 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003110 if (generate_ppconst(cctx, ppconst) == FAIL)
3111 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003112 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003114 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115
Bram Moolenaar23c55272020-06-21 16:58:13 +02003116 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003117 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003118 *arg = NULL;
3119 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003120 }
3121
3122 if (**arg == '}')
3123 break;
3124
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003125 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003127 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003129 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003130 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003131 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3134 if (isn->isn_type == ISN_PUSHS)
3135 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003136 else
3137 {
3138 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003139 [stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003140 if (need_type(keytype, &t_string, -1, 0, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003141 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003142 return FAIL;
3143 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003144 *arg = skipwhite(*arg);
3145 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003146 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003147 emsg(_(e_missing_matching_bracket_after_dict_key));
3148 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003149 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003150 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003152 else
3153 {
3154 // {"name": value},
3155 // {'name': value},
3156 // {name: value} use "name" as a literal key
3157 key = get_literal_key(arg);
3158 if (key == NULL)
3159 return FAIL;
3160 if (generate_PUSHS(cctx, key) == FAIL)
3161 return FAIL;
3162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163
3164 // Check for duplicate keys, if using string keys.
3165 if (key != NULL)
3166 {
3167 item = dict_find(d, key, -1);
3168 if (item != NULL)
3169 {
3170 semsg(_(e_duplicate_key), key);
3171 goto failret;
3172 }
3173 item = dictitem_alloc(key);
3174 if (item != NULL)
3175 {
3176 item->di_tv.v_type = VAR_UNKNOWN;
3177 item->di_tv.v_lock = 0;
3178 if (dict_add(d, item) == FAIL)
3179 dictitem_free(item);
3180 }
3181 }
3182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 if (**arg != ':')
3184 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003185 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003186 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003187 else
3188 semsg(_(e_missing_dict_colon), *arg);
3189 return FAIL;
3190 }
3191 whitep = *arg + 1;
3192 if (!IS_WHITE_OR_NUL(*whitep))
3193 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003194 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 return FAIL;
3196 }
3197
Bram Moolenaar23c55272020-06-21 16:58:13 +02003198 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003199 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003200 *arg = NULL;
3201 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003202 }
3203
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003204 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003206 if (!is_const)
3207 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 ++count;
3209
Bram Moolenaar2c330432020-04-13 14:41:35 +02003210 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003211 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003212 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003213 *arg = NULL;
3214 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 if (**arg == '}')
3217 break;
3218 if (**arg != ',')
3219 {
3220 semsg(_(e_missing_dict_comma), *arg);
3221 goto failret;
3222 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003223 if (IS_WHITE_OR_NUL(*whitep))
3224 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003225 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003226 return FAIL;
3227 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003228 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003229 if (!IS_WHITE_OR_NUL(*whitep))
3230 {
3231 semsg(_(e_white_space_required_after_str), ",");
3232 return FAIL;
3233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 *arg = skipwhite(*arg + 1);
3235 }
3236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 *arg = *arg + 1;
3238
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003239 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003240 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003241 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003242 *arg += STRLEN(*arg);
3243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003245 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return generate_NEWDICT(cctx, count);
3247
3248failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003249 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003250 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003251 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003252 *arg = (char_u *)"";
3253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 dict_unref(d);
3255 return FAIL;
3256}
3257
3258/*
3259 * Compile "&option".
3260 */
3261 static int
3262compile_get_option(char_u **arg, cctx_T *cctx)
3263{
3264 typval_T rettv;
3265 char_u *start = *arg;
3266 int ret;
3267
3268 // parse the option and get the current value to get the type.
3269 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003270 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 if (ret == OK)
3272 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003273 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003274 char_u *name = vim_strnsave(start, *arg - start);
3275 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3276 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277
3278 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3279 vim_free(name);
3280 }
3281 clear_tv(&rettv);
3282
3283 return ret;
3284}
3285
3286/*
3287 * Compile "$VAR".
3288 */
3289 static int
3290compile_get_env(char_u **arg, cctx_T *cctx)
3291{
3292 char_u *start = *arg;
3293 int len;
3294 int ret;
3295 char_u *name;
3296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 ++*arg;
3298 len = get_env_len(arg);
3299 if (len == 0)
3300 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003301 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 return FAIL;
3303 }
3304
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003305 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 name = vim_strnsave(start, len + 1);
3307 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3308 vim_free(name);
3309 return ret;
3310}
3311
3312/*
3313 * Compile "@r".
3314 */
3315 static int
3316compile_get_register(char_u **arg, cctx_T *cctx)
3317{
3318 int ret;
3319
3320 ++*arg;
3321 if (**arg == NUL)
3322 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003323 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 return FAIL;
3325 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003326 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 {
3328 emsg_invreg(**arg);
3329 return FAIL;
3330 }
3331 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3332 ++*arg;
3333 return ret;
3334}
3335
3336/*
3337 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003338 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 */
3340 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003341apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003343 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344
3345 // this works from end to start
3346 while (p > start)
3347 {
3348 --p;
3349 if (*p == '-' || *p == '+')
3350 {
3351 // only '-' has an effect, for '+' we only check the type
3352#ifdef FEAT_FLOAT
3353 if (rettv->v_type == VAR_FLOAT)
3354 {
3355 if (*p == '-')
3356 rettv->vval.v_float = -rettv->vval.v_float;
3357 }
3358 else
3359#endif
3360 {
3361 varnumber_T val;
3362 int error = FALSE;
3363
3364 // tv_get_number_chk() accepts a string, but we don't want that
3365 // here
3366 if (check_not_string(rettv) == FAIL)
3367 return FAIL;
3368 val = tv_get_number_chk(rettv, &error);
3369 clear_tv(rettv);
3370 if (error)
3371 return FAIL;
3372 if (*p == '-')
3373 val = -val;
3374 rettv->v_type = VAR_NUMBER;
3375 rettv->vval.v_number = val;
3376 }
3377 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003378 else if (numeric_only)
3379 {
3380 ++p;
3381 break;
3382 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003383 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 {
3385 int v = tv2bool(rettv);
3386
3387 // '!' is permissive in the type.
3388 clear_tv(rettv);
3389 rettv->v_type = VAR_BOOL;
3390 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3391 }
3392 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003393 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 return OK;
3395}
3396
3397/*
3398 * Recognize v: variables that are constants and set "rettv".
3399 */
3400 static void
3401get_vim_constant(char_u **arg, typval_T *rettv)
3402{
3403 if (STRNCMP(*arg, "v:true", 6) == 0)
3404 {
3405 rettv->v_type = VAR_BOOL;
3406 rettv->vval.v_number = VVAL_TRUE;
3407 *arg += 6;
3408 }
3409 else if (STRNCMP(*arg, "v:false", 7) == 0)
3410 {
3411 rettv->v_type = VAR_BOOL;
3412 rettv->vval.v_number = VVAL_FALSE;
3413 *arg += 7;
3414 }
3415 else if (STRNCMP(*arg, "v:null", 6) == 0)
3416 {
3417 rettv->v_type = VAR_SPECIAL;
3418 rettv->vval.v_number = VVAL_NULL;
3419 *arg += 6;
3420 }
3421 else if (STRNCMP(*arg, "v:none", 6) == 0)
3422 {
3423 rettv->v_type = VAR_SPECIAL;
3424 rettv->vval.v_number = VVAL_NONE;
3425 *arg += 6;
3426 }
3427}
3428
Bram Moolenaar657137c2021-01-09 15:45:23 +01003429 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003430get_compare_type(char_u *p, int *len, int *type_is)
3431{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003432 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003433 int i;
3434
3435 switch (p[0])
3436 {
3437 case '=': if (p[1] == '=')
3438 type = EXPR_EQUAL;
3439 else if (p[1] == '~')
3440 type = EXPR_MATCH;
3441 break;
3442 case '!': if (p[1] == '=')
3443 type = EXPR_NEQUAL;
3444 else if (p[1] == '~')
3445 type = EXPR_NOMATCH;
3446 break;
3447 case '>': if (p[1] != '=')
3448 {
3449 type = EXPR_GREATER;
3450 *len = 1;
3451 }
3452 else
3453 type = EXPR_GEQUAL;
3454 break;
3455 case '<': if (p[1] != '=')
3456 {
3457 type = EXPR_SMALLER;
3458 *len = 1;
3459 }
3460 else
3461 type = EXPR_SEQUAL;
3462 break;
3463 case 'i': if (p[1] == 's')
3464 {
3465 // "is" and "isnot"; but not a prefix of a name
3466 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3467 *len = 5;
3468 i = p[*len];
3469 if (!isalnum(i) && i != '_')
3470 {
3471 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3472 *type_is = TRUE;
3473 }
3474 }
3475 break;
3476 }
3477 return type;
3478}
3479
3480/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003481 * Skip over an expression, ignoring most errors.
3482 */
3483 static void
3484skip_expr_cctx(char_u **arg, cctx_T *cctx)
3485{
3486 evalarg_T evalarg;
3487
3488 CLEAR_FIELD(evalarg);
3489 evalarg.eval_cctx = cctx;
3490 skip_expr(arg, &evalarg);
3491}
3492
3493/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003495 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 */
3497 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003498compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003500 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501
3502 // this works from end to start
3503 while (p > start)
3504 {
3505 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003506 while (VIM_ISWHITE(*p))
3507 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 if (*p == '-' || *p == '+')
3509 {
3510 int negate = *p == '-';
3511 isn_T *isn;
3512
3513 // TODO: check type
3514 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3515 {
3516 --p;
3517 if (*p == '-')
3518 negate = !negate;
3519 }
3520 // only '-' has an effect, for '+' we only check the type
3521 if (negate)
3522 isn = generate_instr(cctx, ISN_NEGATENR);
3523 else
3524 isn = generate_instr(cctx, ISN_CHECKNR);
3525 if (isn == NULL)
3526 return FAIL;
3527 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003528 else if (numeric_only)
3529 {
3530 ++p;
3531 break;
3532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 else
3534 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003535 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003537 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003539 if (p[-1] == '!')
3540 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 }
3543 if (generate_2BOOL(cctx, invert) == FAIL)
3544 return FAIL;
3545 }
3546 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003547 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 return OK;
3549}
3550
3551/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003552 * Compile "(expression)": recursive!
3553 * Return FAIL/OK.
3554 */
3555 static int
3556compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3557{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003558 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003559 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003560
Bram Moolenaar24156692021-01-14 20:35:49 +01003561 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3562 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003563 if (ppconst->pp_used <= PPSIZE - 10)
3564 {
3565 ret = compile_expr1(arg, cctx, ppconst);
3566 }
3567 else
3568 {
3569 // Not enough space in ppconst, flush constants.
3570 if (generate_ppconst(cctx, ppconst) == FAIL)
3571 return FAIL;
3572 ret = compile_expr0(arg, cctx);
3573 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003574 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3575 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003576 if (**arg == ')')
3577 ++*arg;
3578 else if (ret == OK)
3579 {
3580 emsg(_(e_missing_close));
3581 ret = FAIL;
3582 }
3583 return ret;
3584}
3585
3586/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003588 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 */
3590 static int
3591compile_subscript(
3592 char_u **arg,
3593 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003594 char_u *start_leader,
3595 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003596 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003598 char_u *name_start = *end_leader;
3599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 for (;;)
3601 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003602 char_u *p = skipwhite(*arg);
3603
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003604 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003605 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003606 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003607
3608 // If a following line starts with "->{" or "->X" advance to that
3609 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003610 // Also if a following line starts with ".x".
3611 if (next != NULL &&
3612 ((next[0] == '-' && next[1] == '>'
3613 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003614 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003615 {
3616 next = next_line_from_context(cctx, TRUE);
3617 if (next == NULL)
3618 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003619 *arg = next;
3620 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003621 }
3622 }
3623
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003624 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003625 // not a function call.
3626 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003628 garray_T *stack = &cctx->ctx_type_stack;
3629 type_T *type;
3630 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003632 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003633 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003634 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003637 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3638
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003639 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3641 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003642 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 return FAIL;
3644 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003645 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003647 char_u *pstart = p;
3648
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003649 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003650 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003651 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 // something->method()
3654 // Apply the '!', '-' and '+' first:
3655 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003656 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003659 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003660 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003661 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003662 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003663 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003664 int argcount = 1;
3665 char_u *expr;
3666 garray_T *stack;
3667 type_T *type;
3668
3669 // Funcref call: list->(Refs[2])(arg)
3670 // or lambda: list->((arg) => expr)(arg)
3671 // Fist compile the arguments.
3672 expr = *arg;
3673 *arg = skipwhite(*arg + 1);
3674 skip_expr_cctx(arg, cctx);
3675 *arg = skipwhite(*arg);
3676 if (**arg != ')')
3677 {
3678 semsg(_(e_missing_paren), *arg);
3679 return FAIL;
3680 }
3681 ++*arg;
3682 if (**arg != '(')
3683 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003684 if (*skipwhite(*arg) == '(')
3685 emsg(_(e_nowhitespace));
3686 else
3687 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003688 return FAIL;
3689 }
3690
3691 *arg = skipwhite(*arg + 1);
3692 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3693 return FAIL;
3694
3695 // Compile the function expression.
3696 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3697 return FAIL;
3698
3699 stack = &cctx->ctx_type_stack;
3700 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3701 if (generate_PCALL(cctx, argcount,
3702 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 return FAIL;
3704 }
3705 else
3706 {
3707 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003708 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003709 if (!eval_isnamec1(*p))
3710 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003711 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003712 return FAIL;
3713 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003714 if (ASCII_ISALPHA(*p) && p[1] == ':')
3715 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003716 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 ;
3718 if (*p != '(')
3719 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003720 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 return FAIL;
3722 }
3723 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003724 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 return FAIL;
3726 }
3727 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003728 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003730 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003731 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003732 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003733 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003734 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003735
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003736 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003737 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003738 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003739 // TODO: blob index
3740 // TODO: more arguments
3741 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003742 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003743 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003744 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003746 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003747 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003748 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003749 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003750 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003751 // missing first index is equal to zero
3752 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003753 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003754 else
3755 {
3756 if (compile_expr0(arg, cctx) == FAIL)
3757 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003758 if (**arg == ':')
3759 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003760 semsg(_(e_white_space_required_before_and_after_str_at_str),
3761 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003762 return FAIL;
3763 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003764 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003765 return FAIL;
3766 *arg = skipwhite(*arg);
3767 }
3768 if (**arg == ':')
3769 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003770 is_slice = TRUE;
3771 ++*arg;
3772 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3773 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003774 semsg(_(e_white_space_required_before_and_after_str_at_str),
3775 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003776 return FAIL;
3777 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003778 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003779 return FAIL;
3780 if (**arg == ']')
3781 // missing second index is equal to end of string
3782 generate_PUSHNR(cctx, -1);
3783 else
3784 {
3785 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003786 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003787 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003788 return FAIL;
3789 *arg = skipwhite(*arg);
3790 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792
3793 if (**arg != ']')
3794 {
3795 emsg(_(e_missbrac));
3796 return FAIL;
3797 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003798 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003800 // We can index a list and a dict. If we don't know the type
3801 // we can use the index value type.
3802 // TODO: If we don't know use an instruction to figure it out at
3803 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003804 typep = ((type_T **)stack->ga_data) + stack->ga_len
3805 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003806 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003807 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3808 // If the index is a string, the variable must be a Dict.
3809 if (*typep == &t_any && valtype == &t_string)
3810 vtype = VAR_DICT;
3811 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003812 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003813 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003814 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003815 return FAIL;
3816 if (is_slice)
3817 {
3818 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003819 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003820 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003821 return FAIL;
3822 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003823 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003824
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003825 if (vtype == VAR_DICT)
3826 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003827 if (is_slice)
3828 {
3829 emsg(_(e_cannot_slice_dictionary));
3830 return FAIL;
3831 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003832 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003833 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003834 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003835 if (*typep == &t_unknown)
3836 // empty dict was used
3837 *typep = &t_any;
3838 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003839 else
3840 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003841 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003842 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003843 return FAIL;
3844 *typep = &t_any;
3845 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003846 if (may_generate_2STRING(-1, cctx) == FAIL)
3847 return FAIL;
3848 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3849 return FAIL;
3850 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003851 else if (vtype == VAR_STRING)
3852 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003853 *typep = &t_string;
3854 if ((is_slice
3855 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3856 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003857 return FAIL;
3858 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003859 else if (vtype == VAR_BLOB)
3860 {
3861 emsg("Sorry, blob index and slice not implemented yet");
3862 return FAIL;
3863 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003864 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003865 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003866 if (is_slice)
3867 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003868 if (generate_instr_drop(cctx,
3869 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3870 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003871 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003872 }
Bram Moolenaared591872020-08-15 22:14:53 +02003873 else
3874 {
3875 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003876 {
Bram Moolenaared591872020-08-15 22:14:53 +02003877 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003878 if (*typep == &t_unknown)
3879 // empty list was used
3880 *typep = &t_any;
3881 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003882 if (generate_instr_drop(cctx,
3883 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3884 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003885 return FAIL;
3886 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003887 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003888 else
3889 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003890 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003891 return FAIL;
3892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003894 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003896 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003897 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003898 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003899 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003900
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003901 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003902 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003903 {
3904 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003905 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003906 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003907 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003908 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 while (eval_isnamec(*p))
3910 MB_PTR_ADV(p);
3911 if (p == *arg)
3912 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003913 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 return FAIL;
3915 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003916 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 return FAIL;
3918 *arg = p;
3919 }
3920 else
3921 break;
3922 }
3923
3924 // TODO - see handle_subscript():
3925 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3926 // Don't do this when "Func" is already a partial that was bound
3927 // explicitly (pt_auto is FALSE).
3928
3929 return OK;
3930}
3931
3932/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003933 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3934 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003936 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003937 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003938 *
3939 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 */
3941
3942/*
3943 * number number constant
3944 * 0zFFFFFFFF Blob constant
3945 * "string" string constant
3946 * 'string' literal string constant
3947 * &option-name option value
3948 * @r register contents
3949 * identifier variable value
3950 * function() function call
3951 * $VAR environment variable
3952 * (expression) nested expression
3953 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003954 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 *
3956 * Also handle:
3957 * ! in front logical NOT
3958 * - in front unary minus
3959 * + in front unary plus (ignored)
3960 * trailing (arg) funcref/partial call
3961 * trailing [] subscript in String or List
3962 * trailing .name entry in Dictionary
3963 * trailing ->name() method call
3964 */
3965 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003966compile_expr7(
3967 char_u **arg,
3968 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003969 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 char_u *start_leader, *end_leader;
3972 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003973 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003974 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003976 ppconst->pp_is_const = FALSE;
3977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 /*
3979 * Skip '!', '-' and '+' characters. They are handled later.
3980 */
3981 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003982 if (eval_leader(arg, TRUE) == FAIL)
3983 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 end_leader = *arg;
3985
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003986 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 switch (**arg)
3988 {
3989 /*
3990 * Number constant.
3991 */
3992 case '0': // also for blob starting with 0z
3993 case '1':
3994 case '2':
3995 case '3':
3996 case '4':
3997 case '5':
3998 case '6':
3999 case '7':
4000 case '8':
4001 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004002 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004004 // Apply "-" and "+" just before the number now, right to
4005 // left. Matters especially when "->" follows. Stops at
4006 // '!'.
4007 if (apply_leader(rettv, TRUE,
4008 start_leader, &end_leader) == FAIL)
4009 {
4010 clear_tv(rettv);
4011 return FAIL;
4012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 break;
4014
4015 /*
4016 * String constant: "string".
4017 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004018 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 return FAIL;
4020 break;
4021
4022 /*
4023 * Literal string constant: 'str''ing'.
4024 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004025 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return FAIL;
4027 break;
4028
4029 /*
4030 * Constant Vim variable.
4031 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004032 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 ret = NOTDONE;
4034 break;
4035
4036 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004037 * "true" constant
4038 */
4039 case 't': if (STRNCMP(*arg, "true", 4) == 0
4040 && !eval_isnamec((*arg)[4]))
4041 {
4042 *arg += 4;
4043 rettv->v_type = VAR_BOOL;
4044 rettv->vval.v_number = VVAL_TRUE;
4045 }
4046 else
4047 ret = NOTDONE;
4048 break;
4049
4050 /*
4051 * "false" constant
4052 */
4053 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4054 && !eval_isnamec((*arg)[5]))
4055 {
4056 *arg += 5;
4057 rettv->v_type = VAR_BOOL;
4058 rettv->vval.v_number = VVAL_FALSE;
4059 }
4060 else
4061 ret = NOTDONE;
4062 break;
4063
4064 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004065 * "null" constant
4066 */
4067 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4068 && !eval_isnamec((*arg)[5]))
4069 {
4070 *arg += 4;
4071 rettv->v_type = VAR_SPECIAL;
4072 rettv->vval.v_number = VVAL_NULL;
4073 }
4074 else
4075 ret = NOTDONE;
4076 break;
4077
4078 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 * List: [expr, expr]
4080 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004081 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 break;
4083
4084 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 * Dictionary: {'key': val, 'key': val}
4086 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004087 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 break;
4089
4090 /*
4091 * Option value: &name
4092 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004093 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4094 return FAIL;
4095 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 break;
4097
4098 /*
4099 * Environment variable: $VAR.
4100 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004101 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4102 return FAIL;
4103 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 break;
4105
4106 /*
4107 * Register contents: @r.
4108 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004109 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4110 return FAIL;
4111 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 break;
4113 /*
4114 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004115 * lambda: (arg, arg) => expr
4116 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004118 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4119 ret = compile_lambda(arg, cctx);
4120 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004121 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 break;
4123
4124 default: ret = NOTDONE;
4125 break;
4126 }
4127 if (ret == FAIL)
4128 return FAIL;
4129
Bram Moolenaar1c747212020-05-09 18:28:34 +02004130 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004132 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004133 clear_tv(rettv);
4134 else
4135 // A constant expression can possibly be handled compile time,
4136 // return the value instead of generating code.
4137 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 }
4139 else if (ret == NOTDONE)
4140 {
4141 char_u *p;
4142 int r;
4143
4144 if (!eval_isnamec1(**arg))
4145 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004146 if (ends_excmd(*skipwhite(*arg)))
4147 semsg(_(e_empty_expression_str), *arg);
4148 else
4149 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 return FAIL;
4151 }
4152
4153 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004154 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156 {
4157 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004160 {
4161 if (generate_ppconst(cctx, ppconst) == FAIL)
4162 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004163 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 if (r == FAIL)
4166 return FAIL;
4167 }
4168
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004169 // Handle following "[]", ".member", etc.
4170 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004171 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004172 ppconst) == FAIL)
4173 return FAIL;
4174 if (ppconst->pp_used > 0)
4175 {
4176 // apply the '!', '-' and '+' before the constant
4177 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004178 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004179 return FAIL;
4180 return OK;
4181 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004182 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004184 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185}
4186
4187/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004188 * Give the "white on both sides" error, taking the operator from "p[len]".
4189 */
4190 void
4191error_white_both(char_u *op, int len)
4192{
4193 char_u buf[10];
4194
4195 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004196 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004197}
4198
4199/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004200 * <type>expr7: runtime type check / conversion
4201 */
4202 static int
4203compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4204{
4205 type_T *want_type = NULL;
4206
4207 // Recognize <type>
4208 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4209 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004210 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004211 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4212 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004213 return FAIL;
4214
4215 if (**arg != '>')
4216 {
4217 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004218 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004219 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004220 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004221 return FAIL;
4222 }
4223 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004224 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004225 return FAIL;
4226 }
4227
4228 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4229 return FAIL;
4230
4231 if (want_type != NULL)
4232 {
4233 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004234 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004235
Bram Moolenaard1103582020-08-14 22:44:25 +02004236 generate_ppconst(cctx, ppconst);
4237 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004238 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004239 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004240 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004241 return FAIL;
4242 }
4243 }
4244
4245 return OK;
4246}
4247
4248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 * * number multiplication
4250 * / number division
4251 * % number modulo
4252 */
4253 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255{
4256 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004257 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004258 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004261 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 return FAIL;
4263
4264 /*
4265 * Repeat computing, until no "*", "/" or "%" is following.
4266 */
4267 for (;;)
4268 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004269 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 if (*op != '*' && *op != '/' && *op != '%')
4271 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004272 if (next != NULL)
4273 {
4274 *arg = next_line_from_context(cctx, TRUE);
4275 op = skipwhite(*arg);
4276 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004277
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004278 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004280 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004281 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004283 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004286 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004287 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289
4290 if (ppconst->pp_used == ppconst_used + 2
4291 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4292 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004293 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004294 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4295 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004296 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004298 // both are numbers: compute the result
4299 switch (*op)
4300 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004301 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004302 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004303 case '/': res = num_divide(tv1->vval.v_number,
4304 tv2->vval.v_number);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004305 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004306 case '%': res = num_modulus(tv1->vval.v_number,
4307 tv2->vval.v_number);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004308 break;
4309 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004310 tv1->vval.v_number = res;
4311 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004312 }
4313 else
4314 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004315 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004316 generate_two_op(cctx, op);
4317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 }
4319
4320 return OK;
4321}
4322
4323/*
4324 * + number addition
4325 * - number subtraction
4326 * .. string concatenation
4327 */
4328 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330{
4331 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004332 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
4336 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 return FAIL;
4339
4340 /*
4341 * Repeat computing, until no "+", "-" or ".." is following.
4342 */
4343 for (;;)
4344 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004345 op = may_peek_next_line(cctx, *arg, &next);
4346 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 break;
4348 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004349 if (next != NULL)
4350 {
4351 *arg = next_line_from_context(cctx, TRUE);
4352 op = skipwhite(*arg);
4353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004355 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004357 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004358 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 }
4360
Bram Moolenaare0de1712020-12-02 17:36:54 +01004361 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004364 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004365 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 return FAIL;
4367
Bram Moolenaara5565e42020-05-09 15:44:01 +02004368 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004369 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004370 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4371 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4372 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4373 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004375 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4376 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004377
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004378 // concat/subtract/add constant numbers
4379 if (*op == '+')
4380 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4381 else if (*op == '-')
4382 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4383 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004384 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004385 // concatenate constant strings
4386 char_u *s1 = tv1->vval.v_string;
4387 char_u *s2 = tv2->vval.v_string;
4388 size_t len1 = STRLEN(s1);
4389
4390 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4391 if (tv1->vval.v_string == NULL)
4392 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004393 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004394 return FAIL;
4395 }
4396 mch_memmove(tv1->vval.v_string, s1, len1);
4397 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004398 vim_free(s1);
4399 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004400 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 }
4403 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004404 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004406 if (*op == '.')
4407 {
4408 if (may_generate_2STRING(-2, cctx) == FAIL
4409 || may_generate_2STRING(-1, cctx) == FAIL)
4410 return FAIL;
4411 generate_instr_drop(cctx, ISN_CONCAT, 1);
4412 }
4413 else
4414 generate_two_op(cctx, op);
4415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 }
4417
4418 return OK;
4419}
4420
4421/*
4422 * expr5a == expr5b
4423 * expr5a =~ expr5b
4424 * expr5a != expr5b
4425 * expr5a !~ expr5b
4426 * expr5a > expr5b
4427 * expr5a >= expr5b
4428 * expr5a < expr5b
4429 * expr5a <= expr5b
4430 * expr5a is expr5b
4431 * expr5a isnot expr5b
4432 *
4433 * Produces instructions:
4434 * EVAL expr5a Push result of "expr5a"
4435 * EVAL expr5b Push result of "expr5b"
4436 * COMPARE one of the compare instructions
4437 */
4438 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004439compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004441 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004443 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447
4448 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 return FAIL;
4451
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004452 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004453 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454
4455 /*
4456 * If there is a comparative operator, use it.
4457 */
4458 if (type != EXPR_UNKNOWN)
4459 {
4460 int ic = FALSE; // Default: do not ignore case
4461
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004462 if (next != NULL)
4463 {
4464 *arg = next_line_from_context(cctx, TRUE);
4465 p = skipwhite(*arg);
4466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 if (type_is && (p[len] == '?' || p[len] == '#'))
4468 {
4469 semsg(_(e_invexpr2), *arg);
4470 return FAIL;
4471 }
4472 // extra question mark appended: ignore case
4473 if (p[len] == '?')
4474 {
4475 ic = TRUE;
4476 ++len;
4477 }
4478 // extra '#' appended: match case (ignored)
4479 else if (p[len] == '#')
4480 ++len;
4481 // nothing appended: match case
4482
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004483 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004485 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004486 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 }
4488
4489 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004490 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004491 return FAIL;
4492
Bram Moolenaara5565e42020-05-09 15:44:01 +02004493 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
4495
Bram Moolenaara5565e42020-05-09 15:44:01 +02004496 if (ppconst->pp_used == ppconst_used + 2)
4497 {
4498 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4499 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4500 int ret;
4501
4502 // Both sides are a constant, compute the result now.
4503 // First check for a valid combination of types, this is more
4504 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004505 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004506 ret = FAIL;
4507 else
4508 {
4509 ret = typval_compare(tv1, tv2, type, ic);
4510 tv1->v_type = VAR_BOOL;
4511 tv1->vval.v_number = tv1->vval.v_number
4512 ? VVAL_TRUE : VVAL_FALSE;
4513 clear_tv(tv2);
4514 --ppconst->pp_used;
4515 }
4516 return ret;
4517 }
4518
4519 generate_ppconst(cctx, ppconst);
4520 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 }
4522
4523 return OK;
4524}
4525
Bram Moolenaar7f141552020-05-09 17:35:53 +02004526static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528/*
4529 * Compile || or &&.
4530 */
4531 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532compile_and_or(
4533 char_u **arg,
4534 cctx_T *cctx,
4535 char *op,
4536 ppconst_T *ppconst,
4537 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004539 char_u *next;
4540 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 int opchar = *op;
4542
4543 if (p[0] == opchar && p[1] == opchar)
4544 {
4545 garray_T *instr = &cctx->ctx_instr;
4546 garray_T end_ga;
4547
4548 /*
4549 * Repeat until there is no following "||" or "&&"
4550 */
4551 ga_init2(&end_ga, sizeof(int), 10);
4552 while (p[0] == opchar && p[1] == opchar)
4553 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004554 if (next != NULL)
4555 {
4556 *arg = next_line_from_context(cctx, TRUE);
4557 p = skipwhite(*arg);
4558 }
4559
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004560 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4561 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004562 semsg(_(e_white_space_required_before_and_after_str_at_str),
4563 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004564 return FAIL;
4565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004567 // TODO: use ppconst if the value is a constant and check
4568 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 generate_ppconst(cctx, ppconst);
4570
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004571 // Every part must evaluate to a bool.
4572 if (bool_on_stack(cctx) == FAIL)
4573 {
4574 ga_clear(&end_ga);
4575 return FAIL;
4576 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 if (ga_grow(&end_ga, 1) == FAIL)
4579 {
4580 ga_clear(&end_ga);
4581 return FAIL;
4582 }
4583 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4584 ++end_ga.ga_len;
4585 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004586 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587
4588 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004589 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004590 {
4591 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004592 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004593 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004594
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4596 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 {
4598 ga_clear(&end_ga);
4599 return FAIL;
4600 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004601
4602 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004606 // Every part must evaluate to a bool.
4607 if (bool_on_stack(cctx) == FAIL)
4608 {
4609 ga_clear(&end_ga);
4610 return FAIL;
4611 }
4612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 // Fill in the end label in all jumps.
4614 while (end_ga.ga_len > 0)
4615 {
4616 isn_T *isn;
4617
4618 --end_ga.ga_len;
4619 isn = ((isn_T *)instr->ga_data)
4620 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4621 isn->isn_arg.jump.jump_where = instr->ga_len;
4622 }
4623 ga_clear(&end_ga);
4624 }
4625
4626 return OK;
4627}
4628
4629/*
4630 * expr4a && expr4a && expr4a logical AND
4631 *
4632 * Produces instructions:
4633 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004634 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004635 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004637 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 * EVAL expr4c Push result of "expr4c"
4639 * end:
4640 */
4641 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004642compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004644 int ppconst_used = ppconst->pp_used;
4645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 return FAIL;
4649
4650 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652}
4653
4654/*
4655 * expr3a || expr3b || expr3c logical OR
4656 *
4657 * Produces instructions:
4658 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004659 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004660 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004662 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 * EVAL expr3c Push result of "expr3c"
4664 * end:
4665 */
4666 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 int ppconst_used = ppconst->pp_used;
4670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 return FAIL;
4674
4675 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004676 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677}
4678
4679/*
4680 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004682 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 * JUMP_IF_FALSE alt jump if false
4684 * EVAL expr1a
4685 * JUMP_ALWAYS end
4686 * alt: EVAL expr1b
4687 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004688 *
4689 * Toplevel expression: expr2 ?? expr1
4690 * Produces instructions:
4691 * EVAL expr2 Push result of "expr2"
4692 * JUMP_AND_KEEP_IF_TRUE end jump if true
4693 * EVAL expr1
4694 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 */
4696 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004697compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698{
4699 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004701 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702
Bram Moolenaar3988f642020-08-27 22:43:03 +02004703 // Ignore all kinds of errors when not producing code.
4704 if (cctx->ctx_skip == SKIP_YES)
4705 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004706 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004707 return OK;
4708 }
4709
Bram Moolenaar61a89812020-05-07 16:58:17 +02004710 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004711 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 return FAIL;
4713
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004714 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 if (*p == '?')
4716 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004717 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 garray_T *instr = &cctx->ctx_instr;
4719 garray_T *stack = &cctx->ctx_type_stack;
4720 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004721 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004723 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004724 int has_const_expr = FALSE;
4725 int const_value = FALSE;
4726 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004728 if (next != NULL)
4729 {
4730 *arg = next_line_from_context(cctx, TRUE);
4731 p = skipwhite(*arg);
4732 }
4733
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004734 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004735 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004736 semsg(_(e_white_space_required_before_and_after_str_at_str),
4737 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004738 return FAIL;
4739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740
Bram Moolenaara5565e42020-05-09 15:44:01 +02004741 if (ppconst->pp_used == ppconst_used + 1)
4742 {
4743 // the condition is a constant, we know whether the ? or the :
4744 // expression is to be evaluated.
4745 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004746 if (op_falsy)
4747 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4748 else
4749 {
4750 int error = FALSE;
4751
4752 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4753 &error);
4754 if (error)
4755 return FAIL;
4756 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004757 cctx->ctx_skip = save_skip == SKIP_YES ||
4758 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4759
4760 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4761 // "left ?? right" and "left" is truthy: produce "left"
4762 generate_ppconst(cctx, ppconst);
4763 else
4764 {
4765 clear_tv(&ppconst->pp_tv[ppconst_used]);
4766 --ppconst->pp_used;
4767 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004768 }
4769 else
4770 {
4771 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004772 if (op_falsy)
4773 end_idx = instr->ga_len;
4774 generate_JUMP(cctx, op_falsy
4775 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4776 if (op_falsy)
4777 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779
4780 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004781 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004782 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004783 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004784 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786 if (!has_const_expr)
4787 {
4788 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004790 if (!op_falsy)
4791 {
4792 // remember the type and drop it
4793 --stack->ga_len;
4794 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004796 end_idx = instr->ga_len;
4797 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004798
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004799 // jump here from JUMP_IF_FALSE
4800 isn = ((isn_T *)instr->ga_data) + alt_idx;
4801 isn->isn_arg.jump.jump_where = instr->ga_len;
4802 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004805 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004807 // Check for the ":".
4808 p = may_peek_next_line(cctx, *arg, &next);
4809 if (*p != ':')
4810 {
4811 emsg(_(e_missing_colon));
4812 return FAIL;
4813 }
4814 if (next != NULL)
4815 {
4816 *arg = next_line_from_context(cctx, TRUE);
4817 p = skipwhite(*arg);
4818 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004819
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004820 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4821 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004822 semsg(_(e_white_space_required_before_and_after_str_at_str),
4823 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004824 return FAIL;
4825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004827 // evaluate the third expression
4828 if (has_const_expr)
4829 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004830 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004831 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004832 return FAIL;
4833 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4834 return FAIL;
4835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 if (!has_const_expr)
4838 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004839 type_T **typep;
4840
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842
Bram Moolenaara5565e42020-05-09 15:44:01 +02004843 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004844 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4845 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004846
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004847 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 isn = ((isn_T *)instr->ga_data) + end_idx;
4849 isn->isn_arg.jump.jump_where = instr->ga_len;
4850 }
4851
4852 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 }
4854 return OK;
4855}
4856
4857/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004858 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004859 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4860 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004861 */
4862 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004863compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864{
4865 ppconst_T ppconst;
4866
4867 CLEAR_FIELD(ppconst);
4868 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4869 {
4870 clear_ppconst(&ppconst);
4871 return FAIL;
4872 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004873 if (is_const != NULL)
4874 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004875 if (generate_ppconst(cctx, &ppconst) == FAIL)
4876 return FAIL;
4877 return OK;
4878}
4879
4880/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004881 * Toplevel expression.
4882 */
4883 static int
4884compile_expr0(char_u **arg, cctx_T *cctx)
4885{
4886 return compile_expr0_ext(arg, cctx, NULL);
4887}
4888
4889/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 * compile "return [expr]"
4891 */
4892 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004893compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894{
4895 char_u *p = arg;
4896 garray_T *stack = &cctx->ctx_type_stack;
4897 type_T *stack_type;
4898
4899 if (*p != NUL && *p != '|' && *p != '\n')
4900 {
4901 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004902 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 return NULL;
4904
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004905 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004906 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004907 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004908 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004909 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4910 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004911 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004912 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004913 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004914 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004915 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004916 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4917 && stack_type->tt_type != VAR_VOID
4918 && stack_type->tt_type != VAR_UNKNOWN)
4919 {
4920 emsg(_(e_returning_value_in_function_without_return_type));
4921 return NULL;
4922 }
4923 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004924 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004925 return NULL;
4926 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 }
4929 else
4930 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004931 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004932 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004933 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4934 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004936 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 return NULL;
4938 }
4939
4940 // No argument, return zero.
4941 generate_PUSHNR(cctx, 0);
4942 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01004943
4944 // Undo any command modifiers.
4945 generate_undo_cmdmods(cctx);
4946
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004947 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 return NULL;
4949
4950 // "return val | endif" is possible
4951 return skipwhite(p);
4952}
4953
4954/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004955 * Get a line from the compilation context, compatible with exarg_T getline().
4956 * Return a pointer to the line in allocated memory.
4957 * Return NULL for end-of-file or some error.
4958 */
4959 static char_u *
4960exarg_getline(
4961 int c UNUSED,
4962 void *cookie,
4963 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004964 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004965{
4966 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004967 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004968
Bram Moolenaar66250c92020-08-20 15:02:42 +02004969 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004970 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004971 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004972 return NULL;
4973 ++cctx->ctx_lnum;
4974 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4975 // Comment lines result in NULL pointers, skip them.
4976 if (p != NULL)
4977 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004978 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004979}
4980
4981/*
4982 * Compile a nested :def command.
4983 */
4984 static char_u *
4985compile_nested_function(exarg_T *eap, cctx_T *cctx)
4986{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004987 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004988 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004989 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004990 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004991 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004992 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004993
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004994 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004995 {
4996 emsg(_(e_cannot_use_bang_with_nested_def));
4997 return NULL;
4998 }
4999
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005000 if (*name_start == '/')
5001 {
5002 name_end = skip_regexp(name_start + 1, '/', TRUE);
5003 if (*name_end == '/')
5004 ++name_end;
5005 eap->nextcmd = check_nextcmd(name_end);
5006 }
5007 if (name_end == name_start || *skipwhite(name_end) != '(')
5008 {
5009 if (!ends_excmd2(name_start, name_end))
5010 {
5011 semsg(_(e_invalid_command_str), eap->cmd);
5012 return NULL;
5013 }
5014
5015 // "def" or "def Name": list functions
5016 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5017 return NULL;
5018 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5019 }
5020
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005021 // Only g:Func() can use a namespace.
5022 if (name_start[1] == ':' && !is_global)
5023 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005024 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005025 return NULL;
5026 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005027 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5028 return NULL;
5029
Bram Moolenaar04b12692020-05-04 23:24:44 +02005030 eap->arg = name_end;
5031 eap->getline = exarg_getline;
5032 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005033 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005034 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005035 lambda_name = vim_strsave(get_lambda_name());
5036 if (lambda_name == NULL)
5037 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005038 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005039
Bram Moolenaar822ba242020-05-24 23:00:18 +02005040 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005041 {
5042 r = eap->skip ? OK : FAIL;
5043 goto theend;
5044 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005045 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005046 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005047 {
5048 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005049 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005050 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005051
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005052 if (is_global)
5053 {
5054 char_u *func_name = vim_strnsave(name_start + 2,
5055 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005056
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005057 if (func_name == NULL)
5058 r = FAIL;
5059 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005060 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005061 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005062 lambda_name = NULL;
5063 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005064 }
5065 else
5066 {
5067 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005068 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005069 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005070 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005071
Bram Moolenaareef21022020-08-01 22:16:43 +02005072 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005073 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005074 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005075 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005076 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005077
5078 // copy over the block scope IDs
5079 if (block_depth > 0)
5080 {
5081 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5082 if (ufunc->uf_block_ids != NULL)
5083 {
5084 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5085 sizeof(int) * block_depth);
5086 ufunc->uf_block_depth = block_depth;
5087 }
5088 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005089 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005090 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005091 r = OK;
5092
5093theend:
5094 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005095 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005096}
5097
5098/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 * Return the length of an assignment operator, or zero if there isn't one.
5100 */
5101 int
5102assignment_len(char_u *p, int *heredoc)
5103{
5104 if (*p == '=')
5105 {
5106 if (p[1] == '<' && p[2] == '<')
5107 {
5108 *heredoc = TRUE;
5109 return 3;
5110 }
5111 return 1;
5112 }
5113 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5114 return 2;
5115 if (STRNCMP(p, "..=", 3) == 0)
5116 return 3;
5117 return 0;
5118}
5119
5120// words that cannot be used as a variable
5121static char *reserved[] = {
5122 "true",
5123 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005124 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 NULL
5126};
5127
Bram Moolenaar752fc692021-01-04 21:57:11 +01005128// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005129typedef enum {
5130 dest_local,
5131 dest_option,
5132 dest_env,
5133 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005134 dest_buffer,
5135 dest_window,
5136 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005137 dest_vimvar,
5138 dest_script,
5139 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005140 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005141} assign_dest_T;
5142
Bram Moolenaar752fc692021-01-04 21:57:11 +01005143// Used by compile_lhs() to store information about the LHS of an assignment
5144// and one argument of ":unlet" with an index.
5145typedef struct {
5146 assign_dest_T lhs_dest; // type of destination
5147
5148 char_u *lhs_name; // allocated name including
5149 // "[expr]" or ".name".
5150 size_t lhs_varlen; // length of the variable without
5151 // "[expr]" or ".name"
5152 char_u *lhs_dest_end; // end of the destination, including
5153 // "[expr]" or ".name".
5154
5155 int lhs_has_index; // has "[expr]" or ".name"
5156
5157 int lhs_new_local; // create new local variable
5158 int lhs_opt_flags; // for when destination is an option
5159 int lhs_vimvaridx; // for when destination is a v:var
5160
5161 lvar_T lhs_local_lvar; // used for existing local destination
5162 lvar_T lhs_arg_lvar; // used for argument destination
5163 lvar_T *lhs_lvar; // points to destination lvar
5164 int lhs_scriptvar_sid;
5165 int lhs_scriptvar_idx;
5166
5167 int lhs_has_type; // type was specified
5168 type_T *lhs_type;
5169 type_T *lhs_member_type;
5170} lhs_T;
5171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005173 * Generate the load instruction for "name".
5174 */
5175 static void
5176generate_loadvar(
5177 cctx_T *cctx,
5178 assign_dest_T dest,
5179 char_u *name,
5180 lvar_T *lvar,
5181 type_T *type)
5182{
5183 switch (dest)
5184 {
5185 case dest_option:
5186 // TODO: check the option exists
5187 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5188 break;
5189 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005190 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5191 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5192 else
5193 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005194 break;
5195 case dest_buffer:
5196 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5197 break;
5198 case dest_window:
5199 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5200 break;
5201 case dest_tab:
5202 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5203 break;
5204 case dest_script:
5205 compile_load_scriptvar(cctx,
5206 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5207 break;
5208 case dest_env:
5209 // Include $ in the name here
5210 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5211 break;
5212 case dest_reg:
5213 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5214 break;
5215 case dest_vimvar:
5216 generate_LOADV(cctx, name + 2, TRUE);
5217 break;
5218 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005219 if (lvar->lv_from_outer > 0)
5220 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5221 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005222 else
5223 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5224 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005225 case dest_expr:
5226 // list or dict value should already be on the stack.
5227 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005228 }
5229}
5230
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005231/*
5232 * Skip over "[expr]" or ".member".
5233 * Does not check for any errors.
5234 */
5235 static char_u *
5236skip_index(char_u *start)
5237{
5238 char_u *p = start;
5239
5240 if (*p == '[')
5241 {
5242 p = skipwhite(p + 1);
5243 (void)skip_expr(&p, NULL);
5244 p = skipwhite(p);
5245 if (*p == ']')
5246 return p + 1;
5247 return p;
5248 }
5249 // if (*p == '.')
5250 return to_name_end(p + 1, TRUE);
5251}
5252
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005253 void
5254vim9_declare_error(char_u *name)
5255{
5256 char *scope = "";
5257
5258 switch (*name)
5259 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005260 case 'g': scope = _("global"); break;
5261 case 'b': scope = _("buffer"); break;
5262 case 'w': scope = _("window"); break;
5263 case 't': scope = _("tab"); break;
5264 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005265 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005266 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005267 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005268 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005269 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005270 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005271 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005272 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005273 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005274}
5275
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005276/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005277 * For one assignment figure out the type of destination. Return it in "dest".
5278 * When not recognized "dest" is not set.
5279 * For an option "opt_flags" is set.
5280 * For a v:var "vimvaridx" is set.
5281 * "type" is set to the destination type if known, unchanted otherwise.
5282 * Return FAIL if an error message was given.
5283 */
5284 static int
5285get_var_dest(
5286 char_u *name,
5287 assign_dest_T *dest,
5288 int cmdidx,
5289 int *opt_flags,
5290 int *vimvaridx,
5291 type_T **type,
5292 cctx_T *cctx)
5293{
5294 char_u *p;
5295
5296 if (*name == '&')
5297 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005298 int cc;
5299 long numval;
5300 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005301
5302 *dest = dest_option;
5303 if (cmdidx == CMD_final || cmdidx == CMD_const)
5304 {
5305 emsg(_(e_const_option));
5306 return FAIL;
5307 }
5308 p = name;
5309 p = find_option_end(&p, opt_flags);
5310 if (p == NULL)
5311 {
5312 // cannot happen?
5313 emsg(_(e_letunexp));
5314 return FAIL;
5315 }
5316 cc = *p;
5317 *p = NUL;
5318 opt_type = get_option_value(skip_option_env_lead(name),
5319 &numval, NULL, *opt_flags);
5320 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005321 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005322 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005323 case gov_unknown:
5324 semsg(_(e_unknown_option), name);
5325 return FAIL;
5326 case gov_string:
5327 case gov_hidden_string:
5328 *type = &t_string;
5329 break;
5330 case gov_bool:
5331 case gov_hidden_bool:
5332 *type = &t_bool;
5333 break;
5334 case gov_number:
5335 case gov_hidden_number:
5336 *type = &t_number;
5337 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005338 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005339 }
5340 else if (*name == '$')
5341 {
5342 *dest = dest_env;
5343 *type = &t_string;
5344 }
5345 else if (*name == '@')
5346 {
5347 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5348 {
5349 emsg_invreg(name[1]);
5350 return FAIL;
5351 }
5352 *dest = dest_reg;
5353 *type = &t_string;
5354 }
5355 else if (STRNCMP(name, "g:", 2) == 0)
5356 {
5357 *dest = dest_global;
5358 }
5359 else if (STRNCMP(name, "b:", 2) == 0)
5360 {
5361 *dest = dest_buffer;
5362 }
5363 else if (STRNCMP(name, "w:", 2) == 0)
5364 {
5365 *dest = dest_window;
5366 }
5367 else if (STRNCMP(name, "t:", 2) == 0)
5368 {
5369 *dest = dest_tab;
5370 }
5371 else if (STRNCMP(name, "v:", 2) == 0)
5372 {
5373 typval_T *vtv;
5374 int di_flags;
5375
5376 *vimvaridx = find_vim_var(name + 2, &di_flags);
5377 if (*vimvaridx < 0)
5378 {
5379 semsg(_(e_variable_not_found_str), name);
5380 return FAIL;
5381 }
5382 // We use the current value of "sandbox" here, is that OK?
5383 if (var_check_ro(di_flags, name, FALSE))
5384 return FAIL;
5385 *dest = dest_vimvar;
5386 vtv = get_vim_var_tv(*vimvaridx);
5387 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5388 }
5389 return OK;
5390}
5391
5392/*
5393 * Generate a STORE instruction for "dest", not being "dest_local".
5394 * Return FAIL when out of memory.
5395 */
5396 static int
5397generate_store_var(
5398 cctx_T *cctx,
5399 assign_dest_T dest,
5400 int opt_flags,
5401 int vimvaridx,
5402 int scriptvar_idx,
5403 int scriptvar_sid,
5404 type_T *type,
5405 char_u *name)
5406{
5407 switch (dest)
5408 {
5409 case dest_option:
5410 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5411 opt_flags);
5412 case dest_global:
5413 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005414 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5415 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005416 case dest_buffer:
5417 // include b: with the name, easier to execute that way
5418 return generate_STORE(cctx, ISN_STOREB, 0, name);
5419 case dest_window:
5420 // include w: with the name, easier to execute that way
5421 return generate_STORE(cctx, ISN_STOREW, 0, name);
5422 case dest_tab:
5423 // include t: with the name, easier to execute that way
5424 return generate_STORE(cctx, ISN_STORET, 0, name);
5425 case dest_env:
5426 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5427 case dest_reg:
5428 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5429 case dest_vimvar:
5430 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5431 case dest_script:
5432 if (scriptvar_idx < 0)
5433 {
5434 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005435 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005436
Bram Moolenaar41d61962020-12-06 20:12:43 +01005437 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005438 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5439 scriptvar_sid, type);
5440 if (name_s != name)
5441 vim_free(name_s);
5442 return r;
5443 }
5444 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5445 scriptvar_sid, scriptvar_idx, type);
5446 case dest_local:
5447 case dest_expr:
5448 // cannot happen
5449 break;
5450 }
5451 return FAIL;
5452}
5453
Bram Moolenaar752fc692021-01-04 21:57:11 +01005454 static int
5455is_decl_command(int cmdidx)
5456{
5457 return cmdidx == CMD_let || cmdidx == CMD_var
5458 || cmdidx == CMD_final || cmdidx == CMD_const;
5459}
5460
5461/*
5462 * Figure out the LHS type and other properties for an assignment or one item
5463 * of ":unlet" with an index.
5464 * Returns OK or FAIL.
5465 */
5466 static int
5467compile_lhs(
5468 char_u *var_start,
5469 lhs_T *lhs,
5470 int cmdidx,
5471 int heredoc,
5472 int oplen,
5473 cctx_T *cctx)
5474{
5475 char_u *var_end;
5476 int is_decl = is_decl_command(cmdidx);
5477
5478 CLEAR_POINTER(lhs);
5479 lhs->lhs_dest = dest_local;
5480 lhs->lhs_vimvaridx = -1;
5481 lhs->lhs_scriptvar_idx = -1;
5482
5483 // "dest_end" is the end of the destination, including "[expr]" or
5484 // ".name".
5485 // "var_end" is the end of the variable/option/etc. name.
5486 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5487 if (*var_start == '@')
5488 var_end = var_start + 2;
5489 else
5490 {
5491 // skip over the leading "&", "&l:", "&g:" and "$"
5492 var_end = skip_option_env_lead(var_start);
5493 var_end = to_name_end(var_end, TRUE);
5494 }
5495
5496 // "a: type" is declaring variable "a" with a type, not dict "a:".
5497 if (is_decl && lhs->lhs_dest_end == var_start + 2
5498 && lhs->lhs_dest_end[-1] == ':')
5499 --lhs->lhs_dest_end;
5500 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5501 --var_end;
5502
5503 // compute the length of the destination without "[expr]" or ".name"
5504 lhs->lhs_varlen = var_end - var_start;
5505 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5506 if (lhs->lhs_name == NULL)
5507 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005508
5509 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5510 // Something follows after the variable: "var[idx]" or "var.key".
5511 lhs->lhs_has_index = TRUE;
5512
Bram Moolenaar752fc692021-01-04 21:57:11 +01005513 if (heredoc)
5514 lhs->lhs_type = &t_list_string;
5515 else
5516 lhs->lhs_type = &t_any;
5517
5518 if (cctx->ctx_skip != SKIP_YES)
5519 {
5520 int declare_error = FALSE;
5521
5522 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5523 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5524 &lhs->lhs_type, cctx) == FAIL)
5525 return FAIL;
5526 if (lhs->lhs_dest != dest_local)
5527 {
5528 // Specific kind of variable recognized.
5529 declare_error = is_decl;
5530 }
5531 else
5532 {
5533 int idx;
5534
5535 // No specific kind of variable recognized, just a name.
5536 for (idx = 0; reserved[idx] != NULL; ++idx)
5537 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5538 {
5539 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5540 return FAIL;
5541 }
5542
5543
5544 if (lookup_local(var_start, lhs->lhs_varlen,
5545 &lhs->lhs_local_lvar, cctx) == OK)
5546 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5547 else
5548 {
5549 CLEAR_FIELD(lhs->lhs_arg_lvar);
5550 if (arg_exists(var_start, lhs->lhs_varlen,
5551 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5552 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5553 {
5554 if (is_decl)
5555 {
5556 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5557 return FAIL;
5558 }
5559 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5560 }
5561 }
5562 if (lhs->lhs_lvar != NULL)
5563 {
5564 if (is_decl)
5565 {
5566 semsg(_(e_variable_already_declared), lhs->lhs_name);
5567 return FAIL;
5568 }
5569 }
5570 else
5571 {
5572 int script_namespace = lhs->lhs_varlen > 1
5573 && STRNCMP(var_start, "s:", 2) == 0;
5574 int script_var = (script_namespace
5575 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5576 FALSE, cctx)
5577 : script_var_exists(var_start, lhs->lhs_varlen,
5578 TRUE, cctx)) == OK;
5579 imported_T *import =
5580 find_imported(var_start, lhs->lhs_varlen, cctx);
5581
5582 if (script_namespace || script_var || import != NULL)
5583 {
5584 char_u *rawname = lhs->lhs_name
5585 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5586
5587 if (is_decl)
5588 {
5589 if (script_namespace)
5590 semsg(_(e_cannot_declare_script_variable_in_function),
5591 lhs->lhs_name);
5592 else
5593 semsg(_(e_variable_already_declared_in_script),
5594 lhs->lhs_name);
5595 return FAIL;
5596 }
5597 else if (cctx->ctx_ufunc->uf_script_ctx_version
5598 == SCRIPT_VERSION_VIM9
5599 && script_namespace
5600 && !script_var && import == NULL)
5601 {
5602 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5603 return FAIL;
5604 }
5605
5606 lhs->lhs_dest = dest_script;
5607
5608 // existing script-local variables should have a type
5609 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5610 if (import != NULL)
5611 lhs->lhs_scriptvar_sid = import->imp_sid;
5612 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5613 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005614 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005615 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005616 lhs->lhs_scriptvar_sid, rawname,
5617 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5618 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005619 if (lhs->lhs_scriptvar_idx >= 0)
5620 {
5621 scriptitem_T *si = SCRIPT_ITEM(
5622 lhs->lhs_scriptvar_sid);
5623 svar_T *sv =
5624 ((svar_T *)si->sn_var_vals.ga_data)
5625 + lhs->lhs_scriptvar_idx;
5626 lhs->lhs_type = sv->sv_type;
5627 }
5628 }
5629 }
5630 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5631 == FAIL)
5632 return FAIL;
5633 }
5634 }
5635
5636 if (declare_error)
5637 {
5638 vim9_declare_error(lhs->lhs_name);
5639 return FAIL;
5640 }
5641 }
5642
5643 // handle "a:name" as a name, not index "name" on "a"
5644 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5645 var_end = lhs->lhs_dest_end;
5646
5647 if (lhs->lhs_dest != dest_option)
5648 {
5649 if (is_decl && *var_end == ':')
5650 {
5651 char_u *p;
5652
5653 // parse optional type: "let var: type = expr"
5654 if (!VIM_ISWHITE(var_end[1]))
5655 {
5656 semsg(_(e_white_space_required_after_str), ":");
5657 return FAIL;
5658 }
5659 p = skipwhite(var_end + 1);
5660 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5661 if (lhs->lhs_type == NULL)
5662 return FAIL;
5663 lhs->lhs_has_type = TRUE;
5664 }
5665 else if (lhs->lhs_lvar != NULL)
5666 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5667 }
5668
5669 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5670 && lhs->lhs_type->tt_type != VAR_STRING
5671 && lhs->lhs_type->tt_type != VAR_ANY)
5672 {
5673 emsg(_(e_can_only_concatenate_to_string));
5674 return FAIL;
5675 }
5676
5677 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5678 && cctx->ctx_skip != SKIP_YES)
5679 {
5680 if (oplen > 1 && !heredoc)
5681 {
5682 // +=, /=, etc. require an existing variable
5683 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5684 return FAIL;
5685 }
5686 if (!is_decl)
5687 {
5688 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5689 return FAIL;
5690 }
5691
5692 // new local variable
5693 if ((lhs->lhs_type->tt_type == VAR_FUNC
5694 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5695 && var_wrong_func_name(lhs->lhs_name, TRUE))
5696 return FAIL;
5697 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5698 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5699 if (lhs->lhs_lvar == NULL)
5700 return FAIL;
5701 lhs->lhs_new_local = TRUE;
5702 }
5703
5704 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005705 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005706 {
5707 // Something follows after the variable: "var[idx]" or "var.key".
5708 // TODO: should we also handle "->func()" here?
5709 if (is_decl)
5710 {
5711 emsg(_(e_cannot_use_index_when_declaring_variable));
5712 return FAIL;
5713 }
5714
5715 if (var_start[lhs->lhs_varlen] == '['
5716 || var_start[lhs->lhs_varlen] == '.')
5717 {
5718 char_u *after = var_start + lhs->lhs_varlen;
5719 char_u *p;
5720
5721 // Only the last index is used below, if there are others
5722 // before it generate code for the expression. Thus for
5723 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5724 for (;;)
5725 {
5726 p = skip_index(after);
5727 if (*p != '[' && *p != '.')
5728 break;
5729 after = p;
5730 }
5731 if (after > var_start + lhs->lhs_varlen)
5732 {
5733 lhs->lhs_varlen = after - var_start;
5734 lhs->lhs_dest = dest_expr;
5735 // We don't know the type before evaluating the expression,
5736 // use "any" until then.
5737 lhs->lhs_type = &t_any;
5738 }
5739
Bram Moolenaar752fc692021-01-04 21:57:11 +01005740 if (lhs->lhs_type->tt_member == NULL)
5741 lhs->lhs_member_type = &t_any;
5742 else
5743 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5744 }
5745 else
5746 {
5747 semsg("Not supported yet: %s", var_start);
5748 return FAIL;
5749 }
5750 }
5751 return OK;
5752}
5753
5754/*
5755 * Assignment to a list or dict member, or ":unlet" for the item, using the
5756 * information in "lhs".
5757 * Returns OK or FAIL.
5758 */
5759 static int
5760compile_assign_unlet(
5761 char_u *var_start,
5762 lhs_T *lhs,
5763 int is_assign,
5764 type_T *rhs_type,
5765 cctx_T *cctx)
5766{
5767 char_u *p;
5768 int r;
5769 vartype_T dest_type;
5770 size_t varlen = lhs->lhs_varlen;
5771 garray_T *stack = &cctx->ctx_type_stack;
5772
5773 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5774 p = var_start + varlen;
5775 if (*p == '[')
5776 {
5777 p = skipwhite(p + 1);
5778 r = compile_expr0(&p, cctx);
5779 if (r == OK && *skipwhite(p) != ']')
5780 {
5781 // this should not happen
5782 emsg(_(e_missbrac));
5783 r = FAIL;
5784 }
5785 }
5786 else // if (*p == '.')
5787 {
5788 char_u *key_end = to_name_end(p + 1, TRUE);
5789 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5790
5791 r = generate_PUSHS(cctx, key);
5792 }
5793 if (r == FAIL)
5794 return FAIL;
5795
5796 if (lhs->lhs_type == &t_any)
5797 {
5798 // Index on variable of unknown type: check at runtime.
5799 dest_type = VAR_ANY;
5800 }
5801 else
5802 {
5803 dest_type = lhs->lhs_type->tt_type;
5804 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5805 return FAIL;
5806 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005807 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5808 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005809 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005810 }
5811
5812 // Load the dict or list. On the stack we then have:
5813 // - value (for assignment, not for :unlet)
5814 // - index
5815 // - variable
5816 if (lhs->lhs_dest == dest_expr)
5817 {
5818 int c = var_start[varlen];
5819
5820 // Evaluate "ll[expr]" of "ll[expr][idx]"
5821 p = var_start;
5822 var_start[varlen] = NUL;
5823 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5824 {
5825 // this should not happen
5826 emsg(_(e_missbrac));
5827 return FAIL;
5828 }
5829 var_start[varlen] = c;
5830
5831 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5832 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5833 // now we can properly check the type
5834 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005835 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005836 FALSE, FALSE) == FAIL)
5837 return FAIL;
5838 }
5839 else
5840 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5841 lhs->lhs_lvar, lhs->lhs_type);
5842
5843 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5844 {
5845 if (is_assign)
5846 {
5847 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5848
5849 if (isn == NULL)
5850 return FAIL;
5851 isn->isn_arg.vartype = dest_type;
5852 }
5853 else
5854 {
5855 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5856 return FAIL;
5857 }
5858 }
5859 else
5860 {
5861 emsg(_(e_indexable_type_required));
5862 return FAIL;
5863 }
5864
5865 return OK;
5866}
5867
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005868/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005869 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005870 * "let name"
5871 * "var name = expr"
5872 * "final name = expr"
5873 * "const name = expr"
5874 * "name = expr"
5875 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005876 * Return NULL for an error.
5877 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878 */
5879 static char_u *
5880compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5881{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005882 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005884 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885 char_u *ret = NULL;
5886 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005887 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005890 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 int oplen = 0;
5893 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005894 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005896 int is_decl = is_decl_command(cmdidx);
5897 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005899 // Skip over the "var" or "[var, var]" to get to any "=".
5900 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5901 if (p == NULL)
5902 return *arg == '[' ? arg : NULL;
5903
5904 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005906 // TODO: should we allow this, and figure out type inference from list
5907 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005908 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005909 return NULL;
5910 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005911 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 sp = p;
5914 p = skipwhite(p);
5915 op = p;
5916 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005917
5918 if (var_count > 0 && oplen == 0)
5919 // can be something like "[1, 2]->func()"
5920 return arg;
5921
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005922 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005924 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005925 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005926 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005928 if (heredoc)
5929 {
5930 list_T *l;
5931 listitem_T *li;
5932
5933 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005934 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005936 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005937 if (l == NULL)
5938 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939
Bram Moolenaar078269b2020-09-21 20:35:55 +02005940 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005942 // Push each line and the create the list.
5943 FOR_ALL_LIST_ITEMS(l, li)
5944 {
5945 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5946 li->li_tv.vval.v_string = NULL;
5947 }
5948 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 list_free(l);
5951 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005952 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005954 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005956 char_u *wp;
5957
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005958 // for "[var, var] = expr" evaluate the expression here, loop over the
5959 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005960 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005961
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005962 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005963 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005964 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005965 if (compile_expr0(&p, cctx) == FAIL)
5966 return NULL;
5967 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005969 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005971 type_T *stacktype;
5972
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005973 stacktype = stack->ga_len == 0 ? &t_void
5974 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005975 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005977 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005978 goto theend;
5979 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01005980 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005981 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005982 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005983 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005984 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5985 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005986 if (stacktype->tt_member != NULL)
5987 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005988 }
5989 }
5990
5991 /*
5992 * Loop over variables in "[var, var] = expr".
5993 * For "var = expr" and "let var: type" this is done only once.
5994 */
5995 if (var_count > 0)
5996 var_start = skipwhite(arg + 1); // skip over the "["
5997 else
5998 var_start = arg;
5999 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6000 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006001 int instr_count = -1;
6002
Bram Moolenaar752fc692021-01-04 21:57:11 +01006003 vim_free(lhs.lhs_name);
6004
6005 /*
6006 * Figure out the LHS type and other properties.
6007 */
6008 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6009 goto theend;
6010
6011 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006012 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006013 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006014 goto theend;
6015 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006016 if (!is_decl && lhs.lhs_lvar != NULL
6017 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006018 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006019 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006020 goto theend;
6021 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006022
6023 if (!heredoc)
6024 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006025 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006026 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006027 if (oplen > 0 && var_count == 0)
6028 {
6029 // skip over the "=" and the expression
6030 p = skipwhite(op + oplen);
6031 compile_expr0(&p, cctx);
6032 }
6033 }
6034 else if (oplen > 0)
6035 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006036 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006037 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006038
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006039 // For "var = expr" evaluate the expression.
6040 if (var_count == 0)
6041 {
6042 int r;
6043
6044 // for "+=", "*=", "..=" etc. first load the current value
6045 if (*op != '=')
6046 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006047 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6048 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006049
Bram Moolenaar752fc692021-01-04 21:57:11 +01006050 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006051 {
6052 // TODO: get member from list or dict
6053 emsg("Index with operation not supported yet");
6054 goto theend;
6055 }
6056 }
6057
6058 // Compile the expression. Temporarily hide the new local
6059 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006060 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006061 --cctx->ctx_locals.ga_len;
6062 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006063 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006064 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006065 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006066 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006067 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006068 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006069 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006070 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006071 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006072 ++cctx->ctx_locals.ga_len;
6073 if (r == FAIL)
6074 goto theend;
6075 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006076 else if (semicolon && var_idx == var_count - 1)
6077 {
6078 // For "[var; var] = expr" get the rest of the list
6079 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6080 goto theend;
6081 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006082 else
6083 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006084 // For "[var, var] = expr" get the "var_idx" item from the
6085 // list.
6086 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006087 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006088 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006089
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006090 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006091 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006092 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006093 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006094 if ((rhs_type->tt_type == VAR_FUNC
6095 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006096 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006097 goto theend;
6098
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006100 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006101 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006102 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006103 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006104 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006105 }
6106 else
6107 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006108 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006109 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006110 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006111 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006112 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006113 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006114 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006115 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006116 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006117 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006118 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006119 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006120 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006121 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006122 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006123
Bram Moolenaar71abe482020-09-14 22:28:30 +02006124 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006125 if (lhs.lhs_has_index)
6126 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006127 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006128 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006129 goto theend;
6130 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006131 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006133 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006134 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006136 else if (cmdidx == CMD_final)
6137 {
6138 emsg(_(e_final_requires_a_value));
6139 goto theend;
6140 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006141 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006143 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006144 goto theend;
6145 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006146 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006147 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006148 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006149 goto theend;
6150 }
6151 else
6152 {
6153 // variables are always initialized
6154 if (ga_grow(instr, 1) == FAIL)
6155 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006156 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006157 {
6158 case VAR_BOOL:
6159 generate_PUSHBOOL(cctx, VVAL_FALSE);
6160 break;
6161 case VAR_FLOAT:
6162#ifdef FEAT_FLOAT
6163 generate_PUSHF(cctx, 0.0);
6164#endif
6165 break;
6166 case VAR_STRING:
6167 generate_PUSHS(cctx, NULL);
6168 break;
6169 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006170 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006171 break;
6172 case VAR_FUNC:
6173 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6174 break;
6175 case VAR_LIST:
6176 generate_NEWLIST(cctx, 0);
6177 break;
6178 case VAR_DICT:
6179 generate_NEWDICT(cctx, 0);
6180 break;
6181 case VAR_JOB:
6182 generate_PUSHJOB(cctx, NULL);
6183 break;
6184 case VAR_CHANNEL:
6185 generate_PUSHCHANNEL(cctx, NULL);
6186 break;
6187 case VAR_NUMBER:
6188 case VAR_UNKNOWN:
6189 case VAR_ANY:
6190 case VAR_PARTIAL:
6191 case VAR_VOID:
6192 case VAR_SPECIAL: // cannot happen
6193 generate_PUSHNR(cctx, 0);
6194 break;
6195 }
6196 }
6197 if (var_count == 0)
6198 end = p;
6199 }
6200
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006201 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006202 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006203 break;
6204
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006205 if (oplen > 0 && *op != '=')
6206 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006207 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006208 type_T *stacktype;
6209
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006210 if (*op == '.')
6211 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006212 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006213 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006214 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006215 if (
6216#ifdef FEAT_FLOAT
6217 // If variable is float operation with number is OK.
6218 !(expected == &t_float && stacktype == &t_number) &&
6219#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006220 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006221 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006222 goto theend;
6223
6224 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006225 {
6226 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6227 goto theend;
6228 }
6229 else if (*op == '+')
6230 {
6231 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006232 operator_type(lhs.lhs_member_type, stacktype),
6233 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006234 goto theend;
6235 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006236 else if (generate_two_op(cctx, op) == FAIL)
6237 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239
Bram Moolenaar752fc692021-01-04 21:57:11 +01006240 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006241 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006242 // Use the info in "lhs" to store the value at the index in the
6243 // list or dict.
6244 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6245 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006246 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006247 }
6248 else
6249 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006250 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6251 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006252 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006253 generate_LOCKCONST(cctx);
6254
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006255 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006256 && (lhs.lhs_type->tt_type == VAR_DICT
6257 || lhs.lhs_type->tt_type == VAR_LIST)
6258 && lhs.lhs_type->tt_member != NULL
6259 && lhs.lhs_type->tt_member != &t_any
6260 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006261 // Set the type in the list or dict, so that it can be checked,
6262 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006263 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006264
Bram Moolenaar752fc692021-01-04 21:57:11 +01006265 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006266 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006267 if (generate_store_var(cctx, lhs.lhs_dest,
6268 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6269 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6270 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006271 goto theend;
6272 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006273 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006274 {
6275 isn_T *isn = ((isn_T *)instr->ga_data)
6276 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006277
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006278 // optimization: turn "var = 123" from ISN_PUSHNR +
6279 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006280 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006281 && instr->ga_len == instr_count + 1
6282 && isn->isn_type == ISN_PUSHNR)
6283 {
6284 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006285
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006286 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006287 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006288 isn->isn_arg.storenr.stnr_val = val;
6289 if (stack->ga_len > 0)
6290 --stack->ga_len;
6291 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006292 else if (lhs.lhs_lvar->lv_from_outer > 0)
6293 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6294 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006295 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006296 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006297 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006298 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006299
6300 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006301 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006303
6304 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006305 if (var_count > 0 && !semicolon)
6306 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006307 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006308 goto theend;
6309 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006310
Bram Moolenaarb2097502020-07-19 17:17:02 +02006311 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312
6313theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006314 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 return ret;
6316}
6317
6318/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006319 * Check for an assignment at "eap->cmd", compile it if found.
6320 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6321 */
6322 static int
6323may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6324{
6325 char_u *pskip;
6326 char_u *p;
6327
6328 // Assuming the command starts with a variable or function name,
6329 // find what follows.
6330 // Skip over "var.member", "var[idx]" and the like.
6331 // Also "&opt = val", "$ENV = val" and "@r = val".
6332 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6333 ? eap->cmd + 1 : eap->cmd;
6334 p = to_name_end(pskip, TRUE);
6335 if (p > eap->cmd && *p != NUL)
6336 {
6337 char_u *var_end;
6338 int oplen;
6339 int heredoc;
6340
6341 if (eap->cmd[0] == '@')
6342 var_end = eap->cmd + 2;
6343 else
6344 var_end = find_name_end(pskip, NULL, NULL,
6345 FNE_CHECK_START | FNE_INCL_BR);
6346 oplen = assignment_len(skipwhite(var_end), &heredoc);
6347 if (oplen > 0)
6348 {
6349 size_t len = p - eap->cmd;
6350
6351 // Recognize an assignment if we recognize the variable
6352 // name:
6353 // "g:var = expr"
6354 // "local = expr" where "local" is a local var.
6355 // "script = expr" where "script" is a script-local var.
6356 // "import = expr" where "import" is an imported var
6357 // "&opt = expr"
6358 // "$ENV = expr"
6359 // "@r = expr"
6360 if (*eap->cmd == '&'
6361 || *eap->cmd == '$'
6362 || *eap->cmd == '@'
6363 || ((len) > 2 && eap->cmd[1] == ':')
6364 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6365 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6366 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6367 || find_imported(eap->cmd, len, cctx) != NULL)
6368 {
6369 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6370 if (*line == NULL || *line == eap->cmd)
6371 return FAIL;
6372 return OK;
6373 }
6374 }
6375 }
6376
6377 if (*eap->cmd == '[')
6378 {
6379 // [var, var] = expr
6380 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6381 if (*line == NULL)
6382 return FAIL;
6383 if (*line != eap->cmd)
6384 return OK;
6385 }
6386 return NOTDONE;
6387}
6388
6389/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006390 * Check if "name" can be "unlet".
6391 */
6392 int
6393check_vim9_unlet(char_u *name)
6394{
6395 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6396 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006397 // "unlet s:var" is allowed in legacy script.
6398 if (*name == 's' && !script_is_vim9())
6399 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006400 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006401 return FAIL;
6402 }
6403 return OK;
6404}
6405
6406/*
6407 * Callback passed to ex_unletlock().
6408 */
6409 static int
6410compile_unlet(
6411 lval_T *lvp,
6412 char_u *name_end,
6413 exarg_T *eap,
6414 int deep UNUSED,
6415 void *coookie)
6416{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006417 cctx_T *cctx = coookie;
6418 char_u *p = lvp->ll_name;
6419 int cc = *name_end;
6420 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006421
Bram Moolenaar752fc692021-01-04 21:57:11 +01006422 if (cctx->ctx_skip == SKIP_YES)
6423 return OK;
6424
6425 *name_end = NUL;
6426 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006427 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006428 // :unlet $ENV_VAR
6429 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6430 }
6431 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6432 {
6433 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006434
Bram Moolenaar752fc692021-01-04 21:57:11 +01006435 // This is similar to assigning: lookup the list/dict, compile the
6436 // idx/key. Then instead of storing the value unlet the item.
6437 // unlet {list}[idx]
6438 // unlet {dict}[key] dict.key
6439 //
6440 // Figure out the LHS type and other properties.
6441 //
6442 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6443
6444 // : unlet an indexed item
6445 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006446 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006447 iemsg("called compile_lhs() without an index");
6448 ret = FAIL;
6449 }
6450 else
6451 {
6452 // Use the info in "lhs" to unlet the item at the index in the
6453 // list or dict.
6454 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006455 }
6456
Bram Moolenaar752fc692021-01-04 21:57:11 +01006457 vim_free(lhs.lhs_name);
6458 }
6459 else if (check_vim9_unlet(p) == FAIL)
6460 {
6461 ret = FAIL;
6462 }
6463 else
6464 {
6465 // Normal name. Only supports g:, w:, t: and b: namespaces.
6466 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006467 }
6468
Bram Moolenaar752fc692021-01-04 21:57:11 +01006469 *name_end = cc;
6470 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006471}
6472
6473/*
6474 * compile "unlet var", "lock var" and "unlock var"
6475 * "arg" points to "var".
6476 */
6477 static char_u *
6478compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6479{
6480 char_u *p = arg;
6481
6482 if (eap->cmdidx != CMD_unlet)
6483 {
6484 emsg("Sorry, :lock and unlock not implemented yet");
6485 return NULL;
6486 }
6487
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006488 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6489 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006490 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6491}
6492
6493/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 * Compile an :import command.
6495 */
6496 static char_u *
6497compile_import(char_u *arg, cctx_T *cctx)
6498{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006499 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006500}
6501
6502/*
6503 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6504 */
6505 static int
6506compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6507{
6508 garray_T *instr = &cctx->ctx_instr;
6509 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6510
6511 if (endlabel == NULL)
6512 return FAIL;
6513 endlabel->el_next = *el;
6514 *el = endlabel;
6515 endlabel->el_end_label = instr->ga_len;
6516
6517 generate_JUMP(cctx, when, 0);
6518 return OK;
6519}
6520
6521 static void
6522compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6523{
6524 garray_T *instr = &cctx->ctx_instr;
6525
6526 while (*el != NULL)
6527 {
6528 endlabel_T *cur = (*el);
6529 isn_T *isn;
6530
6531 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6532 isn->isn_arg.jump.jump_where = instr->ga_len;
6533 *el = cur->el_next;
6534 vim_free(cur);
6535 }
6536}
6537
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006538 static void
6539compile_free_jump_to_end(endlabel_T **el)
6540{
6541 while (*el != NULL)
6542 {
6543 endlabel_T *cur = (*el);
6544
6545 *el = cur->el_next;
6546 vim_free(cur);
6547 }
6548}
6549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550/*
6551 * Create a new scope and set up the generic items.
6552 */
6553 static scope_T *
6554new_scope(cctx_T *cctx, scopetype_T type)
6555{
6556 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6557
6558 if (scope == NULL)
6559 return NULL;
6560 scope->se_outer = cctx->ctx_scope;
6561 cctx->ctx_scope = scope;
6562 scope->se_type = type;
6563 scope->se_local_count = cctx->ctx_locals.ga_len;
6564 return scope;
6565}
6566
6567/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006568 * Free the current scope and go back to the outer scope.
6569 */
6570 static void
6571drop_scope(cctx_T *cctx)
6572{
6573 scope_T *scope = cctx->ctx_scope;
6574
6575 if (scope == NULL)
6576 {
6577 iemsg("calling drop_scope() without a scope");
6578 return;
6579 }
6580 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006581 switch (scope->se_type)
6582 {
6583 case IF_SCOPE:
6584 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6585 case FOR_SCOPE:
6586 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6587 case WHILE_SCOPE:
6588 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6589 case TRY_SCOPE:
6590 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6591 case NO_SCOPE:
6592 case BLOCK_SCOPE:
6593 break;
6594 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006595 vim_free(scope);
6596}
6597
6598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 * compile "if expr"
6600 *
6601 * "if expr" Produces instructions:
6602 * EVAL expr Push result of "expr"
6603 * JUMP_IF_FALSE end
6604 * ... body ...
6605 * end:
6606 *
6607 * "if expr | else" Produces instructions:
6608 * EVAL expr Push result of "expr"
6609 * JUMP_IF_FALSE else
6610 * ... body ...
6611 * JUMP_ALWAYS end
6612 * else:
6613 * ... body ...
6614 * end:
6615 *
6616 * "if expr1 | elseif expr2 | else" Produces instructions:
6617 * EVAL expr Push result of "expr"
6618 * JUMP_IF_FALSE elseif
6619 * ... body ...
6620 * JUMP_ALWAYS end
6621 * elseif:
6622 * EVAL expr Push result of "expr"
6623 * JUMP_IF_FALSE else
6624 * ... body ...
6625 * JUMP_ALWAYS end
6626 * else:
6627 * ... body ...
6628 * end:
6629 */
6630 static char_u *
6631compile_if(char_u *arg, cctx_T *cctx)
6632{
6633 char_u *p = arg;
6634 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006635 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006637 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006638 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639
Bram Moolenaara5565e42020-05-09 15:44:01 +02006640 CLEAR_FIELD(ppconst);
6641 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006642 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006643 clear_ppconst(&ppconst);
6644 return NULL;
6645 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006646 if (cctx->ctx_skip == SKIP_YES)
6647 clear_ppconst(&ppconst);
6648 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006649 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006650 int error = FALSE;
6651 int v;
6652
Bram Moolenaara5565e42020-05-09 15:44:01 +02006653 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006654 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006655 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006656 if (error)
6657 return NULL;
6658 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006659 }
6660 else
6661 {
6662 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006663 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006664 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006665 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006666 if (bool_on_stack(cctx) == FAIL)
6667 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669
6670 scope = new_scope(cctx, IF_SCOPE);
6671 if (scope == NULL)
6672 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006673 scope->se_skip_save = skip_save;
6674 // "is_had_return" will be reset if any block does not end in :return
6675 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006677 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006678 {
6679 // "where" is set when ":elseif", "else" or ":endif" is found
6680 scope->se_u.se_if.is_if_label = instr->ga_len;
6681 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6682 }
6683 else
6684 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685
6686 return p;
6687}
6688
6689 static char_u *
6690compile_elseif(char_u *arg, cctx_T *cctx)
6691{
6692 char_u *p = arg;
6693 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006694 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 isn_T *isn;
6696 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006697 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006698 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699
6700 if (scope == NULL || scope->se_type != IF_SCOPE)
6701 {
6702 emsg(_(e_elseif_without_if));
6703 return NULL;
6704 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006705 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006706 if (!cctx->ctx_had_return)
6707 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006709 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006710 {
6711 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006713 return NULL;
6714 // previous "if" or "elseif" jumps here
6715 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6716 isn->isn_arg.jump.jump_where = instr->ga_len;
6717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006719 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006720 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006721 if (cctx->ctx_skip == SKIP_YES)
6722 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006723 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006724 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006725 clear_ppconst(&ppconst);
6726 return NULL;
6727 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006728 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006729 if (scope->se_skip_save == SKIP_YES)
6730 clear_ppconst(&ppconst);
6731 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006732 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006733 int error = FALSE;
6734 int v;
6735
Bram Moolenaar7f141552020-05-09 17:35:53 +02006736 // The expression results in a constant.
6737 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006738 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6739 if (error)
6740 return NULL;
6741 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006742 clear_ppconst(&ppconst);
6743 scope->se_u.se_if.is_if_label = -1;
6744 }
6745 else
6746 {
6747 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006748 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006749 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006750 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006751 if (bool_on_stack(cctx) == FAIL)
6752 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006754 // "where" is set when ":elseif", "else" or ":endif" is found
6755 scope->se_u.se_if.is_if_label = instr->ga_len;
6756 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758
6759 return p;
6760}
6761
6762 static char_u *
6763compile_else(char_u *arg, cctx_T *cctx)
6764{
6765 char_u *p = arg;
6766 garray_T *instr = &cctx->ctx_instr;
6767 isn_T *isn;
6768 scope_T *scope = cctx->ctx_scope;
6769
6770 if (scope == NULL || scope->se_type != IF_SCOPE)
6771 {
6772 emsg(_(e_else_without_if));
6773 return NULL;
6774 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006775 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006776 if (!cctx->ctx_had_return)
6777 scope->se_u.se_if.is_had_return = FALSE;
6778 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779
Bram Moolenaarefd88552020-06-18 20:50:10 +02006780 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006781 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006782 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006783 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006784 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006785 if (!cctx->ctx_had_return
6786 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6787 JUMP_ALWAYS, cctx) == FAIL)
6788 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006789 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006790
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006791 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006792 {
6793 if (scope->se_u.se_if.is_if_label >= 0)
6794 {
6795 // previous "if" or "elseif" jumps here
6796 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6797 isn->isn_arg.jump.jump_where = instr->ga_len;
6798 scope->se_u.se_if.is_if_label = -1;
6799 }
6800 }
6801
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006802 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006803 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
6806 return p;
6807}
6808
6809 static char_u *
6810compile_endif(char_u *arg, cctx_T *cctx)
6811{
6812 scope_T *scope = cctx->ctx_scope;
6813 ifscope_T *ifscope;
6814 garray_T *instr = &cctx->ctx_instr;
6815 isn_T *isn;
6816
6817 if (scope == NULL || scope->se_type != IF_SCOPE)
6818 {
6819 emsg(_(e_endif_without_if));
6820 return NULL;
6821 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006822 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006823 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006824 if (!cctx->ctx_had_return)
6825 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006827 if (scope->se_u.se_if.is_if_label >= 0)
6828 {
6829 // previous "if" or "elseif" jumps here
6830 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6831 isn->isn_arg.jump.jump_where = instr->ga_len;
6832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 // Fill in the "end" label in jumps at the end of the blocks.
6834 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006835 cctx->ctx_skip = scope->se_skip_save;
6836
6837 // If all the blocks end in :return and there is an :else then the
6838 // had_return flag is set.
6839 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006841 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 return arg;
6843}
6844
6845/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006846 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 *
6848 * Produces instructions:
6849 * PUSHNR -1
6850 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006851 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6853 * - if beyond end, jump to "end"
6854 * - otherwise get item from list and push it
6855 * STORE var Store item in "var"
6856 * ... body ...
6857 * JUMP top Jump back to repeat
6858 * end: DROP Drop the result of "expr"
6859 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006860 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6861 * UNPACK 2 Split item in 2
6862 * STORE var1 Store item in "var1"
6863 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864 */
6865 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006866compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006868 char_u *arg;
6869 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006870 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006872 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006873 int var_count = 0;
6874 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 size_t varlen;
6876 garray_T *instr = &cctx->ctx_instr;
6877 garray_T *stack = &cctx->ctx_type_stack;
6878 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006879 lvar_T *loop_lvar; // loop iteration variable
6880 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006882 type_T *item_type = &t_any;
6883 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884
Bram Moolenaar792f7862020-11-23 08:31:18 +01006885 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01006886 if (p == NULL)
6887 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006888 if (var_count == 0)
6889 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890
6891 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006892 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006893 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6894 return NULL;
6895 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 {
6897 emsg(_(e_missing_in));
6898 return NULL;
6899 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006900 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006901 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6902 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 scope = new_scope(cctx, FOR_SCOPE);
6905 if (scope == NULL)
6906 return NULL;
6907
Bram Moolenaar792f7862020-11-23 08:31:18 +01006908 // Reserve a variable to store the loop iteration counter and initialize it
6909 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006910 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6911 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006912 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006913 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006914 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006916 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006917 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918
6919 // compile "expr", it remains on the stack until "endfor"
6920 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006921 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006922 {
6923 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006925 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006926 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006928 // Now that we know the type of "var", check that it is a list, now or at
6929 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01006931 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006933 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 return NULL;
6935 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006936
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006937 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006938 {
6939 if (var_count == 1)
6940 item_type = vartype->tt_member;
6941 else if (vartype->tt_member->tt_type == VAR_LIST
6942 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6943 item_type = vartype->tt_member->tt_member;
6944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945
6946 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006947 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006948 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949
Bram Moolenaar792f7862020-11-23 08:31:18 +01006950 arg = arg_start;
6951 if (var_count > 1)
6952 {
6953 generate_UNPACK(cctx, var_count, semicolon);
6954 arg = skipwhite(arg + 1); // skip white after '['
6955
6956 // the list item is replaced by a number of items
6957 if (ga_grow(stack, var_count - 1) == FAIL)
6958 {
6959 drop_scope(cctx);
6960 return NULL;
6961 }
6962 --stack->ga_len;
6963 for (idx = 0; idx < var_count; ++idx)
6964 {
6965 ((type_T **)stack->ga_data)[stack->ga_len] =
6966 (semicolon && idx == 0) ? vartype : item_type;
6967 ++stack->ga_len;
6968 }
6969 }
6970
6971 for (idx = 0; idx < var_count; ++idx)
6972 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006973 assign_dest_T dest = dest_local;
6974 int opt_flags = 0;
6975 int vimvaridx = -1;
6976 type_T *type = &t_any;
6977
6978 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006979 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006980 name = vim_strnsave(arg, varlen);
6981 if (name == NULL)
6982 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006983
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006984 // TODO: script var not supported?
6985 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6986 &vimvaridx, &type, cctx) == FAIL)
6987 goto failed;
6988 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006989 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006990 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6991 0, 0, type, name) == FAIL)
6992 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006993 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006994 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006995 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006996 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006997 {
6998 semsg(_(e_variable_already_declared), arg);
6999 goto failed;
7000 }
7001
Bram Moolenaarea870692020-12-02 14:24:30 +01007002 if (STRNCMP(name, "s:", 2) == 0)
7003 {
7004 semsg(_(e_cannot_declare_script_variable_in_function), name);
7005 goto failed;
7006 }
7007
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007008 // Reserve a variable to store "var".
7009 // TODO: check for type
7010 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7011 if (var_lvar == NULL)
7012 // out of memory or used as an argument
7013 goto failed;
7014
7015 if (semicolon && idx == var_count - 1)
7016 var_lvar->lv_type = vartype;
7017 else
7018 var_lvar->lv_type = item_type;
7019 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7020 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007021
Bram Moolenaar036d0712021-01-17 20:23:38 +01007022 if (*p == ':')
7023 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007024 if (*p == ',' || *p == ';')
7025 ++p;
7026 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007027 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007028 }
7029
7030 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007031
7032failed:
7033 vim_free(name);
7034 drop_scope(cctx);
7035 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036}
7037
7038/*
7039 * compile "endfor"
7040 */
7041 static char_u *
7042compile_endfor(char_u *arg, cctx_T *cctx)
7043{
7044 garray_T *instr = &cctx->ctx_instr;
7045 scope_T *scope = cctx->ctx_scope;
7046 forscope_T *forscope;
7047 isn_T *isn;
7048
7049 if (scope == NULL || scope->se_type != FOR_SCOPE)
7050 {
7051 emsg(_(e_for));
7052 return NULL;
7053 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007054 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007056 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057
7058 // At end of ":for" scope jump back to the FOR instruction.
7059 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7060
7061 // Fill in the "end" label in the FOR statement so it can jump here
7062 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7063 isn->isn_arg.forloop.for_end = instr->ga_len;
7064
7065 // Fill in the "end" label any BREAK statements
7066 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7067
7068 // Below the ":for" scope drop the "expr" list from the stack.
7069 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7070 return NULL;
7071
7072 vim_free(scope);
7073
7074 return arg;
7075}
7076
7077/*
7078 * compile "while expr"
7079 *
7080 * Produces instructions:
7081 * top: EVAL expr Push result of "expr"
7082 * JUMP_IF_FALSE end jump if false
7083 * ... body ...
7084 * JUMP top Jump back to repeat
7085 * end:
7086 *
7087 */
7088 static char_u *
7089compile_while(char_u *arg, cctx_T *cctx)
7090{
7091 char_u *p = arg;
7092 garray_T *instr = &cctx->ctx_instr;
7093 scope_T *scope;
7094
7095 scope = new_scope(cctx, WHILE_SCOPE);
7096 if (scope == NULL)
7097 return NULL;
7098
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007099 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100
7101 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007102 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 return NULL;
7104
Bram Moolenaar13106602020-10-04 16:06:05 +02007105 if (bool_on_stack(cctx) == FAIL)
7106 return FAIL;
7107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007109 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 JUMP_IF_FALSE, cctx) == FAIL)
7111 return FAIL;
7112
7113 return p;
7114}
7115
7116/*
7117 * compile "endwhile"
7118 */
7119 static char_u *
7120compile_endwhile(char_u *arg, cctx_T *cctx)
7121{
7122 scope_T *scope = cctx->ctx_scope;
7123
7124 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7125 {
7126 emsg(_(e_while));
7127 return NULL;
7128 }
7129 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007130 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131
7132 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007133 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134
7135 // Fill in the "end" label in the WHILE statement so it can jump here.
7136 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007137 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138
7139 vim_free(scope);
7140
7141 return arg;
7142}
7143
7144/*
7145 * compile "continue"
7146 */
7147 static char_u *
7148compile_continue(char_u *arg, cctx_T *cctx)
7149{
7150 scope_T *scope = cctx->ctx_scope;
7151
7152 for (;;)
7153 {
7154 if (scope == NULL)
7155 {
7156 emsg(_(e_continue));
7157 return NULL;
7158 }
7159 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7160 break;
7161 scope = scope->se_outer;
7162 }
7163
7164 // Jump back to the FOR or WHILE instruction.
7165 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007166 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7167 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 return arg;
7169}
7170
7171/*
7172 * compile "break"
7173 */
7174 static char_u *
7175compile_break(char_u *arg, cctx_T *cctx)
7176{
7177 scope_T *scope = cctx->ctx_scope;
7178 endlabel_T **el;
7179
7180 for (;;)
7181 {
7182 if (scope == NULL)
7183 {
7184 emsg(_(e_break));
7185 return NULL;
7186 }
7187 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7188 break;
7189 scope = scope->se_outer;
7190 }
7191
7192 // Jump to the end of the FOR or WHILE loop.
7193 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007194 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007196 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7198 return FAIL;
7199
7200 return arg;
7201}
7202
7203/*
7204 * compile "{" start of block
7205 */
7206 static char_u *
7207compile_block(char_u *arg, cctx_T *cctx)
7208{
7209 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7210 return NULL;
7211 return skipwhite(arg + 1);
7212}
7213
7214/*
7215 * compile end of block: drop one scope
7216 */
7217 static void
7218compile_endblock(cctx_T *cctx)
7219{
7220 scope_T *scope = cctx->ctx_scope;
7221
7222 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007223 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224 vim_free(scope);
7225}
7226
7227/*
7228 * compile "try"
7229 * Creates a new scope for the try-endtry, pointing to the first catch and
7230 * finally.
7231 * Creates another scope for the "try" block itself.
7232 * TRY instruction sets up exception handling at runtime.
7233 *
7234 * "try"
7235 * TRY -> catch1, -> finally push trystack entry
7236 * ... try block
7237 * "throw {exception}"
7238 * EVAL {exception}
7239 * THROW create exception
7240 * ... try block
7241 * " catch {expr}"
7242 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007243 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 * EVAL {expr}
7245 * MATCH
7246 * JUMP nomatch -> catch2
7247 * CATCH remove exception
7248 * ... catch block
7249 * " catch"
7250 * JUMP -> finally
7251 * catch2: CATCH remove exception
7252 * ... catch block
7253 * " finally"
7254 * finally:
7255 * ... finally block
7256 * " endtry"
7257 * ENDTRY pop trystack entry, may rethrow
7258 */
7259 static char_u *
7260compile_try(char_u *arg, cctx_T *cctx)
7261{
7262 garray_T *instr = &cctx->ctx_instr;
7263 scope_T *try_scope;
7264 scope_T *scope;
7265
7266 // scope that holds the jumps that go to catch/finally/endtry
7267 try_scope = new_scope(cctx, TRY_SCOPE);
7268 if (try_scope == NULL)
7269 return NULL;
7270
Bram Moolenaar69f70502021-01-01 16:10:46 +01007271 if (cctx->ctx_skip != SKIP_YES)
7272 {
7273 // "catch" is set when the first ":catch" is found.
7274 // "finally" is set when ":finally" or ":endtry" is found
7275 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7276 if (generate_instr(cctx, ISN_TRY) == NULL)
7277 return NULL;
7278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007279
7280 // scope for the try block itself
7281 scope = new_scope(cctx, BLOCK_SCOPE);
7282 if (scope == NULL)
7283 return NULL;
7284
7285 return arg;
7286}
7287
7288/*
7289 * compile "catch {expr}"
7290 */
7291 static char_u *
7292compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7293{
7294 scope_T *scope = cctx->ctx_scope;
7295 garray_T *instr = &cctx->ctx_instr;
7296 char_u *p;
7297 isn_T *isn;
7298
7299 // end block scope from :try or :catch
7300 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7301 compile_endblock(cctx);
7302 scope = cctx->ctx_scope;
7303
7304 // Error if not in a :try scope
7305 if (scope == NULL || scope->se_type != TRY_SCOPE)
7306 {
7307 emsg(_(e_catch));
7308 return NULL;
7309 }
7310
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007311 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007313 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 return NULL;
7315 }
7316
Bram Moolenaar69f70502021-01-01 16:10:46 +01007317 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007319 // Jump from end of previous block to :finally or :endtry
7320 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7321 JUMP_ALWAYS, cctx) == FAIL)
7322 return NULL;
7323
7324 // End :try or :catch scope: set value in ISN_TRY instruction
7325 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7326 if (isn->isn_arg.try.try_catch == 0)
7327 isn->isn_arg.try.try_catch = instr->ga_len;
7328 if (scope->se_u.se_try.ts_catch_label != 0)
7329 {
7330 // Previous catch without match jumps here
7331 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7332 isn->isn_arg.jump.jump_where = instr->ga_len;
7333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 }
7335
7336 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007337 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007339 scope->se_u.se_try.ts_caught_all = TRUE;
7340 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 }
7342 else
7343 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007344 char_u *end;
7345 char_u *pat;
7346 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007347 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007348 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 // Push v:exception, push {expr} and MATCH
7351 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7352
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007353 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007354 if (*end != *p)
7355 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007356 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007357 vim_free(tofree);
7358 return FAIL;
7359 }
7360 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007361 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007362 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007363 len = (int)(end - tofree);
7364 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007365 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007366 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007367 if (pat == NULL)
7368 return FAIL;
7369 if (generate_PUSHS(cctx, pat) == FAIL)
7370 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7373 return NULL;
7374
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007375 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7377 return NULL;
7378 }
7379
Bram Moolenaar69f70502021-01-01 16:10:46 +01007380 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 return NULL;
7382
7383 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7384 return NULL;
7385 return p;
7386}
7387
7388 static char_u *
7389compile_finally(char_u *arg, cctx_T *cctx)
7390{
7391 scope_T *scope = cctx->ctx_scope;
7392 garray_T *instr = &cctx->ctx_instr;
7393 isn_T *isn;
7394
7395 // end block scope from :try or :catch
7396 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7397 compile_endblock(cctx);
7398 scope = cctx->ctx_scope;
7399
7400 // Error if not in a :try scope
7401 if (scope == NULL || scope->se_type != TRY_SCOPE)
7402 {
7403 emsg(_(e_finally));
7404 return NULL;
7405 }
7406
7407 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007408 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 if (isn->isn_arg.try.try_finally != 0)
7410 {
7411 emsg(_(e_finally_dup));
7412 return NULL;
7413 }
7414
7415 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007416 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417
Bram Moolenaar585fea72020-04-02 22:33:21 +02007418 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007419 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 {
7421 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007422 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007424 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 }
7426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 // TODO: set index in ts_finally_label jumps
7428
7429 return arg;
7430}
7431
7432 static char_u *
7433compile_endtry(char_u *arg, cctx_T *cctx)
7434{
7435 scope_T *scope = cctx->ctx_scope;
7436 garray_T *instr = &cctx->ctx_instr;
7437 isn_T *isn;
7438
7439 // end block scope from :catch or :finally
7440 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7441 compile_endblock(cctx);
7442 scope = cctx->ctx_scope;
7443
7444 // Error if not in a :try scope
7445 if (scope == NULL || scope->se_type != TRY_SCOPE)
7446 {
7447 if (scope == NULL)
7448 emsg(_(e_no_endtry));
7449 else if (scope->se_type == WHILE_SCOPE)
7450 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007451 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 emsg(_(e_endfor));
7453 else
7454 emsg(_(e_endif));
7455 return NULL;
7456 }
7457
Bram Moolenaar69f70502021-01-01 16:10:46 +01007458 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007460 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7461 if (isn->isn_arg.try.try_catch == 0
7462 && isn->isn_arg.try.try_finally == 0)
7463 {
7464 emsg(_(e_missing_catch_or_finally));
7465 return NULL;
7466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467
Bram Moolenaar69f70502021-01-01 16:10:46 +01007468 // Fill in the "end" label in jumps at the end of the blocks, if not
7469 // done by ":finally".
7470 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471
Bram Moolenaar69f70502021-01-01 16:10:46 +01007472 // End :catch or :finally scope: set value in ISN_TRY instruction
7473 if (isn->isn_arg.try.try_catch == 0)
7474 isn->isn_arg.try.try_catch = instr->ga_len;
7475 if (isn->isn_arg.try.try_finally == 0)
7476 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007477
Bram Moolenaar69f70502021-01-01 16:10:46 +01007478 if (scope->se_u.se_try.ts_catch_label != 0)
7479 {
7480 // Last catch without match jumps here
7481 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7482 isn->isn_arg.jump.jump_where = instr->ga_len;
7483 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007484 }
7485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 compile_endblock(cctx);
7487
Bram Moolenaar69f70502021-01-01 16:10:46 +01007488 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 return NULL;
7490 return arg;
7491}
7492
7493/*
7494 * compile "throw {expr}"
7495 */
7496 static char_u *
7497compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7498{
7499 char_u *p = skipwhite(arg);
7500
Bram Moolenaara5565e42020-05-09 15:44:01 +02007501 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007503 if (cctx->ctx_skip == SKIP_YES)
7504 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 if (may_generate_2STRING(-1, cctx) == FAIL)
7506 return NULL;
7507 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7508 return NULL;
7509
7510 return p;
7511}
7512
7513/*
7514 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007515 * compile "echomsg expr"
7516 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007517 * compile "execute expr"
7518 */
7519 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007520compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007521{
7522 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007523 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007524 int count = 0;
7525
7526 for (;;)
7527 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007528 if (ends_excmd2(prev, p))
7529 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007530 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007531 return NULL;
7532 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007533 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007534 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007535 }
7536
Bram Moolenaare4984292020-12-13 14:19:25 +01007537 if (count > 0)
7538 {
7539 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7540 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7541 else if (cmdidx == CMD_execute)
7542 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7543 else if (cmdidx == CMD_echomsg)
7544 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7545 else
7546 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 return p;
7549}
7550
7551/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007552 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007553 * instruction to compute it and return OK.
7554 * Otherwise return FAIL, the caller must deal with any range.
7555 */
7556 static int
7557compile_variable_range(exarg_T *eap, cctx_T *cctx)
7558{
7559 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7560 char_u *p = skipdigits(eap->cmd);
7561
7562 if (p == range_end)
7563 return FAIL;
7564 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7565}
7566
7567/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007568 * :put r
7569 * :put ={expr}
7570 */
7571 static char_u *
7572compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7573{
7574 char_u *line = arg;
7575 linenr_T lnum;
7576 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007577 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007578
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007579 eap->regname = *line;
7580
7581 if (eap->regname == '=')
7582 {
7583 char_u *p = line + 1;
7584
7585 if (compile_expr0(&p, cctx) == FAIL)
7586 return NULL;
7587 line = p;
7588 }
7589 else if (eap->regname != NUL)
7590 ++line;
7591
Bram Moolenaar08597872020-12-10 19:43:40 +01007592 if (compile_variable_range(eap, cctx) == OK)
7593 {
7594 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7595 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007596 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007597 {
7598 // Either no range or a number.
7599 // "errormsg" will not be set because the range is ADDR_LINES.
7600 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007601 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007602 return NULL;
7603 if (eap->addr_count == 0)
7604 lnum = -1;
7605 else
7606 lnum = eap->line2;
7607 if (above)
7608 --lnum;
7609 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007610
7611 generate_PUT(cctx, eap->regname, lnum);
7612 return line;
7613}
7614
7615/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007616 * A command that is not compiled, execute with legacy code.
7617 */
7618 static char_u *
7619compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7620{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007621 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007622 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007623 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007624
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007625 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007626 goto theend;
7627
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007628 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007629 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007630 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007631 int usefilter = FALSE;
7632
7633 has_expr = argt & (EX_XFILE | EX_EXPAND);
7634
7635 // If the command can be followed by a bar, find the bar and truncate
7636 // it, so that the following command can be compiled.
7637 // The '|' is overwritten with a NUL, it is put back below.
7638 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7639 && *eap->arg == '!')
7640 // :w !filter or :r !filter or :r! filter
7641 usefilter = TRUE;
7642 if ((argt & EX_TRLBAR) && !usefilter)
7643 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007644 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007645 separate_nextcmd(eap);
7646 if (eap->nextcmd != NULL)
7647 nextcmd = eap->nextcmd;
7648 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007649 else if (eap->cmdidx == CMD_wincmd)
7650 {
7651 p = eap->arg;
7652 if (*p != NUL)
7653 ++p;
7654 if (*p == 'g' || *p == Ctrl_G)
7655 ++p;
7656 p = skipwhite(p);
7657 if (*p == '|')
7658 {
7659 *p = NUL;
7660 nextcmd = p + 1;
7661 }
7662 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007663 }
7664
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007665 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7666 {
7667 // expand filename in "syntax include [@group] filename"
7668 has_expr = TRUE;
7669 eap->arg = skipwhite(eap->arg + 7);
7670 if (*eap->arg == '@')
7671 eap->arg = skiptowhite(eap->arg);
7672 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007673
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007674 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7675 && STRLEN(eap->arg) > 4)
7676 {
7677 int delim = *eap->arg;
7678
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007679 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007680 if (*p == delim)
7681 {
7682 eap->arg = p + 1;
7683 has_expr = TRUE;
7684 }
7685 }
7686
Bram Moolenaarecac5912021-01-05 19:23:28 +01007687 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7688 {
7689 // TODO: should only expand when appropriate for the command
7690 eap->arg = skiptowhite(eap->arg);
7691 has_expr = TRUE;
7692 }
7693
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007694 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007695 {
7696 int count = 0;
7697 char_u *start = skipwhite(line);
7698
7699 // :cmd xxx`=expr1`yyy`=expr2`zzz
7700 // PUSHS ":cmd xxx"
7701 // eval expr1
7702 // PUSHS "yyy"
7703 // eval expr2
7704 // PUSHS "zzz"
7705 // EXECCONCAT 5
7706 for (;;)
7707 {
7708 if (p > start)
7709 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007710 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007711 ++count;
7712 }
7713 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007714 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007715 return NULL;
7716 may_generate_2STRING(-1, cctx);
7717 ++count;
7718 p = skipwhite(p);
7719 if (*p != '`')
7720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007721 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007722 return NULL;
7723 }
7724 start = p + 1;
7725
7726 p = (char_u *)strstr((char *)start, "`=");
7727 if (p == NULL)
7728 {
7729 if (*skipwhite(start) != NUL)
7730 {
7731 generate_PUSHS(cctx, vim_strsave(start));
7732 ++count;
7733 }
7734 break;
7735 }
7736 }
7737 generate_EXECCONCAT(cctx, count);
7738 }
7739 else
7740 generate_EXEC(cctx, line);
7741
7742theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007743 if (*nextcmd != NUL)
7744 {
7745 // the parser expects a pointer to the bar, put it back
7746 --nextcmd;
7747 *nextcmd = '|';
7748 }
7749
7750 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007751}
7752
7753/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007754 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007755 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007756 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007757 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007758add_def_function(ufunc_T *ufunc)
7759{
7760 dfunc_T *dfunc;
7761
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007762 if (def_functions.ga_len == 0)
7763 {
7764 // The first position is not used, so that a zero uf_dfunc_idx means it
7765 // wasn't set.
7766 if (ga_grow(&def_functions, 1) == FAIL)
7767 return FAIL;
7768 ++def_functions.ga_len;
7769 }
7770
Bram Moolenaar09689a02020-05-09 22:50:08 +02007771 // Add the function to "def_functions".
7772 if (ga_grow(&def_functions, 1) == FAIL)
7773 return FAIL;
7774 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7775 CLEAR_POINTER(dfunc);
7776 dfunc->df_idx = def_functions.ga_len;
7777 ufunc->uf_dfunc_idx = dfunc->df_idx;
7778 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007779 dfunc->df_name = vim_strsave(ufunc->uf_name);
7780 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007781 ++def_functions.ga_len;
7782 return OK;
7783}
7784
7785/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007786 * After ex_function() has collected all the function lines: parse and compile
7787 * the lines into instructions.
7788 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007789 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7790 * the return statement (used for lambda). When uf_ret_type is already set
7791 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007792 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007793 * This can be used recursively through compile_lambda(), which may reallocate
7794 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007795 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007797 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007798compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007800 char_u *line = NULL;
7801 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 cctx_T cctx;
7804 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007805 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 int ret = FAIL;
7807 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007808 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007809 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007810 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007812 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007813 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007814 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007816 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7817 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007818 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007820 else
7821 {
7822 if (add_def_function(ufunc) == FAIL)
7823 return FAIL;
7824 new_def_function = TRUE;
7825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826
Bram Moolenaar985116a2020-07-12 17:31:09 +02007827 ufunc->uf_def_status = UF_COMPILING;
7828
Bram Moolenaara80faa82020-04-12 19:37:17 +02007829 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 cctx.ctx_ufunc = ufunc;
7831 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007832 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7834 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7835 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7836 cctx.ctx_type_list = &ufunc->uf_type_list;
7837 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7838 instr = &cctx.ctx_instr;
7839
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007840 // Set the context to the function, it may be compiled when called from
7841 // another script. Set the script version to the most modern one.
7842 // The line number will be set in next_line_from_context().
7843 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7845
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007846 // Make sure error messages are OK.
7847 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7848 if (do_estack_push)
7849 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007850 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007851
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007852 if (ufunc->uf_def_args.ga_len > 0)
7853 {
7854 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007855 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007856 int i;
7857 char_u *arg;
7858 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007859 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007860
7861 // Produce instructions for the default values of optional arguments.
7862 // Store the instruction index in uf_def_arg_idx[] so that we know
7863 // where to start when the function is called, depending on the number
7864 // of arguments.
7865 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7866 if (ufunc->uf_def_arg_idx == NULL)
7867 goto erret;
7868 for (i = 0; i < count; ++i)
7869 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007870 garray_T *stack = &cctx.ctx_type_stack;
7871 type_T *val_type;
7872 int arg_idx = first_def_arg + i;
7873
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007874 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7875 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007876 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007877 goto erret;
7878
7879 // If no type specified use the type of the default value.
7880 // Otherwise check that the default value type matches the
7881 // specified type.
7882 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7883 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007884 {
7885 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007886 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007887 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007888 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7889 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007890 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007891
7892 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007893 goto erret;
7894 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007895 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007896
7897 if (did_set_arg_type)
7898 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007899 }
7900
7901 /*
7902 * Loop over all the lines of the function and generate instructions.
7903 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904 for (;;)
7905 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007906 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007907 int starts_with_colon = FALSE;
7908 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007909 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007910
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007911 // Bail out on the first error to avoid a flood of errors and report
7912 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007913 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007914 goto erret;
7915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916 if (line != NULL && *line == '|')
7917 // the line continues after a '|'
7918 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007919 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007920 && !(*line == '#' && (line == cctx.ctx_line_start
7921 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007923 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007924 goto erret;
7925 }
7926 else
7927 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007928 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007929 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007930 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 }
7933
Bram Moolenaara80faa82020-04-12 19:37:17 +02007934 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 ea.cmdlinep = &line;
7936 ea.cmd = skipwhite(line);
7937
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007938 // Some things can be recognized by the first character.
7939 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007941 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007942 // "#" starts a comment
7943 line = (char_u *)"";
7944 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007946 case '}':
7947 {
7948 // "}" ends a block scope
7949 scopetype_T stype = cctx.ctx_scope == NULL
7950 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007951
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007952 if (stype == BLOCK_SCOPE)
7953 {
7954 compile_endblock(&cctx);
7955 line = ea.cmd;
7956 }
7957 else
7958 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007959 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007960 goto erret;
7961 }
7962 if (line != NULL)
7963 line = skipwhite(ea.cmd + 1);
7964 continue;
7965 }
7966
7967 case '{':
7968 // "{" starts a block scope
7969 // "{'a': 1}->func() is something else
7970 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7971 {
7972 line = compile_block(ea.cmd, &cctx);
7973 continue;
7974 }
7975 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007976 }
7977
7978 /*
7979 * COMMAND MODIFIERS
7980 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007981 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007982 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7983 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007984 {
7985 if (errormsg != NULL)
7986 goto erret;
7987 // empty line or comment
7988 line = (char_u *)"";
7989 continue;
7990 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007991 generate_cmdmods(&cctx, &local_cmdmod);
7992 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007994 // Check if there was a colon after the last command modifier or before
7995 // the current position.
7996 for (p = ea.cmd; p >= line; --p)
7997 {
7998 if (*p == ':')
7999 starts_with_colon = TRUE;
8000 if (p < ea.cmd && !VIM_ISWHITE(*p))
8001 break;
8002 }
8003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008005 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008006 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008007 {
8008 if (*ea.cmd == '(')
8009 // not for "call()"
8010 ea.cmd = p;
8011 else
8012 ea.cmd = skipwhite(ea.cmd);
8013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008015 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008016 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008017 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008018
Bram Moolenaar17126b12021-01-07 22:03:02 +01008019 // Check for assignment after command modifiers.
8020 assign = may_compile_assignment(&ea, &line, &cctx);
8021 if (assign == OK)
8022 goto nextline;
8023 if (assign == FAIL)
8024 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008025 }
8026
8027 /*
8028 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008029 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008031 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008032 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008033 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008034 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008035 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008036 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008037 if (!starts_with_colon)
8038 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008039 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008040 goto erret;
8041 }
8042 if (ends_excmd2(line, ea.cmd))
8043 {
8044 // A range without a command: jump to the line.
8045 // TODO: compile to a more efficient command, possibly
8046 // calling parse_cmd_address().
8047 ea.cmdidx = CMD_SIZE;
8048 line = compile_exec(line, &ea, &cctx);
8049 goto nextline;
8050 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008051 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008052 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008053 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008054 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008055 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008056
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008057 if (p == NULL)
8058 {
8059 if (cctx.ctx_skip != SKIP_YES)
8060 emsg(_(e_ambiguous_use_of_user_defined_command));
8061 goto erret;
8062 }
8063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8065 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008066 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008067 {
8068 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008069 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008070 }
8071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008072 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008073 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008075 // CMD_var cannot happen, compile_assignment() above would be
8076 // used. Most likely an assignment to a non-existing variable.
8077 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008078 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080 }
8081
Bram Moolenaar3988f642020-08-27 22:43:03 +02008082 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008083 && ea.cmdidx != CMD_elseif
8084 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008085 && ea.cmdidx != CMD_endif
8086 && ea.cmdidx != CMD_endfor
8087 && ea.cmdidx != CMD_endwhile
8088 && ea.cmdidx != CMD_catch
8089 && ea.cmdidx != CMD_finally
8090 && ea.cmdidx != CMD_endtry)
8091 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008092 emsg(_(e_unreachable_code_after_return));
8093 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008094 }
8095
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008096 p = skipwhite(p);
8097 if (ea.cmdidx != CMD_SIZE
8098 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8099 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008100 if (ea.cmdidx >= 0)
8101 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008102 if ((ea.argt & EX_BANG) && *p == '!')
8103 {
8104 ea.forceit = TRUE;
8105 p = skipwhite(p + 1);
8106 }
8107 }
8108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109 switch (ea.cmdidx)
8110 {
8111 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008112 ea.arg = p;
8113 line = compile_nested_function(&ea, &cctx);
8114 break;
8115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008117 // TODO: should we allow this, e.g. to declare a global
8118 // function?
8119 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 goto erret;
8121
8122 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008123 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008124 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008125 break;
8126
8127 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008128 emsg(_(e_cannot_use_let_in_vim9_script));
8129 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008130 case CMD_var:
8131 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008132 case CMD_const:
8133 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008134 if (line == p)
8135 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136 break;
8137
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008138 case CMD_unlet:
8139 case CMD_unlockvar:
8140 case CMD_lockvar:
8141 line = compile_unletlock(p, &ea, &cctx);
8142 break;
8143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144 case CMD_import:
8145 line = compile_import(p, &cctx);
8146 break;
8147
8148 case CMD_if:
8149 line = compile_if(p, &cctx);
8150 break;
8151 case CMD_elseif:
8152 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008153 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 break;
8155 case CMD_else:
8156 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008157 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 break;
8159 case CMD_endif:
8160 line = compile_endif(p, &cctx);
8161 break;
8162
8163 case CMD_while:
8164 line = compile_while(p, &cctx);
8165 break;
8166 case CMD_endwhile:
8167 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008168 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169 break;
8170
8171 case CMD_for:
8172 line = compile_for(p, &cctx);
8173 break;
8174 case CMD_endfor:
8175 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008176 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 break;
8178 case CMD_continue:
8179 line = compile_continue(p, &cctx);
8180 break;
8181 case CMD_break:
8182 line = compile_break(p, &cctx);
8183 break;
8184
8185 case CMD_try:
8186 line = compile_try(p, &cctx);
8187 break;
8188 case CMD_catch:
8189 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008190 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191 break;
8192 case CMD_finally:
8193 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008194 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008195 break;
8196 case CMD_endtry:
8197 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008198 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199 break;
8200 case CMD_throw:
8201 line = compile_throw(p, &cctx);
8202 break;
8203
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008204 case CMD_eval:
8205 if (compile_expr0(&p, &cctx) == FAIL)
8206 goto erret;
8207
Bram Moolenaar3988f642020-08-27 22:43:03 +02008208 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008209 generate_instr_drop(&cctx, ISN_DROP, 1);
8210
8211 line = skipwhite(p);
8212 break;
8213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008215 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008216 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008217 case CMD_echomsg:
8218 case CMD_echoerr:
8219 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008220 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008222 case CMD_put:
8223 ea.cmd = cmd;
8224 line = compile_put(p, &ea, &cctx);
8225 break;
8226
Bram Moolenaar3988f642020-08-27 22:43:03 +02008227 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008228
Bram Moolenaarae616492020-07-28 20:07:27 +02008229 case CMD_append:
8230 case CMD_change:
8231 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008232 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008233 case CMD_xit:
8234 not_in_vim9(&ea);
8235 goto erret;
8236
Bram Moolenaar002262f2020-07-08 17:47:57 +02008237 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008238 if (cctx.ctx_skip != SKIP_YES)
8239 {
8240 semsg(_(e_invalid_command_str), ea.cmd);
8241 goto erret;
8242 }
8243 // We don't check for a next command here.
8244 line = (char_u *)"";
8245 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008248 if (cctx.ctx_skip == SKIP_YES)
8249 {
8250 // We don't check for a next command here.
8251 line = (char_u *)"";
8252 }
8253 else
8254 {
8255 // Not recognized, execute with do_cmdline_cmd().
8256 ea.arg = p;
8257 line = compile_exec(line, &ea, &cctx);
8258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 break;
8260 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008261nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262 if (line == NULL)
8263 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008264 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008266 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008267 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008269 if (cctx.ctx_type_stack.ga_len < 0)
8270 {
8271 iemsg("Type stack underflow");
8272 goto erret;
8273 }
8274 }
8275
8276 if (cctx.ctx_scope != NULL)
8277 {
8278 if (cctx.ctx_scope->se_type == IF_SCOPE)
8279 emsg(_(e_endif));
8280 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8281 emsg(_(e_endwhile));
8282 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8283 emsg(_(e_endfor));
8284 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008285 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008286 goto erret;
8287 }
8288
Bram Moolenaarefd88552020-06-18 20:50:10 +02008289 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008290 {
8291 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8292 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008293 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008294 goto erret;
8295 }
8296
8297 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008298 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008299 }
8300
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008301 {
8302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8303 + ufunc->uf_dfunc_idx;
8304 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008305 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008306 dfunc->df_instr = instr->ga_data;
8307 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008308 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008309 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008310 if (cctx.ctx_outer_used)
8311 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008312 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314
8315 ret = OK;
8316
8317erret:
8318 if (ret == FAIL)
8319 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008320 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008321 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8322 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008323
8324 for (idx = 0; idx < instr->ga_len; ++idx)
8325 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008326 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008327 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008328
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008329 // If using the last entry in the table and it was added above, we
8330 // might as well remove it.
8331 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008332 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008333 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008334 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008335 ufunc->uf_dfunc_idx = 0;
8336 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008337 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008338
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008339 while (cctx.ctx_scope != NULL)
8340 drop_scope(&cctx);
8341
Bram Moolenaar20431c92020-03-20 18:39:46 +01008342 // Don't execute this function body.
8343 ga_clear_strings(&ufunc->uf_lines);
8344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345 if (errormsg != NULL)
8346 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008347 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008348 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008349 }
8350
8351 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008352 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008353 if (do_estack_push)
8354 estack_pop();
8355
Bram Moolenaar20431c92020-03-20 18:39:46 +01008356 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008357 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008358 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008359 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008360}
8361
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008362 void
8363set_function_type(ufunc_T *ufunc)
8364{
8365 int varargs = ufunc->uf_va_name != NULL;
8366 int argcount = ufunc->uf_args.ga_len;
8367
8368 // Create a type for the function, with the return type and any
8369 // argument types.
8370 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8371 // The type is included in "tt_args".
8372 if (argcount > 0 || varargs)
8373 {
8374 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8375 argcount, &ufunc->uf_type_list);
8376 // Add argument types to the function type.
8377 if (func_type_add_arg_types(ufunc->uf_func_type,
8378 argcount + varargs,
8379 &ufunc->uf_type_list) == FAIL)
8380 return;
8381 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8382 ufunc->uf_func_type->tt_min_argcount =
8383 argcount - ufunc->uf_def_args.ga_len;
8384 if (ufunc->uf_arg_types == NULL)
8385 {
8386 int i;
8387
8388 // lambda does not have argument types.
8389 for (i = 0; i < argcount; ++i)
8390 ufunc->uf_func_type->tt_args[i] = &t_any;
8391 }
8392 else
8393 mch_memmove(ufunc->uf_func_type->tt_args,
8394 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8395 if (varargs)
8396 {
8397 ufunc->uf_func_type->tt_args[argcount] =
8398 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8399 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8400 }
8401 }
8402 else
8403 // No arguments, can use a predefined type.
8404 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8405 argcount, &ufunc->uf_type_list);
8406}
8407
8408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409/*
8410 * Delete an instruction, free what it contains.
8411 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008412 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008413delete_instr(isn_T *isn)
8414{
8415 switch (isn->isn_type)
8416 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008417 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008419 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008420 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421 case ISN_LOADENV:
8422 case ISN_LOADG:
8423 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008424 case ISN_LOADT:
8425 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008427 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008429 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008430 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008431 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008432 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008434 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008435 case ISN_STOREW:
8436 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 vim_free(isn->isn_arg.string);
8438 break;
8439
8440 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008441 case ISN_STORES:
8442 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008443 break;
8444
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008445 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008446 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008447 vim_free(isn->isn_arg.unlet.ul_name);
8448 break;
8449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008450 case ISN_STOREOPT:
8451 vim_free(isn->isn_arg.storeopt.so_name);
8452 break;
8453
8454 case ISN_PUSHBLOB: // push blob isn_arg.blob
8455 blob_unref(isn->isn_arg.blob);
8456 break;
8457
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008458 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008459#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008460 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008461#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008462 break;
8463
8464 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008465#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008466 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008467#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008468 break;
8469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008470 case ISN_UCALL:
8471 vim_free(isn->isn_arg.ufunc.cuf_name);
8472 break;
8473
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008474 case ISN_FUNCREF:
8475 {
8476 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8477 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008478 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008479
Bram Moolenaar077a4232020-12-22 18:33:27 +01008480 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8481 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008482 }
8483 break;
8484
8485 case ISN_DCALL:
8486 {
8487 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8488 + isn->isn_arg.dfunc.cdf_idx;
8489
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008490 if (dfunc->df_ufunc != NULL
8491 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008492 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008493 }
8494 break;
8495
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008496 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008497 {
8498 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8499 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8500
8501 if (ufunc != NULL)
8502 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008503 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008504 func_ptr_unref(ufunc);
8505 }
8506
8507 vim_free(lambda);
8508 vim_free(isn->isn_arg.newfunc.nf_global);
8509 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008510 break;
8511
Bram Moolenaar5e654232020-09-16 15:22:00 +02008512 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008513 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008514 free_type(isn->isn_arg.type.ct_type);
8515 break;
8516
Bram Moolenaar02194d22020-10-24 23:08:38 +02008517 case ISN_CMDMOD:
8518 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8519 ->cmod_filter_regmatch.regprog);
8520 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8521 break;
8522
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008523 case ISN_LOADSCRIPT:
8524 case ISN_STORESCRIPT:
8525 vim_free(isn->isn_arg.script.scriptref);
8526 break;
8527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 case ISN_2BOOL:
8529 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008530 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008531 case ISN_ADDBLOB:
8532 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008533 case ISN_ANYINDEX:
8534 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008535 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008536 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008537 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008538 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008539 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008540 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008541 case ISN_COMPAREANY:
8542 case ISN_COMPAREBLOB:
8543 case ISN_COMPAREBOOL:
8544 case ISN_COMPAREDICT:
8545 case ISN_COMPAREFLOAT:
8546 case ISN_COMPAREFUNC:
8547 case ISN_COMPARELIST:
8548 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008549 case ISN_COMPARESPECIAL:
8550 case ISN_COMPARESTRING:
8551 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008552 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008553 case ISN_DROP:
8554 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008555 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008556 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008557 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008558 case ISN_EXECCONCAT:
8559 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008561 case ISN_GETITEM:
8562 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008563 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008564 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008565 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008566 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008567 case ISN_LOADBDICT:
8568 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008569 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008571 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008572 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008573 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008574 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008575 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008576 case ISN_NEGATENR:
8577 case ISN_NEWDICT:
8578 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008580 case ISN_OPFLOAT:
8581 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008583 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008584 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008585 case ISN_PUSHF:
8586 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008588 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008589 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008590 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008591 case ISN_SHUFFLE:
8592 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008593 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008594 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008595 case ISN_STORENR:
8596 case ISN_STOREOUTER:
8597 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008598 case ISN_STOREV:
8599 case ISN_STRINDEX:
8600 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008601 case ISN_THROW:
8602 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008603 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008604 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008605 // nothing allocated
8606 break;
8607 }
8608}
8609
8610/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008611 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008612 */
8613 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008614delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008615{
8616 int idx;
8617
8618 ga_clear(&dfunc->df_def_args_isn);
8619
8620 if (dfunc->df_instr != NULL)
8621 {
8622 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8623 delete_instr(dfunc->df_instr + idx);
8624 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008625 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008626 }
8627
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008628 if (mark_deleted)
8629 dfunc->df_deleted = TRUE;
8630 if (dfunc->df_ufunc != NULL)
8631 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008632}
8633
8634/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008635 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008636 * function, unless another user function still uses it.
8637 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 */
8639 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008640unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008641{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008642 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643 {
8644 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8645 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008646
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008647 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008648 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008649 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008650 ufunc->uf_dfunc_idx = 0;
8651 if (dfunc->df_ufunc == ufunc)
8652 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008653 }
8654}
8655
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008656/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008657 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008658 */
8659 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008660link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008661{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008662 if (ufunc->uf_dfunc_idx > 0)
8663 {
8664 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8665 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008666
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008667 ++dfunc->df_refcount;
8668 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008669}
8670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008672/*
8673 * Free all functions defined with ":def".
8674 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008675 void
8676free_def_functions(void)
8677{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008678 int idx;
8679
8680 for (idx = 0; idx < def_functions.ga_len; ++idx)
8681 {
8682 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8683
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008684 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008685 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008686 }
8687
8688 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008689}
8690#endif
8691
8692
8693#endif // FEAT_EVAL